- 查看网卡列表
[root@localhost ~]# ip addr
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: p1p1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
link/ether b4:96:91:06:6c:4b brd ff:ff:ff:ff:ff:ff
3: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 54:bf:64:9d:d5:bf brd ff:ff:ff:ff:ff:ff
由上可知有两块网卡,名字分别为 p1p1、enp0s31f6
- 配置网络(DHCP)
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s31f6
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s31f6
UUID=a26c47b1-9d4f-47e0-b22a-b439a4c781aa
DEVICE=enp0s31f6
ONBOOT=yes
- 配置网络(静态地址)
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s31f6
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s31f6
UUID=a26c47b1-9d4f-47e0-b22a-b439a4c781aa
DEVICE=enp0s31f6
ONBOOT=yes
IPADDR=192.168.10.242
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS1=114.114.114.114
#DNS2=192.168.10.211
PREFIX=24
- 重启网络配置
[root@localhost ~]# systemctl restart network.service
- 配置静态路由(临时添加,重启后会失效)
# 1. 显示路由表
[root@localhost ~]# ip route show
default via 192.168.10.1 dev enp0s31f6 proto static metric 100
172.16.15.0/24 dev p1p1 proto kernel scope link src 172.16.15.3 metric 101
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.10.0/24 dev enp0s31f6 proto kernel scope link src 192.168.10.239 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
# 2. 添加静态路由(p1p1是网卡名称)
[root@localhost ~]# ip route add 172.16.15.0/24 via 172.16.15.0 dev p1p1
# 3.再次查看路由表
default via 192.168.10.1 dev enp0s31f6 proto static metric 100
172.16.15.0/24 dev p1p1 proto kernel scope link src 172.16.15.3 metric 101
172.16.15.0/24 via 172.16.15.1 dev p1p1 proto static metric 101
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.10.0/24 dev enp0s31f6 proto kernel scope link src 192.168.10.239 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
- 删除静态路由
# 把 add 替换成 del
[root@localhost ~]# ip route del 172.16.15.0/24 via 172.16.15.0 dev p1p1
- 设置永久的静态路由(重启之后依然生效)
将静态路由需要写到 /etc/sysconfig/network-scripts/route-{interface} 文件中
[root@localhost ~]# vi /etc/sysconfig/network-scripts/route-p1p1
# 写入如下两条路由
172.16.15.0/24 via 172.16.15.1 dev p1p1
172.16.10.0/24 via 172.16.15.1 dev p1p1
# 再重启网络即可
[root@localhost ~]# systemctl restart network.service