ifconfig是我们最常用的查看当前系统的网卡和 ip 地址信息的命令。如果你的机器上还没安装ifconfig命令,可以使用如下命令安装:
yum install net-tools安装成功以后,我们就可以使用ifconfig查看机器网卡信息了:
[root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1005 bytes 77224 (75.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 349 bytes 47206 (46.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 1000 (Local Loopback) RX packets 4 bytes 352 (352.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4 bytes 352 (352.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0上述输出显示了机器当前激活的网卡信息及每个网卡(网络设备)的 ipv4、ipv6 地址、子网掩码、广播地址等信息,这里一共两个网卡,分别是ens33和lo。
我们可以使用-s选项显示网卡信息的精简列表:
[root@localhost ~]# ifconfig -s Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens33 1500 1086 0 0 0 379 0 0 0 BMRU lo 65536 4 0 0 0 4 0 0 0 LRU默认情况下,ifconfig命令只会显示激活的网卡信息,如果要显示所有的(包括未激活)的网卡,我们可以使用-a参数。
如果我们想激活或者禁用某个网卡,可以使用ifconfig up和ifconfig down命令,用法如下:
ifconfig 网卡名 up ifconfig 网卡名 down默认情况下,ifconfig命令只会显示激活的网卡信息,如果要显示所有的(包括未激活)的网卡,我们可以使用-a参数。
如果我们想激活或者禁用某个网卡,可以使用ifconfig up和ifconfig down命令,用法如下:
ifconfig 网卡名 up ifconfig 网卡名 down我们来演示一下:
[root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1398 bytes 114269 (111.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 601 bytes 97657 (95.3 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 1000 (Local Loopback) RX packets 12 bytes 1056 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1056 (1.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ## 禁用网卡 lo 后默认只能看到一个网卡信息 [root@localhost ~]# ifconfig lo down [root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1510 bytes 123232 (120.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 657 bytes 104751 (102.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ifconfig -a ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1543 bytes 125948 (122.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 675 bytes 107251 (104.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=8<LOOPBACK> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 12 bytes 1056 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1056 (1.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ## 再次启用网卡 lo [root@localhost ~]# ifconfig lo up [root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1615 bytes 131924 (128.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 715 bytes 112423 (109.7 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 1000 (Local Loopback) RX packets 12 bytes 1056 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1056 (1.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ifconfig还可以将一个 ip 地址绑定到某个网卡上,或将一个 ip 从某个网卡上解绑,使用如下命令:
ifconfig 网卡名 add ip地址 ifconfig 网卡名 del ip地址演示如下:
[root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1615 bytes 131924 (128.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 715 bytes 112423 (109.7 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 1000 (Local Loopback) RX packets 12 bytes 1056 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1056 (1.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ifconfig ens33 add 192.168.206.150 [root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 1804 bytes 145940 (142.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 781 bytes 119581 (116.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.150 netmask 255.255.255.0 broadcast 192.168.206.255 ether 00:0c:29:ee:01:80 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 1000 (Local Loopback) RX packets 12 bytes 1056 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1056 (1.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0上述操作中,对于网卡ens33原来绑定了 ip 地址 192.168.206.140,我们使用ifconfig add命令绑定了一个新的 ip :192.168.206.150,现在我们可以使用这个新的 ip 地址来访问原来的网络了。
同理,如果要解绑这个 ip,可以按如下操作:
[root@localhost ~]# ifconfig ens33 del 192.168.206.150 [root@localhost ~]# ifconfig -a ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.206.140 netmask 255.255.255.0 broadcast 192.168.206.255 inet6 fe80::1599:dcc4:b3e8:7fce prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ee:01:80 txqueuelen 1000 (Ethernet) RX packets 2127 bytes 172321 (168.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 953 bytes 139954 (136.6 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 1000 (Local Loopback) RX packets 18 bytes 1560 (1.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 18 bytes 1560 (1.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0