VirtualBox 安装 CentOS 8

环境准备:

虚拟机:Oracle VM VirtualBox 6.1.16,根据需要选择不同平台。

  • 扩展包:支持 USB 2.0和 USB 3.0设备,VirtualBox RDP,磁盘加密,NVMe 和 PXE 启动。

镜像:CentOS 8.3.2011

安装虚拟机

虚拟机安装省略,扩展包安装如下:

image-20201224141248817

安装CentOS 8

  1. 点击新建,选择专家模式:

image-20201224142027093

  1. 自定义虚拟机名称,目标文件夹,类型为Linux,版本为Linux 2.6/3.x/4.x (64-bit)(自选),内存大小2G(自选),现在创建虚拟硬盘。

image-20201224142749616

  1. 选择文件位置(自选),文件大小(自选),磁盘文件(自选),动态分配(推荐)。

image-20201224143156197

  1. 配置NAT,网络名称(自定),CIDR(自定)不要与主机产生冲突(这里的设置为 网络号:192.168.120 默认网关:192.168.120.1),DHCP(自选),IPv6(自选)。

image-20201224143813326

  1. 设置处理器数量(自选),以及运行峰值(自选)。

image-20201224144545839

  1. 选择存储,选择下载好的CentOs 镜像。

image-20201224145050263

  1. 启用网卡1,添加nat网络,选择第4步创建的全局网络,高级默认。

image-20201224145221401

  1. 启用网卡2,添加nat网络地址转换,高级默认。

image-20201224145520264

  1. 选择安装

image-20201224145902205

  1. 选择语言(自选)

image-20201224150247307

  1. 设置时区,软件安装,分区,网络设置

image-20201224150712273

  1. 选择时区(自选)

image-20201224150454880

  1. 设置网络

image-20201224151001468

  1. 选择需要安装的软件(自选),我这里选的最小化安装。

image-20201224151221044

  1. 选择自动分配

image-20201224151409984

  1. 开始安装

image-20201224151531562

  1. 设置root密码

image-20201224151625189

  1. 安装完成后点击重启

image-20201224152256647

  1. 注意:此时由于系统的启动顺序是软驱 –>光驱 –> 硬盘,所以又会进入引导程序,如下所示:

image-20201224152418011

  1. 强制退出,更改启动顺序。将硬盘移动到最上方,重新启动。

image-20201224153009843

  1. 输入用户名和密码即可登录

image-20201224153314449

网络设置

  1. 下面进行网络设置,由于是最小安装,显示不识别ifconfig命令,

image-20201224153553721

  1. 首先进入以下目录查看相应的网卡
 cd /etc/sysconfig/network-scripts/

选择对应的网卡配置项,我这里是ifcfg-enp0s3,第二个是我配的Host-Only,如果你没有配其他的网络模式,那么应该只有一个。修改网卡信息时,如果有多个网卡,需要知道其HWADDR,通过HWADDR来匹配相应的网卡。一般情况下,虚拟机中的HWADDR与虚拟机中的mac地址一样

HWADDR以AA:BB:CC:DD:EE:FF形式的以太网设备的硬件地址.在有多个网卡设备的机器上,它保证设备接口被分配了正确的设备名 ,而不考虑每个网卡模块被配置的加载顺序

image-20201224155759338

  1. 编辑
vi ifcfg-enp0s3

显示以下信息:(i 键进入insert模式,:wq 保存并退出 :!q 不保存并退出)

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"     # 这里设为static是由于个人有ssh连接的需求,如果不是static,那么主机ip发生变化时,要重新配置,很麻烦。没有此需求可设置为DHCP
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s3"
UUID="ef30d5d2-4e2a-420b-bfec-fbda1abe2850"
DEVICE="enp0s3"
ONBOOT="yes"	# 设为yes
IPADDR=192.168.120.11	# 设置ip,参考全局设置,有效范围内随意设置
NETMASK=255.255.255.0
GATEWAY=192.168.120.1	# 默认网关
DNS1=114.114.114.114	#dns可设置多个
DNS2=223.5.5.5
  1. 重新加载网卡

此时,不能使用service和systemctl命令,需要使用nmcli命令:

nmcli c reload 网卡名(我这里是enp0s3)

image-20201224163052709

查看ip地址

ip addr

image-20201224191551909

如果没有配置错误,此时就能ping通了

image-20201224163420193

如果ping不通,可以尝试以下命令(使用这条命令前必须先使用 nmcli c reload 网卡名 命令,重新读取配置):

nmcli c up 网卡名(我这里是enp0s3)  # 立即生效

NET转发

  1. net模式只能单向访问,虚拟机可以通过网络访问主机,但是主机不能访问虚拟机。任何主机都不能直接访问虚拟主机,但是VirtualBox 提供了端口转发,使得我们可以设置特定的端口供实体网络访问。第二张网卡,设置转发规则,自定义主机端口(未占用),子IP为虚拟主机的ip,ssh通过20端口连接虚拟主机(根据需要选择)。

如何查端口是否占用?

windows:在DOS窗口中输入netstat -aon|findstr “端口号”命令查看端口号对应的PID。

linux:netstat -anp|grep 端口号 或者 lsof -i: 端口号

image-20201224224703391

Host-Only

如果net转发不成功,可以使用 Host-Only 模式,例如:

在VirtualBox中的 工具栏 –> 网络 –> 点击创建网卡 –> 配置IP –> 应用

我这里是手动配置的,没有使用DHCP,有需要可自选。

由下图可知,IP地址:192.168.35.1 子网掩码:255.255.255.0

image-20201225092127776

在主机的DOS窗口查看(主机是windos)

ipconfig/all

找到Host-Only这张网卡,查看IP地址,发现与配置的IP相同

image-20201225093332040

接下来启动虚拟机,修改网卡配置。关于如果辨别是哪张网卡,可通过HWADDR辨别。

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"	# 设置为静态的
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s8"
UUID="ef30d5d2-4e2a-420b-bfec-fbda1abe2850"
DEVICE="enp0s8"
ONBOOT="yes"
IPADDR=192.168.35.11	# 设置IP,在范围内自定义
NETMASK=255.255.255.0	# 子网掩码

然后通过ssh连接,我这里用的是MobaXterm。建立连接,配置相关信息。

image-20201225095027981

输入密码即可。

image-20201225095133619

yum换源

由于包管理配置文件链接的国外的服务器,yum源更换为国内镜像源速度较快。

阿里开源镜像站

地址:https://developer.aliyun.com/mirror/centos

操作前请做好相应备份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

或者

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

更新软件包缓存

sudo yum makecache

非阿里云ECS用户会出现 Couldn't resolve host 'mirrors.cloud.aliyuncs.com' 信息,不影响使用。用户也可自行修改相关配置:

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

清华大学开源软件镜像站

地址:https://mirrors.tuna.tsinghua.edu.cn/help/centos/

操作前请做好相应备份

sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

编辑 /etc/yum.repos.d/CentOS-Base.repo 文件,在 mirrorlist= 开头行前面加 # 注释掉;并将 baseurl= 开头行取消注释(如果被注释的话),把该行内的域名(例如mirror.centos.org)替换为 mirrors.tuna.tsinghua.edu.cn

或者,你也可以直接使用如下内容覆盖掉 /etc/yum.repos.d/CentOS-Base.repo 文件:(未经充分测试)

# 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.
#
#



[BaseOS]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/BaseOS/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

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

[PowerTools]
name=CentOS-$releasever - PowerTools
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/PowerTools/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=PowerTools&infra=$infra
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial


#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial



#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

更新软件包缓存

sudo yum makecache

中科大开源镜像站

地址:https://mirrors.ustc.edu.cn/centos/

操作前请做好相应备份

mv /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-AppStream.repo.bak
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
mv /etc/yum.repos.d/CentOS-Extras.repo /etc/yum.repos.d/CentOS-Extras.repo.bak

对于 CentOS 8,使用以下命令替换默认的配置

sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-Base.repo \
         /etc/yum.repos.d/CentOS-Extras.repo \
         /etc/yum.repos.d/CentOS-AppStream.repo

以下是替换之后的文件:

/etc/yum.repos.d/CentOS-Base.repo 文件:

# 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.
#
#

[BaseOS]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

/etc/yum.repos.d/CentOS-Extras.repo 文件:

# CentOS-Extras.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.
#
#

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/extras/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

/etc/yum.repos.d/CentOS-AppStream.repo 文件:

# CentOS-AppStream.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.
#
#

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

更新软件包缓存

sudo yum makecache

实验

更新yum

yum update -y

image-20201224170807617

  1. 安装ifconfig软件包

首先查看哪个包使用 YUM 命令提供了 ifconfig 命令。

sudo yum whatprovides ifconfig

输出:

CentOS Linux 8 - AppStream                                                4.7 MB/s | 6.3 MB     00:01
CentOS Linux 8 - BaseOS                                                   2.7 MB/s | 2.3 MB     00:00
Last metadata expiration check: 0:00:01 ago on Fri 25 Dec 2020 12:01:29 PM CST.
net-tools-2.0-0.52.20160912git.el8.x86_64 : Basic networking tools
Repo        : baseos
Matched from:
Filename    : /usr/sbin/ifconfig

从上面的命令中,可以看到 net-tools 包提供了 ifconfig 命令。现在,使用 yum 命令安装 net-tools 包。

sudo yum -y install net-tools

输出:

Last metadata expiration check: 0:04:08 ago on Fri 25 Dec 2020 12:01:29 PM CST.
Dependencies resolved.
==========================================================================================================
 Package               Architecture       Version                                Repository          Size
==========================================================================================================
Installing:
 net-tools             x86_64             2.0-0.52.20160912git.el8               baseos             322 k

Transaction Summary
==========================================================================================================
Install  1 Package

Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm                             371 kB/s | 322 kB     00:00
----------------------------------------------------------------------------------------------------------
Total                                                                     250 kB/s | 322 kB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                  1/1
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64                                        1/1
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64                                        1/1
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64                                        1/1
Installed products updated.

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64

Complete!
  1. 运行ifconfig。

image-20201224223241094

参考

https://toutoudnf.github.io/2017/06/29/tools/virtualbox usage/

https://blog.csdn.net/liu_shi_jun/article/details/94592804

https://blog.csdn.net/dkfajsldfsdfsd/article/details/79403343

https://my.oschina.net/wiselyming/blog/177153

http://centos.ustc.edu.cn/help/centos.html

https://www.cnblogs.com/langyao/p/11725269.html

https://www.itzgeek.com/how-tos/linux/centos-how-tos/ifconfig-command-not-found-on-centos-8-rhel-8-quick-fix.html

posted @ 2020-12-24 23:34  Yi-Ming  阅读(556)  评论(0)    收藏  举报