ssh配置详解

 

 

补充下基础知识

 

1.1安装SSH

 

yum install ssh

 

1.2启动SSH

 

service sshd start

 

1.3设置开机运行

 

chkconfig sshd on

 

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

    $ vi /etc/ssh/sshd_config

 

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

 

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

 

 

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

    $ service sshd restart

 

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

    $ ssh localhost

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

 

 

1.7 生成证书公私钥的步骤

$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

 

 

1.8 测试登陆 ssh localhost

    $ ssh localhost

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

 

1.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

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

 

1.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:~$

 

 

  可见已经成功登陆。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值