linux修改ssh的22端口并限制相关IP登录

简介: linux修改ssh的22端口并限制相关IP登录

@[TOC]

1 添加ssh端口号

  • 添加7248端口,直接编辑sshd_config 添加一个新的端口,一般保留默认端口(防止用22端口远程修改途中,连接丢失),修改成功后再关闭22端口
    vim /etc/ssh/sshd_config
    
    在这里插入图片描述
  • 查看当前ssh服务器端口号 如果新添加的端口号不存在,那么就要向 SELinux 中添加 ssh 端口
    在这里插入图片描述
  • 向 SELinux 中添加 ssh 端口
    semanage port -a -t ssh_port_t -p tcp 7248
    
  • 成功后删除22端口
    semanage port -d -t ssh_port_t -p tcp 22
    
  • 重启ssh服务
    systemctl restart sshd
    

    2 将新端口添加到防火墙中

  • 添加端口
    firewall-cmd --add-port=2234/tcp --permanent
    // 添加到public操作区域
    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    
  • 22端口从防火墙中移除
    firewall-cmd --remove-port=22/tcp --permanent
    
  • 重启防火墙
    systemctl restart firewalld
    
  • 重新加载防火墙配置
    firewall-cmd -reload
    
  • 其他相关命令

    systemctl status firewalld //查看防火墙状态
    systemctl start firewalld //开启防火墙
    systemctl stop firewalld //关闭防火墙
    systemctl enable firewalld //开启开机自启
    systemctl disable firewalld //关闭开机自启
    firewall-cmd --list-ports //查看防火墙加入的端口配置
    firewall-cmd --add-port=80/tcp //防火墙中加入端口
    firewall-cmd --remove-port=80/tcp //防火墙移除端口配置
    

    3 ssh登录限制IP

    3.1 方法一

  • /etc/hosts.allow中添加允许ssh登陆的ip或者网段
    sshd:192.168.0.16:allowsshd:192.168.0.0/24:allow
    (其中 192.168.0.16 是你要允许登陆 sship, 或者是一个网段 192.168.0.0/24)

  • /etc/hosts.deny添加不允许ssh登陆的IP
    sshd:ALL

    3.2 方法二

  • 修改ssh配置文件:
    vim /etc/ssh/sshd_config 添加一行:
    allowusers xx@192.168.1.2 ( 注:xx为你用来登入服务器的用户名,这样就限定了用户名和IP了)
    注意这里的ip指的是这个:
    在这里插入图片描述
目录
相关文章
|
1月前
|
Linux
linux查看应用对应的端口
linux查看应用对应的端口
17 0
|
1月前
|
网络协议 Linux 网络安全
linux7打开、关闭端口
linux7打开、关闭端口
40 0
|
1月前
|
网络协议 安全 Linux
linux系统安全及应用——端口扫描
linux系统安全及应用——端口扫描
37 0
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
43 1
|
19天前
|
Linux 网络安全
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
25 1
|
9天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
16天前
|
Linux Windows
Windows、Mac、Linux解决端口被占用的问题
Windows、Mac、Linux解决端口被占用的问题
24 1
|
19天前
|
Linux 网络安全 数据安全/隐私保护
linux免密登录最简单--图文详解
linux免密登录最简单--图文详解
20 2
|
19天前
|
网络协议 Linux
Linux如何查询端口被占用?
在Linux环境中,查询端口占用可使用`netstat`、`lsof`和`ss`命令。`netstat -tulnp | grep 80`显示TCP/UDP监听端口,`lsof -i:80`列出使用80端口的进程,而`ss -tuln | grep 80`是`netstat`的现代替代选项。若需解决端口占用问题,先找出占用进程的ID,然后用`kill -9`命令终止它,或调整服务配置以避免冲突。
23 1
|
1月前
|
存储 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示当前登录用户的用户 whoami命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示当前登录用户的用户 whoami命令 使用指南
49 1

热门文章

最新文章