[ubuntu] ssh登录docker
文章目录
方法一
如果镜像已经设置好ssh服务,可以:
docker run -d your_image:tag /usr/sbin/sshd -p 12345 -D-d是docker的指令,后台运行/usr/sbin/sshd -p 12345 -D容器运行的指令,开启12345端口的ssh连接
-D的解释
Do not detach and become daemon. This is often used when sshd is run using systemd. This allows easier monitoring of the process in such environments. Without this option, the SSH server forks and detaches from terminal, making itself a background daemon process. The latter has been the traditional way to run the SSH server until recently. Many embedded systems would still use the latter.
在其他地方连接,密码默认是原主机密码,可以看方法二最后进行修改
$ ssh root@{ip got above} -p 12345方法二
进入容器后,先换apt源
# install ssh
$ apt-get install openssh-client
$ apt-get install openssh-server -y
# start ssh service
$ /etc/init.d/ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
# enable root login
$ vim /etc/ssh/sshd_config
#------------ in file sshd_config -------------#
# "PermitRootLogin yes" needs adding as below
# 像下面一样加上"PermitRootLogin yes"
...
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
...
#---------- end of file sshd_config -----------#
# restart service
$ service ssh restart
* Restarting OpenBSD Secure Shell server sshd [ OK ]
# set password of user root
$ passwd root
# add port to ssh
$ /usr/sbin/sshd -p 12345
# checkout ip
$ apt-get install net-tools
$ ifconfig在其他地方连接
$ ssh root@{ip got above} -p 12345注意!docker容器重新开启之后,需要重新添加port
# 可以在容器内再次添加port
$ /usr/sbin/sshd -p 12345
# 也可以再start容器后,直接使用
$ sudo docker exec -it $DOCKER_ID /bin/bash -c'/usr/sbin/sshd -p 12345'如果有帮到你,请帮我点个赞 : )
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论