ifconfig:配置或显示网络接口信息

xiaohai 2021-06-20 21:09:41 4551人围观 标签: Linux 
简介ifconfig命令用于显示网卡IP地址等网络参数或显示当前网络的接口状态。ifconfig命令在配置网卡信息的时候,必须使用root命令来进行执行。
说明

  ifconfig命令用于显示网卡IP地址等网络参数或显示当前网络的接口状态。ifconfig命令在配置网卡信息的时候,必须使用root命令来进行执行。

注意:若系统中没有ifconfig命令,就需要进行安装,命令:yum install net-tools

格式

  ifconfig [interface(网络接口)] [option]

interface:为网络的接口名,Linux下的网络接口类似于eth0,eth1和lo等,分别表示第一块网卡,第二块网卡和回环接口。这是可选项,如果不输入该选项,那么就显示系统中的所有网卡信息。

注意:ifconfig命令配置网卡信息是临时有效的,重启服务器或重启网络就会失效,如果要永久生效,就需要去修改网卡的配置文件

网卡类型 网卡标识 对应的配置文件位置
第一个物理网卡 eno16777736 /etc/sysconfig/network-scripts/ifcfg-eno16777736
第一个物理网卡别名IP eno16777736:0 /etc/sysconfig/network-scripts/ifcfg-eno16777736:0

注意:这里使用的使用Centos7默认下的网卡名称,以前的网卡名称是:eth0或者是eth1,可以把Centos7修改成这种名称,百度上的文章很多

常用参数说明
  • -a:显示所有的网络接口信息,包括活动和非活动的
  • up:激活指定的网络接口
  • down:关闭指定的网络接口
  • hw:设置网络接口的物理地址(MAC地址)
示例

1、显示系统开启的所有网络接口信息

[root@localhost ~]# ifconfig 
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.71.108  netmask 255.255.255.0  broadcast 192.168.71.255
        inet6 fe80::20c:29ff:fe1e:d826  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1e:d8:26  txqueuelen 1000  (Ethernet)
        RX packets 2463  bytes 185778 (181.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 841  bytes 121903 (119.0 KiB)
        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 0  (Local Loopback)
        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@localhost ~]# ifconfig eno16777736
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.71.108  netmask 255.255.255.0  broadcast 192.168.71.255
        inet6 fe80::20c:29ff:fe1e:d826  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1e:d8:26  txqueuelen 1000  (Ethernet)
        RX packets 2508  bytes 189224 (184.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 856  bytes 124177 (121.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ifconfig lo
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 0  (Local Loopback)
        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@localhost ~]# ifconfig eno16777736 up #开启指定的网卡
[root@localhost ~]# ifconfig eno16777736 down #关闭指定的网卡

4、为网卡配置IP地址(不要去关闭外网的网卡,否则只有重启服务器)

[root@localhost ~]# ifconfig eno16777736 192.168.71.108

5、为网卡配置别名IP,配置别名IP实际上就是为了给一个网卡配置多个IP地址

[root@localhost ~]# ifconfig eno16777736:0 192.168.71.109 netmask 255.255.255.0 up
[root@localhost ~]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.71.108  netmask 255.255.255.0  broadcast 192.168.71.255
        inet6 fe80::20c:29ff:fe1e:d826  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1e:d8:26  txqueuelen 1000  (Ethernet)
        RX packets 3006  bytes 229348 (223.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1134  bytes 158492 (154.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#这里就是配置别名后的IP信息
eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.71.109  netmask 255.255.255.0  broadcast 192.168.71.255
        ether 00:0c:29:1e:d8:26  txqueuelen 1000  (Ethernet)

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 0  (Local Loopback)
        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

6、修改网卡MAC地址

[root@localhost ~]# ifconfig eno16777736 hw ether 00:AB:12:CC:DE:99
[root@localhost ~]# ifconfig 
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.71.108  netmask 255.255.255.0  broadcast 192.168.71.255
        inet6 fe80::20c:29ff:fe1e:d826  prefixlen 64  scopeid 0x20<link>
        ether 00:ab:12:cc:de:99  txqueuelen 1000  (Ethernet)
        RX packets 3374  bytes 260157 (254.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1328  bytes 189263 (184.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.71.109  netmask 255.255.255.0  broadcast 192.168.71.255
        ether 00:ab:12:cc:de:99  txqueuelen 1000  (Ethernet)

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 0  (Local Loopback)
        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