本地CentOS8 配置ssh免密登录服务器

准备工作:
1、确认本机sshd的配置文件(需要root权限)

  $ vi /etc/ssh/sshd_config
  

  找到以下内容,并去掉注释符”#“
  AuthorizedKeysFile .ssh/authorized_keys (网上教程这么写的,然而好像的默认也没注释,而且注释的许多也是默认开启的)

找到此文解答了些疑惑:https://www.cnblogs.com/Leroscox/p/9627809.html
  

  2、如果修改了配置文件需要重启sshd服务 (需要root权限)
 
  $ systemctl restart sshd
  

配置SSH无密码登录需要3步:
1.生成公钥和私钥
2.导入公钥到认证文件,更改权限
3.测试

1.生成公钥和私钥 (这里不需要root权限,使用的账户执行)

ssh-keygen -t rsa (我直接Enter,简单粗暴)

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mycent/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/mycent/.ssh/id_rsa.
Your public key has been saved in /home/mycent/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:vQUMp+pZITtKIYynLJ/fYuR5nqPQkfijmWOBpvucCVY xxxxx@xxxxxxxx
The key's randomart image is:
+---[RSA 3072]----+
|        . .      |
| o       =       |
|. + . . o o      |
|.o o o + o .     |
|oo.E+ + S . .    |
|ooo=.+ +   o     |
|+.++=.o   .      |
|oooO=oo.         |
|.+Ooo=+.         |
+----[SHA256]-----+

 

默认在 ~/.ssh目录生成两个文件:

id_rsa :私钥
id_rsa.pub :公钥

2.导入公钥到认证文件,更改权限
2.1 导入本机
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

2.2 导入要免密码登录的服务器
首先将公钥复制到服务器
scp ~/.ssh/id_rsa.pub xxx@host:/home/id_rsa.pub
然后,将公钥导入到认证文件(这一步的操作在服务器上进行)
cat /home/id_rsa.pub >> ~/.ssh/authorized_keys

2.3 在服务器上更改权限 (很重要,如果不这么设置,就是不让你免密登录)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

最后在本地登录服务器

ssh -v hostname@hostip

posted on 2020-04-19 20:55  smoggy  阅读(5010)  评论(0编辑  收藏  举报