红联Linux门户
Linux帮助

Ubuntu和CentOS配置SSH无密码登录

发布时间:2015-04-20 11:34:40来源:linux网站作者:iam333

一、安装ssh

Ubuntu中使用命令:

$sudo apt-get install openssh-server openssh-client

CentOS中使用命令:

$sudo yum install openssh-server openssh-client

启动ssh使用命令:

$sudo service sshd start


二、配置无密码登录SSH

Ubuntu中配置如下:

$ ssh-keygen -t dsa -P ''
Generating public/private dsa key pair.
Enter file in which to save the key (/home/aaron/.ssh/id_dsa):
Created directory '/home/aaron/.ssh'.
Your identification has been saved in /home/aaron/.ssh/id_dsa.
Your public key has been saved in /home/aaron/.ssh/id_dsa.pub.
The key fingerprint is:
bd:2c:ed:ab:6d:a9:b2:45:88:32:08:5a:d2:d9:ad:cc aaron@ubuntu
The key's randomart image is:
+--[ DSA 1024]----+
| |
| . o . |
|o + . . |
|o+ o o . . |
|o o E . S . |
| o . o . |
| o +. |
| .. +o |
| .oo++. |
+-----------------+

(注:回车后会在~/.ssh/下生成两个文件:id_rsa和id_rsa.pub。这两个文件是成对出现的。)

将id_dsa.pub里面的内容加到用于认证的公钥文件中,命令如下:

$ cat id_dsa.pub >> authorized_keys

可以将这个生成的authorized_keys拷贝到别的机器上面

$ cat ~/.ssh/authorized_keys | \
ssh aarron@192.168.1.108 "cat - >> ~/.ssh/authorized_keys"

登录localhost。在终端输入命令:ssh localhost(注:当ssh远程登录到其它机器后,你控制的是远程的机器,需要执行退出命令才能重新控制本地主机。)执行退出命令。输入命令:exit

如果配置过程中出现问题,一般的问题都是文件权限设置的不对,请把.ssh和authorized_keys的访问权限分别设置为755个600,命令如下:

$ chmod 755 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys

上面是以Ubuntu平台为例子,下面来说说怎么在CentOS平台上面配置。

首先,需要设置一下/etc/ssh/sshd_config文件,请将/etc/ssh/sshd_config文件中下面三行的注释去掉:

$sudo vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

其余的步骤和在Ubuntu下配置是一样的。