ssh免密登录 以及Permission denied问题解决

两台虚拟机:ip:192.168.60.2,192.168.60.3
系统:centos7
前提(关闭防火墙或在防火墙中打开端口22)

准备
1、首先安装查看是否安装有ssh

rpm -qa | grep ssh

在这里插入图片描述
看到上图所示 ,表示已经安装(未安装 )

yum install -y openssl openssh-server

2、安装成功,启动ssh

 systemctl start sshd.service

3、设置ssh开机自启动:

systemctl enable sshd.service

快速实现免密:
前提:两台机器用户名相同
1、生成密钥对

ssh-keygen  -t  rsa

2、使用ssh-copy-id

ssh-copy-id  -i .ssh/id_rsa.pub   目标机器
或者  ssh-copy-id  目标机器

手动实现免密登录
1、首先在本机当前用户目录下生成密钥对

ssh-keygen -t rsa

2、此时在当前用户目录下回生成.ssh文件夹 查看

 ll -a     #查看全部文件  .ssh为隐藏文件

在这里插入图片描述
3、进入.ssh目录 cd .ssh 此时可以看到两个文件 公钥和私钥
在这里插入图片描述
4、创建文件authorized_keys并修改权限(注:文件权限必须是600,否则无效)

touch authorized_keys
chmod 600 authorized_keys

5、将自己的公钥放进authorized_keys实现自登陆免密

cat id_rsa.pub >> authorized_keys

6、复制本地公钥到另一台机器的用户目录下

scp id_rsa.pub  192.168.60.3:/home/centos01/

此时需要输入密码
7、成功之后,登录192.168.60.3,将该公钥加入本机器的authorized_keys(默认本机器已完成上述步骤)

cat id_rsa.pub >> .ssh/authorized_keys

至此,192.168.60.2–>192.168.60.3免密登录完成

途中可能遇到问题:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)

这是因为两台机器的用户名不同,普通用户的时候文件权限有问题
我的解决方式是将两台机器改成相同用户名
root用户登录

vi /etc/passwd 找到原先的用户名,将其改为自己的用户名,但是/home/“原先用户名” 中的不能更改,若更改重启后,便登陆不了系统了。
vi /etc/shadow 找到原先用户名,改为自己的用户名

以上步骤完毕后,sudo reboot重启
如有其他方式成功解决该问题,请告知,感谢!

发布了8 篇原创文章 · 获赞 1 · 访问量 8407
展开阅读全文

ssh登陆“Permission denied ”是怎么回事?

07-19

ssh登陆服务器,输入密码后却一直不能登陆进去,输出的调试信息是: ``` wangxiaoxiaodeMacBook-Air:.ssh wangxiaoxiao$ ssh -v -p 1029 xxwang@115.156.164.251 OpenSSH_6.9p1, LibreSSL 2.1.8 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to 115.156.164.251 [115.156.164.251] port 1029. debug1: Connection established. debug1: identity file /Users/wangxiaoxiao/.ssh/id_rsa type 1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/wangxiaoxiao/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.9 debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3 debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3 pat OpenSSH_6.6.1* compat 0x04000000 debug1: Authenticating to 115.156.164.251:1029 as 'xxwang' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:hEan0tTVdOIgIhYZnv9cee++XRQXxts3RWehu5+cDis debug1: Host '[115.156.164.251]:1029' is known and matches the ECDSA host key. debug1: Found key in /Users/wangxiaoxiao/.ssh/known_hosts:1 debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey,password debug1: Next authentication method: publickey debug1: Offering RSA public key: /Users/wangxiaoxiao/.ssh/id_rsa debug1: Authentications that can continue: publickey,password debug1: Trying private key: /Users/wangxiaoxiao/.ssh/id_dsa debug1: Trying private key: /Users/wangxiaoxiao/.ssh/id_ecdsa debug1: Trying private key: /Users/wangxiaoxiao/.ssh/id_ed25519 debug1: Next authentication method: password xxwang@115.156.164.251's password: debug1: Authentications that can continue: publickey,password Permission denied, please try again. xxwang@115.156.164.251's password: debug1: Authentications that can continue: publickey,password Permission denied, please try again. xxwang@115.156.164.251's password: debug1: Authentications that can continue: publickey,password debug1: No more authentication methods to try. Permission denied (publickey,password).``` ``` 然后我谷歌了一下,按照网上的方法,把.ssh删了,再重新ssh-keygen重新生成,后再ssh,依然存在这个同样的问题? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览