zoukankan      html  css  js  c++  java
  • centos 配置 ssh

    1.在hosts.deny文件尾添加sshd:ALL
    意思是允许所有访问请求

    1 [root@localhost ~]# vi /etc/hosts.deny

    修改后看起来如下:

    1 #
    2 # hosts.deny    This file describes the names of the hosts which are
    3 #               *not* allowed to use the local INET services, as decided
    4 #               by the '/usr/sbin/tcpd' server.
    5 #
    6 # The portmap line is redundant, but it is left to remind you that
    7 # the new secure portmap uses hosts.deny and hosts.allow.  In particular
    8 # you should know that NFS uses portmap!
    9 sshd:ALL


    2.在hosts.allow文件尾添加sshd:192.168.0.
    意思是允许192.168.0.1 到254的主机,内网。

    1 [root@localhost ~]# vi /etc/hosts.allow
    2 #
    3 # hosts.allow   This file describes the names of the hosts which are
    4 #               allowed to use the local INET services, as decided
    5 #               by the '/usr/sbin/tcpd' server.
    6 #
    7 sshd:192.168.0.

    3.重启ssh

    1 [root@localhost ~]# /etc/rc.d/init.d/sshd restart
    2 停止 sshd:                                                [  确定  ]
    3 启动 sshd                                                  [  确定  ]

    好了,用putty和Xshell(Xmanager)可以登录了。^_^
    4.ssh增强配置

    01 [root@localhost ~]# vi /etc/ssh/sshd_config  ← 用vi打开SSH的配置文件
    02  
    03 #Protocol 2,1 ← 找到此行将行头“#”删除,再将行末的“,1”删除,只允许SSH2方式的连接
    04  
    05 Protocol 2 ← 修改后变为此状态,仅使用SSH2
    06  
    07 #ServerKeyBits 768 ← 找到这一行,将行首的“#”去掉,并将768改为1024
    08  
    09 ServerKeyBits 1024 ← 修改后变为此状态,将ServerKey强度改为1024比特
    10  
    11 #PermitRootLogin yes  ← 找到这一行,将行首的“#”去掉,并将yes改为no
    12  
    13 PermitRootLogin no  ← 修改后变为此状态,不允许用root进行登录
    14  
    15 #PasswordAuthentication yes ← 找到这一行,将yes改为no
    16  
    17 PasswordAuthentication no ← 修改后变为此状态,不允许密码方式的登录
    18  
    19 #PermitEmptyPasswords no  ← 找到此行将行头的“#”删除,不允许空密码登录
    20  
    21 PermitEmptyPasswords no  ← 修改后变为此状态,禁止空密码进行登录

    Linux SSH 安全策略一:关闭无关端口

    网络上被攻陷的大多数主机,是黑客用扫描工具大范围进行扫描而被瞄准上的。所以,为了避免被扫描到,除了必要的端口,例如 Web、FTP、SSH 等,其他的都应关闭。值得一提的是,我强烈建议关闭 icmp 端口,并设置规则,丢弃 icmp 包。这样别人 Ping 不到你的服务器,威胁就自然减小大半了。丢弃 icmp 包可在 iptables 中, 加入下面这样一条:

    -A INPUT -p icmp -j DROP

    Linux SSH 安全策略二:更改 SSH 端口

    默认的 SSH 端口是 22。强烈建议改成 10000 以上。这样别人扫描到端口的机率也大大下降。修改方法:

    # 编辑 /etc/ssh/ssh_config
    vi /etc/ssh/ssh_config
    # 在 Host * 下 ,加入新的 Port 值。以 18439 为例(下同):
    Port 22
    Port 18439
    
    # 编辑 /etc/ssh/sshd_config
    vi /etc/ssh/sshd_config
    #加入新的 Port 值
    Port 22
    Port 18439
    
    # 保存后,重启 SSH 服务:
    service sshd restart

    这里我设置了两个端口,主要是为了防止修改出错导致 SSH 再也登不上。更改你的 SSH 客户端(例如:Putty)的连接端口,测试连接,如果新端口能连接成功,则再编辑上面两个文件,删除 Port 22 的配置。如果连接失败,而用 Port 22 连接后再重新配置。

    端口设置成功后,注意同时应该从 iptables 中, 删除22端口,添加新配置的 18439,并重启 iptables。

    如果 SSH 登录密码是弱密码,应该设置一个复杂的密码。Google Blog 上有一篇强调密码安全的文章:Does your password pass the test?

    Linux SSH 安全策略三:限制 IP 登录

    如果你能以固定 IP 方式连接你的服务器,那么,你可以设置只允许某个特定的 IP 登录服务器。例如我是通过自己的 VPN 登录到服务器。设置如下:

    # 编辑 /etc/hosts.allow
    vi /etc/hosts.allow
    # 例如只允许 123.45.67.89 登录
    sshd:123.45.67.89

    Linux SSH 安全策略四: 使用证书登录 SSH

    相对于使用密码登录来说,使用证书更为安全。自来水冲咖啡有写过一篇详细的教程,征得其同意,转载如下:

    为CentOS配置SSH证书登录验证

    帮公司网管远程检测一下邮件服务器,一台CentOS 5.1,使用OpenSSH远程管理。检查安全日志时,发现这几天几乎每天都有一堆IP过来猜密码。看来得修改一下登录验证方式,改为证书验证为好。

    为防万一,临时启了个VNC,免得没配置完,一高兴顺手重启了sshd就麻烦了。(后来发现是多余的,只要事先开个putty别关闭就行了)

    以下是简单的操作步骤:

    1)先添加一个维护账号:msa
    
    2)然后su - msa
    
    3)ssh-keygen -t rsa
    指定密钥路径和输入口令之后,即在/home/msa/.ssh/中生成公钥和私钥:id_rsa id_rsa.pub
    
    4)cat id_rsa.pub >> authorized_keys
    至于为什么要生成这个文件,因为sshd_config里面写的就是这个。
    然后chmod 400 authorized_keys,稍微保护一下。
    
    5)用psftp把把id_rsa拉回本地,然后把服务器上的id_rsa和id_rsa.pub干掉
    
    6)配置/etc/ssh/sshd_config
    Protocol 2
    ServerKeyBits 1024
    PermitRootLogin no  #禁止root登录而已,与本文无关,加上安全些
    
    #以下三行没什么要改的,把默认的#注释去掉就行了
    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile    .ssh/authorized_keys
    
    PasswordAuthentication no
    PermitEmptyPasswords no
    
    7)重启sshd
    /sbin/service sshd restart
    
    8)转换证书格式,迁就一下putty
    运行puttygen,转换id_rsa为putty的ppk证书文件
    
    9)配置putty登录
    在connection--SSH--Auth中,点击Browse,选择刚刚转换好的证书。
    然后在connection-Data填写一下auto login username,例如我的是msa
    在session中填写服务器的IP地址,高兴的话可以save一下
    
    10)解决一点小麻烦
    做到这一步的时候,很可能会空欢喜一场,此时就兴冲冲的登录,没准登不进去:
    No supported authentication methods available
    
    这时可以修改一下sshd_config,把
    PasswordAuthentication no临时改为:
    PasswordAuthentication yes 并重启sshd
    
    这样可以登录成功,退出登录后,再重新把PasswordAuthentication的值改为no,重启sshd。
    以后登录就会正常的询问你密钥文件的密码了,答对了就能高高兴兴的登进去。
    
    至于psftp命令,加上个-i参数,指定证书文件路径就行了。

    如果你是远程操作服务器修改上述配置,切记每一步都应慎重,不可出错。如果配置错误,导致 SSH 连接不上,那就杯具了。

    基本上,按上述四点配置好后,Linux 下的 SSH 访问,是比较安全的了。当然,安全与不安全都是相对的,你应该定期检查服务器的 log,及时发现隐患并排除。

  • 相关阅读:
    解决Ant design vue的Layout布局,左右布局侧边栏无效不显示问题
    寻找写代码感觉(十二)之 封装分页请求参数和返回参数
    解决关于interceptor拦截器跨域AccessControlAllowOrigin报错问题
    解决Vue3引入自定义组件报错Cannot find module ‘xxx‘ or its corresponding type declarations问题
    解决antD中关于table组件中报这样的警告warning.js?2149:7 Warning: [antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.]问题
    Centos 更改MySQL5.7数据库目录位置
    解决typescript:error TS2531: Object is possibly 'null'.问题
    解决npm WARN Local package.json exists, but node_modules missing, did you mean to install?问题
    弱监督学习文章略读记录
    弱监督学习最新文章略读
  • 原文地址:https://www.cnblogs.com/buffer/p/2131262.html
Copyright © 2011-2022 走看看