Docker 容器開啟SSH需要手動安裝SSH相關軟件,並啟動,且做好容器端口和宿主機端口映射
1.安裝軟件
yum install passwd openssl openssh-server -y
2.編輯 /etc/ssh/sshd_config
RSAAuthentication yes #啟用 RSA 認證
PubkeyAuthentication yes #啟用公鑰私鑰配對認證方式
AuthorizedKeysFile .ssh/authorized_keys #公鑰文件路徑(和上面生成的文件同)
PermitRootLogin yes #root能使用ssh登錄
3.啟動SSHD服務
/usr/sbin/sshd -D&
或者service sshd restart
如果不能正常啟動,報錯 sshd: no hostkeys available -- exiting. 需要執行 ssh-keygen -A 再次啟動SSHD服務即可
4.創建新鏡像
docker commit -m "rocky with ssh" -a "terry" [container] [image]:[tag]
5.創建並運行容器
docker run -p [hostport]:[containerport] -itd [image] /usr/sbin/sshd -D