1、顯示路由表linux
route -ndocker
[root@dev-master ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.16.71.1 0.0.0.0 UG 100 0 0 ens160 10.2.8.128 172.16.71.201 255.255.255.192 UG 0 0 0 ens160 10.2.9.0 172.16.71.202 255.255.255.192 UG 0 0 0 ens160 10.2.19.0 0.0.0.0 255.255.255.192 U 0 0 0 docker0 10.2.21.128 172.16.71.203 255.255.255.192 UG 0 0 0 ens160 172.16.71.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
route安全
[root@dev-master ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 100 0 0 ens160 10.2.8.128 tiller.chinaclo 255.255.255.192 UG 0 0 0 ens160 10.2.9.0 172.16.71.202 255.255.255.192 UG 0 0 0 ens160 10.2.19.0 0.0.0.0 255.255.255.192 U 0 0 0 docker0 10.2.21.128 172.16.71.203 255.255.255.192 UG 0 0 0 ens160 172.16.71.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
從以上能夠看出加了-n參數,顯示的信息都變成了ip地址網絡
一、Destination 目標主機或網絡spa
二、Gatewar 網關,若是沒有就顯示*號code
三、Genmask 子網掩碼blog
四、Flags主要包括如下:接口
五、路由距離,到達指定網絡所需的中轉數(linux 內核中沒有使用)ip
六、Ref 路由項引用次數(linux 內核中沒有使用)路由
七、Use 此路由項被路由軟件查找的次數
八、Iface 該路由表項對應的輸出接口
第一條記錄:
0.0.0.0 172.16.71.1 0.0.0.0 UG 100 0 0 ens160
這是默認路由:上面的目標主機和子網掩碼都是0.0.0.0,表示去任何地方(0.0.0.0),都發給172.16.71.1
再看一條記錄:
10.2.19.0 0.0.0.0 255.255.255.192 U 0 0 0 docker0
上面中,網關是0.0.0.0,網關爲0.0.0.0的網絡一般是直連到網絡設備上的。由於到本身的直連設備上是不須要網關的,因此0.0.0.0的網關地址是正常的。
2、配置靜態路由
格式爲:route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
添加到主機的路由
# route add -host 192.168.1.2 dev eth0 # route add -host 10.20.30.148 gw 10.20.30.40 #添加到10.20.30.148的路由
添加到網絡的路由
# route add -net 10.20.30.40 netmask 255.255.255.248 eth0 #添加10.20.30.40的路由 # route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 #添加10.20.30.48的路由 # route add -net 192.168.1.0/24 eth1
添加默認路由
# route add default gw 192.168.1.1
刪除路由
# route del -host 192.168.1.2 dev eth0:0 # route del -host 10.20.30.148 gw 10.20.30.40 # route del -net 10.20.30.40 netmask 255.255.255.248 eth0 # route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 # route del -net 192.168.1.0/24 eth1 # route del default gw 192.168.1.1
在 CentOS 中默認的內核配置已經包含了路由功能,但默認並無在系統啓動時啓用此功能。開啓 Linux 的路由功能能夠經過調整內核的網絡參數來實現。要配置和調整內核參數可使用 sysctl 命令。例如:要開啓 Linux 內核的數據包轉發功能可使用以下的命令。
# sysctl -w net.ipv4.ip_forward=1
這樣設置以後,當前系統就能實現包轉發,但下次啓動計算機時將失效。爲了使在下次啓動計算機時仍然有效,須要將下面的行寫入配置文件/etc/sysctl.conf。
# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
用戶還可使用以下的命令查看當前系統是否支持包轉發。
# sysctl net.ipv4.ip_forward