Linux开放或限制端口和ip访问

Linux开放或限制端口和IP访问

防火墙文件: vi /etc/sysconfig/iptables

开放指定端口
-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

批量开放指定端口
-A INPUT -p tcp -m multiport --dports 22,80,443,8080 -j ACCEPT

开放指定ip访问
-A INPUT -s 192.168.22.137/32 -j ACCEPT

开放指定ip访问指定端口
-A INPUT -s 192.168.22.137/32 -p tcp -m multiport --dports 22,80,443,8080 -j ACCEPT

封禁指定ip
-A INPUT -s 192.168.22.137/32 -j DROP

重启防火墙: service iptables restart


防火墙文件修改,如下:

-A INPUT -s 106.11.0.0/16 -j ACCEPT
-A INPUT -s 203.119.128.0/17 - j ACCEPT
-A INPUT -s 59.82.0.0/16 -j ACCEPT
-A INPUT -s 106.11.192.0/19 -j ACCEPT

重启防火墙时可能会提示如下:

[root@HoneySJ ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
Failed to restart iptables.service: Unit not found.

解决:

1.关闭firewalld

systemctl stop firewalld
systemctl mask firewalld

2.安装iptables-services:

yum install iptables-services

3.设置为开机自启动

systemctl enable iptables

4.对进行iptables进行基本操作

systemctl stop iptables
systemctl start iptables
systemctl restart iptables
systemctl reload iptables

5.iptables保存设置

service iptables save

你可能感兴趣的:(CentOS,linux)