熟悉 Windows 的人多半用過 ipconfig,它能查詢並修改網路介面設定。Linux 有一支功能相近的工具,也就是 ifconfig(interface config)。通常需以 root 身分登入或搭配 sudo 使用。依搭配的參數不同,ifconfig 既能單純查詢介面資訊,也能直接修改設定。
命令格式:ifconfig [網路設備] [參數]
命令功能:查看與設定網路設備,網路環境變動時可藉此調整。
常用參數:
netstat -i)。
[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
說明:eth0 是第一塊網卡,HWaddr 是它的實體 MAC 位址;inet addr 是 IP,Bcast 是廣播位址,Mask 是遮罩。lo 則是主機的回送介面,常用於在本機測試網路程式而不對外暴露,例如把 HTTPD 綁在回送位址上,用瀏覽器開 127.0.0.1 就能看到網站,區網其他主機卻看不到。
逐行來看:第一行是連接類型(Ethernet)與 MAC;第二行是 IP、子網與遮罩;第三行 UP 代表網卡已啟用、RUNNING 代表網線已接上、MULTICAST 代表支援多播、MTU 為 1500 位元組;第四、五行是收發封包統計;第七行是收發位元組數統計。
ifconfig eth0 up ifconfig eth0 down
ssh 登入 Linux 主機操作時要格外小心,一旦關閉就再也開不起來了,除非你還有其他網卡可用。
ifconfig eth0 add 33ffe:3240:800:1005::2/64 ifconfig eth0 del 33ffe:3240:800:1005::2/64
[root@localhost ~]# ifconfig eth0 down
[root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
[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
還原時同樣先 down,改回 00:50:56:BF:26:20,再 up 即可。
[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
三條指令分別是:只設 IP;IP 加子網遮罩;IP 加遮罩再加廣播位址。
[root@localhost ~]# ifconfig eth0 arp [root@localhost ~]# ifconfig eth0 -arp
[root@localhost ~]# ifconfig eth0 mtu 1480
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F
UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
[root@localhost ~]# ifconfig eth0 mtu 1500
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
把可通過的最大封包大小設為 1500 bytes。
備註:用 ifconfig 下達的設定都是暫時的,網卡重啟或主機重啟後即失效。要永久保存,必須去改網卡的設定檔。