Chapter 1. Getting Started with Linux
Linux system administration means different things to different people. Administration, for this book, means the daily actions that a Linux system administrator (sysadmin) must take to manage and support users, maintain system health, implement best practices for security, install software, and perform housekeeping tasks. This chapter covers Linux installation, initial setup, and system exploration using simple shell commands.
You’ll spend a significant portion of your time at the command line, also known as the command-line interface (CLI). Linux system administrators rarely install or use GUIs on their supported server systems. This chapter introduces you to the CLI and some simple commands to navigate the filesystem, locate important files, and familiarize yourself with the Linux CLI.
Installing Linux
One of the first things every Linux system administrator learns is how to install Linux. There’s no single correct way to install Linux, but a few guidelines and suggestions will make your life easier as your users’ needs change.
While this section won’t go into detailed step-by-step instructions on installing Linux, the basic steps are outlined here. For most junior-level sysadmins, system installation generally occurs via automated means such as Kickstart or another enterprise-level delivery system.
Preparing Your System for Linux
If this is your first time installing Linux, I suggest that you install it into a virtual machine (VM). That way, you don’t have to dedicate an entire piece of hardware to a learning system and you won’t potentially render your system inoperable by attempting to install Linux in parallel to your current system, creating a multiboot computer. (Setting up multibooting is a more advanced concept and is beyond the scope of this book.)
If you don’t already have it installed, a good place to start with virtualization is to download and install the latest version of VirtualBox. VirtualBox is an application that allows your current computer to act as a VM host system where you may install virtual guests, such as Linux, into a separate, functioning computer system. VirtualBox runs on various host operating systems (OSs) and supports various guest operating systems, including Linux. The host OS and guest OS can be different from one another. Your computer (host OS) can be a Windows, Mac, or Linux-based system but have guest Linux systems installed on it as VirtualBox VMs.
Downloading and Installing Linux
Next, you’ll need to select a Linux distribution (distro) to install so that you can practice issuing commands, changing configurations, rebooting, installing software, creating users, and so on. I suggest you select a Linux distro based on the one your current employer uses. If your company doesn’t use Linux yet or you’re not employed in a system administrator role, then select from one of the following popular distributions:
- Debian
- Debian is a top-level distribution from which many other distributions are derived. Debian is community-supported, open source, and free.
- OpenSUSE
- OpenSUSE is a community-supported, top-level distribution with many faithful followers worldwide. Its commercial version, SUSE Linux Enterprise, has widespread adoption.
- Red Hat Enterprise Linux
- Red Hat is a commercially supported Linux distro that enjoys worldwide enterprise adoption and is now owned by IBM.
- Ubuntu
- Ubuntu is very popular and has both (Debian-derived) community and commercially supported distributions. Ubuntu also offers ready-made VirtualBox (and other) VMs to help you get a quick start.
The downloaded ISO file is a bootable Linux image. You don’t have to do anything to it if you use it to create a VM. A VM will boot from the ISO image and begin the installation process. After configuring your VM in VirtualBox, select Settings from the Oracle VM VirtualBox Manager, as shown in Figure 1-1.
Then, select Storage, as shown in Figure 1-2.
Select the empty optical disk drive under the IDE controller in the Storage Devices pane and then select the optical disk icon in the Attributes pane to browse for your ISO image file. Figure 1-3 shows several ISO files available in this menu.
Once you’ve selected your ISO image, click OK to proceed. When you start your VM, it will boot from this ISO image to begin installation onto your VM’s virtual disk.
When your system boots, you can accept the default settings. If you have experience installing Linux, you can change the default settings to suit your needs. Create a user account when prompted to do so. If your distribution prompts you to give the root account a password, do so. You must remember this password because, without it, you’ll have to reinstall your Linux VM or try to recover it. Installing Linux can take several minutes, and a reboot is required at the end of the installation process.
Getting to Know Your New Linux System
After installation, the first thing you need to do is log in using the username and password that you created during installation. You’re placed into your home directory inside a shell (or operating environment) upon login. Your home directory (/home) is a subdirectory of the / directory. The Linux filesystem is a hierarchical filesystem, similar to Microsoft Windows. At the top level, there is the root directory, which is represented by the / symbol. Windows uses a drive letter, such as C:, for the root directory. On Windows, you can have many drive letters with their own root levels, such as C:, D:, E:, and so on. In Linux, there is only one root directory, /. All other directories are subdirectories of the root directory. The following directory tree illustrates the Linux root directory and its subdirectories:
/ │─ bin │─ dev │─ etc │─ home │─ lib │─ media │─ mnt │─ opt │─ proc │─ root │─ run │─ sbin │─ srv │─ sys │─ tmp │─ usr └─ var
Note that there are only directories under the / (root) filesystem and no individual files, although some Linux distributions have a few symbolically linked files in /. All files are kept in directories. You’ll explore many of these subdirectories throughout the rest of the book. Table 1-1 provides a brief overview of the files and information in each directory.
Directory | Description |
---|---|
/ | The root filesystem only contains other directories but no individual files. |
/bin | The binaries directory contains executable files. Points to /usr/bin. |
/dev | The device directory contains device files to address peripherals. |
/etc | Contains system configuration files for users and services. |
/home | Users’ home directories. |
/lib | System libraries files. Points to /usr/lib. |
/media | Directory for mounting media such as USB drives or DVD disks. |
/mnt | The mount directory for mounting remote filesystems. |
/opt | Directory in which third-party software is installed. |
/proc | A virtual filesystem that tracks system processes. |
/root | The root user’s home directory. |
/run | Variable and volatile runtime data. |
/sbin | System binary (executable) files. |
/srv | Might contain data from system services. |
/sys | Contains kernel information. |
/tmp | Directory for storing session information and temporary files. |
/usr | Programs and libraries for users and user-related programs. |
/var | Variable files such as logs, spools, and queues. |
System files are protected from user modification. Only the root (administrative) user can modify system configuration files and settings. Users generally have write access only to their own home directories, the /tmp directory, and shared directories specifically created and modified by the administrator.
In the next section, you learn how to interact with your new Linux system at the command line.
Learning the CLI
The command-line interface, or CLI, is how most system administrators interact with their Linux systems because server systems don’t typically have a graphical interface. In Microsoft Windows terminology, a CLI-only system, such as a Linux server, would be equivalent to a Windows Server Core system where you only have access to command-line utilities.
As the name suggests, you interact with the Linux system using commands that you enter with a keyboard or standard input (stdin). The source from stdin can also be file redirection, programs, and other sources, but in the context of this book, stdin refers to keyboard input unless otherwise noted. Many commands are informational and display data about the system or system activities to the screen or standard output (stdout). Sometimes you’ll receive an error from the system known as standard error (stderr). You’ll see the full and abbreviated versions of these terms used interchangeably throughout this text and in other Linux-related documentation that you’ll find elsewhere.
You must learn a few commands to interact successfully with the filesystem. By “learn,” I mean commit to memory rather than looking them up online. There are only a few commands like this, and there are a few options that you should also commit to memory so that your interaction with the system becomes natural and efficient. Don’t worry about harming the system with any command I cover—I’ll warn you when a command should be used carefully.
There are a few things you need to know before jumping into issuing commands. The first is that Linux doesn’t use file extensions. This means that the file filename.exe has no more meaning to Linux than the file Financial_Report.txt or Résumé.doc. They are all files and might not be executable or text files. In Linux, you can name a file almost anything you want to (there are a few limitations), but use whitespace in filenames with caution because doing so requires you to use quotation marks around the filename.
The second thing to know about Linux is that filenames are case-sensitive. In other words, filename.txt and filename.TXT are two different files. I will prove this later in the chapter. For now, take my word for it. The third thing to know is that a file’s permission determines whether you can execute the file, edit the file, or even look at the file’s contents. Fourth, every Linux location is uniquely named by its path from the root (/) directory. For example, if you mention the password file in Linux, it’s shown as /etc/passwd. This is known as the absolute path and is the standard convention for speaking about or referring to files on the system. Fifth, Linux assumes you know what you want to do and that you’ve spelled everything correctly when you issue a command, so be careful, because some actions are irreversible.
Finally, Linux (like Unix, or more generally “*nix” systems) is not “chatty” like the Windows operating systems are. Linux systems, for example, don’t prompt you with an “Are you sure?” message when you remove (delete) files. Again, the Linux system assumes you want to execute the command you issue if you correctly spell all parts of the command. Spelling counts at the Linux command line.
Navigating the Linux filesystem means exploring the various system directories, learning to return to your home directory, and listing directory contents differently. If you’re a Windows user and you’ve worked at the Command Prompt (cmd) or PowerShell prompt (PS) on that platform, then the Linux command line will be familiar to you.
The short list of commands in the following sections will acquaint you with the Linux filesystem, files, and the contents of your home directory.
pwd
The pwd
(print working directory) command displays where you are on the filesystem. If you type the pwd
command now, followed by the Enter key, the command responds with /home/your_login_name
. Always ensure that you press the Enter key after each command so that you can receive a response:
$ pwd /home/student1
The $
is your shell prompt that shows you’re logged into the system as a user. You’ll use this command more later in the chapter.
cd
The cd
(change directory, or current directory) command places you into a new directory, returns you to your home directory, and moves you to a higher level or a subdirectory. The cd
command is analogous to the Windows cd
command:
$ cd /etc $ pwd /etc
Simply entering cd
returns you to your home directory regardless of where you are on the filesystem, as demonstrated here:
$ pwd /etc $ cd $ pwd /home/student1
When you cd
to a directory, use its absolute path:
$ cd /usr/bin
You can cd
to a subdirectory without the absolute path if you’re currently in the parent directory:
$ cd $ pwd /home/student1 $ cd /usr $ cd bin $ pwd /usr/bin
The cd
command is one that you’ll use every time that you connect to a Linux system.
ls
The ls
(list) command displays a list of files and directories within the location you specify. If you don’t specify a location, ls
displays the list of files and directories in your current directory:
$ cd $ pwd $ /home/student1 $ ls
You have no visible files in your home directory yet because you haven’t created any and none exist by default. However, you can list files from other directories by specifying the absolute path to the directory list you wish to see. There are too many files to list here in the /usr/bin directory, so I’ve truncated it to these few:
$ ls /usr/bin a2x getcifsacl p11-kit snmpping a2x.py getconf pack200 snmpps ac getent package-cleanup snmpset ...
There are files in your home directory, but they’re hidden because of the way they’re named. Files that begin with a period (.) are hidden from a regular ls
command. To see these files, you must use a command option to allow you to see all files:
$ ls -a . .. .bash_history .bash_logout .bash_profile .bashrc .gnupg .zshrc
Your listing might vary slightly from this, but understand that all directories and files whose names begin with a period are hidden from standard file lists using the ls
command with no options. You can cd
into hidden directories or list files within them:
$ ls .gnupg private-keys-v1.d pubring.kbx
The ls
command is one you’ll use every time you log into your Linux system and is certainly one that you want to commit to memory. I will use the ls
command throughout this book and introduce you to many more options for it along the way. Now that you’ve learned the basics of filesystem navigation, it’s time to cover rebooting and shutting down your system.
Starting, Rebooting, and Shutting Down a Linux System
The most basic tasks facing any Linux sysadmin are starting, restarting, and shutting down a system. If you have computer experience, you know that powering off a system without issuing a shutdown command is bad. It’s bad because doing so can, for example, corrupt open files, leave open files in an “open” state, disrupt running services, and cause problems with database transaction logs, possibly resulting in data loss.
Proper knowledge of how to start, restart, and shut down a system is of great value to sysadmins.
Starting a System
For physical systems, you press the power button and release it to start a system. This begins the power-on self-test (POST) and boot process. Watching the console during boot is important because the system notifies you and logs any issues as it starts up. Watch the screen for any errors or anomalies along the way. (The worst message you can experience on boot is a “kernel panic,” which will be covered later in the book.) Hopefully, all is well with your system, and the process ends with a login prompt.
The startup process is quite short but can identify systemic problems such as memory, disk, filesystem, and network issues. I’ll cover troubleshooting in a later chapter, but be aware now that you should watch the boot process carefully, noting any problems for later investigation.
Restarting a System
Restarting or rebooting a system is a standard sysadmin practice. Although you might read or hear the contrary, there’s nothing wrong with rebooting your system. You should do so regularly for all of the reasons stated in the previous section. Restarting a system clears memory, refreshes connections, and ensures the system is healthy. A good reboot can cure certain nagging problems, such as an application that drains your system’s memory, but only temporarily.
Any issues resolved with a reboot should be investigated more thoroughly after the system is up and stable. Restarting a system allows you to troubleshoot before application problems, logging problems, or network problems place the system back into a state when it requires another reboot.
Shutting Down a System
Shutting down means issuing a command that gently and appropriately closes all programs and eventually powers off the system. This gentle shutdown also warns shell users that the system is going down so that everyone can save their work and log off.
System shutdown should be reserved for hardware maintenance, relocation, or decommissioning. Some enterprise policies require that systems go through a full shutdown once a year to identify hardware issues that might not manifest themselves except through a complete system failure. Technicians and sysadmins usually take this opportunity to perform hardware maintenance or hardware checks at the same time.
Summary
This first chapter got you up and running with a live Linux system, covered some Linux basics, explored a few essential commands, and instructed you on the how and why of system startup, rebooting, and shutdown. In Chapter 2, you’ll learn more about using the CLI and how to use commands to create, remove, and modify files. You will also learn about Linux permissions, how to set and interpret them, and how to set a global default permission for users.
Get Practical Linux System Administration now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.