嘘~ 正在从服务器偷取页面 . . .

CentOS/RHEL7 配置iptables总结


简介

大多数Linux发行版默认运行的基于主机的防火墙都是iptables。要允许主机彼此通信,可以关闭iptables
或配置iptables以允许通信。显然第二个选项更为安全。在CentOS/RHEL7以前的版本中,默认使用的是
iptables防火墙管理服务来配置防火墙。到了CentOS/RHEL7版本,系统默认的防火墙已经不是iptables,
而是引入了firewalld。但是在7版本中还是可以使用iptables的。出于各种原因,很多CentOS/RHEL7用户
还是会选择继续使用iptables。这里就来总结一下iptables的使用方法。

防火墙以从上到下的顺序来读取配置的策略规则,找到匹配项后就立即结束匹配工作并去执行匹配项中定义的
行为(放行或阻止)。如果在读取完所有的策略规则之后没有匹配项,就去执行默认的策略。一般而言,防火
墙策略规则的设置有两种:一种是“通”(即放行),一种是“堵”(即阻止)。当防火墙的默认策略为拒绝时时
(阻止),就要设置允许规则(放行),否则任何流量都进不来;如果防火墙的默认策略为允许时(放行),
就要设置拒绝规则,否则任何流量都能进来,防火墙也就失去了防范的作用。一般来说,配置防火墙都是用来
防范由外到内的入站流量,因此配置iptables时使用的最多的就是INPUT规则链,INPUT规则链可以有管理控
制入站流量是否符合安全要求。对应INPUT规则的是相应的动作,比ACCEPT(允许流量通过)、REJECT(拒绝
流量通过)、LOG(记录日志信息)、DROP(拒绝流量通过)等。通过规则与动作的组合,就形成了完整的防
火墙字全策略。
正确地配置iptables是一项复杂的工作,这需要对联网有深入的了解。iptables 服务把用于处理或过滤流量
的策略条目称之为规则,多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类,具
体如下:
● 在进行路由选择前处理数据包( PREROUTING)
● 处理流入的数据包( INPUT)
● 处理流出的数据包( OUTPUT)
● 处理转发的数据包( FORWARD)
● 在进行路由选择后处理数据包( POSTROUTING)

iptables中常用的参数
-P 设置默认策略
-F 清空规则链
-L 查看规则链
-A 在规则链的末尾加入新规则
-I num 在规则链的头部加入新规则
-D num 删除某一条规则
-s 匹配来源地址 IP/MASK,加叹号“!”表示除这个 IP 外
-d 匹配目标地址
-i 网卡名称 匹配从这块网卡流入的数据
-o 网卡名称 匹配从这块网卡流出的数据
-p 匹配协议,如 TCP、 UDP、 ICMP
–dport num 匹配目标端口号
–sport num 匹配来源端口号

准备工作

1,停止并禁用firewalld,iptables和firewalld无法同时使用,所以要使用iptables,需要先禁用
firewalld。
systemctl stop firewalld
systemctl disable firewalld
2,安装iptables
yum install -y iptables iptables-services
3,启动并设置开机自动启动
systemctl start iptables
systemctl enable iptables
4,查看服务状态
systemctl status iptables
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since Sat 2018-10-13 12:48:20 CST; 3s ago
Process: 1802 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 1802 (code=exited, status=0/SUCCESS)
Oct 13 12:48:20 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables…
Oct 13 12:48:20 localhost.localdomain iptables.init[1802]: iptables: Applying firewall rules…]
Oct 13 12:48:20 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
Hint: Some lines were ellipsized, use -l to show in full.

操作示例(先禁止所有,再配置放行规则):

1,查看现有规则
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all – anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp – anywhere anywhere
ACCEPT all – anywhere anywhere
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:ssh
REJECT all – anywhere anywhere reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all – anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2,清空防火墙规则,这里主要是清空现有的默认规则
iptables -F
3,默认拒绝所有入站流量
iptables -P INPUT DROP
4,允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
5,开放22端口
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
6,如果只在指定的接口开放22端口,例如ens192
iptables -A INPUT -i ens192 -p tcp –dport 22 -j ACCEPT
7,开放21端口(FTP)
iptables -A INPUT -p tcp –dport 21 -j ACCEPT
8,开放80端口(http)
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
9,开放443端口(https)
iptables -A INPUT -p tcp –dport 443 -j ACCEPT
10,只允许IP地址172.16.60.200访问8088端口
iptables -I INPUT -p tcp -s 172.16.80.200 –dport 8088 -j ACCEPT
11,只允许IP地址段172.16.70.0/24访问3388端口
iptables -I INPUT -s 172.16.70.0/16 -p tcp –dport 3388 -j ACCEPT
12,允许接受本机请求之后的返回数据 RELATED,这个是为FTP设置的
iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
13,允许ping请求
iptables -I INPUT -p icmp -j ACCEPT
14,放行所有出站流量
iptables -P OUTPUT ACCEPT
15,禁止所有转发流量
iptables -P FORWARD DROP
16,保存配置
service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
17,重启服务
systemctl restart iptables
18,查看修改后的配置
iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp – anywhere anywhere
ACCEPT tcp – 172.16.0.0/16 anywhere tcp dpt:cbserver
ACCEPT tcp – 172.16.80.200 anywhere tcp dpt:radan-http
ACCEPT tcp – anywhere anywhere tcp dpt:ssh
ACCEPT tcp – anywhere anywhere tcp dpt:ftp
ACCEPT tcp – anywhere anywhere tcp dpt:http
ACCEPT tcp – anywhere anywhere tcp dpt:https
ACCEPT all – anywhere anywhere state RELATED,ESTABLISHED
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

操作示例(先允许所有,再配置放行规则):

1,清空默认防火墙规则
iptables -F
2,清空自定义规则
iptables -X
3,放行所有
iptables -P INPUT ACCEPT
4,允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
5,开放22端口
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
6,开放21端口(FTP)
iptables -A INPUT -p tcp –dport 21 -j ACCEPT
7,开放80端口(HTTP)
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
8,开放443端口(HTTPS)
iptables -A INPUT -p tcp –dport 443 -j ACCEPT
9,允许ping
iptables -A INPUT -p icmp –icmp-type 8 -j ACCEPT
10,允许接受本机请求之后的返回数据 RELATED,这个是为FTP设置的
iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
11,只允许IP地址172.16.60.200访问8088端口
iptables -I INPUT -p tcp -s 172.16.80.200 –dport 8088 -j ACCEPT
12,只允许IP地址段172.16.70.0/24访问3388端口
iptables -I INPUT -s 172.16.70.0/16 -p tcp –dport 3388 -j ACCEP
13,其他入站一律丢弃
iptables -P INPUT DROP
14,所有出站一律绿灯
iptables -P OUTPUT ACCEPT
15,所有转发一律丢弃
iptables -P FORWARD DROP
16,保存配置
service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
17,重启服务
systemctl restart iptables
18,查看配置
iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp – anywhere anywhere
ACCEPT tcp – 172.16.0.0/16 anywhere tcp dpt:cbserver
ACCEPT tcp – 172.16.80.200 anywhere tcp dpt:radan-http
ACCEPT tcp – anywhere anywhere tcp dpt:ssh
ACCEPT tcp – anywhere anywhere tcp dpt:ftp
ACCEPT tcp – anywhere anywhere tcp dpt:http
ACCEPT tcp – anywhere anywhere tcp dpt:https
ACCEPT all – anywhere anywhere state RELATED,ESTABLISHED
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

使用netstat查看监听进程

t – TCP
u – UDP
l – 只显示监听进程
n – 不解析网络IP地址名称或端口号
p – 显示监听端口的进程名
示例:
netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1066/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1270/master
tcp6 0 0 :::22 :::* LISTEN 1066/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1270/master
udp 0 0 127.0.0.1:323 0.0.0.0:* 749/chronyd
udp6 0 0 ::1:323 :::* 749/chronyd

使用ss查看监听进程

ss -nutlp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 127.0.0.1:323 : users:((“chronyd”,pid=749,fd=1))
udp UNCONN 0 0 ::1:323 :::* users:((“chronyd”,pid=749,fd=2))
tcp LISTEN 0 128 *:22 *:* users:((“sshd”,pid=1066,fd=3))
tcp LISTEN 0 100 127.0.0.1:25 : users:((“master”,pid=1270,fd=13))
tcp LISTEN 0 128 :::22 :::* users:((“sshd”,pid=1066,fd=4))
tcp LISTEN 0 100 ::1:25 :::* users:((“master”,pid=1270,fd=14))

使用lsof查找打开的端口

lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1254 rpc 6u IPv4 12592 0t0 UDP *:sunrpc
rpcbind 1254 rpc 7u IPv4 12596 0t0 UDP *:1001
rpcbind 1254 rpc 8u IPv4 12597 0t0 TCP *:sunrpc (LISTEN)
rpc.statd 1274 rpcuser 5r IPv4 12784 0t0 UDP *:xmlrpc-beep
rpc.statd 1274 rpcuser 8u IPv4 12788 0t0 UDP *:44165
rpc.statd 1274 rpcuser 9u IPv4 12792 0t0 TCP *:44349 (LISTEN)
cupsd 1355 root 7u IPv4 13147 0t0 TCP localhost:ipp (LISTEN)
cupsd 1355 root 9u IPv4 13150 0t0 UDP *:ipp
sshd 1484 root 3u IPv4 13707 0t0 TCP *:ssh (LISTEN)
master 1562 root 12u IPv4 13923 0t0 TCP localhost:smtp (LISTEN)
sshd 1657 root 3r IPv4 14745 0t0 TCP 192.168.1.50:ssh->172.16.60.101:49549 (ESTABLISHED)


文章作者: kclouder
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 kclouder !
  目录