iptables 源地址伪装

环境说明:

虚拟机1:路由

双网卡:

     NAT网卡 ens160:
        IP 192.168.40.22/24
   	    网关 192.168.40.2
    仅主机网卡 ens224:
        IP 172.25.40.22/24

若未安装iptables-services
先安装iptables-services

yum install -y iptables-services

后停止firewalld,启用iptables

systemctl stop firewalld && systemctl disable firewalld
systemctl start iptables && systemctl enable iptables

虚拟机2:NAT网卡客户端

    IP 192.168.40.201

虚拟机3:仅主机网卡客户端

    IP 172.25.40.200

步骤一

将两台虚拟机网关分别配置为路由的IP

nmcli connection modify ens160 ipv4.gateway 192.168.40.22
nmcli connection modify ens160 ipv4.gateway 172.25.40.22

步骤二

	[root@route ~]# sysctl -a | grep ip_forward
	net.ipv4.ip_forward = 1
	net.ipv4.ip_forward_update_priority = 1
	net.ipv4.ip_forward_use_pmtu = 0
	[root@route ~]# 

net.ipv4.ip_forward = 0 # 内核转发关闭
net.ipv4.ip_forward = 1 # 内核转发开启

若内核转发未开启

	echo "net.ipv4.ip_forward=1"  >> /etc/sysctl.conf #添加配置信息到配置文件中
	sysctl -p #配置生效

步骤三

清除已有的nat表POSTROUTING规则(可选,避免冲突)

iptables -t nat -F POSTROUTING

将来自源 IP 地址为 172.25.40.200 的数据包,在从 ens160 网卡转发出去之前,将其源 IP 地址修改为 111.222.111.222

iptables -t nat -A POSTROUTING -s 172.25.40.200 -o ens160 -j SNAT --to-source 111.222.111.222
[root@route ~]# iptables -L POSTROUTING -t nat
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  172.25.40.0/24       anywhere             to:111.222.111.222

使用ssh同时连接两台客户端然后再用172.25.40.22连接另一台输入指令w -i则会输出

[root@cli192201 ~]# w -i
17:42:02 up  1:45,  3 users,  load average: 0.00, 0.00, 0.04
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                17:04   33:54   0.08s  0.08s -bash
root     pts/0    111.222.111.222  17:41    1.00s  0.08s  0.02s w -i
root     pts/1    192.168.40.22    17:41   12.00s  0.05s  0.05s -bash
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值