一、docker拉取centOs镜像
使用docker命令进行拉取:
docker pull centos : 此种方式是拉取最新版的centOs镜像,
docker pull centos:[版本] :这种方式可以拉取不同版本的centOs镜像
二、运行centOs容器
使用命令:docker run -it -d --name centos-latest -p 5000:22 --privileged=true centos /sbin/init
=======================================================================================
centOs镜像只是最基本的骨架,好多命令不支持,所以先安装一些常用的命令:
1. 安装网络环境
yum -y install net-tools
2. 安装openssh-server
yyum -y install openssh-server
3. 安装vim
yum install vim
4. 安装passwd
yum install passwd
4.1. 修改root密码
[root@7a1b1888cc86 /]# 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@7a1b1888cc86 /]#
三、启动容器sshd服务
[root@7a1b1888cc86 /]# /usr/sbin/sshd
如果出现下面的情况:
[root@7a1b1888cc86 /]# /usr/sbin/sshd Unable to load host key: /etc/ssh/ssh_host_rsa_key Unable to load host key: /etc/ssh/ssh_host_ecdsa_key Unable to load host key: /etc/ssh/ssh_host_ed25519_key sshd: no hostkeys available -- exiting.
执行1:
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""
[root@7a1b1888cc86 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" Generating public/private rsa key pair. Your identification has been saved in /etc/ssh/ssh_host_rsa_key. Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub. The key fingerprint is: SHA256:ythgDxqFyYmXYuY1X/Lin8txH5jmwoDtazqePCCdVXs root@7a1b1888cc86 The key‘s randomart image is: +---[RSA 3072]----+ | | | o = . | |ooBo.o o | |+o..+ = E | | o.o*o oS | |..o=.X.. o | |. o o.B. = . | | .o.ooo* . . | | .==..=o. . | +----[SHA256]-----+
执行2:
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
[root@7a1b1888cc86 /]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" Generating public/private ecdsa key pair. Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key. Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub. The key fingerprint is: SHA256:6y/gjIVmy6NzaD9i1qTESiJVKxW4aRHvOc0adOG8A+Y root@7a1b1888cc86 The key‘s randomart image is: +---[ECDSA 256]---+ | .o... | | o.oo . | | *=.+ | | *=.* . | | + .E.= S | |o.o =+o. . | |+o O.* .. | |. O O o.. | | +.*.o .o. | +----[SHA256]-----+
执行3:
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
[root@7a1b1888cc86 /]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" Generating public/private ed25519 key pair. Your identification has been saved in /etc/ssh/ssh_host_ed25519_key. Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub. The key fingerprint is: SHA256:MpXueojz012ZGl29vrQCr18E3H0oGs/fzmYV8ZVUf1Q root@7a1b1888cc86 The key‘s randomart image is: +--[ED25519 256]--+ | ..E| | . . .=+| | o . .o.oO| | o = .o.=| | o S ..o+ .o| | + ..=..o.| | . o.. +o ooo| | o o.o o oo++| | ooo .o..=+| +----[SHA256]-----+
最后看下是否成功:
[root@7a1b1888cc86 /]# netstat -antp | grep sshd [root@7a1b1888cc86 /]# /usr/sbin/sshd [root@7a1b1888cc86 /]# netstat -antp | grep sshd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 48/sshd tcp6 0 0 :::22 :::* LISTEN 48/sshd [root@7a1b1888cc86 /]#
OK,已经可以远程访问啦!