作者:E4b9a6, 创建:2021-06-16, 字数:3121, 已阅:114, 最后更新:2024-06-05
假设我有一台Linux设备,网络信息如下:
chancel@j3455 ~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master virbr1 state UP group default qlen 1000
link/ether 70:85:c2:82:20:27 brd ff:ff:ff:ff:ff:ff
inet 172.16.21.100/24 brd 172.16.21.255 scope global dynamic enp1s0
valid_lft 6057sec preferred_lft 6057sec
这台机器有一个网口 enp1s0
和一个 IPV4 地址 172.16.21.20
假设 enp1s0
接口接着另外一台电脑/路由器
想要实现通过这台Linux设备访问外网,这台Linux设备访问外网的方式可以是接手机共享网络/接移动SIM卡/外界网卡等
实现方法如下,首先是启用内核的网络转发功能
编辑: /etc/sysctl.conf
# 取消这一行的#号以启用IPV4转发
net.ipv4.ip_forward=1
让上述配置立即生效
sudo sysctl -p
编辑:/etc/iptables.sh
# 对所有出 enp1s0 以外的接口的流量进行源地址伪装
/sbin/iptables -t nat -A POSTROUTING -o !enp1s0 -j MASQUERADE
# 允许从 enp1s0 接口进入的所有流量转发到任何接口
/sbin/iptables -A FORWARD -i enp1s0 -j ACCEPT
# 允许处于 RELATED 或 ESTABLISHED 状态的流量转发到 enp1s0 接口
/sbin/iptables -A FORWARD -o enp1s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
借助 crontab 让这个脚本开机执行
执行命令:sudo crontab -e
# ...
# m h dom mon dow command
@reboot /bin/bash /etc/iptables.sh
重启设备,重启后检查 nat表 和 filter表 的配置,我的参考如下
chancel@j3455 ~$ sudo iptables -L -n -v -t nat
[sudo] password for chancel:
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !enp1s0 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
chancel@j3455 ~$ sudo iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- enp1s0 * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- -- enp1s0 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
这些规则配置后的效果: