网络配置拾遗
一、网桥设置
将设备中的两个网卡桥接
ifconfig eth0 0.0.0.0 up
ifconfig eth1 0.0.0.0 up
btctl addbr br0
brctl addif br0 eth0
brctl addif br0 eth1
ifconfig br0 192.168.10.1 up
桥接的两个网卡不设置ip,只需给网桥设置ip
二、设备中有两个网卡,当两个网卡设置同网段不同ip时(192.168.10.11、192.168.10.22),只有一个网卡能ping通
因为系统只从其中一张网卡获取网关,其他网卡来的ICMP请求包,会被系统过滤掉。
1、可以关闭反向路由检查防止系统过滤:
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/eth1/rp_filter
2、可以把两个网卡分到2个不同的路由表中
echo "210 table0" >> /etc/iproute2/rt_tables
echo "211 table1" >> /etc/iproute2/rt_tables
ip route flush table table0
ip route flush table table1
ip route add 192.168.10.0/24 dev eth0 src 192.168.10.11 table table0
ip route add 192.168.10.0/24 dev eth1 src 192.168.10.22 table table1
ip route add default dev eth0 via 192.168.10.1 table table0
ip route add default dev eth1 via 192.168.10.1 table table1
ip rule add from 192.168.10.11 table table0
ip rule add from 192.168.10.22 table table1
三、NAT路由转发
参考自https://www.latelee.org/using-gnu-linux/iptables-note-outside-to-inside.html
设备A有两张网卡eth0和eth1,eth0接外网,eth1(192.168.10.1)接内网设备B(192.168.10.2),
eth0和eth1不同网段。现把从eth0收到的包,通过eth1转发到设备B。
1、转发特定端口包
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 22 -j DNAT --to 192.168.10.2:22
iptables -t nat -A POSTROUTING -o eth1 -d 192.168.10.2 -p tcp --dport 22 -j SNAT --to 192.168.10.1
2、转发所有包
iptables -t nat -A PREROUTING -i eth0 -p tcp -j DNAT --to 192.168.10.2
iptables -t nat -A POSTROUTING -o eth1 -d 192.168.10.2 -p tcp -j SNAT --to 192.168.10.1