码迷,mamicode.com
首页 > 其他好文 > 详细

如何在centos docker容器中支持sshd

时间:2018-12-14 21:13:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:lib   man   yum   ted   for   tar   [1]   config   pdb   

### 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
[root@7df194571ba4 /]# 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
[root@7df194571ba4 /]# systemctl start sshd
[root@7df194571ba4 /]# 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.
[root@7df194571ba4 /]#

```
### 6 设置密码
```shell
[root@7df194571ba4 /]# 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.
[root@7df194571ba4 /]#
```
### 7 本地登录测试一下,
##### 远程要么映射端口,要么net=host模式改22为其他端口,登录成功
```shell
[root@7df194571ba4 /]# 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

标签:lib   man   yum   ted   for   tar   [1]   config   pdb   

原文地址:https://www.cnblogs.com/aibug/p/10121503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!