CLI指令

指令的基本含义为-设置容器名称-映射宿主机端口-挂载文件夹-设置root权限-拉取镜像名称

1
docker run -itd --name centOS8 -p 7022:22 -p 7080:80 -p 7443:443 -p 7088:8080 -v /root/cengos8:/root --privileged=true centos:centos8

CentOS8的基本配置

使用docker安装的CentOS容器一般会遇到两个问题,一个是yum源异常导致无法安装各类软件,一个是不知道root密码,不能直接ssh连接到系统中。
这个时候可以先通过docker进入到CentOS的命令行,修改root密码。

进入容器的bash

1
docker exec -it centOS8 /bin/bash

修改root密码

通常使用passwd指令来直接修改登录账号的密码。

1
2
whoami
passwd

但是很不幸的一点,这个系统上可能passwd都没有 command not found: passwd

更新yum源

yum源配置文件在 /etc/yum.repos.d 目录下。一般需要更改的是 BaseOS.repoAppStream.repo文件的内容。

这里还是推荐aliyum的源,源链接,我试过了tencent的好多软件还是找不到。

备份原文件

1
mv CentOS-Linux-BaseOS.repo CentOS-Linux-BaseOS.repo.backup

通过curl下载aliyun源的BaseOS.repo,可能wget用不起,我没试。

1
curl -o CentOS-Linux-BaseOS.repo http://mirrors.aliyun.com/repo/Centos-8.repo

所有的文件名都需要依照你的系统内的名字来更改,保持源文件名称一致很重要。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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-$releasever - Base - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-$releasever - PowerTools - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/PowerTools/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-$releasever - AppStream - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

上面是BaseOS.repo的文件内容。然后在更新一下AppStream.repo,把原有的配置全部注释,在将BaseOS.repo中[AppStream]相关的内容粘贴过去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# CentOS-Linux-AppStream.repo
#
# The mirrorlist system uses the connecting IP address of the client and the
# update status of each mirror to pick current mirrors that are 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, you can try the commented out
# baseurl line instead.

#[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/
#gpgcheck=1
#enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

#tencent
#[AppStream]
#name=Qcloud centos AppStream - $basearch
#baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/AppStream/$basearch/os/
#enabled=0
#gpgcheck=1
#gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-Official

#aliyun
[AppStream]
name=CentOS-$releasever - AppStream - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

配置完成后清理和再建缓存,在安装epel-release源,基本就可以了。

1
2
3
yum clean
yum mackcache
yum install epel-release -y

接下来就可以愉快的安装一些必要的软件了。

1
yum install zsh git passwd screen nginx #...

最后更改root密码,非必要。

shell脚本

写了一个脚本文件来一键执行更改源的操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
#echo "Hello World !"
base_str='Base'
app_stream_str='AppStream'
tmp='appstream.tmp'
repos_path='/etc/yum.repos.d/'
for file in $(ls $repos_path)
do
#echo $file
#备份并替换Base文件
if [[ $file = *$find_str* ]]
then
# echo $file'包含'$find_str
mv $repos_path$file $repos_path$file.backup && curl -s -o $repos_path$file http://mirrors.aliyun.com/repo/Centos-8.repo
fi
#修改AppStream文件,属于是比较取巧的方案了,不建议学习。
if [[ $file = *$app_stream_str* ]]
then
# echo $file'包含'$app_stream_str
mv $repos_path$file $repos_path$file.backup && touch $repos_path$file && curl -s -o $repos_path$tmp http://mirrors.aliyun.com/repo/Centos-8.repo && sed -n "54,61p" $repos_path$tmp >> $repos_path$file && rm $repos_path$tmp
fi
done
yum clean && yum makecache && yum install epel-release -y

安装zsh和配置oh-my-zsh,更改默认shell

1
2
3
4
5
6
#安装zsh
yum install zsh git util-linux-user -y
#配置oh-my-zsh,执行成功与否和网络有关,可以先ping一下 raw.githubusercontent.com
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
#更改默认shell为zsh
chsh -s /bin/zsh root

当我执行到更改默认shell这一步的时候才发现这个系统启动时候的pid进程似乎有问题,不是使用的/sbin/init。pid(1)居然是bash而不是systemd,导致systemd相关指令包括systemctl和reboot指令都无法执行。网上查询了一下大都是关于WSL系统的问题,和我在docker下创建的centos关系也不大,最后怀疑是在docker run的时候没有指定启动进程为/sbin/init有关,之后验证。

配置JAVA和NODEJS环境

在zsh的指令环境下,他的profile文件为/etc/zprofile,通过cat /etc/zprofile我们可以看到其中的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#
# /etc/zprofile and ~/.zprofile are run for login shells
#

PATH="$PATH:$HOME/bin"
export PATH

_src_etc_profile()
{
# Make /etc/profile happier, and have possible ~/.zshenv options like
# NOMATCH ignored.
#
emulate -L ksh

# source profile
if [ -f /etc/profile ]; then
source /etc/profile
fi
}
_src_etc_profile

unset -f _src_etc_profile

以root账户为例,他已经帮我们把/root/bin文件夹加入到了PATH变量中,意味着我们可以直接把一些可执行文件放在/root/bin中就可以全局执行了。

创建/root/soft/文件夹,统一管理自定义的软件。

下载并解压openjdk21 [网络链接随时会更新,已实时为准]

1
2
3
4
md /root/soft && cd /root/soft
culr -0 jdk21.tar.gz https://download.java.net/java/GA/jdk21.0.1/415e3f918a1f4062a0074a2794853d0d/12/GPL/openjdk-21.0.1_linux-x64_bin.tar.gz
tar -zxvf jdk21.tar.gz #tar的解压并重命名怎么那么抽象
mv openjdk-21.0.1_linux-x64_bin jdk21

将你所要用到的java可执行文件,link到/root/bin文件夹中。

1
2
3
link -s /root/soft/jdk21/bin/java /root/bin/java
link -s /root/soft/jdk21/bin/javac /root/bin/javac
...

现在你可以执行 java –version 来查看版本信息了。

下载并解压node20 [网络链接随时会更新,已实时为准]

1
2
3
4
cd /root/soft
curl -o nodejs20.tar.xz https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.xz
tar -xvf nodejs20.tar.xz
mv node-v20.10.0-linux-x64 node20

将你所要用到的node可执行文件,link到/root/bin文件夹中。由于node/bin下本身存在着link,所以在不太熟悉link的情况下,我选择直接link了源文件。

1
2
3
4
ln -s  /root/soft/node20/bin/node /root/bin/node
ln -s /root/soft/node20/lib/node_modules/corepack/dist/corepack.js /root/bin/corepack
ln -s /root/soft/node20/lib/node_modules/npm/bin/npx-cli.js /root/bin/npx
ln -s /root/soft/node20/lib/node_modules/npm/bin/npm-cli.js /root/bin/npm

现在你可以在任意未知执行node,npm等指令了。指令版本与你安装的软件版本相同。