docker 容器centos安装ssh

发表:2018-03-22 09:32:49

1.pull ubuntu镜像

docker pull centos


2.启动并进入

docker run -it -v /data/docker/data/:/data -p 8080:80  centos  /bin/bash


3.安装openssh-server并启动

yum install openssh-server
# 启动之前需手动创建/var/run/sshd,不然启动sshd的时候会报错
mkdir -p /var/run/sshd
# sshd以守护进程运行
/usr/sbin/sshd -D &
# 安装netstat,查看sshd是否监听22端口
yum install net-tools
netstat -apn | grep ssh

如果已经监听22端口,说明sshd服务启动成功


4.生成验证文件

[root@b5926410fe60 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
[root@b5926410fe60 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
[root@b5926410fe60 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key


5.修改 /etc/ssh/sshd_config 配置信息

需要修改两处:

1)UsePAM yes 改为 UsePAM no  
2)UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no

最终整个sshd_config文件如下

#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server


6.修改完sshd-config之后需要重启sshd服务

// 找到pid
ps -aux | grep ssh
kill -9 pid
/usr/sbin/sshd -D &

7.端口影射

如果要让宿主机之外的机器访问,就需要添加影射了。

// 11122宿主机端口,22为容器端口
docker run -it -p 11122:22
// 在其他机器上可以使用以下命令登陆,假设宿主机ip为192.168.1.101
ssh -p 11122 root@192.168.1.101


8.保存修改

有两种方式

1)导出镜像方式

// 把id为id的容器保存
docker commit id sshd:centos
// 停止容器
docker stop id

2)dockerfile方式

FROM centos
                                                                                                  
// 将本目录下的sources.list作为容器的一个文件
ADD sources.list /root/sources.list
// 使用阿里Ubuntu源,更新快
RUN cp /root/sources.list /etc/apt/sources.list.d/aliyun.list
                                                                                                  
RUN yum update
RUN yum install -y openssh-server
RUN yum install -y net-tools
RUN yum install -y vim
RUN mkdir -p /var/run/sshd
RUN mkdir -p mkdir/root/.ssh/
// 修改root密码,便于远程登录
RUN echo root:123456 | chpasswd
// 将key生成在指定文件内
RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -P '' -N ''
// 配置ssh可以使用root登陆
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
                                                                                                  
// 开放22端口
EXPOSE 22
                                                                                                  
CMD /usr/sbin/sshd -D &


相关文章