网络基础管理
网卡命名
1.1 传统网卡命名机制
传统命名:
以太网[eth0.1,2…]
wlan[0.1,2…]
1.2 redhat 7 网卡命名机制systemd对网络设备的命名方式
-
如果Firmware或BIOS为主板上集成的设备提供的索引信息可用,且可预测,则根据此索引进行命名,例如eno1
-
如果Firmware或BIOS为PCI-E扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如ens1
-
如果硬件接口的物理位置信息可用,则根据此信息进行命名,例如enp2s0
-
如果用户显式启动,也可根据MAC地址进行命名,例如enx2387a1dc56
-
.上述均不可用时,则使用传统命名机制
上述命名机制中,有的需要biosdevname程序的参与。所以必须安装biosdevname程序且启用它。
1.3网络接口名称组成格式
基于固件、设备结构、设备类型
-
由两个字母开头标示固件
-
以太网网卡以en开头
-
线网卡以wl开头
-
-
设备结构
-
o:主板上集成的设备的设备索引号:
-
s:扩展槽的索引号
-
x:基于MAC地址的命名
-
ps: 基于物理位置拓扑的命名。如enp2s1,表示PCI总线上第2个总线的第1个插槽的设备
-
回归传统命名
- 修改网卡配置文件
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens160
- 修改文件名
[root@localhost network-scripts]# mv ifcfg-ens160 ifcfg-eth0
- 进入/etc/default/grub进行编辑
#编辑/etc/default/grub配置文件,在以GRUB_ CMDLINE_ LINUX开头的行内rhgb的前面加上net . ifnames=0
[root@localhost network-scripts]# vim /etc/default/grub
- 为grub2生成配置文件
[root@localhost network-scripts]# grub2-mkconfig -o /etc/grub2.cfg
Generating grub configuration file ...
done
[root@localhost network-scripts]# reboot
- 网卡回归传统命名完成
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.5/24 brd 192.168.72.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe91:bd4/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:91:0b:de brd ff:ff:ff:ff:ff:ff
网络管理常用命令
1.1 ifconfig
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.5 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::20c:29ff:fe91:bd4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:91:0b:d4 txqueuelen 1000 (Ethernet)
RX packets 311 bytes 25623 (25.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 167 bytes 17551 (17.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:91:0b:de txqueuelen 1000 (Ethernet)
RX packets 129 bytes 8439 (8.2 KiB)
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
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 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 ip
语法: ip [ OPTIONS ] OBJECT { COMMAND| help }
OBJECT:
link:网络接口属性
addr:协议地址
route:路由
- 查看网卡连接状态、IP地址、路由
ip link show
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:de brd ff:ff:ff:ff:ff:ff
ip -s link show
[root@localhost ~]# ip -s link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
221401 1725 0 0 0 0
TX: bytes packets errors dropped carrier collsns
56014 591 0 0 0 0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:de brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
62428 955 0 0 0 0
TX: bytes packets errors dropped carrier collsns
62971 358 0 0 0 0
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:e8 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
108617 1098 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1146 15 0 0 0 0
ip addr show
[root@localhost ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.5/24 brd 192.168.72.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe91:bd4/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:91:0b:de brd ff:ff:ff:ff:ff:ff
ip route show
[root@localhost ~]# ip route show
default via 192.168.72.254 dev eth0 proto static metric 100
192.168.72.0/24 dev eth0 proto kernel scope link src 192.168.72.5 metric 100
启用/禁用网卡
- 禁用网卡
[root@localhost ~]# ip link set eth0 down
[root@localhost ~]# ip link show eth0
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
#此时状态为DOWN mode
- 启用网卡
[root@localhost ~]# ip link set eth0 up
[root@localhost ~]# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
#此时状态为UP mode
查看指定网卡的IP地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:91:0b:d4 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.5/24 brd 192.168.72.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forev