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

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

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

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

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 地址

6.开关网卡

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

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

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

// 注意:这些都不建议在服务器上使用

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)

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

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

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

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

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

//设置网卡的最大传输单元
sudo ifconfig eth0 mtu 1480

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
// 注意:这些都不建议在服务器上使用

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

对此文件进行修改,可以永久改变网卡信息,但不建议在服务器上使用
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值