每天一個 Linux 命令:ifconfig 命令

許多 windows 非常熟悉 ipconfig 命令行工具,它被用來獲取網絡接口配置信息並對此進行修改。

Linux 系統擁有一個類似的工具,也就是 ifconfig(interfaces config)。通常需要以 root 身份登錄或使用 sudo 以便在 Linux 機器上使用 ifconfig 工具。依賴於 ifconfig 命令中使用一些選項屬性,ifconfig 工具不僅可以被用來簡單地獲取網絡接口配置信息,還可以修改這些配置。

1.命令格式:

ifconfig [網絡設備] [參數]

2.命令功能:

ifconfig 命令用來查看和配置網絡設備。當網絡環境發生改變時可通過此命令對網絡進行相應的配置。

3.命令參數:

up 啓動指定網絡設備 / 網卡。

down 關閉指定網絡設備 / 網卡。該參數可以有效地阻止通過指定接口的 IP 信息流,如果想永久地關閉一個接口,我們還需要從核心路由表中將該接口的路由信息全部刪除。

arp 設置指定網卡是否支持 ARP 協議。

-promisc 設置是否支持網卡的 promiscuous 模式,如果選擇此參數,網卡將接收網絡中發給它所有的數據包

-allmulti 設置是否支持多播模式,如果選擇此參數,網卡將接收網絡中所有的多播數據包

-a 顯示全部接口信息

-s 顯示摘要信息(類似於 netstat -i)

add 給指定網卡配置 IPv6 地址

del 刪除指定網卡的 IPv6 地址

<硬件地址> 配置網卡最大的傳輸單元

mtu <字節數> 設置網卡的最大傳輸單元 (bytes)

netmask <子網掩碼> 設置網卡的子網掩碼。掩碼可以是有前綴 0x 的 32 位十六進制數,也可以是用點分開的 4 個十進制數。如果不打算將網絡分成子網,可以不管這一選項;如果要使用子網,那麼請記住,網絡中每一個系統必須有相同子網掩碼。

tunel 建立隧道

dstaddr 設定一個遠端地址,建立點對點通信

-broadcast <地址> 爲指定網卡設置廣播協議

-pointtopoint <地址> 爲網卡設置點對點通訊協議

multicast 爲網卡設置組播標誌

address 爲網卡設置 IPv4 地址

txqueuelen <長度> 爲網卡設置傳輸列隊的長度

4.使用實例:

實例 1:顯示網絡設備信息(激活狀態的)

命令:

ifconfig

輸出:

[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:20 
     inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
     TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:68 errors:0 dropped:0 overruns:0 frame:0
     TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)

說明:

eth0 表示第一塊網卡, 其中 HWaddr 表示網卡的物理地址,可以看到目前這個網卡的物理地址 (MAC 地址)是 00:50:56:BF:26:20

inet addr 用來表示網卡的 IP 地址,此網卡的 IP 地址是 192.168.120.204,廣播地址, Bcast:192.168.120.255,掩碼地址 Mask:255.255.255.0

lo 是表示主機的回壞地址,這個一般是用來測試一個網絡程序,但又不想讓局域網或外網的用戶能夠查看,只能在此臺主機上運行和查看所用的網絡接口。比如把 HTTPD 服務器的指定到回壞地址,在瀏覽器輸入 127.0.0.1 就能看到你所架 WEB 網站了。但只是您能看得到,局域網的其它主機或用戶無從知道。

第一行:連接類型:Ethernet(以太網)HWaddr(硬件 mac 地址)

第二行:網卡的 IP 地址、子網、掩碼

第三行:UP(代表網卡開啓狀態)RUNNING(代表網卡的網線被接上)MULTICAST(支持組播)MTU:1500(最大傳輸單元):1500 字節

第四、五行:接收、發送數據包情況統計

第七行:接收、發送數據字節數統計信息。

實例 2:啓動關閉指定網卡

命令:

ifconfig eth0 up

ifconfig eth0 down

輸出:

說明:

ifconfig eth0 up 爲啓動網卡 eth0 ;ifconfig eth0 down 爲關閉網卡 eth0。ssh 登陸 linux 服務器操作要小心,關閉了就不能開啓了,除非你有多網卡。

實例 3:爲網卡配置和刪除 IPv6 地址

命令:

ifconfig eth0 add 33ffe:3240:800:1005::2/64

ifconfig eth0 del 33ffe:3240:800:1005::2/64

輸出:

說明:

ifconfig eth0 add 33ffe:3240:800:1005::2/64 爲網卡 eth0 配置 IPv6 地址;

ifconfig eth0 add 33ffe:3240:800:1005::2/64 爲網卡 eth0 刪除 IPv6 地址;

練習的時候,ssh 登陸 linux 服務器操作要小心,關閉了就不能開啓了,除非你有多網卡。

實例 4:用 ifconfig 修改 MAC 地址

命令:

ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

輸出:

[root@localhost ~]# ifconfig eth0 down //關閉網卡
[root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改 MAC 地址
[root@localhost ~]# ifconfig eth0 up //啓動網卡
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:EE 
     inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
     TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:68 errors:0 dropped:0 overruns:0 frame:0
     TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)
[root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20 //關閉網卡並修改 MAC 地址 
[root@localhost ~]# ifconfig eth0 up //啓動網卡
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:20 
     inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
     TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:68 errors:0 dropped:0 overruns:0 frame:0
     TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)

說明:

實例 5:配置 IP 地址

命令:

輸出:

[root@localhost ~]# ifconfig eth0 192.168.120.56 
[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 
[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255

說明:

ifconfig eth0 192.168.120.56

給 eth0 網卡配置 IP 地:192.168.120.56

ifconfig eth0 192.168.120.56 netmask 255.255.255.0

給 eth0 網卡配置 IP 地址:192.168.120.56 ,並加上子掩碼:255.255.255.0

ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255

給 eth0 網卡配置 IP 地址:192.168.120.56,加上子掩碼:255.255.255.0,加上個廣播地址:192.168.120.255

實例 6:啓用和關閉 ARP 協議

命令:

ifconfig eth0 arp

ifconfig eth0 -arp

輸出:

[root@localhost ~]# ifconfig eth0 arp 
[root@localhost ~]# ifconfig eth0 -arp

說明:

ifconfig eth0 arp 開啓網卡 eth0 的 arp 協議;

ifconfig eth0 -arp 關閉網卡 eth0 的 arp 協議;

實例 7:設置最大傳輸單元

命令:

ifconfig eth0 mtu 1500

輸出:

[root@localhost ~]# ifconfig eth0 mtu 1480
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:1F 
     inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
     RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0
     TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:597062089 (569.4 MiB) TX bytes:2643973 (2.5 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
     TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)

[root@localhost ~]# ifconfig eth0 mtu 1500
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:1F 
     inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0
     TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:597072333 (569.4 MiB) TX bytes:2650581 (2.5 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
     TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)

[root@localhost ~]#

說明:

設置能通過的最大數據包大小爲 1500 bytes

備註:用 ifconfig 命令配置的網卡信息,在網卡重啓後機器重啓後,配置就不存在。要想將上述的配置信息永遠的存的電腦裏,那就要修改網卡的配置文件了。

轉自:www.cnblogs.com/peida/archive/2013/02/27/2934525.html

****推薦關注「算法愛好者」,修煉編程內功

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/oKpqNrhILZyM7i9zvnvbNA