03-iptables-nat
总体模型
PC1
forward
PC2
新增网卡后请使用nmtui命令,ip addr add … dev ..,ip link set … up/down命令设置网卡上的IP地址
地址属于kernel中的TCPIP协议栈
开启forward的核心转发功能以及添加正确的静态路由
[root@localhost ~]# cd /proc/sys/net/ipv4
[root@localhost ipv4]# ls
ip_dynaddr tcp_frto_response tcp_tso_win_divisor
ip_forward tcp_keepalive_intvl tcp_tw_recycle
ip_forward_use_pmtu tcp_keepalive_probes tcp_tw_reuse
[root@localhost ipv4]# echo '1' > ip_forward
forward路由条目
[root@localhost ipv4]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.200.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
172.16.0.0 0.0.0.0 255.255.0.0 U 1 0 0 eth0
0.0.0.0 172.16.0.1 0.0.0.0 UG 0 0 0 eth0
PC1路由条目
[root@husa ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.0.1 0.0.0.0 UG 100 0 0 eno16777736
172.16.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eno16777736
PC2路由条目
可以看到forward的默认条目由172.16.0.1转发,但是PC2的路由条目中只有192.168.200.0网段通过eno33554984转发;因此PC2无法ping通172.16.11.207,要想使得PC2ping通forward就必须添加默认条目
同样PC1也是无法ping通192.168.200.206的,因为其默认条目通过172.16.0.1转发,是不可能到达192.168.200.206的。
要想建立forward的转发功能,就要把PC1和PC2的路由条目设置好了再进行接下来的任务
添加路由条目
PC1
[root@husa ~]# route add -net 192.168.200.0/24 gw 172.16.11.207 dev eno16777736
[root@husa ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.0.1 0.0.0.0 UG 100 0 0 eno16777736
172.16.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eno16777736
192.168.200.0 172.16.11.207 255.255.255.0 UG 0 0 0 eno16777736
[root@husa ~]# !ping
ping 192.168.200.206
PING 192.168.200.206 (192.168.200.206) 56(84) bytes of data.
64 bytes from 192.168.200.206: icmp_seq=1 ttl=63 time=0.899 ms
64 bytes from 192.168.200.206: icmp_seq=2 ttl=63 time=0.487 ms
^C
--- 192.168.200.206 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.487/0.693/0.899/0.206 ms
PC2
通过对PC1、PC2的路由条目的设置,PC1、forward之间可以互相ping通;PC2、forward之间可以互相ping通;但是PC1和PC2之间无法ping通,因为forward还没有打开核心转发功能…
删除默认路由:route del default gw x.x.x.x
何谓转发?转发就是将消息发送给另一台主机而不是自己的另一张网卡,正因为如此,在forward没有打开核心转发功能之前,PC1(或PC2)只能和forward之间通信,而PC1、PC2之间不能相互通信;地址属于kernel而转发是需要网卡之间的配合工作
打开forward核心转发功能
1、 直接修改/proc/sys/net/ipv4/ip_forward中的值
[root@localhost ~]# echo '1' > /proc/sys/net/ipv4/ip_forward
2、 通过syscrtl修改
# 显示所有系统内核参数
[root@localhost ~]# sysctl -a
kernel.sched_child_runs_first = 0
kernel.sched_min_granularity_ns = 2000000
kernel.sched_latency_ns = 10000000
kernel.sched_wakeup_granularity_ns = 2000000
kernel.sched_tunable_scaling = 1
kernel.sched_features = 3183
kernel.sched_migration_cost = 500000
kernel.sched_nr_migrate = 32
kernel.sched_time_avg = 1000
kernel.sched_shares_window = 10000000
....
# 临时修改内核参数
[root@localhost ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
# 从配置文件中读取内核参数,默认为/etc/sysctl.conf文件
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
3、 开机立即生效的设定
直接在/etc/sysctl.conf文件中添加一行net.ipv4.ip_forward = 1
[root@localhost ~]# vim /etc/sysctl.conf
# Kernel sysctl configuration file for Re