最近装了 Win10 + Ubuntu20.04 双系统,Win10 用来打游戏,Ubuntu 用来学习。

/手动狗头

环境:

Ubuntu20.04  Linux Kernel 5.4.0-48-generic
Docker Version 19.03.13

1、启动 2 个 centos7 镜像

docker pull centos:7
docker run -itd --name centos7-1 centos:7
docker run -itd --name centos7-2 centos:7
docker ps

2、容器中安装 openssh

docker exec -it centos7-1 bash
yum install net-tools
yum install passwd openssl openssh-server -y

修改 ssh 服务配置文件:

yum install vim
vim /etc/ssh/sshd_config
# 开启 22 端口监听(Port)
# 允许 any 地址登录(AddressFamily && ListenAddress)
# 允许 root 用户登录(PermitRootLogin)
# UseDNS no 或者注释掉

启动 ssh 服务报错:

[root@ b3426410ff43 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key

解决方案:

[root@b3426410ff43 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@b3426410ff43 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@b3426410ff43 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''

然后再次启动 ssh 服务即可。

3、宿主机安装 ansible 并测试

安装 ansible:

# 方法 1(如果宿主机是 centos 注意安装一下 epel-release)
sudo apt-get install ansible
# 方法 2
pip install ansible

生成 ssh key:

ssh-keygen -t rsa
# 拷贝宿主机 public key 到两个容器内
ssh-copt-id root@container-ip1
ssh-copt-id root@container-ip2

测试一下:

ansible all -i root@172.17.0.2,root@172.17.0.3 -m ping

待续

Logo

一站式 AI 云服务平台

更多推荐