ifconfig 命令详解

2,699 阅读5分钟

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 命令常见参数说明

image.png

image.png

2.3 网卡字段简单说明

[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