Fundamentals of Linux - Oliver Pelz - E-Book

Fundamentals of Linux E-Book

Oliver Pelz

0,0
23,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 260

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Fundamentals of Linux
Explore the essentials of the Linux command line
Oliver Pelz
BIRMINGHAM - MUMBAI

Fundamentals of Linux

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Vijin BorichaAcquisition Editor:Prachi BishtContent Development Editor:Dattatraya MoreTechnical Editors:Aditya Khadye, Cymon Pereira, Sayli ThanekarCopy Editors: Laxmi Subramanian, Safis EditingProject Coordinator:Kinjal BariProofreader: Safis EditingIndexer:Aishwarya GangawaneGraphics:Jisha ChirayilProduction Coordinator: Deepika Naik

First published: June 2018

Production reference: 1300618

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78953-095-7

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributor

About the author

Oliver Pelz has more than 10 years' 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 has authored and co-authored several scientific publications in the field of bioinformatics. He loves coding and riding his mountain bike in the Black Forest of Germany. He develops web applications and biological databases for his department and scientists all over the world and administers a division-wide Linux-based datacenter.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Fundamentals of Linux

Packt Upsell

Why subscribe?

PacktPub.com

Contributor

About the author

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the color images

Conventions used

Get in touch

Reviews

Introduction to Linux

An overview of the Linux system

Virtualization

Installing VirtualBox and CentOS

Working with VirtualBox

Connecting VMs through SSH

Summary

The Linux Command Line

Introducing the command line

File globbing

Quoting commands

Getting help

Working with the Linux shell

Understanding standard streams

Understanding regular expressions

Working with sed

Working with awk

Navigating the Linux filesystem

Summary

The Linux Filesystem

Understanding the filesystem

Working with file links

Searching for files

Working with users and groups

Working with file permissions

Working with text files

Working with VIM text editor

Summary

Working with the Command Line

Essential Linux commands

Additional programs

net-tools

Nmap

links

iotop

iftop

htop

lsof

Understanding processes

Cloning

Signals

kill

hang-up

Working with Bash shell variables

Introduction to Bash shell scripting

Implementing Bash shell scripting

Automating script execution

Summary

More Advanced Command Lines and Concepts

Basic networking concepts

Installing new software and updating the system

Introduction to services

Basic system troubleshooting and firewalling

Introducing ACLs

setuid, setgid and sticky bit

setuid

setgid

sticky bit

Summary

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

In this book, the goal is to build a solid foundation of learning all the essentials of the Linux command line to get you started. It has been designed to strongly focus on learning only the practical core skills and essential Linux knowledge, which is really important when beginning this wonderful OS in an easy way. All the examples shown in this course have been carefully chosen to be everyday and real-world tasks, use cases, and problems Linux beginners or system administrators will probably encounter when starting out from scratch. We begin our journey with the virtualization software and install CentOS 7 Linux as a VM. Then, we will gently introduce you to the most basic command-line operations, such as cursor movement, commands, options and arguments, history, quoting and globbing, file streams and pipes, and getting help, and then introduce you to the wonderful art of regular expressions and how to work with files. Then, the most essential everyday Linux commands are demonstrated and explained, and a compact introduction to Bash shell scripting provided. Finally, the reader is introduced to advanced topics such as networking, how to troubleshoot your system, advanced file permissions, ACL, setuid, setgid, and sticky bit. This is just the starting point and there is so much more you can learn about Linux.

Who this book is for

This book is for individuals looking to work as Linux system administrator.

What this book covers

Chapter 1, Introduction to Linux, introduces you to the general idea of Linux. Topics range from virtualization, and the installation of VirtualBox and CentOS, through to the working dynamics of VirtualBox, and SSH connectivity with VirtualBox.

Chapter 2, The Linux Command Line, sheds some light on a wide range of topics, including shell globbing, an introduction to command-line operations, the navigation of files and folders in the Linux filesystem, the central idea of different streams, regular expressions, and important commands such as grep, sed, and awk.

Chapter 3, The Linux Filesystem, focuses on the working dynamics of the system, including file links, users and groups, file permissions, text files, text editor, and an understanding of the Linux filesystem.

Chapter 4, Working with the Command Line, walks you through essential Linux commands, signals, additional programs, processes, and Bash shell scripting.

Chapter 5, More Advanced Command Lines and Concepts, provides an overview of basic networking concepts, services, ACL, troubleshooting, setuid, setgid, and sticky bit.

To get the most out of this book

You will need a basic lab setup and at least a system with 8 GB of RAM and a dual-core processor. If you are planning to create a virtual environment, then a system with the same memory and a quad-core processor is recommended.

A VirtualBox and a VMware workstation are the best options for Windows. For Mac systems, run the testing system on parallels.

Throughout the book, we have used CentOS 7 minimal as the operating system.

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/FundamentalsofLinux_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "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."

Any command-line input or output is written as follows:

# yum update -y

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select our CentOS 7 server VM and click on the green Start button to start it."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

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:

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.

On a modern and fast computer, you can install a fully working operating system such as CentOS 7 inside VirtualBox within a few minutes.

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.

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.

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.

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.

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.

The first rule of any secure Linux system is never work with the root user unless you need to.

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 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:

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.