CentOS SSH 单向访问无需密码登陆

一、环境配置

1、服务端:CentOS release 5.3  IP:223.73.125.198

2、客服端:CentOS release 5.8  IP:192.168.4.244

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

三、客户端配置

1、在客户端生成私钥与公钥;

 #  ssh-keygen

[root@jw02 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   #回车(代表无需密码登陆)
Enter passphrase (empty for no passphrase):               #回车
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:
04:45:0b:47:10:92:0c:b2:b9:d7:11:5b:49:05:e4:d9 
root@jw02
[root@jw02 ~]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts 

2、默认在 ~/.ssh或者/root/.ssh/目录生成两个文件:
    公钥为:id_rsa.pub
    私钥为:id_rsa

三、服务器的配置

1、将客户端的公钥id_rsa.pub复制到服务器/home/ken/.ssh/authorized_keys;

[root@mon .ssh]# cd
[root@mon ~]# cd /home/ken/.ssh/
[root@mon .ssh]# ll
total 12
-rw-r--r-- 1 ken ken 1192 Feb  5 13:17 authorized_keys
-rw-r--r-- 1 ken ken 5720 Feb  5 14:03 known_hosts
[root@mon .ssh]# vim authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAu3FLhX1VlMQvuxVq8RAfdYEZwYwvorJN/iYkZXOz/itUB6OhO+oIUCcyvaSEHVVOeaPlmcau8HtuBp1V2SmwVl8LpRNYRgeUkSnQKFbjm7z2ALkuL4wAuCC8dcoEflzAOysZfjxoF2h1jPl2pwPd2CWlmPcN5l1kjhpQGJHzgfkkrsC2VR4KJkeqGVckdaKTvBdwElKrwugQhBllWkhsHq+xfoHoQz3n+Jjclpf0vN4z5WRq5+x68akYEG2Sm1s+iYYxO0oOoCJNSEQY7wz1U5q84kwG+0tnPfZDNwIa3ImrEiq6eqdZ5L/rZzht+Hv95t3K39TUKNRbiXXaW+1LsQ== 
root@localhost.localdomain

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwO3o13oscFLnBlzFGPLNkJVNQd1LW2ff/EG0CFjZmw5e2ClqW1YV9fcrPMG4ifmXwmmuF8Ns9WcQAQU/S6RfKHytwHvgpNCeOamzkcfVchHLunk1IDsW3eWARBhKxxdK4pOatPJzcsGtdrBl/ba7W6Z6uYVMlgQzQK7hxI4gXyh6jxvn4gIJZTBvhfMCM1oki+eA38qZZ6zdCfjDc617kHeINQRkSIaUxikReL8X7RigiBPACohfNzKtImZca2oO5EHtDv1b4GuUMFwYSa//EMcwDug9pMQ2LMrOSxgWBokZxq3r/7bybZT+9eGvnwNYoZSej0/qq8A43rhnhXF4pQ== root@dev


ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA17tmVraR6bpJXZVw7RH+uW3DjVRCv5itVP/9lgO2JePNwqPEy+SShZrlbjq/umziaEow5OrgYHWxlf2V1cqKhk2TR4y9ZD9B5lQTHudZO4I+JMV7wU/oEdBNl5S5RzYOIds6PnGUAEqmeCSLu3ozWZq0vDTokoLo41IdcgcLR1sqTOOViBP6oJKT5gG3ws96oe3c0s9irhuzAVE3zV3sptldRBDLKJSB+oXgpugE1WTEi6mGuNVgQc3VRZEWKNyAizVUJprGEZ2nBTeJGJb6NPRFcoNs9OdLpeSEXN/HsRLaMfJ6QMWC90azmmrDFm5BSxqntr+q6DVGGFILQWmPMw==
 root@jw02
[root@mon .ssh]#

2、如果没有这个目录的话,则# mkdir -p /root/.ssh

3、更改文件夹以及文件权限
 

[root@jw02 ~]#chmod 700 ~/.ssh
[root@jw02 ~]#chmod 600 ~/.ssh/authorized_keys  

4、重新启动SSH服务

 [root@jw02 ~]# service sshd restart

 

四、测试

[root@jw02 script]# sftp -oPort=511190 ken@223.73.125.198
Connecting to 223.73.125.198...
sftp>

    如果没有输入密码的提示就是成功了!O(∩_∩)O~



本文转自 kuangling 51CTO博客,原文链接:http://blog.51cto.com/kling/1132172

上一篇:类型转换操作符


下一篇:如何合理的规划jvm性能调优