SSH使用密钥登录并禁止口令登录实践

前言

不管是个人的VPS还是企业同意公网訪问的server。假设开放22port的SSHpassword登录验证方式。被众多黑客暴力猜解捅破菊花也可能是常常发生的慘剧。

企业能够通过防火墙来做限制。普通用户也可能借助改动22port和强化弱口令等方式防护,但眼下相对安全和简单的方案则是让SSH使用密钥登录并禁止口令登录。

这是最相对安全的登录管理方式


更新历史

2015年07月07日 - 初稿

阅读原文 - http://wsgzao.github.io/post/ssh/

扩展阅读


生成PublicKey

建议设置并牢记passphrasepassword短语。以Linux生成为例

Linux:ssh-keygen -t rsa
[私钥 (id_rsa) 与公钥 (id_rsa.pub)]
Windows:SecurCRT/Xshell/PuTTY
[SSH-2 RSA 2048]


#生成SSH密钥对
ssh-keygen -t rsa

Generating public/private rsa key pair.
#建议直接回车使用默认路径
Enter file in which to save the key (/root/.ssh/id_rsa): 
#输入password短语(留空则直接回车)
Enter passphrase (empty for no passphrase): 
#反复password短语
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
aa:8b:61:13:38:ad:b5:49:ca:51:45:b9:77:e1:97:e1 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|    .o.          |
|    ..   . .     |
|   .  . . o o    |
| o.  . . o E     |
|o.=   . S .      |
|.*.+   .         |
|o.*   .          |
| . + .           |
|  . o.           |
+-----------------+

复制密钥对

也能够手动在client建立文件夹和authorized_keys,注意改动权限


#复制公钥到无password登录的server上,22port改变能够使用以下的命令
#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 10022 user@server"
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.15.241 

改动SSH配置文件

#编辑sshd_config文件
vi /etc/ssh/sshd_config

#禁用password验证
PasswordAuthentication no
#启用密钥验证
RSAAuthentication yes
PubkeyAuthentication yes
#指定公钥数据库文件
AuthorsizedKeysFile .ssh/authorized_keys

重新启动SSH服务前建议多保留一个会话以防不測

#RHEL/CentOS系统
service sshd restart
#ubuntu系统
service ssh restart
#debian系统
/etc/init.d/ssh restart

手动添加管理用户

能够在== 后添加用户凝视标识方便管理

echo 'ssh-rsa XXXX' >>/root/.ssh/authorized_keys

# 复查
cat /root/.ssh/authorized_keys
原文地址:https://www.cnblogs.com/mfmdaoyou/p/6746963.html