1. 生成用于SSH的公钥和私钥(本例用户为ifshow)

ssh-keygen -t rsa

2. 导入公钥

cat ~/.ssh/id\_rsa.pub >> ~/.ssh/authorized\_keys

3. 设置正确的文件和文件夹权限

chown -R 0700  ~/.ssh
chown -R 0644  ~/.ssh/authorized_keys
chown -R ifshow:ifshow /home/ifshow

开启SELinux时,还需要执行(root用户把/home改成/root)

restorecon -R -v /home

4. 修改SSH配置文件,支持使用证书登录(root权限)

vi /etc/ssh/sshd_config

查找RSAAuthentication、StrictModes、PubkeyAuthentication、AuthorizedKeysFile把所在行修改为:

RSAAuthentication yes
StrictModes no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

重启SSH服务

systemctl restart sshd.service

重启SSH服务 systemctl restart sshd.service

5. Windows客户端配置Putty使用证书登录

5.1 制作用于Putty的私钥

下载id_rsa到本地; 运行PuTTY Key Generator; 点击File - Load private key(All Files *.*),导入id_rsa文件; 点击Save private key按钮,命名并生成ppk文件,比如ifshow.ppk。

这个ppk文件就是用于Putty的私钥。

5.2 设置putty实现用证书登录

Putty→Session:输入Host Name 或者 IP Address; Putty→Connection→Data:输入Auto-login username(自动登陆用户名); Putty→Connection→SSH→Auth:在Private key file for authentication选择私钥文件; Putty→Session:Saved Session:输入某个名称保存,以后直接双击该名称就可登录。

也可以用带参数的快捷方式执行证书登录,比如:

"D:\\PUTTY\\PUTTY.EXE" -i "D:\\key\\ifshow.ppk" [email protected]

6. CentOS 7客户端使用证书登录

ssh命令

ssh -i ~/.ssh/id\_rsa remote\_username@remote\_ip

scp命令

scp -i ~/.ssh/id\_rsa local\_file remote\_username@remote\_ip:remote\_file

也可以把私钥写入客户端本地SSH配置文件,省去每次手动指定私钥:

vi /etc/ssh/sshd\_config

在配置文件中加入:

IdentityFile ~/.ssh/id\_rsa

重启SSH服务

systemctl restart sshd.service

7. 修改SSH配置文件,禁止使用密码登录(root权限)

vi /etc/ssh/sshd_config

查找PasswordAuthentication yes修改为:

PasswordAuthentication no

重启SSH服务

systemctl restart sshd.service