一个小时内创建一个 CentOS 家庭实验室
家庭实验室

> 
> 用一个带有虚拟化软件的系统（CentOS ISO），花费一个小时左右来设置一套自给自足的基本 Linux 服务器。
> 
> 
> 


![](/data/attachment/album/202112/30/093738krqqc3c5mc35lu36.jpg "metrics and data shown on a computer screen")


当学习新的 Linux 技能时（或像我一样学习 Linux 认证），在笔记本电脑上有一些可用的虚拟机（VM）是很有帮助的，这样你就可以在上面学习。


但是，如果在没有良好互联网连接的地方，你却想在一个 Web 服务器上工作，该怎么办? 想要使用没有安装过的其他软件，该怎么办? 如果需要从发行版的存储库中下载它，那么你可能会碰壁。只要做一点准备，你就可以设置一个 [家庭实验室](https://opensource.com/article/19/3/home-lab)，你就在任何地方安装所需的任何东西，无论是否有网络连接。


要求如下：


* 一个你打算使用的已下载的 Linux 发行版（例如，CentOS、Red Hat 等）ISO 文件
* 一台具有虚拟化功能的主机。这里使用带有 [KVM](https://en.wikipedia.org/wiki/Kernel-based_Virtual_Machine) 和 [virt-manager](https://virt-manager.org/) 的 [Fedora](https://getfedora.org/)，但其它 Linux 也可以类似工作。你甚至可以使用 Windows 或 Mac 进行虚拟化，但在实现方面存在一些差异
* 大约一个小时


### 1、为存储主机创建一个虚拟机


使用 `virt-manager` 创建一个中等规格的虚拟机：1GB RAM、一个 CPU 和 16GB 磁盘空间就足够。


在虚拟机上安装 [CentOS 7](https://www.centos.org/download/)。


![Installing a CentOS homelab](/data/attachment/album/202112/30/093749xz3sahijwwgxojw4.png "Installing a CentOS homelab")


选择你的语言并继续。


单击 “<ruby> 安装位置 <rt>  Installation Destination </rt></ruby>”，选择本地磁盘，勾选 “<ruby> 自动配置分区 <rt>  Automatically Configure Partitioning </rt></ruby>” 复选框，然后单击左上角的 “<ruby> 完成 <rt>  Done </rt></ruby>”。


在 “<ruby> 软件选择 <rt>  Software Selection </rt></ruby>” 下，选中 “<ruby> 基础设施服务器 <rt>  Infrastructure Server </rt></ruby>” ，选中 “<ruby> FTP 服务器 <rt>  FTP Server </rt></ruby>” 复选框，然后单击 “<ruby> 完成 <rt>  Done </rt></ruby>”。


![Installing a CentOS homelab](/data/attachment/album/202112/30/093749f6r777pgrekz9yy1.png "Installing a CentOS homelab")


选择 “<ruby> 网络和主机名 <rt>  Network and Host Name </rt></ruby>”，启用右上方的 “<ruby> 以太网 <rt>  Ethernet </rt></ruby>” ，然后单击左上角的 “<ruby> 完成 <rt>  Done </rt></ruby>”。


单击 “<ruby> 开始安装 <rt>  Begin Installation </rt></ruby>” 开始安装操作系统。


在安装时务必设置一个 root 密码，就可以创建一个带有密码的用户。


### 2、启动 FTP 服务


下一步是启动并设置 FTP 服务以运行并允许它通过防火墙。


用 root 密码登录，然后启动 FTP 服务器：



```
systemctl start vsftpd

```

允许它在每次启动时都能工作：



```
systemctl enable vsftpd

```

设置防火墙允许通过的端口：



```
firewall-cmd --add-service=ftp --perm

```

立即启用此更改：



```
firewall-cmd --reload

```

获取你的 IP 地址：



```
ip a

```

（可能是 `eth0`），在之后会用到。


### 3、复制本地存储库中的文件


通过虚拟化软件将安装 CD 挂载到虚拟机。


创建待挂载光盘的临时目录:



```
mkdir /root/temp

```

挂载安装 CD：



```
mount /dev/cdrom /root/temp

```

将所有文件复制到 FTP 服务器目录下:



```
rsync -avhP /root/temp/ /var/ftp/pub/

```

### 4、将服务器指向本地存储库


基于 Red hat 的系统使用以 `repo` 结尾的文件来确定从哪里获得软件更新和新的软件。这些文件在以下路径



```
cd /etc/yum.repos.d

```

你需要摆脱那些使你的服务器在互联网上寻找 CentOS 存储库的存储库文件。可以把它们复制到根目录下保存起来：



```
mv * ~

```

然后创建一个新的存储库文件来指向你的服务器。使用常用的文本编辑器创建一个名为 `network.repo` 文件，输入以下内容（将 `<your IP>` 替换为在步骤 2 中获得的 IP 地址），然后保存：



```
[network]
name=network
baseurl=ftp://192.168.122.<your ip>/pub
gpgcheck=0

```

完成后，可以用下面的命令来测试：



```
yum clean all; yum install ftp

```

如果 FTP 客户端按照预期从 “network” 存储库安装，那么本地仓库就已经设置好了！


![Installing a CentOS homelab](/data/attachment/album/202112/30/093749oin3xj5nkjtq5qrk.png "Installing a CentOS homelab")


### 5、使用你设置的存储库安装一个新的虚拟机


回到虚拟机管理器，并创建另一个虚拟机。但这一次，选择 “<ruby> 网络安装 <rt>  Network Install </rt></ruby>”， URL 为：



```
ftp://192.168.122.<your IP>/pub

```

如果你使用的是不同的宿主机操作系统或虚拟管理器，像前面一样安装虚拟机，并进行下一步。


### 6、将新的虚拟机设置为使用现有的“network”存储库


你可以从现有服务器复制存储库文件到这里使用。


和第一个服务器示例一样，输入：



```
cd /etc/yum.repos.d
mv * ~

```

然后：



```
scp root@192.168.122.<your IP>:/etc/yum.repos.d/network.repo /etc/yum.repos.d

```

现在可以使用新的虚拟机并从本地存储库获取所有软件了。


再测试一遍：



```
yum clean all; yum install screen

```

这将从本地存储服务器安装软件。


这种设置，让你能够脱离网络安装软件，创建一个更可靠的环境，扩展你的技能。




---


via: <https://opensource.com/article/19/6/create-centos-homelab-hour>


作者：[Bob Murphy](https://opensource.com/users/murph) 选题：[lujun9972](https://github.com/lujun9972) 译者：[JaphiaChen](https://github.com/JaphiaChen) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
When working on new Linux skills (or, as I was, studying for a Linux certification), it is helpful to have a few virtual machines (VMs) available on your laptop so you can do some learning on the go.

But what happens if you are working somewhere without a good internet connection and you want to work on a web server? What about using other software that you don't already have installed? If you were depending on downloading it from the distribution's repositories, you may be out of luck. With a bit of preparation, you can set up a [homelab](https://opensource.com/article/19/3/home-lab) that will allow you to install anything you need wherever you are, with or without a network connection.

The requirements are:

- A downloaded ISO file of the Linux distribution you intend to use (for example, CentOS, Red Hat, etc.)
- A host computer with virtualization. I use
[Fedora](https://getfedora.org/)with[KVM](https://en.wikipedia.org/wiki/Kernel-based_Virtual_Machine)and[virt-manager](https://virt-manager.org/), but any Linux will work similarly. You could even use Windows or Mac with virtualization, with some difference in implementation - About an hour of time

## 1. Create a VM for your repo host

Use virt-manager to create a VM with modest specs; 1GB RAM, one CPU, and 16GB of disk space are plenty.

Install [CentOS 7](https://www.centos.org/download/) on the VM.

![Installing a CentOS homelab Installing a CentOS homelab](https://opensource.com/sites/default/files/uploads/homelab-3b_0.png)

Select your language and continue.

Click *Installation Destination*, select your local disk, mark the *Automatically Configure Partitioning* checkbox, and click *Done *in the upper-left corner.

Under *Software Selection*, select *Infrastructure Server*, mark the *FTP Server* checkbox, and click *Done*.

![Installing a CentOS homelab Installing a CentOS homelab](https://opensource.com/sites/default/files/uploads/homelab-5b.png)

Select *Network and Host Name*, enable Ethernet in the upper-right, then click *Done* in the upper-left corner.

Click *Begin Installation* to start installing the OS.

You must create a root password, then you can create a user with a password as it installs.

## 2. Start the FTP service

The next step is to start and set the FTP service to run and allow it through the firewall.

Log in with your root password, then start the FTP server:

`systemctl start vsftpd`


Enable it to work on every start:

`systemctl enable vsftpd`


Set the port as allowed through the firewall:

`firewall-cmd --add-service=ftp --perm`


Enable this change immediately:

`firewall-cmd --reload`


Get your IP address:

`ip a`


(it's probably **eth0**). You'll need it in a minute.

## 3. Copy the files for your local repository


Mount the CD you installed from to your VM through your virtualization software.

Create a directory for the CD to be mounted to temporarily:

`mkdir /root/temp`


Mount the install CD:

`mount /dev/cdrom /root/temp`


Copy all the files to the FTP server directory:

`rsync -avhP /root/temp/ /var/ftp/pub/`


## 4. Point the server to the local repository

Red Hat-based systems use files that end in **.repo** to identify where to get updates and new software. Those files can be found at

`cd /etc/yum.repos.d`


You need to get rid of the repo files that point your server to look to the CentOS repositories on the internet. I prefer to copy them to root's home directory to get them out of the way:

`mv * ~`


Then create a new repo file to point to your server. Use your favorite text editor to create a file named **network.repo** with the following lines (substituting the IP address you got in step 2 for *<your IP>*), then save it:

```
[network]
name=network
baseurl=ftp://192.168.122.<your ip>/pub
gpgcheck=0
```


When that's done, we can test it out with the following:

`yum clean all; yum install ftp`


If your FTP client installs as expected from the "network" repository, your local repo is set up!

![Installing a CentOS homelab Installing a CentOS homelab](https://opensource.com/sites/default/files/uploads/homelab-14b.png)

## 5. Install a new VM with the repository you set up

Go back to the virtual machine manager, and create another VM—but this time, select *Network Install* with a URL of:

`ftp://192.168.122.<your IP>/pub`


If you're using a different host OS or virtualization manager, install your VM similarly as before, and skip to the next section.

## 6. Set the new VM to use your existing network repository

You can copy the repo file from your existing server to use here.

As in the first server example, enter:

```
cd /etc/yum.repos.d
mv * ~
```


Then:

`scp root@192.168.122.<your IP>:/etc/yum.repos.d/network.repo /etc/yum.repos.d`


Now you should be ready to work with your new VM and get all your software from your local repository.

Test this again:

`yum clean all; yum install screen`


This will install your software from your local repo server.

This setup, which gives you independence from the network with the ability to install software, can create a much more dependable environment for expanding your skills on the road.

*Bob Murphy will present this topic as well as an introduction to GNU Screen at Southeast Linux Fest, June 15-16 in Charlotte, N.C.*

## 2 Comments