ifconfig命令詳解

2021-08-08 16:26:13 字數 4707 閱讀 8668

ifconfig 配置網路介面的工具介紹

ifconfig 是乙個用來檢視、配置、啟用或禁用網路介面的工具,這個工具極為常用的。可以用這個工具來臨時性的配置網絡卡的ip位址、掩碼、廣播位址、閘道器等。也可以把它寫入乙個檔案中(比如/etc/rc.

d/rc.local),這樣系統引導後,會讀取這個檔案,為網絡卡設定ip位址

1 ifconfig 檢視網路介面狀態;

ifconfig 如果不接任何引數,就會輸出當前網路介面的情況;

[root@localhost ~]# ifconfig

eth0 link encap:ethernet hwaddr 00:c0:9f:94:78:0e

inet addr:192.168.1.88 bcast:192.168.1.255 mask:255.255.255.0

inet6 addr: fe80::2c0:9fff:fe94:780e/64 scope:link

up broadcast running multicast mtu:1500 metric:1

rx packets:850 errors:0 dropped:0 overruns:0 frame:0

tx packets:628 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

rx bytes:369135 (360.4 kib) tx bytes:75945 (74.1 kib)

interrupt:10 base address:0x3000

lo link encap:local loopback

inet addr:127.0.0.1 mask:255.0.0.0

inet6 addr: ::1/128 scope:host

up loopback running mtu:16436 metric:1

rx packets:57 errors:0 dropped:0 overruns:0 frame:0

tx packets:57 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

rx bytes:8121 (7.9 kib) tx bytes:8121 (7.9 kib)

解說:eth0 表示第一塊網絡卡, 其中 hwaddr 表示網絡卡的實體地址,可以看到目前這個網絡卡的實體地址(mac位址)是 00:c0:

9f:94:78:

0e ; inet addr 用來表示網絡卡的ip位址,此網絡卡的 ip位址是 192.168.1.

88,廣播位址, bcast:192.168.

1.255,掩碼位址mask:255.

255.255.0

lo 是表示主機的回壞位址,這個一般是用來測試乙個網路程式,但又不想讓區域網或外網的使用者能夠檢視,只能在此台主機上執行和檢視所用的網路介面。比如把 httpd伺服器的指定到回壞位址,在瀏覽器輸入 127.0.

0.1 就能看到你所架web**了。但只是您能看得到,區域網的其它主機或使用者無從知道;

如果想知道主機所有網路介面的情況,請用下面的命令;

[root@localhost ~]# ifconfig -a

如果想檢視某個埠,比如檢視eth0 的狀態,就可以用下面的方法;

[root@localhost ~]# ifconfig eth0

2 ifconfig 配置網路介面;

ifconfig 可以用來配置網路介面的ip位址、掩碼、閘道器、實體地址等;值得一說的是用ifconfig 為網絡卡指定ip位址,這只是用來除錯網路用的,並不會更改系統關於網絡卡的配置檔案。如果您想把網路介面的ip位址固定下來,目前有三個方法:一是通過各個發行和版本專用的工具來修改ip位址;二是直接修改網路介面的配置檔案;三是修改特定的檔案,加入ifconfig 指令來指定網絡卡的ip位址,比如在redhat或fedora中,把ifconfig 的語名寫入/etc/rc.

d/rc.local檔案中;

ifconfig 配置網路埠的方法:

ifconfig 工具配置網路介面的方法是通過指令的引數來達到目的的,我們只說最常用的引數;

ifconfig 網路埠 ip位址 hw mac位址 netmask 掩碼位址 broadcast 廣播位址 [up/down]

* 例項一:

比如我們用ifconfig 來除錯 eth0網絡卡的位址

[root@localhost ~]# ifconfig eth0 down

[root@localhost ~]# ifconfig eth0 192.168.1.

99 broadcast 192.168.1.

255 netmask 255.255.255.

0[root@localhost ~]# ifconfig eth0 up

[root@localhost ~]# ifconfig eth0

eth0 link encap:ethernet hwaddr 00:11:00:00:11:11

inet addr:192.168.1.99 bcast:192.168.1.255 mask:255.255.255.0

up broadcast multicast mtu:1500 metric:1

rx packets:0 errors:0 dropped:0 overruns:0 frame:0

tx packets:0 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

rx bytes:0 (0.0 b) tx bytes:0 (0.0 b)

interrupt:11 base address:0x3400

註解: 上面的例子我們解說一下;

第一行:ifconfig eth0 down 表示如果eth0是啟用的,就把它down掉。此命令等同於 ifdown eth0;

第二行:用ifconfig 來配置 eth0的ip位址、廣播位址和網路掩碼;

第三行:用ifconfig eth0 up 來啟用eth0 ; 此命令等同於 ifup eth0

第四行:用 ifconfig eth0 來檢視 eth0的狀態;

當然您也可以用直接在指令ip位址、網路掩碼、廣播位址的同時,啟用網絡卡;要加up引數;比如下面的例子;

[root@localhost ~]# ifconfig eth0 192.168.1.

99 broadcast 192.168.1.

255 netmask 255.255.255.

0 up

* 例項二:在這個例子中,我們要學會設定網路ip位址的同時,學會設定網絡卡的實體地址(mac位址);

比如我們設定網絡卡eth1的ip位址、網路掩碼、廣播位址,實體地址並且啟用它;

[root@localhost ~]# ifconfig eth1 192.168.1.

252 hw ether 00:11:00:

00:11:11 netmask 255.

255.255.0 broadcast 192.

168.1.255 up

或[root@localhost ~]# ifconfig eth1 hw ether 00:11:00:00:11:22

[root@localhost ~]# ifconfig eth1 192.168.1.

252 netmask 255.255.255.

0 broadcast 192.168.1.

255 up

其中 hw 後面所接的是網路介面型別, ether表示乙太網, 同時也支援 ax25 、arcnet、netrom等,詳情請檢視 man ifconfig ;

3.3 如何用ifconfig 來配置虛擬網路介面;

有時我們為了滿足不同的需要還需要配置虛擬網路介面,比如我們用不同的ip位址來架執行多個httpd伺服器,就要用到虛擬位址;這樣就省卻了同乙個ip位址,如果開設兩個的httpd伺服器時,要指定埠號。

虛擬網路介面指的是為乙個網路介面指定多個ip位址,虛擬介面是這樣的 eth0:0 、 eth0:1、eth0:

2 ... .. eth1n。

當然您為eth1 指定多個ip位址,也就是 eth1:0、eth1:1、eth1:

2 ... ...以此類推;

其實用ifconfig 為乙個網絡卡配置多個ip位址,就用前面我們所說的ifconfig的用法,這個比較簡單;看下面的例子;

[root@localhost ~]# ifconfig eth1:0 192.168.

1.251 hw ether 00:11:

00:00:11:

33 netmask 255.255.255.

0 broadcast 192.168.1.

255 up

或[root@localhost ~]# ifconfig eth1 hw ether 00:11:00:00:11:33

[root@localhost ~]# ifconfig eth1 192.168.1.

251 netmask 255.255.255.

0 broadcast 192.168.1.

255 up

在 redhat/fedora 或與redhat/fedora類似的系統,您可以把配置網路ip位址、廣播位址、掩碼位址、實體地址以及啟用網路介面同時放在乙個句子中,寫入/etc/rc.d/rc.local中。

比如下面的例子;

AT命令詳解

at命令解釋 1 常用操作 1.1 at 命令解釋 檢測module與串列埠是否連通,能否接收at命令 命令格式 at 命令返回 ok 與串列埠通訊正常 無返回,與串列埠通訊未連通 測試結果 at ok 1.2 at csq 命令解釋 檢查網路訊號強度和sim卡情況 命令格式 at csq 命令返回...

route命令詳解

在windows下手動設定路由主要在dos系統中命令符下 在執行輸入欄中鍵入 mand或者cmd即可 進行。鍵入命令route回車會出現大約幾十行英文說明,主要解說在windows系統中如何新增 刪除 修改路由。現簡單介紹如下 route命令格式如下 route f p mand destinati...

rpm命令詳解

一 rpm格式檔案簡介 rpm是 redhat package manager,是有紅帽公司開發的套件管理程式。srpm顧名思義,他是 source rpm 的意思 兩種檔案的格式 rpm rpm 的格式,已經經過編譯且包裝完成的 rpm 檔案 xx.src.rpm srpm的格式,包含未編譯的原始...