路由表的产生
# 查看当前路由表 [root@CentOS ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth4 192.168.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
路由表的设计规则:
(1) 依据网络接口产生的 IP 而存在的路由
(2) 手动或默认路由
(3) 动态路由
路由规则通过内核实现,所以运行在内存中。IP Alias: 一个网卡绑定多个 IP
使同一张网卡具有多个 IP ,具有多个 IP 的功能就被称为 IP Alias
IP Alias的用途:
(1) 测试用。# 虚拟一个网络接口 [root@CentOS ~]# ifconfig [device] [ IP ] netmask [netmask ip] [up|down] [root@CentOS ~]# ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0 up
(2) 在一个实体网络中含有多个 IP 网络
(3) 既有设备无法连接提供更多实体网卡时
所有的 IP Alias 都是由实体网卡仿真的,所以当要启动 eth0:0 时,eth0 必须要先被启动才行。而当 eth0 被关闭后,所有eth0:n 的沫尼网咖也将同时被关闭# 开机的时候启动 IP Alias [root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0 DEVICE=eth0:0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.5.100 NETMASK=255.255.255.0 [root@CentOS ~]# ifup eth0:0 [root@CentOS ~]# ifdown eth0:0 [root@CentOS ~]# /etc/init.d/network restart
重复路由的问题
如果
eth0:192.168.0.100
eth1: 192.168.0.200[root@CentOS ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1 192.168.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
不应设置同一网段的不通 IP 在同一台主机上面,因为进出的数据包都只会通过第一条路由的网络接口
路由器配置
# 查看是否打开路由转发功能 [root@CentOS ~]# cat /proc/sys/net/ipv4/ip_forward 0 #0代表未启动 ,1代表启动 # 临时设置路由转发功能 [root@CentOS ~]# echo 1 > /proc/sys/net/ipv4/ip_forward # 设置永久有效 [root@CentOS ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 # 立即让该设置生效 [root@CentOS ~]# sysctl -p
静态路由的路由器
# 配置网关 A: [root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" HWADDR="00:0C:29:70:C4:BB" NM_CONTROLLED=no ONBOOT=yes BOOTPROTO=none IPADDR=192.168.1.254 NETMASK=255.255.255.0
# 配置router B : # 配置网卡eth6: [root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth6 DEVICE="eth6" HWADDR="00:0C:29:70:94:9C" NM_CONTROLLED=no ONBOOT=yes BOOTPROTO=none IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.254 #网关,作为网关的主机必须存在 # 配置网卡eth7: [root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth7 DEVICE="eth7" HWADDR="00:0C:29:70:94:7E" NM_CONTROLLED="no" ONBOOT="yes" BOOTPROTO=none IPADDR=192.168.100.250 NETMASK=255.255.255.0 # 启动 IP 传递,实际操作成功才行 [root@CentOS ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 # 立即生效 [root@CentOS ~]# sysctl -p # 检查是否生效 [root@CentOS ~]# cat /proc/sys/net/ipv4/ip_forward 1 # 重启网络,查看路由,并进行 ping 测试 [root@CentOS ~]# /etc/init.d/network restart # 查看路由表 [root@CentOS ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth7 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth6 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth7 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth6 0.0.0.0 192.168.1.254 0.0.0.0 UG 0 0 0 eth6 [root@CentOS ~]# ping 192.168.1.254 # 必要的时候关闭防火墙 [root@CentOS ~]# /etc/init.d/iptables stop
# 配置内网主机 C: [root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" HWADDR="00:0C:29:70:C4:DE" NM_CONTROLLED="no" ONBOOT="yes" BOOTPROTO=none IPADDR=192.168.100.10 NETMASK=255.255.255.0 GATEWAY=192.168.100.254 DNS1=168.95.1.1 #有这个就不用自己改 /etc/resolv.conf [root@CentOS ~]# /etc/init.d/network [root@CentOS ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth0 0.0.0.0 192.168.100.254 0.0.0.0 UG 0 0 0 eth0 [root@CentOS ~]# ping -c 2 192.168.100.254 # ping 自己的网关会成功 [root@CentOS ~]# ping -c 2 192.168.1.254 # ping 外部的网关不成功 (隔了一个子网)
# 添加路由规则 (在作为网关的那台主机A添加路由规则) # 临时有效(在作为网关的那台主机A添加路由规则) [root@CentOS ~]# route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.1.100 # 永久有效(在作为网关的那台主机A添加路由规则) [root@CentOS ~]# vim /etc/sysconfig/network-scripts/router-eth0 192.168.100.0/24 via192.168.10.100 dev eth0 [root@CentOS ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 120.114.142.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.100.0 192.168.1.100 255.255.255.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1 0.0.0.0 120.114.142.254 0.0.0.0 UG 0 0 0 eth1
鸟哥的服务器《八》路由与路由器的设置
最新推荐文章于 2022-06-29 11:46:44 发布