如何配置ssh使用密钥登录,禁止口令登录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何配置ssh使用密钥登录,禁止口令登录相关的知识,希望对你有一定的参考价值。
1、配置私钥a、使用命令ssh-keygen -t rsa生成密钥,会生成一个私钥和一个公钥,在提示输入passphrase时如果不输入,直接回车,那么以后你登录服务器就不会验证密码,否则会要求你输入passphrase,默认会将私钥放在/root/.ssh/id_rsa公钥放在
/root/.ssh/id_rsa.pub。
b、将公钥拷贝到远程服务器上的/root/.ssh/authorized_keys文件
(scp /root/.ssh/id_rsa.pub server:/root/.ssh/authorized_keys),注意,文件名一定要叫authorized_keys。
c、客户端上保留私钥,公钥留不留都可以。也就是服务器上要有公钥,客户端上要有私钥。这样就可以实现无密码验证登录了。
2、如果想要获得最大化的安全性,禁止口令登录,可以修改www.example.com上/etc/ssh/sshd_conf中的
PasswordAuthentication yes 改为
PasswordAuthentication no
也即只能使用密匙认证的openssh,禁止使用口令认证。 参考技术A 1、配置私钥
a、使用命令ssh-keygen -t rsa生成密钥,会生成一个私钥和一个公钥,在提示输入passphrase时如果不输入,直接回车,那么以后你登录服务器就不会验证密码,否则会要求你输入passphrase,默认会将私钥放在/root/.ssh/id_rsa公钥放在
/root/.ssh/id_rsa.pub。
b、将公钥拷贝到远程服务器上的/root/.ssh/authorized_keys文件
(scp /root/.ssh/id_rsa.pub server:/root/.ssh/authorized_keys),注意,文件名一定要叫authorized_keys。
c、客户端上保留私钥,公钥留不留都可以。也就是服务器上要有公钥,客户端上要有私钥。这样就可以实现无密码验证登录了。
2、如果想要获得最大化的安全性,禁止口令登录,可以修改www.example.com上/etc/ssh/sshd_conf中的
PasswordAuthentication yes 改为
PasswordAuthentication no
也即只能使用密匙认证的openssh,禁止使用口令认证。
SSH使用密钥登录并禁止口令登录实践
前言
不管是个人的VPS还是企业同意公网訪问的server。假设开放22port的SSHpassword登录验证方式。被众多黑客暴力猜解捅破菊花也可能是常常发生的慘剧。
企业能够通过防火墙来做限制。普通用户也可能借助改动22port和强化弱口令等方式防护,但眼下相对安全和简单的方案则是让SSH使用密钥登录并禁止口令登录。
这是最相对安全的登录管理方式
更新历史
2015年07月07日 - 初稿
阅读原文 - http://wsgzao.github.io/post/ssh/
扩展阅读
- SSH原理与运用 - http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html
- Linode - https://www.linode.com/docs/networking/ssh/use-public-key-authentication-with-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 [email protected]
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 [email protected]"
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]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
以上是关于如何配置ssh使用密钥登录,禁止口令登录的主要内容,如果未能解决你的问题,请参考以下文章
Xshell5配置ssh免密码登录-公钥与私钥登录linux服务器(xshell如何登陆上阿里云服务器)
Xshell配置ssh使用密钥公钥(publice key)登录