內容來源於人民郵電出版社《Linux 命令應用大詞典》,該書講述 729 個命令、1935 個範例,是學習 Linux 系統的參考書與案頭書,遇到不懂的命令或選項一查即可。
ifconfig可以檢視與設定網路介面,例如設定 IP、MAC 位址,以及啟用或關閉介面。
語法:
ifconfig [介面] [選項| IP位址]
-a 顯示所有網路介面的狀態 add <位址> 設定網路裝置的 IPv6 位址 del <位址> 刪除網路裝置的 IPv6 位址 media <類型> 設定網路裝置的媒介類型 metric <數目> 指定計算封包轉送次數時要加上的數目 mtu <位元組> 設定最大傳輸單元 netmask <子網路遮罩> 設定子網路遮罩 up 啟用指定的網路裝置 down 關閉指定的網路裝置 hw <類型> <硬體位址> 設定介面的 MAC,支援 ether、ax25、ARCnet、netrom txqueuelen <長度> 設定裝置傳送佇列的長度 tunnel <位址> 建立 IPv4 與 IPv6 之間的通道位址
設定 eth0 的 IP 並同時啟用:
[root@rhel ~]# ifconfig eth0 192.168.0.2 netmask 255.255.255.0 up
為 eth0 建立別名 eth0:1 並定址:
[root@rhel ~]# ifconfig eth0:1 192.168.0.3 [root@rhel ~]# ifconfig eth0:1 up
檢視 eth0 的設定:
[root@rhel~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:FC:2F:E5 inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fefc:2fe5/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11833 errors:0 dropped:0 overruns:0 frame:0 TX packets:14789 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:953120 (930.7 KiB) TX bytes:12258316 (11.6 MiB)
輸出中可以看到 IP 為 192.168.0.2、MAC 為 00:0C:29:FC:2F:E5,以及封包統計與 MTU 等資訊。
檢視所有介面(含未啟用的):
[root@rhel~]# ifconfig -a
關閉 eth0:1:
[root@rhel ~]# ifconfig eth0:1 down
修改 eth0 的 MAC:
[root@rhel ~]# ifconfig eth0 hw ether 00:0C:29:18:2E:3D
本文出自「airfish2000」部落格,請務必保留此出處 https://airfish2000.blog.51cto.com/10829608/1872272