Fundamentals of Linux

3.5 (2 reviews total)
By Oliver Pelz
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

Linux is a Unix-like operating system assembled under the model of free and open source software development and distribution. Fundamentals of Linux will help you learn all the essentials of the Linux command line required to get you started. The book will start by teaching you how to work with virtualization software and install CentOS 7 Linux as a VM. Then, you will get to grips with the workings of various command line operations, such as cursor movement, commands, options, and arguments. As you make your way through the chapters, the book will not only focus on the most essential Linux commands but also give an introduction to Bash shell scripting. Finally, you will explore advanced topics, such as networking and troubleshooting your system, and you will get familiar with the advanced file permissions: ACL, setuid, and setgid. Fundamentals of Linux includes real-world tasks, use cases, and problems that, as a system administrator, you might encounter in your day-to-day activities.

Publication date:
June 2018
Publisher
Packt
Pages
234
ISBN
9781789530957

 

Introduction to Linux

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
  • Virtualization
  • Installing VirtualBox and CentOS
  • Working with VirtualBox
  • Connecting VMs through SSH
 

An overview of the Linux system

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.

 

Virtualization

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.
 

Installing VirtualBox and CentOS

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:

  1. 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.
  2. Select a target host OS of your choice. In our example, we will select Windows.
  3. 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:

  1. Open a web browser and navigate to https://www.centos.org/. Navigate to Get CentOS Now | Minimal ISO.
  2. 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.
  1. Wait until the download has finished.
On a modern and fast computer, you can install a fully working operating system such as CentOS 7 inside VirtualBox within a few minutes.
  1. Run VirtualBox on your system. Now, let's reproduce the following steps to install our first CentOS 7 VM:
    1. 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.
    2. Click on the Next button to proceed to the next step.
    3. 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.
    4. 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.
    5. Click on the Next button and on the next screen leave the default settings as is. Now, click on Create.
    6. Select the VDI option and click on Next. Now, on this screen, stay with the Dynamically allocated option. Click on Next.
    7. On the next screen, double the virtual hard disk size to 16 GB, as 8 GB is way too little for our work.
    8. 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:

  1. 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.
  2. 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.
  3. 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.
  1. After waiting for some time, you will be presented with the first graphical installation screen.
Before we click or type something into the VM window for the first time, we need to know how we can switch back to our host system once we are in. If you click once on the guest window, a popup will show up telling you how you can switch controls back and forth.
  1. Select the installer language. In our example, we used the default English language.
This is not the language of your CentOS 7 installation. We will set this type of information on the next screen.
  1. 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.
Here, all the items which are marked with an exclamation mark need to be done before we can proceed with the installation, but you can also do optional settings here like setting your location information.
  1. 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.
  2. 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.
  3. 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.
  4. Now, from the same screen, you can create a normal user account for your everyday work.
The first rule of any secure Linux system is never work with the root user unless you need to.
  1. Click on Done after creating the new user account.
  2. Now wait until the installation has finished. Once the installation is finished, click on the Reboot button to restart your system.
  3. Pressing the Enter key on the start screen will always select and use the latest kernel.
  4. Now, wait until you get a login screen in this window, which is also called our Service Terminal.
  5. Log in using the root user and password you set during the installation.
  6. 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 you get a new error while this command is running, something must be wrong with your internet connection, so troubleshoot internet connectivity on your host system.

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:

shutdown -h
 

Working with VirtualBox

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:

  1. 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....
  2. 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.
  3. Now, click on Export Appliance... to start the process.
  4. In the Export Virtual Appliance screen, select the VM you want to create an image of and then click on Next.
  5. Now, on the Storage settings screen leave the default settings as it is and then click on Next again.
  6. 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.
  7. After the export process has been completed, let's examine what the resulting file will look like.
  1. 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:

  1. Select the appropriate VM and then click on the Snapshots option for your marked VM. Give it a suitable name and an optional description.
  2. 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.
  3. 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.
  4. 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:

  1. 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:
  1. Now, you can start the VM. As you can see from the following screenshot, we are just about to execute the delete command:
  1. 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.
  1. Finally, we can easily adjust our VM's hardware parameters if we need to, as shown in the following screenshot:
Before making any hardware changes, ensure that you shut down the VM and then proceed.

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.

 

Connecting VMs through SSH

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:

  1. Select your VM of choice and navigate to the Settings option. Click on the Network tab.
  2. Explore the Advanced option. Now, click on the Port Forwarding button to create a new port forwarding rule.
  3. 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:
  1. Click on OK to create this rule.
  2. 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.
  3. 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.
  4. Open your favorite terminal emulator, for example, xterm, GNOME Terminal, or (as is in my example) the Xfce4 Terminal.
  5. 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]     
  1. 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.
  2. When prompted, type yes in the Terminal, if you are logging into the server for the first time.
  3. Now, enter the credentials of the root user you set during installation.
  1. 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.
  2. To exit the SSH session, type in exit and press Enter.
  3. On a Windows system, you can use the free program named PuTTY to do exactly the same operations.
  4. 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.
  5. 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.
  6. 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 .    
  1. When asked, type in your root password. Now we can view the file locally:
  1. 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]:~.
  2. 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 ~.
 

Summary

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.

About the Author

  • Oliver Pelz

    Oliver Pelz has more than 10 years of experience as a software developer and system administrator. He graduated with a diploma degree in Bioinformatics and is currently working at the German Cancer Research center in Heidelberg where he authored and co-authored several scientific publications in the field of Bioinformatics. Next to developing web applications and biological databases for his department and scientists all over the world he is administrating a division-wide Linux-based data center and has setup two high-performance CentOS clusters for the analysis of high-troughput microscope and genome sequencing data. He loves writing code, riding his mountainbike in the Black Forest of Germany and is an absolute Linux and open-source enthusiast for many years. He has contributed to several open-source projects in the past and also worked as a reviewer on the book Centos 7 High Performance. He maintains an IT tech blog at www.oliverpelz.de **********************************Acknowledgement************************************** I would like to thank my family and especially my wonderful wife Beatrice and little son Jonah for their patience and understanding for all these long working hours while writing this book. Also I would like to thank the folks at Packt Publishing for all their support and the opportunity to to write this book, it was a great pleasure for me. Last but not least I would like to thank Jonathan Hobson for writing the first edition of this book, without him no second edition of this book would have been possible. I would also like to thank all of the mentors that I’ve had over the years, especially Tobias Dykerhoff who introduced me to the whole world of Linux a long time ago and infected me with his enthusiasm about open-source and the free software movement. **********************************Dedication*********************************** This work is dedicated to my son Marlin Pelz who was stillborn on 2.10.15 just a few days before his expected date of delivery while writing the last few chapters of this book. Marlin, words can not express how much I miss you!

    Browse publications by this author

Latest Reviews

(2 reviews total)
After comprehensively read this ebook, I found that this ebook is not as my expectation. Bad sequence of the subjects, long paragraph that is difficult to understand, I don't like the use of the picture of screen shot of the Linux command
Effective howto on real development

Recommended For You

Book Title
Unlock this book and the full library for FREE
Start free trial