one piece

设置 SSH 通过密钥登录

Tags: ,

制作密钥对
首先在服务器上制作密钥对。首先用密码登录到你打算使用密钥登录的账户,然后执行以下命令:
[root@host ~]$ ssh-keygen <== 建立密钥对 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空 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:

0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host

拷贝公钥至远程服务器
在远程服务器上要登录的用户下建立用户目录,然后把公钥拷贝到用户目录的.ssh目录下,并改名为authorized_keys。
[usrname@hostname ~] scp .ssh/id_dsa.pub remote_usrname@remote_host:
通过scp复制公钥 (id_dsa.pub) 到远程服务器,此类型文件会被复制到默认的/home/remote_usrname目录下。

在服务器上安装公钥
键入以下命令,在服务器上安装公钥:

[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

# 查看authorized_keys最后一行
tail -1 ~/.ssh/authorized_keys

Created By: sb123

Form Page: 设置 SSH 通过密钥登录


Comments are closed.