怎样修改 CentOS 7 SSH 端口

2019-06-18 03:14

打开 SELinux 端口

为 ssh 打开 2048 端口

# 为 ssh 添加新的允许端口
$ semanage port -a -t ssh_port_t -p tcp 2048
# 查看当前 SELinux 允许的端口
$ semanage port -l | grep ssh
ssh_port_t tcp 2048, 22

配置防火墙 firewalld

systemctl enable firewalld

查看防火墙当前「默认」和「激活」zone(区域):

$ firewall-cmd --get-default-zone
public
$ firewall-cmd --get-active-zones
public
interfaces: eth0 eth1

为 public zone 永久开放 2048/TCP 端口:

$ firewall-cmd --permanent --zone=public --add-port=22/tcp
$ firewall-cmd --permanent --zone=public --add-port=2048/tcp
success
# 重载防火墙
$ firewall-cmd --reload
# 查看暴露端口规则
$ firewall-cmd --permanent --list-port
443/tcp 80/tcp 22/tcp 2048/tcp


禁用 22 端口,墙移除 22 端口

$ firewall-cmd --permanent --zone=public --remove-port=22/tcp
success
$ firewall-cmd --reload
$ firewall-cmd --permanent --list-port
443/tcp 80/tcp 2048/tcp

来自 https://sebastianblade.com/how-to-modify-ssh-port-in-centos7/


禁止ping: firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'