docker中配置ssh自启动
时间: 2024-05-03 21:21:46 浏览: 148
要在Docker中配置SSH自启动,可以按照以下步骤:
1. 创建一个Dockerfile文件,内容如下:
```
FROM ubuntu:latest
RUN apt-get update && apt-get -y install openssh-server
RUN mkdir /var/run/sshd
COPY sshd_config /etc/ssh/sshd_config
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
```
2. 创建sshd_config文件,内容如下:
```
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
PasswordAuthentication yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
```
3. 在Dockerfile所在的目录中执行以下命令进行构建:
```
docker build -t my-ssh-image .
```
4. 运行容器,并映射22端口:
```
docker run -d -p 2222:22 my-ssh-image
```
5. 使用ssh客户端连接到容器:
```
ssh root@localhost -p 2222
```
6. 如果需要在容器启动时自动启动SSH服务,可以将CMD命令修改为:
```
CMD ["/bin/bash", "-c", "/usr/sbin/sshd && tail -f /dev/null"]
```
阅读全文