如何在centos docker容器中支持sshd

Posted aibug

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在centos docker容器中支持sshd相关的知识,希望对你有一定的参考价值。

### 1 启动容器,以centos 为例,其他没试过

#####root用户执行下面的语句,非root,加个sudo:
```shell
docker run -d --privileged=true --name=centos centos /usr/sbin/init whlile true
```
解释一下
--privileged=true可以解决dbus没有权限启动的问题
/usr/sbin/init 可以解决不启动dbus的问题,因为centos镜像Dockerfile的默认CMD为/bin/bash,不启动dbus

### 2 进入容器查看dbus
```shell
systemctl status dbus,
```
状态正常应该如下所示
```shell
[[email protected] /]# systemctl status dbus
● dbus.service - D-Bus System Message Bus
Loaded: loaded (/usr/lib/systemd/system/dbus.service; static; vendor preset: disabled)
Active: active (running) since Fri 2018-12-14 11:31:36 UTC; 1min 16s ago
Docs: man:dbus-daemon(1)
Main PID: 183 (dbus-daemon)
CGroup: /docker/7df194571ba40af24540694149be9c353ed201ef3495f97b7f91c7c3e5f6031b/system.slice/dbus.service
└─183 /usr/bin/dbus-daemon --system --address=systemd: --nofork --...
? 183 /usr/bin/dbus-daemon --system --address=systemd: --nofork --...

```
### 3 安装openssh-server,如果要ssh到其他机器,也可以把openssh-clients也装了
##### -y 直接确认 -q 不想看一堆的安装信息的时候加上
##### 命令行:
```shell
yum -y -q install openssh-server
```
### 4 编辑sshd配置文件,可以修改端口等配置
非必须,net的host模式启动会和宿主机冲突,不改起不来
```shell
vi /etc/ssh/sshd_config
#Port 22
```
### 5 启动sshd服务并查看即可
```shell
systemctl start sshd
systemctl status sshd
```
##### 如下启动成功
```shell
[[email protected] /]# systemctl start sshd
[[email protected] /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2018-12-14 11:48:34 UTC; 12s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 2292 (sshd)
CGroup: /docker/7df194571ba40af24540694149be9c353ed201ef3495f97b7f91c7c3e5f6031b/system.slice/sshd.service
└─2292 /usr/sbin/sshd -D
? 2292 /usr/sbin/sshd -D

Dec 14 11:48:34 7df194571ba4 systemd[1]: Starting OpenSSH server daemon...
Dec 14 11:48:34 7df194571ba4 sshd[2292]: Server listening on 0.0.0.0 port 22.
Dec 14 11:48:34 7df194571ba4 sshd[2292]: Server listening on :: port 22.
Dec 14 11:48:34 7df194571ba4 systemd[1]: Started OpenSSH server daemon.
[[email protected] /]#

```
### 6 设置密码
```shell
[[email protected] /]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[[email protected] /]#
```
### 7 本地登录测试一下,
##### 远程要么映射端口,要么net=host模式改22为其他端口,登录成功
```shell
[[email protected] /]# ssh localhost
The authenticity of host ‘localhost (127.0.0.1)‘ can‘t be established.
ECDSA key fingerprint is SHA256:CGSoUYr82TXpUZXL2xM3PdB+1ku8ZS45yKp8JQdnW18.
ECDSA key fingerprint is MD5:c0:0d:f1:54:fe:f0:f2:83:ca:3b:b3:50:49:42:78:54.
Are you sure you want to continue connecting (yes/no)? yes

Last failed login: Fri Dec 14 11:54:27 UTC 2018 from localhost on ssh:notty
There were 3 failed login attempts since the last successful login.
```
转载请注明出处
https://www.aibug.cn/doc/web/#/page/edit/9/12













































































以上是关于如何在centos docker容器中支持sshd的主要内容,如果未能解决你的问题,请参考以下文章

docker 之 commit提交sshd镜像

docker容器开启sshd

系列2使用Dockerfile创建带sshd服务的Centos Docker镜像

为啥不需要在 Docker 容器中运行 sshd

用dockerfile创建支持ssh服务的centos镜像

可以在docker容器里跑windows么