为CentOS配置SSH证书登录验证

来源:互联网 发布:linux 总线驱动 编辑:程序博客网 时间:2024/05/22 17:26

最近查看服务器日志发现有N多IP在扫描我的CentOS服务器密码,这个很讨厌,于是开始为CentOS配置SSH证书登录验证,杜绝此类扫描。

准备:开启2个putty,以防没配置完,一高兴顺手重启了sshd就麻烦了。

开始吧:

————————–

1)先添加一个维护账号:xiaoxing

adduser xiaoxing

passwd xiaoxing

2)然后su  xiaoxing

3)ssh-keygen -t rsa
指定密钥路径和输入口令之后,即在/home/xiaoxing/.ssh/中生成公钥和私钥:id_rsa id_rsa.pub

4)

cd /home/xiaoxing/.ssh

cat id_rsa.pub >> authorized_keys
至于为什么要生成这个文件,因为sshd_config里面默认写的就是这个。
然后chmod 400 authorized_keys,稍微保护一下。

5)用Filezilla把把id_rsa拉回本地,然后把服务器上的id_rsa和id_rsa.pub干掉

6)配置/etc/ssh/sshd_config

Port 6519              #修改默认的登陆端口
#Protocol 2
ServerKeyBits 1024
PermitRootLogin no    #禁止root登录而已,与本文无关,加上安全些

#以下三行没什么要改的,把默认的#注释去掉就行了
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile    .ssh/authorized_keys

PasswordAuthentication no    #禁止使用密码登陆
PermitEmptyPasswords no


7)重启sshd

首先添加防火墙规则开放6519端口

/sbin/iptables -I INPUT -p tcp --dport 6519 -ACCEPT

service iptables save

重启sshd
/sbin/service sshd restart

8)转换证书格式,迁就一下putty
运行puttygen(没有到官网下载一下),转换id_rsa为putty的ppk证书文件。

直接Import key    输入秘钥后,save private key即可

9)配置putty登录
在Connection–>SSH–>Auth中,点击Browse,选择刚刚转换好的证书。
(然后在Connection->Data填写一下Auto-login username,例如我的是xiaoxing   这一步可以不做)
在session中填写服务器的IP地址,高兴的话可以save一下

10)解决一点小麻烦
做到这一步的时候,很可能会空欢喜一场,此时就兴冲冲的登录,没准登不进去:
No supported authentication methods available

这时可以修改一下sshd_config,把
PasswordAuthentication no临时改为:
PasswordAuthentication yes 并重启sshd

这样可以登录成功,退出登录后,再重新把PasswordAuthentication的值改为no,重启sshd,以后登录就会正常的询问你密钥文件的密码了,答对了就能高高兴兴的登进去。

11)如果还不能登陆,将sshd_config中StrictModes yes改成StrictModes no就能登录了。

12)配置Filezilla登陆

在编辑->设置->SFTP中导入私钥即可登陆了,不过不能root登陆。

OK,搞定了。

原创粉丝点击