在linux系统中,ssh是远程登录的默认工具,因为该工具的协议使用了RSA/DSA的加密算法.该工具做linux系统的远程管理是非常安全的。telnet,因为其不安全性,在linux系统中被搁置使用了。

  " 公私钥"认证方式简单的解释:首先在客户端上创建一对公私钥 (公钥文件:~/.ssh/id_rsa.pub; 私钥文件:~/.ssh/id_rsa)。然后把公钥放到服务器上(~/.ssh/authorized_keys), 自己保留好私钥.在使用ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配.如果匹配成功就可以登录了。

  在Ubuntu和Cygwin 配置都很顺利,而在Centos系统中配置时遇到了很多问题。故此文以Centos(Centos5 ) 为例详细讲解如何配置证书验证登陆,具体操作步骤如下:

确认系统已经安装好OpenSSH的server 和client

确认本机sshd的配置文件(root)

$ vi /etc/ssh/sshd_config

找到以下内容,并去掉注释符"#"

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

如果修改了配置文件需要重启sshd服务(root)

$ vi /sbin/service sshd restart

ssh登陆系统 后执行测试命令

$ ssh localhost

  回车会提示你输入密码,因为此时我们还没有生成证书。

2.7 生成证书公私钥的步骤

$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa

$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

2.8 测试登陆 ssh localhost

$ ssh localhost

  正常情况下会登陆成功,显示一些成功登陆信息,如果失败请看下面的"一般调试步骤"。

2.9 一般调试步骤

  本人在配置时就失败了,按照以上步骤依旧提示要输入密码。于是用ssh -v 显示详细的登陆信息查找原因:

$ ssh -v localhost

  回车显示了详细的登陆信息如下:

。。。。。。省略

debug1: Authentications that can continue: publickey,gssapi-with-mic,password

debug1: Next authentication method: gssapi-with-mic

debug1: Unspecified GSS failure. Minor code may provide more information

Unknown code krb5 195

debug1: Unspecified GSS failure. Minor code may provide more information

Unknown code krb5 195

debug1: Unspecified GSS failure. Minor code may provide more information

Unknown code krb5 195

debug1: Next authentication method: publickey

debug1: Trying private key: /home/huaxia/.ssh/identity

debug1: Trying private key: /home/huaxia/.ssh/id_rsa

debug1: Offering public key: /home/huaxia/.ssh/id_dsa

debug1: Authentications that can continue: publickey,gssapi-with-mic,password

debug1: Next authentication method: password

huaxia@localhost's password:

  同时用root用户登陆查看系统的日志文件:

$tail /var/log/secure -n 20

。。。。。。省略

Jul 13 11:21:05 shnap sshd[3955]: Accepted password for huaxia from 192.168.8.253 port 51837 ssh2

Jul 13 11:21:05 shnap sshd[3955]: pam_unix(sshd:session): session opened for user huaxia by (uid=0)

Jul 13 11:21:47 shnap sshd[4024]: Connection closed by 127.0.0.1

Jul 13 11:25:28 shnap sshd[4150]: Authentication refused: bad ownership or modes for file /home/huaxia/.ssh/authorized_keys

Jul 13 11:25:28 shnap sshd[4150]: Authentication refused: bad ownership or modes for file /home/huaxia/.ssh/authorized_keys

Jul 13 11:26:30 shnap sshd[4151]: Connection closed by 127.0.0.1

。。。。。。省略

  从上面的日志信息中可知文件/home/huaxia/.ssh/authorized_keys 的权限有问题。

  查看/home/huaxia/.ssh/ 下文件的详细信息如下:

$ ls -lh ~/.ssh/

总计 16K

-rw-rw-r-- 1 huaxia huaxia 602 07-13 11:22 authorized_keys

-rw------- 1 huaxia huaxia 672 07-13 11:22 id_dsa

-rw-r--r-- 1 huaxia huaxia 602 07-13 11:22 id_dsa.pub

-rw-r--r-- 1 huaxia huaxia 391 07-13 11:21 known_hosts

  修改文件authorized_keys的权限(权限的设置非常重要,因为不安全的设置安全设置,会让你不能使用RSA功能 ):

$ chmod 600 ~/.ssh/authorized_keys

  再次测试登陆如下:

$ ssh localhost

Last login: Wed Jul 13 14:04:06 2011 from 192.168.8.253

  看到这样的信息表示已经成功实现了本机的无密码登陆。

2.10 认证登陆远程服务器

  备注:远程服务器OpenSSH的服务当然要启动。

  拷贝本地生产的key到远程服务器端(两种方法)

  1)方法一:

$cat ~/.ssh/id_rsa.pub | ssh 远程用户名@远程服务器ip 'cat - >> ~/.ssh/authorized_keys'

  2)方法二:

  在本机上执行:

$ scp ~/.ssh/id_dsa.pub michael@192.168.8.148:/home/michael/

  登陆远程服务器michael@192.168.8.148 后执行:

$ cat id_dsa.pub >> ~/.ssh/authorized_keys

  本机远程登陆192.168.8.148的测试:

$ssh michael@192.168.8.148

Linux michael-VirtualBox 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686 GNU/Linux

Ubuntu 10.10

Welcome to Ubuntu!

* Documentation: https://help.ubuntu.com/

216 packages can be updated.

71 updates are security updates.

New release 'natty' available.

Run 'do-release-upgrade' to upgrade to it.

Last login: Wed Jul 13 14:46:37 2011 from michael-virtualbox

michael@michael-VirtualBox:~$

  可见已经成功登陆。

  如果登陆测试不成功,需要修改远程服务器192.168.8.148上的文件authorized_keys的权限(权限的设置非常重要,因为不安全的设置安全设置,会让你不能使用RSA功能

chmod 600 ~/.ssh/authorized_keys

重点来了,本次配置出现过多个问题,首先是authorized_keys内容错误,主要还是粗心问题

然后就是权限问题了,必须按照这样要求 :

.ssh上级目录hadoop/的权限素要是700

drwx------. 32 hadoop hadoop 4096 11月 29 16:22 hadoop

.ssh也是700

drwx------.  2 hadoop hadoop     4096 11月 29 16:19 .ssh

authorized_keys的权限需要是644

-rw-r--r--.  1 hadoop hadoop  610 11月 29 16:09 authorized_keys

centos ssh 无密码登录的更多相关文章

  1. CentOS配置ssh无密码登录

      CentOS配置ssh无密码登录的注意点   前提配置:使用root登录修改配置文件:/etc/ssh/sshd_config,将其中三行的注释去掉,如下: 然后重启ssh服务:service s ...

  2. CentOS 下SSH无密码登录的配置

    CentOS 下SSH无密码登录的配置 最近学习Hadoop.它要求各节点之间通过SSH无密码登录,配置SSH的时候费了一番功夫,记录下来,以备忘. 配置SSH无密码登录需要3步: 1.生成公钥和私钥 ...

  3. CentOS系统实现SSH无密码登录的方法

    一.环境配置 1.服务端:CentOS release 5.3 IP:222.73.115.198 2.客服端:CentOS release 5.8 IP:192.168.4.244 二.配置SSH无 ...

  4. CentOS下搭建VNC/TEAMVIEW/SSH无密码登录

    VNC 配置桌面 # 安装gnome桌面环境 yum groupinstall Desktop -y # 安装中文语言支持包(可选) yum groupinstall 'Chinese Support ...

  5. CentOS下SSH无密码登录的配置

    1.确认本机sshd的配置文件(需要root权限) $ gedit /etc/ssh/sshd_config 找到以下内容,并去掉注释符"#" RSAAuthentication ...

  6. ssh 无密码登录

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  7. Linux SSH无密码登录

    Linux服务器常见的登录方式有两种:密码登录.秘钥登录.工作中我们最常使用的是用秘钥登录的方法,因为使用秘钥登录更高效.更安全. 如何实现SSH无密码登录: 原理:无密码ssh登录的主要操作为将本机 ...

  8. ssh 无密码登录要使用公钥与私钥

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  9. 配置SSH无密码登录【原著】

    环境:两台Centos虚拟机,配置了静态的ip.(详见虚拟机如何配置静态的IP地址的操作步骤) 192.168.75.21192.168.75.22 第一步:为每台服务器配置静态IP地址参见: 虚拟机 ...

随机推荐

  1. Sqlserver_判断该路径是否存在该文件

    declare @result int =0declare @path nvarchar(200)='d:\1.csv'execute master.dbo.xp_fileexist @path ,@ ...

  2. Windows 内存架构

    理解 Virtual Memory, Physical Memory, Committed Memory, Page File, Working Set, Modified Pages, Standb ...

  3. Linux下scp命令的用法

    scp 对拷文件夹 和 文件夹下的所有文件 对拷文件并重命名 对拷文件夹 (包括文件夹本身) scp -r   /home/wwwroot/www/charts/util root@192.168.1 ...

  4. C#读写EXCEL

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  5. 【MYSQL】update/delete/select语句中的子查询

    update或delete语句里含有子查询时,子查询里的表不能在update或是delete语句中,如含有运行时会报错:但select语句里含有子查询时,子查询里的表可以在select语句中. 如:把 ...

  6. struts 标签库注解

    在struts2中有着一套像html一样的标签,俗称struts2标签,大多数公司使用ssh都是使用html标签,但为了保持项目的统一性,有的公司还是使用的struts2的标签,下面是一些常用的str ...

  7. android 模拟器 使用键盘的配置

    1 打开 android Manageer , 克隆一个设备 2.

  8. hdu 4033Regular Polygon(二分+余弦定理)

    Regular Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)T ...

  9. webBrowser1执行函数

    IHTMLWindow2 Win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;string s = "alert('x');& ...

  10. [转载]ME51n,ME52n,ME53n屏幕增强

    原文地址:ME51n,ME52n,ME53n屏幕增强作者:cyzhang811 http://blog.sina.com.cn/s/blog_721b218c0100zch9.html 使用增强:ME ...