linux 查看端口状态 linux命令查看端口状态



文章插图
linux 查看端口状态 linux命令查看端口状态

文章插图
1. 前言
本文主要讲解如何修改CentOS 7默认的SSH端口号 。
ssh协议默认端口号为TCP 22,这个端口也是端口扫描软件重点扫描的对象,也是黑客最感兴趣的端口号之一 。
2. 修改ssh配置文件
[[email protected] ~]# vi /etc/ssh/sshd_config
修改 ssh端口
取消 #Port 22 的注释,在下一行添加你需要修改的新端口 Port 2048 。(这里不删除 22 端口是为了防止修改后新端口无法访问,造成无法用 ssh 连接服务器 。)
配置文件应该有这样两行
Port 22
Port 2048
修改保存 sshd_config 文件后重启 sshd 服务:
[[email protected] ~]# systemctl restart sshd退出 ssh 会话后,再用新的端口连接:
假设你使用Linux连接到服务器:
[[email protected] ~]# ssh -p 2048 [email protected]如果你是使用Windows+Xshell或者SecureCRT,则需要在SSH客户端软件里修改TCP端口为2048, 。
使用新的SSH端口登录Linux
得出以下结论:
ssh: connect to host 0.0.0.0 port 2048: Connection refused
连接被拒绝,表明CentOS 7这一套修改端口的方法已经不能生效了 。因为CentOS7还需要配置SElinux 。
3. 设置SElinux允许使用SSH新端口
打开 SELinux 端口
SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件、网络端口等) 。
对于 ssh,SELinux 默认只允许 22 端口,我们可以用 SELinux 管理配置工具 semanage,来修改 ssh 可访问的端口 。
安装 semanage 工具
[[email protected] ~]# yum provides semanage[[email protected] ~]# yum -y install policycoreutils-python为 ssh 添加新的允许端口
[[email protected] ~]# semanage port -a -t ssh_port_t -p tcp 2048查看当前 SELinux 允许的端口
[[email protected] ~]# semanage port -l | grep sshssh_port_t tcp 2048, 22
错误处理
当 SELINUX 配置为禁用状态时,使用 semanage 会报错提示无法读取 policy 文件:
SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux: Could not open policy file <= /etc/selinux/targeted/policy/policy.30: No such file or directory
/sbin/load_policy: Can’t load policy: No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
FileNotFoundError: [Errno 2] No such file or directory
修改 /etc/selinux/config 配置,启用 SELinux:
[[email protected] ~]# vi /etc/selinux/configSELINUX=permissive
重启服务器
[[email protected] ~]# init 6重启后查看 SELinux 状态
[[email protected] ~]# sestatus检查配置:
[[email protected] ~]# semanage port -a -t ssh_port_t -p tcp 2048[[email protected] ~]# semanage port -l | grep sshssh_port_t tcp 2048, 22
重启 ssh 服务
[[email protected] ~]# systemctl restart sshd注:semange 不能禁用 ssh 的 22 端口:
[[email protected] ~]# semanage port -d -t ssh_port_t -p tcp 22ValueError: 在策略中定义了端口 tcp/22,无法删除 。
4. 配置防火墙 firewalld
CentOS-7-SSH-port-configuration-03.png
启用防火墙 && 查看防火墙状态:[[email protected] ~]# systemctl enable firewalld[[email protected] ~]# systemctl start firewalld[[email protected] ~]# systemctl status firewalld● firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2016-12-20 02:12:59 CST; 1 day 13h ago
Main PID: 10379 (firewalld)
CGroup: /system.slice/firewalld.service
└─10379 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid
[[email protected] ~]# firewall-cmd --staterunning
查看防火墙当前默认和激活zone(区域):
[[email protected] ~]# firewall-cmd --get-default-zonepublic
[[email protected] ~]# firewall-cmd --get-active-zonespublic
interfaces: eth0 eth1
若没有激活区域的话,要执行下面的命令 。
激活 public 区域,增加网卡接口,假设你的端口号为eth0,使用ip addr命令查看网卡标识号:
[[email protected] ~]# firewall-cmd --set-default-zone=public[[email protected] ~]# firewall-cmd --zone=public --add-interface=eth0success
假设eth1也加入public区域
[[email protected] ~]# firewall-cmd --zone=public --add-interface=eth1success
为 public zone 永久开放 2048/TCP 端口:
以防新端口不生效,先把 22 端口暴露
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-port=22/tcp[[email protected] ~]# firewall-cmd --permanent --zone=public --add-port=2048/tcpsuccess
重载防火墙
[[email protected] ~]# firewall-cmd --reload查看暴露端口规则
[[email protected] ~]# firewall-cmd --permanent --list-port443/tcp 80/tcp 22/tcp 2048/tcp
[[email protected] ~]# firewall-cmd --zone=public --list-allpublic (default, active)
interfaces: eth0 eth1
sources:
services: dhcpv6-client ssh
ports: 443/tcp 80/tcp 22/tcp 2048/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
退出 ssh 后,尝试连接新端口
[[email protected] ~]# ssh -p 2048 [email protected]成功登录的话,就可以做收尾工作了 。
5. 禁用TCP 22 端口
删除 ssh 允许端口
[[email protected] ~]# vi /etc/ssh/sshd_configPort 22
Port 2048
即在Port 22前面加#号
重启SSH服务
[[email protected] ~]# systemctl restart sshd用 ss 命令检查 ssh 监听的端口,没有看到22 证明修改成功
[[email protected] ~]# ss -tnlp | grep sshLISTEN 0 128 *:2048 *:* users:((“sshd”,18233,3))
防火墙移除 22 端口
[[email protected] ~]# firewall-cmd --permanent --zone=public --remove-port=22/tcpsuccess
重载防火墙配置
[[email protected] ~]# firewall-cmd --reload查看永久生效的端口号
[[email protected] ~]# firewall-cmd --permanent --list-port443/tcp 80/tcp 2048/tcp
ssh 取消监听 22 端口,就已经配置好了,防火墙只不过是在 ssh 外多一层访问限制 。
检验修改 ssh 端口是否成功:
[[email protected] ~]# ssh -p 22 [email protected]无响应,因为端口号已经改变了 。
试试使用TCP 2048端口连接
[[email protected] ~]# ssh -p 2048 [email protected]成功 success
6. 总结
【linux 查看端口状态 linux命令查看端口状态】至此,我们已经成功修改了linux默认的ssh端口,由22改为2048,提升了系统安全,使得端口扫描工具无法推测出SSH的连接端口 。