linux 本地用ssh连接服务器遇到的坑

使用ssh root@xx.xx.xx.xx 报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

我用的是Centos 8,一般来说输入这个命令会弹出输入密码的,但是这里直接报错,Permission denied (权限被拒绝)

可能是服务器的ssh设置的问题?于是我在服务器打开sshd_config文件

vim /etc/ssh/sshd_config 
发现我的密码验证(PasswordAuthentication)关闭了

重新开启密码验证,改成PasswordAuthentication yes
同时检查以下另外两项,用于后面修改本地连接服务器的登陆模式为免密登陆
PermitRootLogin yes (允许root用户登陆)
PubkeyAuthentication yes (允许公钥认证)

在这里插入图片描述
然后重启sshd服务

systemctl restart sshd.service

回到客户端,再次ssh连接:

[root@lee]# ssh root@192.xx.xx.xx
root@192.xx.xx.xx's password: 
Last login: Sun Nov 24 16:59:08 2019 from 112.96.102.242
[root@VM_0_5_centos ~]# 

成功使用密码进入,接着就是配置公钥认证,免密登陆了,这样配置好后,再次把密码验证关闭了,因为前面开启了公钥认证,所以在下面配置好公钥后,就可以直接ssh 用户名@192.xx.xx.xx 进行访问了

  1. 首先在客户端创建公钥与密钥对
ssh-keygen

然后不停的回车,就会生成成功了,我是已经生成过一次了,所以选择覆盖
在这里插入图片描述
然后进入 /root/.ssh 里面就会发现,成功多出了 id_rsa(密钥) 和 id-rsa.pub(公钥)

cd /root/.ssh/

在这里插入图片描述
接下来就是用ssh-copy-id 命令把公钥复制到服务器上,开启无密码SSH登录

[root@DESKTOP-BVI7HEQ .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@你的服务器IP

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@xx.xx.xx.xx's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@xx.xx.xx.xx'"
and check to make sure that only the key(s) you wanted were added.

这一步操作完后,就可以直接ssh root@你服务器IP进入了

ssh 用户名@192.xxx.xxx.xxx

还有一种是拿服务器创建的密钥来登陆,方法在https://cloud.tencent.com/document/product/213/35700#LoginWithKey

接下来就是配置方便自己的访问名称,例如leeon,然后就可以直接 ssh leeon访问服务器了

  1. 如果/root/.ssh/ 里面没有config这个文件,就自己创建一个
vim /root/.ssh/config

加入以下配置:

Host leeon
HostName 192.xx.xxx.xxx
Port 22
User root
IdentityFile /YourPath/YourPrivateKey (这一行可以不写,如果你是使用密钥登陆的,这个地方就写上面密钥登陆教程中从服务器拉下来的密钥文件所的绝对路径)
保存后直接 ssh leeon

[root@DESKTOP-BVI7HEQ .ssh]# ssh leeon
Last login: Sun Nov 24 17:29:53 2019 from 111.230.154.177
[root@VM_0_5_centos ~]# 

接下来就可以登陆服务器了,然后把PasswordAuthentication 改成 no,再次在客户端ssh连接发现依然可以连接上服务器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值