SSH服务器拒绝了密码

SSH服务器拒绝了密码,第1张

在平常项目中出现ssh拒绝密码,无外乎下面两种情况
1、账号密码不正确

可通过修改实例密码来解决,最好是复制在文本然后粘贴,否则会出错

2、sshd的设置不允许root用户用密码远程登录

修改 vim /etc/ssh/sshd_config

修改:

1、将PermitRootLogin no改成PermitRootLogin yes

2、service sshd restart

前言
我们在进行客户机到主机的SSH连接的时候,往往采用PASSWORD登陆的方式,总是显得很繁琐。我们可以更具RSA密钥来避免这种登陆方式。

场景
我们需要从client机器上远程登陆server机器。登陆方式采用RSA密钥免密码登陆方式。其中client机器为Fedora,server为Ubuntu。

步骤
客户机生成密钥
用corey登陆客户机client。
[corey@corey-fedora ssh]$Su
[corey@corey-fedora ssh]$Chmod a+w /home/corey/ssh
[corey@corey-fedora ssh]$Su corey
[corey@corey-fedora ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair
Enter file in which to save the key (/home/corey/ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/corey/ssh/id_rsa
Your public key has been saved in /home/corey/ssh/id_rsapub
The key fingerprint is:
d4:4b:69:f3:19:bc:bf:82:56:40:43:4c:57:02:43:1a corey@corey-fedora
The key's randomart image is:
+--[ RSA 2048]----+
| E=o |
| ++= |
| oo |
| o+ + |
| S + |
| |
| o |
| o |
| |
+-----------------+
[corey@corey-fedora ssh]$ ls
id_rsa id_rsapub
上传公钥至服务器
用corey登陆server服务器。将id_rsapub上传到/home/corey/ssh
corey@corey-ubuntu:~/ssh$ cat id_rsapub >>authorized_keys
corey@corey-ubuntu:~/ssh$ ls
authorized_keys id_rsa id_rsapub
corey@corey-ubuntu:~/ssh$ chmod 644 authorized_keys
corey@corey-ubuntu:~/ssh$ ls
authorized_keys id_rsa id_rsapub
corey@corey-ubuntu:~/ssh$
从客户机client登陆server
[corey@corey-fedora ssh]$ ssh 1921681103
The authenticity of host '1921681103 (1921681103)' can't be established
RSA key fingerprint is 5b:3b:4d:00:b6:9f:e7:d6:fb:e9:9f:25:40:4d:4a:50
Are you sure you want to continue connecting (yes/no) yes
Warning: Permanently added '1921681103' (RSA) to the list of known hosts
Linux corey-ubuntu 2628-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc//copyright

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law

To access official Ubuntu documentation, please visit:
>1、要配置ssh首先要先有ssh,所以第一步要进行安装。sudo apt-get install
openssh-server
2、首先需要给几台互联的计算机配置hosts文件,例如TOM机
sudo vi /etc/hosts
添加几条内容
192168037 TOM
1921680111 CAT
192168
等等这些就是要联机的计算机名和IP对应直
保存退出,并且每台计算机都要这样配置
3、然后特别要注意一点的就是你需要在哪个用户下面使用无密码访问,一定要弄清楚,因为linux是多用户系统,所以不同的用户设置不同,可能会引起不必要的问题。
4、所以我这里设定一个场景,描述为计算机TOM,计算机CAT,分别都有一个用户为shu,设置要求:
1)先让TOM可以自己无密码访问自己;
2)让TOM无密码访问CAT;
3)让CAT无密码访问TOM;
先来解决第一个问题
在任何访问以前首先要确保要访问的计算机没有开启防火墙
1)在TOM上,以shu的用户身份生成密码,使用命令ssh-keygen -t
rsa,接下来会出现系列提示,一直回车即可,出现以下说明生成密码成功
shu@tom:~$ ssh-keygen -t rsa
Generating public/private rsa key pair
Enter file in which to save the key (/home/shu/ssh/id_rsa):
/home/shu/ssh/id_rsa already exists
Overwrite (y/n) y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/shu/ssh/id_rsa
Your public key has been saved in /home/shu/ssh/id_rsapub
The key fingerprint is:
21:94:35:a3:cc:30:0b:41:18:8f:4b:55:02:61:51:ad shu@tom
The key's randomart image is:
+--[ RSA 2048]----+
|OO==o+
|
|ooO
o
|
| o =

|
| E

|
|
S
|
|
|
|
|
|
|
|
|
+-----------------+
这时会产生几个文件,然后进入文件夹cd /home/shu/ssh就会发现有:
id_rsa id_rsapub
known_hosts文件,其中,id_rsa是密钥放在本地机上,id_rsapub是公钥,需要给访问的计算机上,所以接下来,要生成一个authorized_keys文件,将id_rsapub内容放入authorized_keys,需要指令
cp id_rsapub authorized_keys即可,或者ls id_rsapub
复制内容,粘贴到authorized_keys也行,这样就是说把TOM的shu当成要登录的计算机,自己无密码访问自己,此时就可以使用ssh
shu@TOM即可无密码访问,也可能第一次需要密码,以后就不需要密码了。
2)要使TOM无密码访问CAT
可以使用远程访问ssh登录到CAT上,在shu用户下面使用ssh-keygen -t rsa命令,再进入cd
/home/shu/ssh
对authorized_keys进行编辑,把TOM下shu的authorized_keys的内容复制给CAT下的authorized_keys就可以了。
3)TOM和CAT相互无密码访问
只需在2步的基础上,把CAT下面的id_rsapub的内容也复制给他自己和TOM的authorized_keys就可以了,这样双方都具有对方和自己的公钥,就可以实现无密码访问了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/12884374.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存