【轉】linux中ifconfig 命令詳解詳解


1 概述 

ifconfig工具不僅可以被用來簡單地獲取網絡接口配置信息,還可以修改這些配置。用ifconfig命令配置的網卡信息,在網卡重啟后機器重啟后,配置就不存在。要想將上述的配置信息永遠的存的電腦里,那就要修改網卡的配置文件了。

2 命令詳解

2.1 命令常見參數

Usage:

ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
[add <address>[/<prefixlen>]]
[del <address>[/<prefixlen>]]
[[-]broadcast [<address>]] [[-]pointopoint [<address>]]
[netmask <address>] [dstaddr <address>] [tunnel <address>]
[outfill <NN>] [keepalive <NN>]
[hw <HW> <address>] [metric <NN>] [mtu <NN>]
[[-]trailers] [[-]arp] [[-]allmulti]
[multicast] [[-]promisc]
[mem_start <NN>] [io_addr <NN>] [irq <NN>] [media <type>]
[txqueuelen <NN>]
[[-]dynamic]
[up|down] ...

2.2 命令常見參數說明

參數

說明

-a

顯示全部接口信息。

-s

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

<interface> address

為網卡設置IPv4地址。

<interface> add <address>

給指定網卡配置IPv6地址。

<interface> del <address>

刪除指定網卡的IPv6地址。

<interface> netmask <address>

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

<interface> dstaddr <address>

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

<interface> tunnel <address>

建立隧道。

<interface> hw <address>

設置硬件地址。

<interface> mtu <NN>

設置最大傳輸單元。

<interface> [-]arp

設置指定網卡是否支持ARP協議。-表示不支持arp。

<interface> multicast

為網卡設置組播標志。

<interface> [-]promisc

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

<interface> txqueuelen <NN>

為網卡設置傳輸列隊的長度。

<interface> up

啟動指定網卡。

<interface> down

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

2.3 網卡字段簡單說明
 

(1) 簡單分析 

[root@localhost ~]# ifconfig eth0

// UP:表示“接口已啟用”。
// BROADCAST :表示“主機支持廣播”。
// RUNNING:表示“接口在工作中”。
// MULTICAST:表示“主機支持多播”。
// MTU:1500(最大傳輸單元):1500字節
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

// inet :網卡的IP地址。
// netmask :網絡掩碼。
// broadcast :廣播地址。
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255

// 網卡的IPv6地址
inet6 fe80::2aa:bbff:fecc:ddee prefixlen 64 scopeid 0x20<link>

// 連接類型:Ethernet (以太網) HWaddr (硬件mac地址)
// txqueuelen (網卡設置的傳送隊列長度)
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)


// RX packets 接收時,正確的數據包數。
// RX bytes 接收的數據量。
// RX errors 接收時,產生錯誤的數據包數。
// RX dropped 接收時,丟棄的數據包數。
// RX overruns 接收時,由於速度過快而丟失的數據包數。
// RX frame 接收時,發生frame錯誤而丟失的數據包數。
RX packets 2825 bytes 218511 (213.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0

// TX packets 發送時,正確的數據包數。
// TX bytes 發送的數據量。
// TX errors 發送時,產生錯誤的數據包數。
// TX dropped 發送時,丟棄的數據包數。
// TX overruns 發送時,由於速度過快而丟失的數據包數。
// TX carrier 發送時,發生carrier錯誤而丟失的數據包數。
// collisions 沖突信息包的數目。
TX packets 1077 bytes 145236 (141.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3 簡單實例

3.1 顯示網絡設備信息 

(1) 顯示激活的網卡信息

ifconfig 

(2) 顯示所有的網卡信息

ifconfig -a 

(3) 顯示簡要的網卡信息

ifconfig -s 

3.2 啟動關閉指定網卡
(1) 關閉網卡

ifconfig eth0 down 

(2) 啟動網卡

ifconfig eth0 up 

3.3 配置和刪除ip地址 

(1) 配置ip

// 配置ip地址
ifconfig eth0 192.168.1.100

// 配置ip地址和子網掩碼
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

// 配置ip地址、子網掩碼和廣播地址
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 

(2) 單網卡添加多個IP地址

ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0 up

ifconfig eth0:1 192.168.2.100 netmask 255.255.255.0 up
 

(3) 刪除IP地址

ifconfig eth0 del 192.168.1.100

3.4 修改MAC地址

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

3.5 啟用和關閉ARP協議

(1) 啟用arp

ifconfig eth0 arp

(2) 禁用arp

ifconfig eth0 -arp

禁用arp的時候,可以看到出現NOARP字段。

[root@localhost ~]# ifconfig eth0 arp
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe9b:52d3 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
RX packets 2635 bytes 204710 (199.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1071 bytes 144688 (141.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@localhost ~]# ifconfig eth0 -arp
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0
eth0: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1500
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe9b:52d3 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
RX packets 2636 bytes 204770 (199.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1071 bytes 144688 (141.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3.6 設置最大傳輸單元
 

ifconfig eth0 mtu 1500

3.7 設置網卡的promiscuous模式
 

(1) 啟用

ifconfig eth0 promisc
 

(2) 禁用

ifconfig eth0 -promisc
 

如果選擇此參數,網卡將接收網絡中發給它所有的數據包。當啟用時出現PROMISC字段。

[root@localhost ~]# ifconfig eth0 promisc
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0
eth0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe9b:52d3 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
RX packets 2659 bytes 206696 (201.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1071 bytes 144688 (141.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


[root@localhost ~]#
[root@localhost ~]# ifconfig eth0 -promisc
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe9b:52d3 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
RX packets 2661 bytes 206816 (201.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1071 bytes 144688 (141.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3.8 設置網卡的多播模式
 

(1) 啟用

ifconfig eth0 allmulti
 

(2) 禁用

ifconfig eth0 -allmulti

如果選擇此參數,網卡將接收網絡中所有的多播數據包。當啟用時會出現MULTICAST字段。

[root@localhost ~]# ifconfig eth0 allmulti
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0
eth0: flags=4675<UP,BROADCAST,RUNNING,ALLMULTI,MULTICAST> mtu 1500
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe9b:52d3 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
RX packets 2676 bytes 207716 (202.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1071 bytes 144688 (141.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


[root@localhost ~]#
[root@localhost ~]# ifconfig eth0 -allmulti
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.135 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe9b:52d3 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
RX packets 2676 bytes 207716 (202.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1071 bytes 144688 (141.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3.9 配置和刪除IPv6地址
 

(1) 添加

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

(2) 刪除

ifconfig eth0 del 3ffe:3240:800:1005::2/64
————————————————
版權聲明:本文為CSDN博主「路痴的旅行」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u011857683/article/details/83758503


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM