linux 端口管理
参考:https://www.jianshu.com/p/b3068288d80d
端口查看
查看端口是否打开
netstat -an | grep 端口 或 netstat -tunlp |grep 端口
查看端口是否对外开放
查看端口是否可访问:telnet ip 端口号
查看所有开放的端口
cat /etc/sysconfig/iptables
/sbin/iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:9200
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:9100
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
开放端口
直接修改配置文件 /etc/sysconfig/iptables(推荐) :
centos 7以下版本:
(1)通过vim /etc/sysconfig/iptables 进入编辑增添一条:-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
(2)执行 /etc/init.d/iptables restart 命令将iptables服务重启
(3)保存 /etc/rc.d/init.d/iptables save
centos 7及以上版本:
(1)通过vim /etc/sysconfig/iptables 进入编辑增添一条:-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
(2)执行 systemctl restart iptables 命令将iptables服务重启
(3)保存 service iptables save
也可能试下这个方式:
firewall-cmd --zone=public --add-port=9200/tcp --permanent
#--permanent永久生效,没有此参数重启后失效
重新加载防火墙:
$ firewall-cmd --reload
注意:通过 /etc/sysconfig/iptables 配置开放端口时,一定要注意内容的添加位置,要放在合适的位置,不能随意放,最好相同类型的端口位置是连续的。
使用命令(不建议):
#iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
若/etc/sysconfig/iptables不存在;
原因:在新安装的linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件。
解决:
在控制台使用iptables命令随便写一条防火墙规则,如:iptables -P OUTPUT ACCEPT
使用service iptables save进行保存,默认就保存到了/etc/sysconfig目录下的iptables文件中
执行service iptables save,如果报错:
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
解诀方案:https://blog.csdn.net/xu_melon/article/details/79043320