linux ifconfig 命令详解
ifconfig 概述
ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置。用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。
Linux ifconfig命令用于显示或设置网络设备。
ifconfig可设置网络设备的状态,或是显示目前的设置。
ifconfig 语法
ifconfig [网络设备][down up -allmulti -arp -promisc][add<地址>][del<地址>][<hw<网络设备类型><硬件地址>][io_addr<I/O地址>][irq<IRQ地址>][media<网络媒介类型>][mem_start<内存地址>][metric<数目>][mtu<字节>][netmask<子网掩码>][tunnel<地址>][-broadcast<地址>][-pointopoint<地址>][IP地址]
参数说明:
-
add<地址> 设置网络设备IPv6的IP地址。
-
del<地址> 删除网络设备IPv6的IP地址。
-
down 关闭指定的网络设备。
-
<hw<网络设备类型><硬件地址> 设置网络设备的类型与硬件地址。
-
io_addr<I/O地址> 设置网络设备的I/O地址。
-
irq<IRQ地址> 设置网络设备的IRQ。
-
media<网络媒介类型> 设置网络设备的媒介类型。
-
mem_start<内存地址> 设置网络设备在主内存所占用的起始地址。
-
metric<数目> 指定在计算数据包的转送次数时,所要加上的数目。
-
mtu<字节> 设置网络设备的MTU。
-
netmask<子网掩码> 设置网络设备的子网掩码。
-
tunnel<地址> 建立IPv4与IPv6之间的隧道通信地址。
-
up 启动指定的网络设备。
-
-broadcast<地址> 将要送往指定地址的数据包当成广播数据包来处理。
-
-pointopoint<地址> 与指定地址的网络设备建立直接连线,此模式具有保密功能。
-
-promisc 关闭或启动指定网络设备的promiscuous模式。
-
[IP地址] 指定网络设备的IP地址。
-
[网络设备] 指定网络设备的名称。
ifconfig 命令常见参数说明
参数 |
说明 |
-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信息流,如果想永久地关闭一个接口,我们还需要从核心路由表中将该接口的路由信息全部删除。 |
ifconfig 网卡字段简单说明
[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
ifconfig 实例
ifconfig 显示网络设备信息
(1) 显示激活的网卡信息
[root@xinbiancheng.cn$] ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet xxx.xx.xxx.xx netmask 255.255.240.0 broadcast xxx.xx.xxx.xxx
ether 00:16:3e:09:6c:36 txqueuelen 1000 (Ethernet)
RX packets 17886645 bytes 4082695370 (3.8 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18336366 bytes 19109264562 (17.7 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 14649377495 bytes 2185992570491 (1.9 TiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14649377495 bytes 2185992570491 (1.9 TiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(2) 显示所有的网卡信息
ifconfig -a
(3) 显示简要的网卡信息
ifconfig -s
ifconfig 启动关闭指定网卡
(1) 关闭网卡
ifconfig eth0 down
(2) 启动网卡
ifconfig eth0 up
ifconfig 配置和删除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
修改MAC地址
ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
ifconfig 启用和关闭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
ifconfig 设置最大传输单元
ifconfig eth0 mtu 1500
ifconfig 设置网卡的promiscuous模式
(1) 启用
ifconfig eth0 promisc
(2) 禁用
ifconfig eth0 -promisc
如果选择此参数,网卡将接收网络中发给它所有的数据包。当启用时出现PROMISC字段。
[root@localhost ~]# ifconfig eth0 promisc
[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 ~]# ifconfig eth0 -promisc
[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
ifconfig 设置网卡的多播模式
(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 ~]# 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
ifconfig 配置和删除IPv6地址
(1) 添加
ifconfig eth0 add 3ffe:3240:800:1005::2/64
(2) 删除
ifconfig eth0 del 3ffe:3240:800:1005::2/64