CentOS下设置ssh免密码登录

设置SSH免密码登录命令如下:

[root@centos-lx /]# ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa

[root@centos-lx /]# cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

输入以下命令验证是否设置成功:

[root@centos-lx /]# ssh localhost

如果仍然需要密码登录。则检查对应配置文件权限,命令如下:

[root@centos-lx /]# ls -ld ~/.ssh

输出:

drwx------. 2 root root 4096 3月   4 21.:17   /root/.ssh

[root@centos-lx /]# ls -l ~/.ssh

输出:

总用量  16

-rw-r--r--. 1 hadoop root 2416 3月   6 09:17 authorized_keys

-rw-------. 1 hadoop root   668 3月   6 09:17 id_dsa

-rw-r--r--. 1 hadoop root   604 3月   6 09:17 id_dsa.pub

-rw-r--r--. 1 hadoop root   391 3月   4 00:31 known_hosts

[root@centos-lx /]# chmod 600 ~/.ssh/*  // 修改文件权限

[root@centos-lx /]# ls -l ~/.ssh

总用量  16

-rw-------. 1 hadoop root 2416 3月   6 09:17 authorized_keys

-rw-------. 1 hadoop root   668 3月   6 09:17 id_dsa

-rw-------. 1 hadoop root   604 3月   6 09:17 id_dsa.pub

-rw-------. 1 hadoop root   391 3月   4 00:31 known_hosts

[root@centos-lx /]# ssh localhost  // 验证ssh登录模式

输出:

Last login: Wed Mar   5  09:25:19   2014    from localhost

此设置只能够满足root用户登录到时候ssh可以免密码登录,如果需要非root用户也能够免密码登录,需要进行如下设置。(假设hadoop用户)

首选要确定/home/hadoop目录中没有.ssh及子文件:

[root@centos-lx hadoop]# rm -rf /home/hadoop/.ssh/*

[root@centos-lx hadoop]# rm -rf /home/hadoop/.ssh

[root@centos-lx hadoop]# su hadoop

[hadoop@centos-lx hadoop]# ssh-keygen -t rsa

输出:()

Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Created directory '/home/hadoop/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
5c:8e:b9:e0:ca:24:b9:80:11:71:75:06:07:71:d6:82 hadoop@centos-lx
The key's randomart image is:
+--[ RSA 2048]----+
|. ..==*.         |
| o  E*. .        |
|.      .  .      |
| .     . =       |
|.     . S .      |
|.. . . . .       |
|o o . . .        |
| . = .           |
|  . o            |
+-----------------+
[hadoop@centos-lx hadoop]# cd .ssh

[hadoop@centos-lx hadoop]# cat id_rsa.pub >> authorized_keys  // 导入公钥到认证文件

修改文件权限:

[hadoop@centos-lx hadoop]# chmod 700 .ssh

[hadoop@centos-lx hadoop]# chmod 600 .ssh/authorized_keys

验证hadoop用户ssh免密码登录:

[hadoop@centos-lx hadoop]# ssh localhost

输出:(第一次登录,需要确认“yes")

The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is 03:87:0e:10:66:05:d0:90:4c:de:9d:b9:d9:b7:76:17.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
Last login: Wed Mar  5 09:32:53 2014 from localhost
至此,非root用户ssh免密码登录设置完成。

 

posted @ 2014-03-06 10:29  naterliu  阅读(356)  评论(0)    收藏  举报