Linux笔记-iptables开放指定端口,开放ICMP协议,其他端口禁止访问
生活随笔
收集整理的這篇文章主要介紹了
Linux笔记-iptables开放指定端口,开放ICMP协议,其他端口禁止访问
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
下面實現(xiàn)3個規(guī)則:
①對所有的地址開放本機的tcp(80、22、10~21)端口的訪問。
②運行對所有地址開放本機的基于ICMP協(xié)議的數(shù)據(jù)包訪問。
③其他未允許的端口則禁止訪問。
#查看本機開放的端口 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 ~]#設(shè)置80端口可以訪問,設(shè)置22端口可以訪問,不然ssh就不能登錄了,設(shè)置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:80Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination再支持ICMP協(xié)議,然后-A,在最后添加一條拒絕所有的規(guī)則。
iptables -I INPUT -p icmp -j ACCEPT iptables -A INPUT -j REJECT這里就實現(xiàn)了上面的3個規(guī)則,對應的策略是這樣的。
[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-unreachableChain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination總結(jié)
以上是生活随笔為你收集整理的Linux笔记-iptables开放指定端口,开放ICMP协议,其他端口禁止访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker 覆盖 entrypoint
- 下一篇: 科来数据包生成器找不到网卡_CPU:网卡