04-iptables-nat

本文详细介绍了iptables的NAT配置,包括地址转换前后的状态、源地址转换(source NAT)、目标地址转换以及端口映射。通过示例展示了如何开启Linux系统的IP转发功能,设置静态路由,以及在iptables中添加和修改规则,以实现内网与外网的通信。此外,还讨论了public和private IP的概念。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值