docker 启动 centos8 并配置 ssh

首先说明为何不用centos7,因为在使用centos7时遇到问题,无法通过搜索引擎解决。

安装docker,不同机器不再描述。

1、参数很重要,不然会导致一些问题,端口可自行映射,镜像命名这里为centos8

sudo docker run -d -i -t -p 5555:22 --privileged --name=centos8 <容器id> /usr/sbin/init

2、进入对应容器命令行,或者通过可视化工具,点击对应的命令行按钮进入

docker exec -it centos8 /bin/bash

 3、mac修改yum源,解决yum获取不了资源问题,如果是Windows请看文章结尾补充

cd /etc/yum.repos.d

  修改两个配置

vi CentOS-Linux-BaseOS.repo

[baseos]
name=CentOS Linux $releasever - BaseOS
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
vi CentOS-Linux-AppStream.repo

[appstream]
name=CentOS Linux $releasever - AppStream
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/AppStream/$basearch/os/ 
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

4、安装工具

yum install openssh-server -y

yum install initscripts -y

yum install vim -y

yum install net-tools -y

yum install passwd -y

5、vim打开sshd服务配置文件,开启如下配置

vim /etc/ssh/sshd_config

...
Port22
#AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::
...

重启sshd配置

service sshd start

#看sshd是否启动
ps -e | grep sshd

#看22端口是否启动
netstat -an|grep 22

将sshd服务设置为开机自启动

systemctl enable sshd.service

#查看是否成功
systemctl list-unit-files | grep sshd

6、修改当前账户密码,如果没设置过应该是root账户

passwd root

7、主机ssh连接centos8,注意是主机的ip,不是容器ip,是映射的主机端口,不是容器端口

ssh root@<主机ip> -p <容器映射的主机端口,这里配置的5555>

补充:

如何查看主机端口

#windows
ipconfig

#linux
ifconfig

本次演示环境是mac主机,可能会出现不同系统的一些问题。还有就是sshd配置那块,有的配置此centos8已经默认开启了,如果不一致有问题,请自行搜索解决。

对于修改镜像源,如果是Windows系统,请看这篇文章

CentOS8镜像源问题【解决镜像源已经弃用的问题】_GokuCode的博客-CSDN博客_centos8镜像源

主要内容,通过脚本修改镜像源

选择一个目录

vi centos.sh

 复制如下脚本内容粘贴


#!/bin/bash
cp -r /etc/yum.repo.d /etc/yum.repo.d.bak
rm -rf /etc/yum.repo.d/*
cat >> /etc/yum.repo.d/CentOS-Base.repo << EOF
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-8.5.2111 - Base - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that may be useful
[extras]
name=CentOS-8.5.2111 - Extras - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/extras/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/extras/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/extras/\$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-8.5.2111 - Plus - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/centosplus/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/centosplus/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/centosplus/\$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
[PowerTools]
name=CentOS-8.5.2111 - PowerTools - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/PowerTools/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/PowerTools/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/PowerTools/\$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-8.5.2111 - AppStream - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

EOF

yum clean all && yum makecache

 执行脚本,注意使用bash执行,诸如./centos.sh不可用

bash centos.sh

你可能感兴趣的:(解决方案,docker,centos,ssh,macos,linux)