1

docker centos容器ssh安装、启动、排坑、免密配置

 2 years ago
source link: https://www.zoucz.com/blog/2020/08/11/e4dc6560-dbc9-11ea-90b5-eb40e9720ed0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

docker centos容器ssh安装、启动、排坑、免密配置

作者: 邹成卓 2020-08-11 19:57:56 分类: linux

标签: linux

评论数:

备忘一下我要说话

安装wget和ssh

yum install wget
yum install openssh-server 我要说话

生成key

ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key 我要说话

#这里注意,只需要生成rsa key就行了,dsa连通性测试会报错 我要说话

#ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key 我要说话

#ssh-keygen -t dsa -f /etc/ssh/ssh_host_ecdsa_key 我要说话

#ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key 我要说话

注意,这里的key如果重新生成重启sshd,客户端连接时若报错 我要说话

Add correct host key in /Users/czzou/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/czzou/.ssh/known_hosts:22
RSA host key for [1.2.3.4]:36002 has changed and you have requested strict checking.
Host key verification failed. 我要说话

那么在/Users/czzou/.ssh/known_hosts文件中把对应这个ssh连接的缓存信息删除,即可重新连接。我要说话

启动ssh服务

/usr/sbin/sshd我要说话

passwd 然后 123456 123456我要说话

一个不好排查的坑

docker run 启动容器,不带--privileged选项时,会出现一个很难排查的现象,ssh连接建立连接成功的一瞬间,会直接关闭连接。 我要说话

抓包发现连接建立完成后,客户端会给服务端发Fin包,断开连接。
image.png
感觉有点无从下手,找不到原因。
当然,有可能是前面ssh server给client发送的数据包里边有断开连接的指令。
从网上看了一些资料,没找到类似的案例,但是有提到可能是用户权限问题。 我要说话

尝试重新启动容器,docker run -it --name ch --privileged -p 36002:22 centos:7 bash,安装并启动sshd,问题解决。 我要说话

ssh免密登录配置

~/.ssh目录下若已经存在有公钥(id_rsa.pub)、私钥文件(id_rsa),则可以跳过客户端配置步骤;
否则运行ssh-keygen生成公私钥。 我要说话

  1. 修改sshd服务配置

/etc/ssh/sshd_config文件中,下面的选项如果有则解开注释 我要说话

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys
  1. 配置authorized_keys文件

~/.ssh/authorized_keys不存在,则创建此文件
将客户端的公钥(id_rsa.pub)内容拷贝到authorized_keys中我要说话

  1. 重启sshd

本文链接:https://www.zoucz.com/blog/2020/08/11/e4dc6560-dbc9-11ea-90b5-eb40e9720ed0/我要说话

☞ 参与评论我要说话


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK