[ubuntu] ssh登录docker

Ray Zhang

方法一

如果镜像已经设置好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

方法二

进入容器后

# install ssh
$ apt-get install openssh-client
$ apt-get install openssh-server

# 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
阅读 177
1 声望
2 粉丝
0 条评论
1 声望
2 粉丝
文章目录
宣传栏