当前位置:   article > 正文

linux中ifconfig指令详解

linux中ifconfig指令详解

ifconfigLinux 中用来查询和配置网络接口卡(网卡)的命令,英文全称是network interfaces configuring。
使用 ifconfig 命令对网卡所做的配置都是临时配置,重启之后都会被还原的。如果要永久配置网卡则需要通过修改配置文件的方式

1.显示激活的网卡信息

[root@VM-12-14-centos ~]# ifconfig

br-23353f657e8b: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.19.0.1  netmask 255.255.0.0  broadcast 172.19.255.255
        ether 02:42:69:59:5d:f8  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-a0d112e232a1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:65:59:4e:7a  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:4a:93:97:6b  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.12.14  netmask 255.255.252.0  broadcast 10.0.15.255
        inet6 fe80::5054:ff:fe39:b685  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)
        RX packets 9602  bytes 6948348 (6.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6976  bytes 1061648 (1.0 MiB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 269  bytes 16180 (15.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 269  bytes 16180 (15.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:fa:a2:70  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

2.显示网卡的全部信息

[root@VM-12-14-centos ~]# ifconfig -a

br-23353f657e8b: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.19.0.1  netmask 255.255.0.0  broadcast 172.19.255.255
        ether 02:42:69:59:5d:f8  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-a0d112e232a1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:65:59:4e:7a  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:4a:93:97:6b  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.12.14  netmask 255.255.252.0  broadcast 10.0.15.255
        inet6 fe80::5054:ff:fe39:b685  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)
        RX packets 11027  bytes 7072871 (6.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8905  bytes 1379393 (1.3 MiB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 349  bytes 20755 (20.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 349  bytes 20755 (20.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:fa:a2:70  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:fa:a2:70  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

3.显示网卡的简要信息

[root@VM-12-14-centos ~]# ifconfig -s

Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
br-23353f657e8b  1500        0      0      0 0             0      0      0      0 BMU
br-a0d112e232a1  1500        0      0      0 0             0      0      0      0 BMU
docker0          1500        0      0      0 0             0      0      0      0 BMU
eth0             1500    11990      0      0 0         10208      0      0      0 BMRU
lo              65536      389      0      0 0           389      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.显示指定网卡的信息

[root@VM-12-14-centos ~]# ifconfig eth0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.12.14  netmask 255.255.252.0  broadcast 10.0.15.255
        inet6 fe80::5054:ff:fe39:b685  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)
        RX packets 12639  bytes 7213120 (6.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11045  bytes 1734458 (1.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5.网卡信息解释

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.12.14  netmask 255.255.252.0  broadcast 10.0.15.255
        inet6 fe80::5054:ff:fe39:b685  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)
        RX packets 11027  bytes 7072871 (6.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8905  bytes 1379393 (1.3 MiB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 349  bytes 20755 (20.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 349  bytes 20755 (20.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
网卡名称:eth0、lo
Link encap:网卡接口类型,Ethernet(以太网)、Local Loopback (本地环回)
HWaddr:网卡的硬件地址,即MAC地址
inet addr:IPv4地址
Bcast:广播地址
Mask:子网掩码地址
inet6 addr:IPv6地址
Scope:是用来限制 IPv6 组播的作用范围,Host(主机本地范围)、Link(链路本地范围)、Site(站点本地范围,已废弃)、Global(全局范围)
UP:表示网卡开启状态,如果网卡关闭时则不显示。
BROADCAST:表示网卡支持广播
RUNNING:表示网卡正在运行
MULTICAST :表示网卡支持组播,如果网卡不支持则不显示
MTU:最大传输单元,详细的参考:https://developer.aliyun.com/article/222535
Metric:跃点数,通常是指到达目的地址所需的跃点数量,一个跃点代表一个路由器。另外,跃点值越大表示优先级越大
RX一行:分别表示网卡从启动到现在所接收的 总包数、错误数、丢弃数、过载数、帧数
TX一行:分别表示网卡从启动到现在所发送的 总包数、错误数、丢弃数、过载数、帧数
collisions:数据包发生冲突、碰撞的次数,次数多了说明网络不太好。
txqueuelen:发送队列长度
RX bytes:接收到的字节数
TX bytes:发送出的字节数
Interrupt:IRQ中断地址
Base address:基本地址

ether(Ethernet):表示 连接类型(以太网)
52:54:00:39:b6:85(Hwaddr):表示 硬件Mac 地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

6.开关网卡

// 命令不建议在服务器上使用
//启动eth0网卡
sudo ifconfig eth0 up
//关闭eth0网卡
sudo ifconfig eth0 down
  • 1
  • 2
  • 3
  • 4
  • 5

7.指定网卡的ARP协议开关

这里以 eth0 网卡为例。
ARP协议是地址解析协议(Address Resolution Protocol),每台主机或路由器都维护着一份ARP缓存表(ARP table),这个表包含IP地址到MAC地址的映射关系,表中记录了<IP地址,MAC地址>对,从而实现IP地址与MAC地址的查询与转换。
详细的查看:https://blog.csdn.net/jiejiemcu/article/details/88406088

//启动eth0网卡的ARP协议
sudo ifconfig eth0 arp
//关闭enth0网卡的ARP协议
sudo ifconfig eth0 -arp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

8.设置网卡的IPv4地址及子网掩码(临时)

这里以 eth0 网卡为例。
子网掩码换算: 24 与 255.255.255.0 是相等的,24意思是按二进制算,从左开始的24位全部取1,其余为0。
即 11111111 11111111 11111111 00000000,也就是255.255.255.0

//修改网卡 eth0 的 IPv4地址
sudo ifconfig eth0 10.0.2.16
//修改网卡 eth0 的 IPv4地址及子网掩码,方法一
sudo ifconfig eth0 10.0.2.16/24
//修改网卡 eth0 的 IPv4地址及子网掩码,方法二
sudo ifconfig eth0 10.0.2.16 netmask 255.255.255.0

// 注意:这些都不建议在服务器上使用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

9.配置网卡的虚拟接口(临时)

这里以 eth0 网卡为例。
相当于给网卡再配置一个IP地址。

sudo ifconfig eth0:0 10.0.2.16/24 // 因为这一条涉及到对网卡eth0本体的修改,所以不建议在服务器上使用,本次就不使用了
sudo ifconfig eth0:1 10.0.2.17/24
sudo ifconfig eth0:2 10.0.2.18/24

改过之后
[root@VM-12-14-centos ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.12.14  netmask 255.255.252.0  broadcast 10.0.15.255
        inet6 fe80::5054:ff:fe39:b685  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)
        RX packets 32419  bytes 8673343 (8.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33551  bytes 4815318 (4.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.17  netmask 255.255.255.0  broadcast 10.0.2.255
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)

eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.18  netmask 255.255.255.0  broadcast 10.0.2.255
        ether 52:54:00:39:b6:85  txqueuelen 1000  (Ethernet)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

10.设置网卡的MAC地址(临时)

这里以 eth0 网卡为例。
注意:需要将网卡down掉后才能设置,否则会提示 “SIOCSIFHWADDR: 设备或资源忙 - you may need to down the interface”

//修改网卡的MAC地址
sudo ifconfig eth0 hw ether 08:00:27:bd:61:3d
// 注意:这些都不建议在服务器上使用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

11.设置网卡的最大传输单元(临时)

这里以 eth0 网卡为例。
单位是字节。

//设置网卡的最大传输单元
sudo ifconfig eth0 mtu 1480
  • 1
  • 2
  • 3
  • 4
  • 5

12.增加及删除网卡的IPv6地址

这里以 eth0 网卡为例。
//增加网卡的IPv6地址
sudo ifconfig eth0 add fe80::7035:2dfa:8d80:ea7d/64
//删除网卡的IPv6地址
sudo ifconfig eth0 del fe80::7035:2dfa:8d80:ea7d/64
// 注意:这些都不建议在服务器上使用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

13.永久修改网卡信息

网卡的配置文件路径为/etc/sysconfig/network-scripts/
例如:
[root@VM-12-14-centos ~]# ls /etc/sysconfig/network-scripts/

ifcfg-eth0
ifcfg-lo
ifdown
ifdown-bnep
ifdown-eth
ifdown-ib
ifdown-ippp
ifdown-ipv6
ifdown-isdn
ifdown-post
ifdown-ppp
ifdown-routes
ifdown-sit
ifdown-Team
ifdown-TeamPort
ifdown-tunnel
ifup
ifup-aliases
ifup-bnep
ifup-eth
ifup-ib
ifup-ippp
ifup-ipv6
ifup-isdn
ifup-plip
ifup-plusb
ifup-post
ifup-ppp
ifup-routes
ifup-sit
ifup-Team
ifup-TeamPort
ifup-tunnel
ifup-wireless
init.ipv6-global
network-functions
network-functions-ipv6
route6-eth0

我们可以查看网卡eth0的信息,就是上述的ifcfg-eth0文件
[root@VM-12-14-centos ~]# cat /etc/sysconfig/network-scripts/ifcfg-etho

# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
HWADDR=52:54:00:39:b6:85
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no

对此文件进行修改,可以永久改变网卡信息,但不建议在服务器上使用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/247456
推荐阅读
相关标签
  

闽ICP备14008679号