2016-08-15-Linux-NAT
设置 IPv4 转发
1 | vim /etc/sysctl.conf |
sysctl
sysctl 配置与显示在/proc/sys 目录中的内核参数.可以用 sysctl 来设置或重新设置联网功能,如 IP 转发、IP 碎片去除以及源路由检查等。用户只需要编辑/etc/sysctl.conf 文件,即可手工或自动执行由 sysctl 控制的功能。
命令格式:
1 | sysctl [-n] [-e] -w variable=value |
如果仅仅是想临时改变某个系统参数的值,可以用两种方法来实现,例如想启用 IP 路由转发功能:
1 | echo 1 > /proc/sys/net/ipv4/ip_forward |
以上两种方法都可能立即开启路由功能,但如果系统重启,或执行了service network restart
命令,所设置的值即会丢失,如果想永久保留配置,可以修改/etc/sysctl.conf 文件
将 net.ipv4.ip_forward=0 改为 net.ipv4.ip_forward=1
iptables
详细链接:iptables.html
查看 iptables 规则内容
1 | service iptables status |
清空 iptables 规则
1 | iptables -F |
允许 192.168.10.0.0/16 连接 ssh
1 | iptables -t filter -A INPUT -s 192.168.0.0/16 -p tcp --dport 22 -j ACCEPT |
INPUT 默认策略改为 DROP
1 | iptables -P INPUT DROP |
OUTPUT 默认策略改为 ACCEPT
1 | iptables -P OUTPUT ACCEPT |
添加 SNAT
1 | iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o em1 -j MASQUERADE |
不需要下面这条
1 | iptables -t nat -R POSTROUTING 2 -d 192.168.10.2 -p tcp --dport 8081 -j SNAT --to 172.10.2.201:80 |
添加 DNAT
1 | iptables -t nat -A PREROUTING -d 172.10.2.201 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.10:80 |
添加 INPUT 允许
1 | iptables -t filter -A INPUT -s 172.10.2.201/32 -p tcp --dport 80 -j ACCEPT |
允许 PING
1 | iptables -A INPUT -p icmp -j ACCEPT |
临时添加多个公网 IP
1 | ip address add 172.10.2.203/24 dev em1 |
永久添加多个公网 IP
仿照/etc/sysconfig/network-scripts/ifcfg-em1 增加一文件根据网络虚拟接口的名字进行命名
/etc/sysconfig/network-scripts 目录下复制 ifcfg-em1 到 ifcfg-em1:0
1 | cp ifcfg-em1 ifcfg-em1:0 |
修改 ifcfg-em1:0 文件
1 | DEVICE=em1:0 |
ifconfig em1:0 down
关闭虚拟接口
1 | ifconfig eth*[:x] down(*代表的是网卡编号,x代表虚拟接口号0-255) |
ifconfig em1:0 up
启动虚拟网口出现错误
SIOCSIFFLAGS: Cannot assign requested address
出现以上错误的原因是把 em1:1 的信息写在了 ifcfg-em1:1 的配置文件里,本意是想,开机启动的时候自动加载,实现一块网卡双 IP。
但当手动把 em1:1 设备 down(执行了:“ifconfig em1:1 down”),然后再启用的时候会报以上错误,主要是 mac 地址重复了
解决办法:
1、手动分配一个 mac 地址(不建议)
2、手动执行添加 IP 的命令,把写在 ifcfg-eth0:1 里的信息配置上,如
ifconfig eth0:1 192.168.1.2/24
route add default gw 192.168.1.1 dev eth0:1
查看 IP
1 | ip addr |
添加默认网关
1 | route add default gw 172.10.2.1 |
问题
iptables -P INPUT DROP 后无法 ping 通 www.baidu.com
(无法进行域名解析)
使用 iptables -P INPUT ACCEPT 允许所有通过
1 | iptables -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT |
demo
配置后内容
1 | [root@mysql ~]# vim /etc/sysconfig/iptables |
IP 命令
链接:https://www.cnblogs.com/bamboo-talking/archive/2013/01/10/2855306.html
ip link set–改变设备的属性. 缩写:set、s
示例 1:up/down 起动/关闭设备。
ip link set dev eth0 up
这个等于传统的 ifconfig eth0 up(down)
ip link show–显示设备属性. 缩写:show、list、lst、sh、ls、l
-s 选项出现两次或者更多次,ip 会输出更为详细的错误信息统计。
示例:
ip -s -s link ls eth0
eth0: mtu 1500 qdisc cbq qlen 100
link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
2449949362 2786187 0 0 0 0
RX errors: length crc frame fifo missed
0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
178558497 1783946 332 0 332 35172
TX errors: aborted fifo window heartbeat
0 0 0 332
这个命令等于传统的 ifconfig eth0
ip address add–添加一个新的协议地址. 缩写:add、a
示例 1:为每个地址设置一个字符串作为标签。为了和 Linux-2.0 的网络别名兼容,这个字符串必须以设备名开头,接着一个冒号,
ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0
示例 2: 在以太网接口 eth0 上增加一个地址 192.168.20.0,掩码长度为 24 位(155.155.155.0),标准广播地址,标签为 eth0:Alias:
ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1
这个命令等于传统的: ifconfig eth1:1 192.168.4.2
ip address delete–删除一个协议地址. 缩写:delete、del、d
ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1
ip address show–显示协议地址. 缩写:show、list、lst、sh、ls、l
ip addr ls eth0
ip address flush–清除协议地址. 缩写:flush、f
示例 1 : 删除属于私网 10.0.0.0/8 的所有地址:
ip -s -s a f to 10/8
示例 2 : 取消所有以太网卡的 IP 地址
ip -4 addr flush label “eth0”
ip neighbour–neighbour/arp 表管理命令
缩写 neighbour、neighbor、neigh、n
命令 add、change、replace、delete、fulsh、show(或者 list)
ip neighbour add – 添加一个新的邻接条目
ip neighbour change–修改一个现有的条目
ip neighbour replace–替换一个已有的条目
缩写:add、a;change、chg;replace、repl
示例 1: 在设备 eth0 上,为地址 10.0.0.3 添加一个 permanent ARP 条目:
ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm
示例 2:把状态改为 reachable
ip neigh chg 10.0.0.3 dev eth0 nud reachable
ip neighbour delete–删除一个邻接条目
示例 1:删除设备 eth0 上的一个 ARP 条目 10.0.0.3
ip neigh del 10.0.0.3 dev eth0
ip neighbour show–显示网络邻居的信息. 缩写:show、list、sh、ls
示例 1: ip -s n ls 193.233.7.254
193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable
ip neighbour flush–清除邻接条目. 缩写:flush、f
示例 1: (-s 可以显示详细信息)
ip -s -s n f 193.233.7.254