免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4778 | 回复: 7
打印 上一主题 下一主题

SSH无需密码登录SSHD服务器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-20 13:22 |只看该作者 |倒序浏览
SSH无需密码登录SSHD服务器



上段时间,需要在将一台主CVS服务器上的文件自动备份到另一台备份CVS服务上,其中需要用到SSH的加密传输,所以,按照我的操作方式整理一下了,供大家参考。此方式在HA、多服务器ssh远程管理等方面都可以用到。

0、引言
两台服务器,分别是服务器A192.168.3.212,服务器B192.168.3.213。服务器A ssh到服务器B,通过用户证书,无需输入口令直接登录服务器Bsshd。具体方法如下。

1、采用root用户登录

1.1、在A服务器上用ssh-keygen产生root用户的公钥和私钥。
[root@FC8Server01 /]# 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:
49:3a:87:11:11:15:1f:3d:21:c0:cf:e2:db:3b:d4:48 root@FC8Server01
注意:Enter passphrase (empty for no passphrase):Enter same passphrase again:提示处直接回车

1.2、进入/root/.ssh/目录,并列举文件
[root@FC8Server01 /]# cd /root/.ssh/;ll
-rw------- 1 root root 1671 11-12 00:15 id_rsa
-rw-r--r-- 1 root root
398 11-12 00:15 id_rsa.pub


1.3、将/root/.ssh/id_rsa.pub文件拷贝为B服务器的/root/.ssh/authorized_keys
[root@FC8Server01 .ssh]# scp id_rsa.pub 192.168.3.213:/root/.ssh/authorized_keys
The authenticity of host '192.168.3.213 (192.168.3.213)' can't be established.
RSA key fingerprint is 73:42:09:61:ec:7a:49:8a:d2:5b:63:c0:70:cf:dd:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.213' (RSA) to the list of known hosts.
root@192.168.3.213's password:
id_rsa.pub
100%
398
0.4KB/s
00:00


1.4、测试
[root@FC8Server01 .ssh]# ssh 192.168.3.213
Last login: Tue Nov 10 23:40:50 2009 from 192.168.3.212

如果ssh 192.168.3.213,不再提示输入密码,则表示成功,否则失败。

2、采用非root用户登录
2.1、在A服务器上创建登录用户testuser,并设置密码为testuser
[root@FC8Server01 ~]# useradd -d "/home/testuser" -s "/bin/bash" -c "testuser File Owner" testuser

[root@FC8Server01 ~]# passwd testuser
Changing password for user testuser.
新的 UNIX 口令:
无效的口令: 它基于字典单词
重新输入新的 UNIX 口令:
passwd: all authentication tokens updated successfully.

2.2、在B服务器上创建登录用户testuser,并设置密码为testuser
[root@FC8Server02 ~]# useradd -d "/home/testuser" -s "/bin/bash" -c "testuser File Owner" testuser

[root@FC8Server02 ~]# passwd testuser
Changing password for user testuser.
新的 UNIX 口令:
无效的口令: 它基于字典单词
重新输入新的 UNIX 口令:
passwd: all authentication tokens updated successfully.

2.3、用testuser用户登录A服务器
[testuser@FC8Server01 ~]$ pwd
/home/testuser
[testuser@FC8Server01 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/testuser/.ssh/id_rsa):
Created directory '/home/testuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/testuser/.ssh/id_rsa.
Your public key has been saved in /home/testuser/.ssh/id_rsa.pub.
The key fingerprint is:
36:02:61:f5:a7:9f:c8:d6:99:4f:ed:d1:d1:97:eb:64 testuser@FC8Server01

2.4、用testuser用户登录B服务器
创建.ssh目录
[testuser@FC8Server02 ~]$ ll -a
总计 32
drwx------ 3 testuser testuser 4096 11-11 00:28 .
drwxr-xr-x 5 root
root
4096 11-11 00:28 ..

-rw-r--r-- 1 testuser testuser
33 11-11 00:28 .bash_logout

-rw-r--r-- 1 testuser testuser
176 11-11 00:28 .bash_profile

-rw-r--r-- 1 testuser testuser
124 11-11 00:28 .bashrc

drwxr-xr-x 2 testuser testuser 4096 11-11 00:28 .gnome2
-rw-r--r-- 1 testuser testuser
658 11-11 00:28 .zshrc

[testuser@FC8Server02 ~]$ mkdir .ssh
[testuser@FC8Server02 ~]$ ll -a
总计 36
drwx------ 4 testuser testuser 4096 11-11 00:32 .
drwxr-xr-x 5 root
root
4096 11-11 00:28 ..

-rw-r--r-- 1 testuser testuser
33 11-11 00:28 .bash_logout

-rw-r--r-- 1 testuser testuser
176 11-11 00:28 .bash_profile

-rw-r--r-- 1 testuser testuser
124 11-11 00:28 .bashrc

drwxr-xr-x 2 testuser testuser 4096 11-11 00:28 .gnome2
drwxrwxr-x 2 testuser testuser 4096 11-11 00:32 .ssh
-rw-r--r-- 1 testuser testuser
658 11-11 00:28 .zshrc


2.5、将/home/testuser/.ssh/id_rsa.pub文件拷贝为B服务器的/home/testuser/.ssh/authorized_keys
[testuser@FC8Server01 ~]$ scp .ssh/id_rsa.pub
192.168.3.213:/home/testuser/.ssh/authorized_keys

testuser@192.168.3.213's password:
id_rsa.pub


100%
402
0.4KB/s
00:00


注意此处的密码为testuser的密码

2.6、调整B服务器上的.sshauthorized_keys权限
[testuser@FC8Server02 ~]$ chmod 0700 .ssh
[testuser@FC8Server02 ~]$ chmod 0600 .ssh/authorized_keys
此处非常重要,如果权限不正确,则ssh时,提示还需要密码

2.7、测试
[testuser@FC8Server01 ~]$ ssh 192.168.3.213
Last login: Wed Nov 11 00:32:49 2009 from 192.168.3.2
如果ssh 192.168.3.213,不再提示输入密码,则表示成功,否则失败


[ 本帖最后由 suky78 于 2009-11-20 13:25 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-11-20 15:46 |只看该作者
你所做的一些步骤都没有涉及到sshd-config也就是说,你没有把密码认证改为密钥认证。
所以每次你输入用户的时候都是对应的密码啊!

论坛徽章:
1
天秤座
日期:2013-10-23 13:20:42
3 [报告]
发表于 2009-11-20 20:05 |只看该作者
对,手上说的对,sshd-config都未配置,何来用密钥认证

论坛徽章:
381
CU十二周年纪念徽章
日期:2014-01-04 22:46:58CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52CU大牛徽章
日期:2013-03-14 14:08:55CU大牛徽章
日期:2013-04-17 11:17:19CU大牛徽章
日期:2013-04-17 11:17:32CU大牛徽章
日期:2013-04-17 11:17:37CU大牛徽章
日期:2013-04-17 11:17:42CU大牛徽章
日期:2013-04-17 11:17:47CU大牛徽章
日期:2013-04-17 11:17:52CU大牛徽章
日期:2013-04-17 11:17:56
4 [报告]
发表于 2009-11-20 21:19 |只看该作者
还需要配置sshd_config使用key认证,禁止passwd认证

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-07-11 22:20:00
5 [报告]
发表于 2009-11-21 09:18 |只看该作者
同学你可以考虑下
# ssh-copy-id

[ 本帖最后由 cst05001 于 2009-11-21 09:23 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-11-21 10:54 |只看该作者

回复 #5 cst05001 的帖子

你又复出了。。。

论坛徽章:
0
7 [报告]
发表于 2009-11-21 10:56 |只看该作者

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
8 [报告]
发表于 2009-11-21 15:12 |只看该作者
如果配置成不使用密码的话,就比较不安全了啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP