Linux笔记-iptables开放指定端口,开放ICMP协议,其他端口禁止访问

x33g5p2x  于2022-03-28 转载在 Linux  
字(2.6k)|赞(0)|评价(0)|浏览(271)

下面实现3个规则:

①对所有的地址开放本机的tcp(80、22、10~21)端口的访问。

②运行对所有地址开放本机的基于ICMP协议的数据包访问。

③其他未允许的端口则禁止访问。

#查看本机开放的端口
netstat -luntp
[root@bogon ~]# netstat -luntp
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:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:6000            0.0.0.0:*               LISTEN      9215/X              
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      9106/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      8591/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      8587/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      9024/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      20030/sshd: root@pt 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      20313/sshd: root@pt 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::6000                 :::*                    LISTEN      9215/X              
tcp6       0      0 :::22                   :::*                    LISTEN      8591/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      8587/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      9024/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      20030/sshd: root@pt 
tcp6       0      0 ::1:6011                :::*                    LISTEN      20313/sshd: root@pt 
udp        0      0 0.0.0.0:50213           0.0.0.0:*                           8206/avahi-daemon:  
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           8206/avahi-daemon:  
udp        0      0 192.168.122.1:53        0.0.0.0:*                           9106/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           9106/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           20242/dhclient      
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1/systemd           
udp        0      0 127.0.0.1:323           0.0.0.0:*                           8237/chronyd        
udp        0      0 0.0.0.0:693             0.0.0.0:*                           8158/rpcbind        
udp6       0      0 :::111                  :::*                                1/systemd           
udp6       0      0 ::1:323                 :::*                                8237/chronyd        
udp6       0      0 :::693                  :::*                                8158/rpcbind        
[root@bogon ~]#

查看iptables版本

iptables -v
[root@bogon ~]# iptables -v
iptables v1.4.21: no command specified
Try `iptables -h' or 'iptables --help' for more information.
[root@bogon ~]#

查看iptables配置的链,-n是让主机名等不显示

iptable -nL
[root@bogon ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@bogon ~]#

设置80端口可以访问,设置22端口可以访问,不然ssh就不能登录了,设置10~21都允许访问。

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -p tcp --dport 10:21 -j ACCEPT

此时配置的策略有:

[root@bogon ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpts:10:21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

再支持ICMP协议,然后-A,在最后添加一条拒绝所有的规则。

iptables -I INPUT -p icmp -j ACCEPT
iptables -A INPUT -j REJECT

这里就实现了上面的3个规则,对应的策略是这样的。

[root@bogon ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpts:10:21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

相关文章

微信公众号