An operating system (OS) is a special piece of software running on your computer to make it possible to start and run programs such as Microsoft Word and Excel. Besides that, it handles your computer's input and output for you and delivers filesystem and hardware control. Examples of operating systems you may already know are Windows, macOS, iOS, or Android.
In this chapter, we'll cover the following topics:
- An overview of the Linux system
- Installing VirtualBox and CentOS
- Working with VirtualBox
- Connecting VMs through SSH
Linux is not the name of a specific and full working OS, but implements only the essential inner core of an OS, which is referred to as the kernel. Most types of Linux OS do not cost anything, but provide thousands of programs and software to use completely free of charge. Most of these programs are also open source, which means you can view the exact blueprint of how the program has been created and it can be changed by anyone. One very important area in which Linux is very popular and is used heavily is managing many network services. These are programs that run in the background of your Linux server, continuously waiting for external events to respond with some kind of action or information. Examples are popular internet services such as a web server, which presents websites to the user; main servers for email communications; and also database servers to store and deliver any kind of data. As mentioned before, Linux is only the name of the kernel part, not a full working OS. To make it complete, you need to bundle it together with all kinds of programs, which is then referred to as a distribution.
Nowadays, one can choose between an insane amount of different Linux distributions, all designed for a special purpose and all with their own pros and cons. The main difference between them is the software selected and bundled together with the Linux kernel. The most important Linux distribution families are Red Hat- and Debian-based Linux distributions. CentOS is one of the most important free Red Hat-based Linux server distributions out there at the moment. It's a very stable, secure, and reliable OS, which is why it's often used in running very critical networking services in enterprise environments. Also, it's good to know that the development of this OS is very strong and updates are well selected and suitably tested. The first chapter of this book is about installing Linux; here we will be starting easy by introducing you to the concepts of virtualization. Then, we will create a new CentOS 7 virtual machine (VM) using a free virtualization software called VirtualBox.
In this section, we will give you an overview of the concepts of virtualization. We will also show you how to work with VirtualBox, and afterward show you how you can install your first CentOS 7 virtual machine in VirtualBox. It is a very hot topic and the core IT skill at the moment is virtualization.
Simply put, it is a technology to run separate operating systems parallel to your main operating system on the same computer. For example, if you are currently working on a Windows computer, you can run another operating system such as Linux or macOS in parallel to a simple Windows application on your desktop. Modern virtualization software does not even limit you to run only one parallel OS, but you can run multiple systems in parallel. The limits are only defined by your own computer hardware. The applications for virtualization technology in modern IT are endless, and you will find them everywhere. The advantages can range from shifting IT infrastructure paradigms to undoing changes to your operating systems, which is quite ideal for beginners.
In cloud computing or modern data centers, powerful virtualization server clusters are running many different operating systems simultaneously, instead of using dedicated server hardware. If you wanted to use Linux before the age of virtualization, you needed access to dedicated physical computers. Also, most beginners will mess up their new Linux installation several times when starting out on Linux. Oftentimes, such system changes can be hard to reboot and the only efficient and useful consequence is to reinstall a complete system when getting stuck. Virtualization gets rid of all these problems. We will use it for conveniently working with all the examples throughout the entire chapter thanks to its powerful features such as cloning, taking snapshots, or creating images, which also eliminates the fear of breaking something. At the moment, there is a broad range of different virtualization products available for both desktop and non-graphical server environments to choose from, both in the commercial and open source sector.
At the core level, all of these different virtualization products have the same basic features and common definitions, which we need to make clear before we can dive deeper. When we talk about operating systems running in parallel to your physical machine's operating system, we will refer to them as VMs, or virtual machines, from this point forward. In the same context, our main operating system running our physical machine which runs the virtualization software is called the host system or hypervisor. The VMs running on this host are called the guest systems or guests. Copying and cloning is one of the most important features of virtualization. This can save you precious time, for example, if you need another identical machine or to make your work more portable. Just copy the image to your laptop or a different data center and you're done. The portable copy of a VM is also called an image. Another awesome feature is taking snapshots of your VM. Taking such a snapshot only takes a few seconds but will save the complete and current state of your VM at any given point in time. This is very useful if you want to preserve a given version of your VM you may later want to revert to. Another feature different virtualization products have in common are the types of supported network modes.
The different modes a VM can use to connect to a network can be as follows:
- NAT: All incoming and outgoing network traffic of your guest VM will go through the host network adapter. This means that the VM is not visible in the network we are currently on and we only see our host's MAC and IP addresses.
- Bridged: This network mode means the VM exposes itself and connects to the surrounding physical network as if it were a normal physical machine with its own unique MAC address. A DHCP server in this network will give the machine its own IP address that differs from the host machine.
- Host-only: This means that the VM can only communicate with and is visible to its host and not the rest of the network.
- Specific virtual network: This is a very great feature where you can define private and isolated subnet works, independent of the surrounding physical network, and then associate VMs to it. This can be useful so that only VMs can see and talk to other machines, which are in the same virtual network.
In this section, we will show you how to install the free virtualization software called VirtualBox, before creating a new CentOS 7 VM. We will also finish very important post-installation tasks, which we will need to perform in the upcoming sections. Installation of VirtualBox is really straightforward. Executable installers are available for every major operating system. The following are the steps to install VirtualBox:
- Open your favorite web browser and navigate to https://www.virtualbox.org/. Now, click on the download button that is clearly visible on the home page.
- Select a target host OS of your choice. In our example, we will select Windows.
- Click on Windows hosts to start the download. Also, don't forget to download the VirtualBox extension pack that you can find on the same Downloads page.
This is a package that will provide better USB support, among other useful features. After the download has finished, open the downloaded installer to run it and install it using the default settings.
Now, let's create a new CentOS 7 VM in VirtualBox. In order to do so, we first need to download the CentOS 7 Minimal ISO file version 1611 from the official CentOS website (https://www.centos.org/). This contains only the most important software packages needed to run a non-graphical Linux server, which is exactly what we want.
The following are the steps for creating a new CentOS 7 VM:
- Open a web browser and navigate to https://www.centos.org/. Navigate to Get CentOS Now | Minimal ISO.
- On the next screen, select the download URL near to your current location for fast download speed. I'm currently located in Germany, so my actual download URL will most likely be different than yours if you're somewhere else.
- Wait until the download has finished.
- Run VirtualBox on your system. Now, let's reproduce the following steps to install our first CentOS 7 VM:
- Click on the New button to create a new VM. If you type the VM Name as CentOS 7, VirtualBox will recognize the two other fields, Type and Version, to be Linux and Red Hat (64-bit) correctly for you.
- Click on the Next button to proceed to the next step.
- Now, select how much memory or RAM your VM must have. If you don't want any performance issues for your host system, stay in the green area displayed to you in the Memory size window.
- For a basic headless server, which means a non-graphical server, at least 2 GB of RAM is recommended. Don't worry, you can change this setting later too.
- Click on the Next button and on the next screen leave the default settings as is. Now, click on Create.
- Select the VDI option and click on Next. Now, on this screen, stay with the Dynamically allocated option. Click on Next.
- On the next screen, double the virtual hard disk size to 16 GB, as 8 GB is way too little for our work.
- Finally, click on the Create button to create a fresh and empty VM ready for installation of CentOS 7.
Now, let's install CentOS 7 in our empty VM:
- Select our CentOS 7 server VM and click on the green Start button to start it. Here, our downloaded CentOS 7 ISO file will be used as a virtual CD-ROM by VirtualBox and this is where it can be booted or started from.
- To do this, click on the small folder symbol and navigate the file browser to your downloaded CentOS 7 Minimal ISO file, and then click on the Start button.
- Now, your VM will present you with a text-based start menu, where we will use the up arrow key on the keyboard to select Install CentOS Linux 7 and then press Enter to start the installer.
- After waiting for some time, you will be presented with the first graphical installation screen.
- Select the installer language. In our example, we used the default English language.
- Click on Continue. Now, we are on the main installation screen, where we can customize our installation. You will need to wait until all the items have been loaded.
- Next, we need to set the installation destination. Click on INSTALLATION DESTINATION. As we are using an empty VM, we will use the full hard disk for installation, which is the default, so just click on Done which is present at the top-left side of the screen.
- Before we start the actual installation, let's quickly enable our Ethernet network card here, so we don't have to do this post installation using the command line. If you are behind a proxy, you can also add this kind of information here in this menu. Click on Done if you're ready.
- Now, let's click on Begin Installation. While the installation is ongoing, set a strong and secure password for the administrator or root account, which is the account that has all the rights and control over a system. Click on Done after setting up the strong password.
- Now, from the same screen, you can create a normal user account for your everyday work.
- Click on Done after creating the new user account.
- Now wait until the installation has finished. Once the installation is finished, click on the Reboot button to restart your system.
- Pressing the Enter key on the start screen will always select and use the latest kernel.
- Now, wait until you get a login screen in this window, which is also called our Service Terminal.
- Log in using the root user and password you set during the installation.
- Then, type the following command in the Terminal and press the Enter key:
# yum update -y
This command will install all the latest software updates available for your CentOS 7 installation, as the installer media does not have them included.
If the core of the CentOS 7 system, which is called the kernel, has been updated, we need to reboot the system. So, type reboot in the Terminal, and then press Enter. After rebooting again, press the Enter key then wait for the login screen to load and log in again.
Next, we type in another two commands, which will clear out all our free space so that we can create a smaller backup image of the system. Now, type the following command with administrative/root access in the Terminal:
dd if=/dev/zero of=/dd.img; rm -f /dd.img
Now, press the Enter key. This command will override all the free space of your Linux filesystem by creating one big file containing only zeros until the disk is full. This will take some time, so you will need to be patient. If this command outputs some text, it has been finished. You can ignore the error output as this is the expected behavior.
Finally, for setting up SSH port forwarding, we need to write down the actual IP address of our VM's connected network adapter. Run the following command:
ip addr list
Press Enter and, in the output line type in the IP address, which is the value after the word inet. In our example, it's 10.0.2.15.
To shut down the VM, use the following command and then press the Enter key:
In this section, we will learn the most important steps needed to properly work with the VirtualBox. Note that most of the settings discussed here can be directly translated from VirtualBox to any other desktop virtualization software such as KVM, VMware, Workstation, or Parallels Desktop.
Let's follow these steps to export the VM image:
- Navigate your cursor to the top-left corner of the Oracle VM VirtualBox Manager screen. Here, click on the File menu and in the drop-down menu select Export Appliance....
- In the same drop-down menu, you'll find another menu item called Import Appliance..., which allows you to import an image file once it has been created.
- Now, click on Export Appliance... to start the process.
- In the Export Virtual Appliance screen, select the VM you want to create an image of and then click on Next.
- Now, on the Storage settings screen leave the default settings as it is and then click on Next again.
- Again, on the Appliance settings screen, we don't want to change anything, so click on the Export button to start the process. This will take some time, so you will need to be patient.
- After the export process has been completed, let's examine what the resulting file will look like.
- Go to the location where VirtualBox has exported your file (usually, you'll find it in the Documents folder) and then right-click and select the Properties... option to view the file's properties:
As you can see, the exported VM size is over 600 MB in size, which is pretty awesome. This image file could now be copied to a backup location or transferred to another machine or data center for running it. The next thing we should do before working with our VM is also make a snapshot from the current state right after installation, so we can revert to the status quo whenever we need to.
Follow these steps to create a snapshot of the VM:
- Select the appropriate VM and then click on the Snapshots option for your marked VM. Give it a suitable name and an optional description.
- The next thing we want to do here is create some exact copies of our VM so that we have multiple CentOS 7 servers. To do this, right-click on the VM and select the Clone... option. Give it a suitable name and mark the option named Reinitialize the MAC address of all network cards, so it will be seen as a unique machine in our network.
- On the Clone type windows, select Full clone and click on Next to continue. Now, click on the Clone button while leaving the default option selected.
- Repeat the previous steps to create another fully cloned VM.
Now, let's demonstrate the power of working with snapshots. A snapshot should always be taken before doing something risky. For example, start one of our CentOS 7 VMs and log in to the system. Now, let's imagine we want to work on the /boot directory, where the Linux kernels reside. This is a critical directory, so it's a good idea to create a snapshot of the current VM state before proceeding:
In the previous screenshot, you can see that I've made a severe mistake. I completely deleted the whole kernel directory, so the directory is now empty. What will happen if I restart the system now? Let's see:
As you can see in the previous screenshot, I cannot boot without any kernel and the system is now completely unresponsive, and this gets really hard to fix. What is the best solution for this problem? Reverting to the state of your last snapshot.
Perform the following steps to revert the VM state to the previous snapshot:
- First shut down your VM. Now, select the snapshot of your choice and then click on the Restore button. This will ask you if you want to create a snapshot of the current state. If we don't want to do this, then we click on the Restore button, as shown in the following screenshot:
- Now, you can start the VM. As you can see from the following screenshot, we are just about to execute the delete command:
- If we start the machine again, all the problems are gone and we are back where we were before deleting the kernel files. It is recommended to use the snapshot feature often since it can save your precious time.
- Finally, we can easily adjust our VM's hardware parameters if we need to, as shown in the following screenshot:
You can only do this on a part of the machine. Select your VM of choice and click on Settings and then on System, where you can adjust your memory. Also, you can adjust your virtual process here. Click on Display to change the Video Memory settings. Under Storage, you can attach virtual hard disks or create new ones. Under Network, you can create new network adapters for your VM and choose between different network modes. Under USB, select USB 2.0 (EHCI) Controller so that we can connect a physical USB device to our VM properly if we want to.
Working with your new Linux OS using its Terminal window through the VirtualBox user interface will get you started and can be used for configuring the most basic settings of your new server. A more convenient, efficient, and professional way of accessing your server's command line is by using a terminal emulator program and SSH. A terminal emulator is an external program running outside VirtualBox in a separate window on your OS. It can be used exactly the same way as your server's main black and white Terminal:
It has a lot of convenient features, such as easy copy and paste in clipboards from your OS window and customizing font size and colors. Also, it uses tabs for better navigation, but how can you communicate with such a terminal emulator from your server? This is done using a client-server connection. The terminal emulator running on your host machine will be declined; we will connect to the CentOS 7 server to execute commands on it. Such a connection can be done using SSH. This is a system for remotely accessing and using Linux servers. It uses strong encryption for secure connections. As SSH is one of the most fundamental core services for communication on any Linux server, it is already installed and enabled on CentOS 7. All we need to do is run an SSH client within our terminal emulator program, which can connect and communicate with any server running the SSH service on Linux and macOS, a terminal emulator program, and an open source, as a stage client is already installed by default and ready to use.
On Windows, you need to install the program PuTTY, which contains not only the SSH client program, but is also a fully operational terminal emulator. Before we can access the CentOS 7 SSH service, we need to make its correct network address available to the host system where our terminal emulator is running. A correct network connection for communicating between a client and server always consists of an IP address, or domain name, bundled together with a specific port number. The domain name or IP is like a house number, whereas the port number is like the exact apartment number in that house. We always need both values for the correct delivery of network packages. By default, the ports of any guest VM are not available to the outside host system, so we first need to create the link between the host and the guest by using a feature that is called port forwarding. SSH, by default, is running on port 22, but this low port number cannot be used for forwarding to by non admin users or route users. As we are running VirtualBox as a normal user, we need to forward port 22 to a user port higher than 1024.
In our example, we use port 2222. To do this, perform the following steps:
- Select your VM of choice and navigate to the Settings option. Click on the Network tab.
- Explore the Advanced option. Now, click on the Port Forwarding button to create a new port forwarding rule.
- In the Port Forwarding Rules window, click on the Adds new port forwarding rule button. Now, use the 127.0.0.1 IP for the Host IP section, which is the IP of the localhost, then the Host Port, 2222. For the Guest IP section, type 10.0.2.15 and Guest Port 22, as illustrated in the following screenshot:
- Click on OK to create this rule.
- Repeat the same for the other two CentOS 7 VMs we cloned earlier. Make sure to use different host ports so that we can create distinct network endpoint connections for every host.
- The first CentOS 7 VM server can now be accessed using the IP 127.0.0.1 with port 2222, the second at port 2223, and the third at port 2224. Now, start all three of your CentOS 7 VMs.
- Open your favorite terminal emulator, for example, xterm, GNOME Terminal, or (as is in my example) the Xfce4 Terminal.
- To log in to your first CentOS 7 server using the terminal emulator, use the root credentials. Type the following command in the Terminal window:
ssh -p space 2222 [email protected]
- Press the Enter key. This command will connect your local SSH client to the SSH server running at the IP address 127.0.0.1 on port 2222, which gets redirected to your VM's network address 10.0.2.15 at port 22.
- When prompted, type yes in the Terminal, if you are logging into the server for the first time.
- Now, enter the credentials of the root user you set during installation.
- Here, we can now work and type commands as we would on the real Terminal screen. We can also log into the other two CentOS 7 VMs using the other two ports.
- To exit the SSH session, type in exit and press Enter.
- On a Windows system, you can use the free program named PuTTY to do exactly the same operations.
- Just open the PuTTY graphical user interface, type in the SSH server's IP address, 127.0.0.1, and use port 2222 for connection. Now, use the root account to access the VM.
- After setting up port forwarding, the easiest way is to use the free SCP program available on Mac or Linux. On Windows, you need to download PSCP.
- To download a file called /etc/passwd from the CentOS 7 VM guest to the host using the root user in the current directory, type the following command:
scp -P 2222 [email protected]:/etc/passwd .
- When asked, type in your root password. Now we can view the file locally:
- The other way around to upload a local file called my-local-file, filled with some random data to the server, and type scp -P 2222 my-local-file [email protected]:~.
- Press Enter, and type your root's password. The file has now been uploaded to the server into the /home folder, which is specified by the ~.
In this chapter, we've covered the introductory concepts of Linux and VirtualBox. We started off by gaining an understanding of the working principle of an operating system and progressed towards virtualization. Next, we covered the installation of VirtualBox and CentOS. Then, we learned how to work with VirtualBox and connected it using SSH.
In the next chapter, we'll understand the workings of the command line.