天天看点

CentOS开启,关闭,查看端口

Centos7 开启 , 关闭端口

CentOS7

默认没有使用iptables , 所以不能通过编辑

iptables

的配置文件来开启端口 ,

CentOS7

采用了

firewalld

防火墙

如要查询是否开启3306端口则 :

# firewall-cmd --query-port=3306/tcp
           

开启端口 :

# firewall-cmd --zone=public --add-port=3306/tcp --permanent
           
命令含义 :

--zone #作用域

,

--add-port=80/tcp #添加端口 , 格式为 : 端口/通讯协议

,

--permanent #永久生效 , 没有此参数重启后失效

重启防火墙 :

# firewall-cmd --reload
           

关闭端口 :

# firewall-cmd --zone=public --remove-port=3306/tcp --permanent
           

查看防火墙状态 :

# systemctl status firewalld
           

开启防火墙服务 :

# systemctl start firewalld.service
           

设置防火墙开机启动 :

# systemctl enable firewalld.service
           

Centos6.X 开启 , 关闭端口

使用

iptables

开放如下端口 :

# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
           

保存 :

# /etc/rc.d/init.d/iptables save
           

重启服务 :

# /etc/rc.d/init.d/iptables restart
           

查看端口是否开放 :

# /etc/init.d/iptables status
           

关闭端口 :

# /sbin/iptables -I INPUT -p tcp --dport 3306 -j DROP
           

清除 iptables :

# /sbin/iptables -F
           

查看默认连接规则 :

# iptables -L
           

设置入方向默认规则为接受连接 :

# /sbin/iptables -P INPUT ACCEPT
           

查看系统端口占用情况

1.

lsof -i:

, 用于查看某一端口的占用情况 , 比如查看 8000 端口使用情况 :

# lsof -i:22
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     1087 root    3u  IPv4  17344      0t0  TCP *:ssh (LISTEN)
sshd     1087 root    4u  IPv6  17353      0t0  TCP *:ssh (LISTEN)
           
出现

-bash: lsof: command not found

时 , 是命令没安装 , 直接安装

yum install lsof

2.

netstat -tunlp

, 用于查看系统端口占用情况

# netstat -tunlp
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:80              0.0.0.0:*               LISTEN      10083/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1087/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1409/master         
           
说明一下几个参数的含义 :

-t (tcp) 仅显示tcp相关选项

,

-u (udp)仅显示udp相关选项

,

-n 拒绝显示别名,能显示数字的全部转化为数字

,

-l 仅列出在Listen(监听)的服务状态

,

-p 显示建立相关链接的程序名

3.

netstat -apn

, 用于查看所有的进程和端口使用情况

# netstat -apn
Active Internet connections (servers and established)
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:80              0.0.0.0:*               LISTEN      10083/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1087/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1409/master         
tcp        0     52 192.168.0.150:22        192.168.0.102:2565      ESTABLISHED 15354/sshd: [email protected] 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1087/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1409/master  
           
作者 Github : tojohnonly , 博客 : EnskDeCode

继续阅读