新增Docker ssh管理

This commit is contained in:
2026-01-05 00:26:30 +08:00
parent a0a57e2fb5
commit 46124ba752
2 changed files with 66 additions and 4 deletions

View File

@@ -0,0 +1,34 @@
FROM centos:7
# 设置环境变量(登录密码)
ARG ROOT_PASSWORD
# 切换镜像源
RUN mv /etc/yum.repos.d/* /tmp \
&& curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 安装SSH服务及相关工具
RUN yum install -y \
openssh-server \
openssh-clients \
passwd \
sudo \
vim \
net-tools \
iproute \
which \
&& yum clean all
# 配置SSH服务
RUN ssh-keygen -A \
&& mkdir -p /var/run/sshd \
&& echo "root:${ROOT_PASSWORD}" | chpasswd \
&& sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config \
&& sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config \
&& sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
# 开放SSH端口
EXPOSE 22
# 启动SSH服务
CMD ["/usr/sbin/sshd", "-D"]

View File

@@ -2,9 +2,37 @@
> 本文作者:丁辉
# Centos 镜像构建
# Centos
```bash
./build.sh
```
- Centos 基础镜像构建
```bash
./build.sh
```
- Centos 配置策略允许外部通过 SSH 登录
1. 构建镜像
```bash
docker build --build-arg ROOT_PASSWORD=password -t centos-ssh:7 -f Dockerfile-ssh .
```
**参数解释**
`ROOT_PASSWORD`:配置登录 Centos 默认 ROOT 密码
2. 启动镜像并登录
```bash
docker run --name centos -p 2222:22 -d centos-ssh:7
```
3. 登录
```bash
ssh -p 2222 root@IP
```