Linux[CentOS 7]免密登陆

2018-10-18 14:00 By "Powerless" 2954 0 3

一、修改sshd_config

vim /etc/ssh/sshd_config

找到以下内容,删除#

RSAAuthentication yes 
PubkeyAuthentication yes 
AuthorizedKeysFile .ssh/authorized_keys

修改好ssh的配置文件后保存并退出,重启SSH服务,这一步建议在公钥上传后再操作,不然你一旦断开了服务器,又没有允许登录的服务器公钥,后果...嘿嘿嘿

systemctl restart sshd.service


二、客户端生成公钥,执行下面的命令

ssh-keygen -t rsa

提示Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

    输入要保存密钥的文件(/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

    输入密码(无密码短语为空):

Enter same passphrase again:

    再次输入相同的密码:

一般情况下我们会直接按3次回车。默认会在 ~/.ssh目录生成两个文件: id_rsa私钥,id_rsa.pub公钥。known_hosts文件会记录ssh密钥登陆的主机列表。


三、导入公钥到认证文件,更改权限

    导入公钥到本机

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

    导入本地公钥,至免密码登录的远程服务器

scp ~/.ssh/id_rsa.pub xxx@host:/home/id_rsa.pub

然后,把公钥导入到认证文件(这一步在服务器进行)

cat /home/id_rsa.pub >> ~/.ssh/authorized_keys

在服务器上更改文件权限

chmod 700 ~/.ssh 

chmod 600 ~/.ssh/authorized_keys


到此,配置完成。快去试试能不能连接成功吧

评 论

View in WeChat

Others Discussion

  • 程序员年中考试题-段子版
    Posted on 2021-06-23 15:57
  • MySQL事务介绍
    Posted on 2019-06-05 18:14
  • BASE原则
    Posted on 2020-12-17 16:42
  • PHP8.1 性能基准测试
    Posted on 2022-10-08 17:40
  • Mysql联合索引的最左前缀匹配原则
    Posted on 2018-08-25 15:00
  • HTTP和HTTPS的区别
    Posted on 2020-08-10 23:00
  • 2018年云计算热词
    Posted on 2019-06-12 18:19
  • MySQL分组
    Posted on 2019-11-18 14:00