虚拟机的创建
创建虚拟机


















配置虚拟机
硬件








选项












安装Centos 8.5
安装前的配置



安装中的配置




语言配置


磁盘分区配置

时区配置


软件选择

这里选择Server环境。

账号配置
系统账号


普通账号



主机与网址


安装


安装后的配置
启动

登录
root账号登录



普通账号登录


配置
查看主机IP

IP地址配置
通常情况下, 我们习惯于将服务器IPv4地址配置为静态的,这里,我们将虚拟机主机修改为192.168.43.119,网关指定为192.168.43.1,域名服务器指定为223.5.5.5、223.6.6.6,命令如下:
shell
[root@shanghai ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens160 7585d00a-ea8f-420f-8909-c586c00af900 ethernet ens160
[root@shanghai ~]# nmcli connection delete ens160
Connection 'ens160' (7585d00a-ea8f-420f-8909-c586c00af900) successfully deleted.
[root@shanghai ~]# nmcli connection add type ethernet ifname ens160 con-name ens160 ipv4.method manu
al ipv4.addresses 192.168.11.119/24 ipv4.gateway 192.168.11.1 ipv4.dns 223.5.5.5,223.6.6.6 connectio
n.autoconmect yes
Connection 'ens160' (8b848f47-8dd0-48c2-a78f-8c6cec6b3d4d)successfully added.
[root@shanghai ~]#
命令执行完成以后,虚拟主机IP地址已经被永久性改变,我们可以通过以下命令得到验证。
- 查看IP地址
shell
[root@shanghai ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:71:ae:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.43.119/24 brd 192.168.43.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 240a:42d4:0:3c08:20c:29ff:fe71:ae0b/64 scope global dynamic noprefixroute
valid_lft 2731sec preferred_lft 2731sec
inet6 fe80::20c:29ff:fe71:ae0b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@shanghai ~]#
我们看到,IP地址已经变更过来了。
- 查看域名
shell
[root@shanghai ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 240a:42d4:0:3c08:207f:7aee:3ad4:c9a6
[root@shanghai ~]#
这里,我们也可以看到,域名配置中已经追加上了关于223.5.5.5与223.6.6.6的配置。
- 查看IP地址配置文件
shell
[root@shanghai ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=8b848f47-8dd0-48c2-a78f-8c6cec6b3d4d
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.43.119
PREFIX=24
GATEWAY=192.168.43.1
DNS1=223.5.5.5
DNS2=223.6.6.6
[root@shanghai ~]#
我们看到,配置文件已经自动生成。
连接到主机
上面我们已经在虚拟机中安装好了Cenots 8.5,并获取到了该主机的IPv4地址,接下来,我们介绍一下,如何从安装有Linux mint 22.1操作系统的物理主机连接到虚拟机上,以便从远程来管理该主机。
为虚拟主机添加伪域名
为了方便记忆,我们不防为远程虚拟主机添加一个伪域名,以后就可以借助该伪域名来代替具体的IPv4地址。
我们可以借助如下命令在系统中为虚拟主机IP地址192.168.43.11添加一个域名vm.shanghai.lan:
shell
lwk@think:~$ sudo tee -a /etc/hosts <<-'EOF'
> 192.168.43.11 vm.shanghai.lan
> EOF
192.168.43.11 vm.shanghai.lan
lwk@think:~$
lwk@think:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 thinkpad
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.43.11 vm.shanghai.lan
lwk@think:~$
为本地物理生成ssh key
为了后续可以免密码登录到虚拟主机上对该主机做管理,我们不防为物理主机生成一个rsa类型的公密钥:
shell
lwk@think:~$ ssh-keygen -t rsa -b 4096 -C "thinkpad"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lwk/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/lwk/.ssh/id_rsa.
Your public key has been saved in /home/lwk/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:vhw+8rhQZXNN9KuDzokLzTfItkLM+xIO9YR8Q9nhgas thinkpad
The key's randomart image is:
+---[RSA 4096]----+
| +ooo |
| +..+ . |
| . o+.o . . |
| +o=o . |
| +.=S. . |
| ..E=.. . . |
| .+.oO + o |
| .**.O o . |
| oBXo+ |
+----[SHA256]-----+
lwk@think:~$
免密配置
我们借助ssh-copy-id命令将物理主机公钥追加到远程虚拟主机的文件~/.ssh/authorized_keys中,以便后续可以实现免密登录。
这里,~代表用户目录。
shell
lwk@think:~$ ssh-copy-id root@vm.shanghai.lan
The authenticity of host 'vm.shanghai.lan (192.168.43.11)' can't be established.
ED25519 key fingerprint is SHA256:NPtgVKK5TmYHc4y5Qbo1YygpyXZH5X1X/kZ4/SpE3Qs.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@vm.shanghai.lan's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@vm.shanghai.lan'"
and check to make sure that only the key(s) you wanted were added.
lwk@think:~$
免密登录到远程主机
shell
lwk@think:~$ ssh root@vm.shanghai.lan
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Jul 12 08:33:41 2025
[root@shanghai ~]#
软件源配置
Centos官方已经停止了关于Centos 9以下版本的维护工作,这些工作由开源项目centos vault来继续接替,所以关于Centos 9以下版本的软件源都是基于仓库centos vault展开的。
中国大陆社区关于仓库centos vault的镜像站很多,清华大学、中国科技大学、阿里云、华为云、腾讯云是大学日常用的比较多的几个镜像站。
清华镜像站
这里,我们用以下命令完成关于Centos 8.5基于清华镜像站的配置替换。
shell
[root@shanghai ~]# sed -i \
> -e "s|^mirrorlist=|#mirrorlist=|g" \
> -e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111|g" \
> -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111|g" \
> /etc/yum.repos.d/CentOS-*.repo
[root@shanghai ~]#
shell
[root@shanghai ~]# grep -n tsinghua /etc/yum.repos.d/*.repo
/etc/yum.repos.d/CentOS-Linux-AppStream.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/AppStream/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-BaseOS.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/BaseOS/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-ContinuousRelease.repo:21:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/cr/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-Devel.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/Devel/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-Extras.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/extras/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-FastTrack.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/fasttrack/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-HighAvailability.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/HighAvailability/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-Plus.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/centosplus/$basearch/os/
/etc/yum.repos.d/CentOS-Linux-PowerTools.repo:14:baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/PowerTools/$basearch/os/
[root@shanghai ~]#
替换完成以后,需要执行如下命令使其生效。
shell
[root@shanghai ~]# dnf makecache
[root@shanghai ~]#
或者
shell
[root@shanghai ~]# yum makecache
[root@shanghai ~]#
dnf是centos官方用来替代yum的新工具,在执行效率上,比yum更高,所以官方推荐用dnf,不过,yum依然可以使用。
本地语言环境调整
在全球化场景下,Linux 系统需支持多语言界面、文件编码与用户交互。字符编码与语言环境(Locale)是操作系统支持多语言的核心机制。
CentOS 8 作为企业级 Linux 发行版,其语言与编码配置需兼顾兼容性、稳定性与国际化需求。CentOS 8(基于 Red Hat Enterprise Linux 8)通过标准化 Locale 机制与 UTF-8 编码优先策略,简化了多语言环境的部署。
Locale 基础原理
Locale 的定义与组成
Locale(区域设置)是操作系统用于处理语言、地区特定行为的配置集合,由以下组件构成(以 zh_CN.UTF-8为例):
语言代码(Language Code):zh表示中文(ISO 639-1 标准)。
地区代码(Country Code):CN表示中国大陆(ISO 3166-1 标准)。
字符集(Character Set):UTF-8表示 Unicode 编码(RFC 8259 标准)。
Locale 决定了日期/时间格式、货币符号、排序规则(Collation)等本地化行为。例如,en_US.UTF-8会使用美式英语的日期格式(MM/DD/YYYY),而 zh_CN.UTF-8使用中式格式(YYYY/MM/DD)。
CentOS 8 的 Locale 配置文件
CentOS 8 的 Locale 配置遵循分层优先级:
系统级配置:/etc/locale.conf(全局生效,重启后保留)。
用户级配置:~/.config/locale.conf(仅当前用户生效)。
临时环境变量:通过 LANG、LC_*等环境变量临时覆盖(如 export LANG=zh_CN.UTF-8)。
系统启动时,systemd会读取 /etc/locale.conf并设置全局 Locale(通过 localectl命令管理)。
字符编码规范
编码演进与 UTF-8 的优势
早期系统使用单字节编码(如 ASCII)、双字节编码(如 GBK、Shift_JIS)或多字节编码(如 Big5),但这些编码存在兼容性问题(如"乱码")。UTF-8(Unicode Transformation Format - 8-bit)作为 Unicode 的可变长编码方案,覆盖了全球所有字符(超 14 万个),且与 ASCII 完全兼容,因此成为 CentOS 8 的默认编码。
CentOS 8 的默认编码
CentOS 8 预安装的 Locale 以 UTF-8 为主。通过 locale -a命令可查看系统支持的 Locale 列表(包含 en_US.UTF-8、zh_CN.UTF-8等)。若未找到目标 Locale(如 fr_FR.UTF-8),需手动生成。
locale配置的生成
通常情况下,我们可以通过命令localectl完成locale配置文件的生成,命令如下:
bash
[root@qiantang ~]# localectl set-locale LANG=zh_CN.UTF-8
Failed to issue method call: Locale zh_CN.UTF-8 not installed, refusing.
[root@qiantang ~]#
我们看到系统无法识别 zh_CN.UTF-8 等中文 Locale,这是因为CentOS 8 默认仅预装部分基础语言包(如英文),中文(zh_CN)等非默认语言需要手动安装对应的语言包(glibc-langpack-zh)才能支持其 Locale。若未安装该包,系统无法识别 zh_CN.UTF-8 等中文 Locale。
安装依赖包
可以使用下面的命令安装软件包glibc-langpack-zh。
bash
[root@shanghai ~]# dnf install glibc-langpack-zh
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream 1.8 MB/s | 8.4 MB 00:04
CentOS Linux 8 - BaseOS 870 kB/s | 4.6 MB 00:05
CentOS Linux 8 - Extras 12 kB/s | 10 kB 00:00
Dependencies resolved.
==============================================================================================================================================================================================
Package Architecture Version Repository Size
==============================================================================================================================================================================================
Installing:
glibc-langpack-zh x86_64 2.28-164.el8 baseos 2.2 M
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package
Total download size: 2.2 M
Installed size: 15 M
Is this ok [y/N]: y
Downloading Packages:
glibc-langpack-zh-2.28-164.el8.x86_64.rpm 515 kB/s | 2.2 MB 00:04
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 515 kB/s | 2.2 MB 00:04
CentOS Linux 8 - BaseOS 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : glibc-langpack-zh-2.28-164.el8.x86_64 1/1
Running scriptlet: glibc-langpack-zh-2.28-164.el8.x86_64 1/1
Verifying : glibc-langpack-zh-2.28-164.el8.x86_64 1/1
Installed:
glibc-langpack-zh-2.28-164.el8.x86_64
Complete!
[root@shanghai ~]#
生成locale配置
这个时候再次配置语言环境
bash
[root@qiantang ~]# localectl set-locale LANG=zh_CN.UTF-8
[root@qiantang ~]#
这个时候,我们用命令locale再来查看,
shell
[lwk@qiantang ~]$ locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=
[lwk@qiantang ~]$
如此就对了。
ls命令
shell
[root@shanghai ~]# ll
total 4
-rw-------. 1 root root 1266 Jul 12 08:13 anaconda-ks.cfg
[root@shanghai ~]# ls -al
total 28
dr-xr-x---. 3 root root 147 Jul 12 08:38 .
dr-xr-xr-x. 17 root root 224 Jul 12 08:08 ..
-rw-------. 1 root root 1266 Jul 12 08:13 anaconda-ks.cfg
-rw-------. 1 root root 556 Jul 12 09:44 .bash_history
-rw-r--r--. 1 root root 18 May 11 2019 .bash_logout
-rw-r--r--. 1 root root 176 May 11 2019 .bash_profile
-rw-r--r--. 1 root root 176 May 11 2019 .bashrc
-rw-r--r--. 1 root root 100 May 11 2019 .cshrc
drwx------. 2 root root 61 Jul 12 08:48 .ssh
-rw-r--r--. 1 root root 129 May 11 2019 .tcshrc
[root@shanghai ~]#
通常情况下,我们希望在centos 8.5上也可以使用ll命令查看文件列表,要打开这个配置项,需要我们修改配置文件/etc/profile.d/colorls.sh,内容如下:
shell
[root@shanghai ~]# cat /etc/profile.d/colorls.sh
# color-ls initialization
# Skip all for noninteractive shells.
[ ! -t 0 ] && return
#when USER_LS_COLORS defined do not override user LS_COLORS, but use them.
if [ -z "$USER_LS_COLORS" ]; then
alias ll='ls -l' 2>/dev/null
alias l.='ls -d .*' 2>/dev/null
INCLUDE=
COLORS=
for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" \
"$HOME/.dir_colors" "$HOME/.dircolors"; do
[ -e "$colors" ] && COLORS="$colors" && \
INCLUDE="`/usr/bin/cat "$COLORS" | /usr/bin/grep '^INCLUDE' | /usr/bin/cut -d ' ' -f2-`" && \
break
done
[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.$TERM" ] && \
COLORS="/etc/DIR_COLORS.$TERM"
[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.256color" ] && \
[ "x`/usr/bin/tty -s && /usr/bin/tput colors 2>/dev/null`" = "x256" ] && \
COLORS="/etc/DIR_COLORS.256color"
[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS" ] && \
COLORS="/etc/DIR_COLORS"
# Existence of $COLORS already checked above.
[ -n "$COLORS" ] || return
if [ -e "$INCLUDE" ];
then
TMP="`/usr/bin/mktemp .colorlsXXX -q --tmpdir=/tmp`"
[ -z "$TMP" ] && return
/usr/bin/cat "$INCLUDE" >> $TMP
/usr/bin/grep -v '^INCLUDE' "$COLORS" >> $TMP
eval "`/usr/bin/dircolors --sh $TMP 2>/dev/null`"
/usr/bin/rm -f $TMP
else
eval "`/usr/bin/dircolors --sh $COLORS 2>/dev/null`"
fi
[ -z "$LS_COLORS" ] && return
/usr/bin/grep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null && return
fi
unset TMP COLORS INCLUDE
alias ll='ls -l --color=auto' 2>/dev/null
alias l.='ls -d .* --color=auto' 2>/dev/null
alias ls='ls --color=auto' 2>/dev/null
[root@shanghai ~]#
将56行中的修改为
shell
alias ll='ls -la --color=auto' 2>/dev/null
修改完成,退出,重新登录即可生效。