Chapter 1. Welcome to Linux

This chapter covers

  • What makes Linux different
  • Basic survival skills
  • Getting help

This book turns technology training sideways. Although other books, courses, and online resources organize their content around skills, I’m going to use real-world projects as teaching tools. Each of the core skills and the functionality of Linux systems will be covered—and covered well—but only when needed for a project. When you’re done, you’ll have learned everything you would have from a traditional source, but you’ll also know how to perform more than a dozen vital and sophisticated administration tasks and be comfortable tackling dozens more.

The first two or three chapters will quickly guide you through your initial introduction to the world of Linux servers. After that, you’ll work through and adapt practical hands-on projects, and nothing but practical hands-on projects. Through those projects, you’ll learn more than just commands and skills. Get ready to dive deeper and to eventually create solutions to your own business problems.

No single book can anticipate all the challenges that you’ll face throughout your career. But, by demonstrating how to approach real-world problems using real-world tools, this book will make it a whole lot easier for you to use the vast resources available through both inline documentation and the internet. If your prior Linux experience is limited, this chapter introduces some basic command-line survival skills and points to places you can go for help when things don’t work.

Note

As you’ll see, a command line is an interface provided by an operating system (OS) that permits you to type text commands to control the OS or to query data it manages.

I should note that in this and every chapter, you’re strongly encouraged to try everything out for yourself. There’s no better way to really get to the core of an IT skill than actually doing it, realizing that it’s not working the way you expected, and playing with it until it becomes yours forever. Good luck and have fun!

1.1. What makes Linux different from other operating systems

Linux is free, which means it’s a lot easier than other OSs to install exactly where and when needed for any use you can imagine. Not having to worry about purchasing site licenses and jumping through Digital Rights Management hoops makes testing all kinds of hardware combinations and server configurations much more straightforward.

Linux makes it possible to do various really useful and creative things. For instance, you can load a Linux live boot image on a USB stick, boot a PC whose own hard disk has been corrupted, and then troubleshoot and fix the problem. (You’ll learn how to do that in chapter 6.) Or, because Linux is a true multiuser OS, whole teams can concurrently log in to work locally or remotely, confident in the privacy and stability of the system.

Linux was built with some of the same technology and comes with most of the same tools as the deeply mature UNIX OS. This adds a great deal of stability and security. Linux distributions also provide sophisticated software package management systems that reliably install and maintain any of the thousands of free software applications available through online curated repositories.

But beyond free, Linux is open source, which means anyone can take the code base and reshape it into anything they want. Practically, this has spawned a vast ecosystem of specialty Linux distributions. A distribution (sometimes shortened to distro) is a customized stack of software that’s packaged along with the Linux kernel and distributed with tools for installing a working version of Linux of user computers. Table 1.1 provides a very incomplete distro list to illustrate the kinds of things that are available.

Table 1.1. Some of the many available Linux distros

Purpose

Distribution

Security/anti-hacking Kali Linux
  Parrot
Consumer desktop Mint
  Elementary OS
Lightweight (old hardware; diagnostics) Puppy Linux
  LXLE
Internet of Things administration Snappy Ubuntu Core
Enterprise server room CentOS (community version of Red Hat Enterprise Linux)
  OpenSUSE (community version of SUSE)
Cloud computing Amazon Linux (AWS AMI)
  Ubuntu Server (AWS AMI)
All-purpose (except lightweight) Ubuntu

Can’t find what you’re after? Create your own. Need help? There’s a large and active community online where, if someone hasn’t already solved your problem, they’ll know where to go to get it done. More than anything else, I’d say it’s the community-based resources that really make Linux so powerful.

1.2. Basic survival skills

Before beginning with the enterprise-ready projects that make up the rest of the book, it’s worthwhile to make sure we’re starting on the same page. This chapter covers the Linux basics: the UNIX Filesystem Hierarchy Standard (including pseudo file systems), navigation (ls, pwd, and cd), file management tools (cat, less, touch, mkdir, rmdir, rm, cp, and mv), some tricks (like tab completion and file globbing), sudo, and where to turn for help (man, info, and journalctl).

It’s possible you have enough experience already that you won’t need any of that material. Feel free to skip this chapter altogether. Don’t worry about the rest of us. We’ll catch up.

Installing Linux

I’m not going to spend time talking about how to install Linux on your PC. It’s not because installation is so ridiculously simple; it can sometimes get quite complicated. Rather, it’s because the approach you choose depends on your specific circumstances. Describing one possibility or even half a dozen would do nothing more than annoy the 75% of you for whom those scenarios won’t work.

Need some help getting started with installation? Check out Learn Linux in a Month of Lunches (Manning, 2016). Encountering a particular installation issue? Take a minute to write a brief but detailed description, and then use it to search the internet for help. Looking for a laptop or a desktop with Linux preinstalled? Search the internet for “pc with Linux preinstalled.” Have some unused hardware and a USB stick? Search for “install Linux from usb.” Prefer to install Linux as a virtual machine? Smart move. Stick around for chapter 2.

1.2.1. The Linux file system

It’s often said that everything in Linux works through plain text files, so it probably makes the most sense to start by understanding the Linux file system. But before we can get to Linux, what’s a file system? You can think of it as a data table (or an index) that creates apparent connections between individual files and groups of files with identifiable locations on a disk. Figure 1.1 can help you visualize how data spread across a disk partition can be exposed to system users within a directory structure.

Figure 1.1. Raw data on storage devices can be visually represented by the OS as organized directory hierarchies.

Why would you need an index? A digital storage device like a hard drive or USB device isn’t divided into physical divisions that can be used as organizing folders (or directories, as they’re known in Linux circles). One particular file can reside in a location on the actual media that’s a great distance away from another, nearly identical file created minutes or seconds apart, and all the parts of a single file might not be contiguous. Not only that, a file’s geographic location on the disk won’t necessarily remain static over time.

If you want your data to be reliably retrievable, you’ll need some kind of index that can consistently point you to the resources you’re after. A file system uses such an index to provide the appearance of an organized set of directories and files within a single disk division known as a partition.

Note

In case you need to dig deeper on your own some time, it’ll be useful to know that these days, the most commonly used Linux file system is ext4. But Linux can also work with storage drives that were formatted using file systems from other platforms like FAT32 and NTFS.

All the files in a disk partition are kept in directories beneath the root directory, which is represented by the / (forward slash) character. The way these directories are arranged is largely governed by the UNIX Filesystem Hierarchy Standard (FHS). You’re going to see pretty much the same basic layout whether you’re using a Linux distribution, UNIX, or even macOS. Figure 1.2 shows some of the most used, top-level directories.

Figure 1.2. Common top-level directories as defined by the UNIX FHS

Top-level directories—those directories located directly beneath the root—include /etc/, which contains configuration files that define the way individual programs and services function, and /var/, which contains variable files belonging to the system or individual applications whose content changes frequently through the course of normal system activities. You’ll also want to know about the /home directory where individual users are given directories for their private files.

1.2.2. Getting around: Linux navigation tools

Here’s where you’ll learn the five most basic, must-have Linux navigation commands (ls, pwd, cd, cat, and less). Because the command line is not a particularly visual environment no matter what you’re trying to do, you’re going to rely a great deal on these five tools to orient yourself.

Note

I hope it’s obvious that you should be trying each of these tools out for yourself on your own computer. That’s the only way you’ll learn.

The rest of the book requires a command-line terminal of one sort or another. Unfortunately, there’s no single way to open a terminal window that’ll work in all Linux distributions. For example, the terminal’s location in the Ubuntu menu system won’t necessarily match that of Fedora or Mint. And Ubuntu itself? Well, that depends on which version you’re running.

The Ctrl-Alt-t keyboard combination should work in at least most environments, as will looking through the application menus searching for an item with terminal in the name. By default, once your terminal opens, your home directory (/home/yourname/) will be active.

ls (list)

No point hanging around the terminal if you can’t see what’s there. You can list the names of the files and subdirectories in your current directory using ls. The ls command with the l flag (l stands for long) lists not only the object names, but their file permissions, owner, group, file size, and time stamp. Adding a directory designation like /var/ displays the contents of that directory:

$ ls -l /var
total 40
drwxr-xr-x  2 root root   4096 May  3 06:25 backups
drwxr-xr-x 11 root root   4096 Jan 17 21:16 cache
drwxr-xr-x 39 root root   4096 Jan 17 21:16 lib
drwxrwsr-x  2 root staff  4096 Apr 12  2016 local
lrwxrwxrwx  1 root root      9 Aug 12  2016 lock -> /run/lock
drwxrwxr-x  7 root syslog 4096 May  3 06:25 log
drwxrwsr-x  2 root mail   4096 Aug 12  2016 mail
drwxr-xr-x  2 root root   4096 Aug 12  2016 opt
lrwxrwxrwx  1 root root      4 Aug 12  2016 run -> /run
drwxr-xr-x  5 root root   4096 Jan 17 21:16 spool
drwxrwxrwt  2 root root   4096 Nov  7  2016 tmp
drwxr-xr-x  3 root root   4096 Sep 11  2016 www

The h argument when added to ls -l displays file sizes in a human-readable format—kilobytes, megabytes, and gigabytes, rather than bytes, which tend to involve a great many hard-to-count digits:

$ ls -lh /var/log
total 18M                                                      1
-rw-r--r-- 1 root   root    0 May  3 06:25 alternatives.log
drwxr-xr-x 2 root   root 4.0K May  3 06:25 apt
-rw-r----- 1 syslog adm  265K Jun  9 00:25 auth.log
-rw-r--r-- 1 root   root 312K Aug 12  2016 bootstrap.log
-rw------- 1 root   utmp    0 May  3 06:25 btmp
-rw-r----- 1 root   adm    31 Aug 12  2016 dmesg
-rw-r--r-- 1 root   root  836 May 21 14:15 dpkg.log
-rw-r--r-- 1 root   root  32K Nov  7  2016 faillog
drwxr-xr-x 2 root   root 4.0K Aug 12  2016 fsck
-rw-r----- 1 syslog adm  128K Jun  8 20:49 kern.log
-rw-rw-r-- 1 root   utmp 287K Jun  9 00:25 lastlog
-rw-r----- 1 syslog adm  1.7M Jun  9 00:17 syslog
-rw-rw-r-- 1 root   utmp 243K Jun  9 00:25 wtmp

  • 1 The total disk space (in MB) consumed by files in this directory
Note

In general, you add arguments to Linux commands in one of two ways: a dash followed by a single letter (like the h that modifies ls), or two dashes introducing more verbose versions of the same argument. In the example, ls --human-readable generates exactly the same output as ls -h. Nearly all Linux commands come packaged with full documentation, which we’ll explore later in the chapter.

Want to know what’s going on beneath your current directory? Adding uppercase R as an argument to ls displays subdirectories and the files and subdirectories they contain, no matter how many nested layers of directories. Just to get an idea of how involved that can get, and therefore how useful it can be to properly visualize it, run ls -R against the /etc/ directory tree:

$ ls -R /etc
pwd (present work directory)

In many cases, your current location within the file system will be displayed to the left of your command prompt. In this example, I’m in the network directory that lives just below /etc/:

ubuntu@base:/etc/network$

As you’ll likely find yourself working on systems where that prompt isn’t available, you might sometimes need a quick heads-up on your position. For that, typing pwd will print your present working directory:

$ pwd
/etc/network
cd (change directory)

Once you’ve got a decent idea of where you are and what’s immediately available to you in your current directory, you’ll need to know how to change locations. Typing cd tells the command-line interpreter (which will usually be Bash) to move you to the directory you specify. When you first open a terminal session (often referred to as a shell), by default, you’ll find yourself in your own account’s home directory. If you run pwd, you’ll probably see something like this:

$ pwd
/home/yourname
What’s Bash?

Bash is probably the most popular UNIX shell. Great! But what’s a shell? A shell is any user interface that interprets a user’s commands, either through a command-line interface (CLI) or a graphical user interface (GUI). You can think of a shell (visualized in the figure) as a software layer meant to execute all appropriately formatted commands using the underlying kernel and hardware system resources. In other words, it’s the way you talk to your computer.

The shell interprets the execution of user input commands.

Now, let’s move back to the root directory by typing cd and a forward slash:

cd /

Run ls again to take a look at what’s there. (You’ll see the directories illustrated earlier in figure 1.2.) Notice the home directory from which you can access your yourname directory. To move to any of the subdirectories listed there, type cd and then the directory you’d like to visit. Because the path you’re specifying here is relative to your current location, you don’t need to preface the directory name with a forward slash character. The command cd .. will move you up one level in the directory hierarchy, from /home/yourname/ to /home/, for instance.

If, however, you’ve got more ambitious travel plans and you’d like to see parts of the world lying far beyond your current directory, you’ll need to use an absolute path. That means you’ll always use a path that begins with the root directory (represented by a forward slash). To move back to your home directory from somewhere else on the system, you’ll type the forward slash, then home (which, you’ll remember, exists within the root directory), and then your username. Try it:

$ cd /home/yourname

That said, typing cd without any arguments will take you back to the home directory of the current logged-in user.

cat (print file contents to output)

Accessing the contents of text files within a terminal can sometimes be a bit tricky. The cat tool will print a file to the screen where it can be read, but not edited. This works pretty well for shorter documents like the fstab file in /etc/. The next example uses an absolute path so that the file can be found no matter where in the file system you happen to be at the moment:

$ cat /etc/fstab
Note

The name cat is actually short for concatenate, which reflects the tool’s value in joining multiple strings or files into a single text stream.

Suppose the file you want to read contains more lines than will display in a single screen. Try viewing the /etc/group file:

cat /etc/group

The odds are that the first lines scrolled up and off your screen far too fast for you to have read them. What’s the good of a plain text file if you can’t read it? Of course, as you’ll soon see, Linux has plenty of text editors for actively managing content, but it might be nice to be able to read a longer file, one screen at a time.

less (display file contents)

Welcome to less—thus named, presumably, because it can quickly read and display less than the complete file contents (or perhaps to distinguish it from the older more command). You launch less by running it against an existing filename:

less /etc/services

Using less, you can scroll up and down through the file with the arrow, PgUp, PgDn, and spacebar keys. When you’re done, press the q key to exit.

1.2.3. Getting things done: Linux file management tools

If you’ve got files and directories, you’ll need to know how to create, destroy, move, and copy them. Files are often automatically created by some external process such as a software installation or an automated log generation or, say, by saving your work within an office productivity package like LibreOffice. There isn’t much need to discuss all that here. I’ll note, however, that you can quickly create an empty file using the touch command, followed by the name you’d like to give it:

$ touch myfile

You can then see the file listed within your current directory through the ls command. Displaying its contents with cat will, of course, display nothing at all because you’ve only just created the file:

$ ls
myfile
$ cat myfile

“Touching” an existing file with touch updates its time stamp without making any changes. This can be useful if, for some reason, you want to change how various commands like ls list or display a file. (It can also be helpful if you’d like your boss to think that you’ve been hard at work on a data file that, in fact, you haven’t opened for weeks.)

Of course, you won’t get too far in this fast-paced, dog-eat-dog world by just creating directories full of empty files. Eventually, you’ll need to fill them with stuff and then edit the stuff that’s already there. For that, you’ll want to introduce yourself to a reliable text editor.

Before I throw myself headfirst into a very dangerous place, I should mention that lots of folks develop strong feelings for their text editors. Have you ever politely hinted to a Vim user that their venerable editor might not be as useful and important as it once was? Of course you haven’t. You wouldn’t be physically able to read this book if you’d done something like that.

I definitely won’t be telling you which text editor you must use. I will, however, tell you that full-featured word processors like LibreOffice and MS Word should never be used for your Linux administration work. Those applications will add all kinds of hidden formatting to your documents that will break system-level files. What I can say is that, roughly speaking, there are three categories of editors that could work for you:

  • If you prefer to work on documents from the GUI environment, then a simple plain-text editor like gedit (Ubuntu calls this the Text Editor) is great. Various syntax highlighting tools are also available to make coding and scripting more productive, and you can be confident that such a tool will save nothing but the text you see.
  • For those times when you need to edit a file from inside a terminal session, then a command-line editor like nano (or Pico) with its intuitive interface can do the job.
  • And, finally, there’s Vim (or its original iteration: vi). Ah, Vim. If you’re willing to invest a few months of your life into learning what is largely a nonintuitive interface, then you’ll be rewarded with a lifetime of greatly enhanced productivity. It’s that simple.
Note

All my books, articles, and course-related documents are written in gedit. Why? I like it.

Why not take a minute or two right now and make some edits to the myfile document you just created using each of the three text editors mentioned? For example:

$ nano myfile
$ vi myfile

For Vim, enter Insert Mode by pressing the i key and then typing your text. If you don’t want to spend the rest of your life trapped inside Vim, you can save your work by pressing Esc, then type :w, and then exit by typing :q.

Creating and deleting directories

Every object within a Linux file system is represented by a unique collection of metadata called an inode. I suppose you could say that the file system index discussed earlier is built from the metadata associated with all the many inodes on a drive. To display more information about the file you just created using touch, including inode information, you can use the stat command:

$ stat myfile
  File: 'myfile'
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 55185258    Links: 1                          1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/  ubuntu)                           2
                           Gid: ( 1000/  ubuntu)
Access: 2017-06-09 13:21:00.191819194 +0000
Modify: 2017-06-09 13:21:00.191819194 +0000
Change: 2017-06-09 13:21:00.191819194 +0000
 Birth: -

  • 1 The file’s inode ID
  • 2 The file’s permissions and ownership status

As you can see, the output includes data describing the file’s name, attributes, and time stamps. But it also tells you its inode ID number. It’s important to be aware that when you move, copy, or delete a file or directory, all you’re really doing is editing its inode attributes, not its ID. An inode, by the way, is an object used by UNIX systems to identify the disk location and attributes of files within a file system (as illustrated in figure 1.2). Usually there’ll be exactly one inode for each file or directory.

Assuming that you’re in your home directory, why not create a new directory that you can use for your experiments? For that, you’ll use mkdir:

$ mkdir myplace

Now move to your new directory and create a file there:

$ cd myplace
$ touch newfile
$ ls
newfile

So you can see how deleting objects works, move back up to the parent directory (using cd ..) and delete the directory you just made. Oddly enough, the predefined command for deleting directories, rmdir, won’t work in this case. Try it yourself:

$ cd ..
$ rmdir myplace
rmdir: failed to remove 'myplace': Directory not empty

“Directory not empty?” So what? This is a built-in check to prevent you from accidentally deleting directories filled with important files and subdirectories that you might have forgotten about. To get around this, there are a couple of things you can do.

One requires that you add the --ignore-fail-on-non-empty argument to the rmdir command, but that involves an awful lot of typing. Another approach would be to manually work through each subdirectory and individually delete every object you find. But that can sometimes be even worse. For those times when you are 100% sure that there’s absolutely nothing you need beneath the directory, the quickest route is to add the -r flag (meaning recursive) to the rm command:

$ rm -r myplace

Now is probably a good time to tell you about one very important difference between working with a GUI desktop interface and the command line: the command line has no trash can. If you delete something using rm (or rmdir) and then regret it, by and large, you’ll have no way of getting it back. But hey; think of all the disk space you’ll have freed up.

Copying and moving files

For this next step, create a few more files and a new directory:

$ touch file1 file2 file3
$ mkdir newdir

You can make an identical copy of an object using cp. This example creates a copy of file1 within the directory newdir:

$ cp file1 newdir

By the way, the cp command knows what to do with this command line because it’s smart enough to recognize newdir as a directory rather than a file. If there were no directory called newdir in the current location, cp would instead make a new copy of file1 named newdir. If you’re anything like me, at some point you’re probably going to accidentally misspell a command and end up with an odd new file rather than the directory you were after. In any case, check everything to confirm it all works out the way it was supposed to.

Unlike cp, the mv command will permanently move an object from one place to another. Therefore, if you were to move a file from your home directory to the newdir subdirectory, the original would no longer be available:

$ mv file2 newdir

Again, check the results for yourself. You can copy, move, or delete directories using the same commands as for files, adding the -r flag where necessary. Remember that you might be moving more than just the directory you see: any existing layers of unseen nested levels will also be dragged along for the ride.

File globbing

Had I been there when they came up with the name globbing, I’d have definitely urged them to reconsider. Perhaps it’s referring to a steaming swamp creature? Or an accidental discharge from the chemical plant up the highway? Actually, as it turns out, globbing (derived from the word global) describes applying wildcard characters to the filenames addressed by your commands.

If you need to move or copy multiple files and would like to avoid typing all the names individually, you can often apply the operation globally using the asterisk (*) wildcard. To move all the contents of the current directory to some other location, you might do something like this:

$ mv * /some/other/directory/

To move only files with names partially matching a particular sequence, try this:

$ mv file* /some/other/directory/

This command moves all files whose names begin with the letters file, but leaves everything else untouched. If you had files named file1, file2...file15 and wanted to move only those between file1 and file9, you’d use the question mark (?) instead of the asterisk:

$ mv file? /some/other/directory/

The question mark applies an operation to only those files whose names contain the letters file and one other character. It would leave file10 through file15 in the current directory.

Deleting files

As you learned earlier, objects can be deleted using rm. But keep in mind that these operations are effectively irreversible. If you wanted to delete file1 from the directory, you’d type:

$ rm file1

File globbing can be applied to rm the same way as to cp or mv, and with the same efficiency. So, for instance, this command

$ rm file*

deletes all files in the current directory whose names begin with the letters file. Adding the -r argument to a delete operation will make the action recursive and delete the contents of any subdirectories in the specified path:

$ rm -r *

In fact, that is a very dangerous combination, and even more so when you’re working with root authority, giving you power over all system files as well. Think very carefully indeed before investing too much into an rm command.

1.2.4. Keyboard tricks

I doubt there’s anyone who types just for the sheer joy of it. And I suspect that most people would really appreciate being told that they can get their typing done with, say, 40% fewer keystrokes. Well, I’m about to save you some fairly significant keyboard time.

Cutting and pasting

First of all, despite anything you might have seen to the contrary, you can copy and paste text into and out of a terminal. It’s true that the familiar Ctrl-c (copy) and Ctrl-v (paste) key combinations won’t work for a Bash shell session, but Shift-Ctrl-c and Shift-Ctrl-v will. You can also cut and paste by right-clicking your mouse and selecting the appropriate operation from the menu. Believe me, that can make a very big difference. Just imagine you came across a really long command sequence from a reliable online source that looks something like this:

$ find ./ -name *.html -printf '%CD	%p
' | grep "09/10/17"
 | awk '{print $2}' | xargs -t -i mv {} temp/

Do you want to type that whole thing in? Me neither. Here’s where cutting and pasting comes to the rescue.

Tab completion

You’ll really want to know about this one. Bash keeps track of your location and environment, and watches as you compose a new command. If the characters you’ve typed, based on the files and directories in your current environment, contain any hints about your ultimate goal, pressing the Tab key tells Bash to display its best guess on the command line. If you’re happy with the suggestion, press Enter and you’re on your way.

Here’s an example. Suppose you’ve downloaded a software archive file thoughtfully named something like foo-matic-plus_0.9.1-3_amd64.deb. You’d like to copy it to a work directory where you can extract it. Normally, you’d have to type

$ sudo cp foo-matic-plus_0.9.1-3_amd64.deb /usr/bin/foo-matic/

But if the file is in your current directory then, assuming it’s the only file in that directory that begins with foo, all you’ll have to type is cp foo and press the Tab key. Bash fills in the rest of the filename for you. Because Bash can’t read your mind, of course, you’ll still have to type at least enough of the destination address to give tab completion something to work with.

Try it yourself. Use touch to create a file with some ridiculously long name, and then try deleting or copying it using tab completion. Here’s what I came up with:

$ touch my-very-silly-filename_66-b.txt
$ rm my-<tab>

1.2.5. Pseudo file systems

A normal file is a collection of data that can be reliably accessed over and over again, even after a system reboot. By contrast, the contents of a Linux pseudo (or virtual) file, like those that might exist in the /sys/ and /proc/ directories, don’t really exist in the normal sense. A pseudo file’s contents are dynamically generated by the OS itself to represent specific values.

For instance, you might be curious to know how much total space you’ve got on one of your hard drives. Let me assure you that Linux will be only too happy to tell you. Let’s use a command-line program called cat to read a file containing the number of bytes on the disk, designated by the system as sda:

$ cat /sys/block/sda/size
1937389568
Note

If the first storage device on a system is called /dev/sda, then, as you might guess, the second one would be called /dev/sdb and the third, /dev/sdc. Originally, sda probably stood for SCSI Device A, but I find that thinking of it as Storage Device A makes it more meaningful. You might also run into device designations like /dev/hda (hard drive), /dev/sr0 (DVD drive), /dev/cdrom (that’s right, a CD-ROM drive), or even /dev/fd0 (floppy drive).

To get this kind of information, there are far simpler ways. You could right-click a drive’s icon within your GUI file manager, for instance, but the pseudo files in /sys/ are the common source on which all system processes rely.

Don’t happen to know your drive designation? No problem. Knowing that Linux organizes attached storage as block devices, you can move to the /sys/block/ directory and list its contents. Among the contents will be a directory called sda/. (Remember that sda stands for Storage Drive A.) That’s the first drive used by your system on boot:

$ cd /sys/block
$ ls
loop0  loop1  loop2 sda  sr0            1

  • 1 All the currently available block devices. A loop device is a pseudo device that allows a file to be used as though it’s an actual physical device.

Change to the sda/ directory and run ls. Among its contents, you’ll probably see files with names like sda1, sda2, and sda5. Each of these represents one of the partitions created by Linux to better organize the data on your drive:

$ cd sda
$ ls
alignment_offset  discard_alignment  holders    range      sda3       trace
bdi               events             inflight   removable  size       uevent
capability        events_async       integrity  ro         slaves
dev               events_poll_msecs  power      sda1       stat
device            ext_range          queue      sda2       subsystem

1.2.6. Showing ’em who’s boss: sudo

For practical reasons, using an OS account that enjoys full administration powers for day-to-day computing activities is unnecessarily risky. On the other hand, fully restricting yourself to a non-administration account makes it pretty much impossible to get anything done.

Many flavors of Linux solve this problem by providing selected accounts with admin authority that under most circumstances are purely theoretical, but that can be invoked when necessary by prefacing a command with the word sudo. Once you confirm your identity by providing your password, your command will be treated as though it was issued by the root user:

$ cat /etc/shadow
cat: /etc/shadow: Permission denied         1
$ sudo cat /etc/shadow
[sudo] password for ubuntu:

  • 1 The /etc/shadow file can’t be displayed without sudo powers.
Note

By default, the user created during the initial Linux installation will have sudo powers.

When illustrating command-line examples throughout this book, I use a command prompt of $ for commands that don’t require administrator privileges and, instead of $ sudo, I use # for those commands that do. Thus a non-admin command will look like this:

$ ls

And a sudo command will look like this:

# nano /etc/group

1.3. Getting help

One way or another, IT projects will always give you trouble. It can be complicated troubleshooting something you’re trying for the first time, or maybe a task you haven’t faced in so long that you’ve forgotten the exact syntax. You’re going to need help. Here are some solid places to look.

1.3.1. Man files

By accepted convention, the people who create and maintain the software behind a Linux command also write a highly structured documentation manual known as a man file. When a Linux program is installed, its man file is nearly always installed with it and can be viewed from the command line by typing man followed by the command name. Believe it or not, the man system itself has a man file, so we’ll start there:

$ man man

When you run this on your own computer, you’ll see that NAME, the first section, includes a brief introduction, SYNOPSIS offers a detailed syntax overview, and DESCRIPTION provides a more in depth explanation of the program, which usually includes a list of command-line arguments and flags. If you’re lucky, you’ll also find some useful EXAMPLES.

Man files can sometimes be quite large, so skimming through the document looking for one particular detail is not always practical. For various historical reasons, the Ctrl-f combination that launches local search operations within more modern applications like web browsers and word processors isn’t available. Instead, press the / key to get a text entry field at the bottom of the screen where you can type your search pattern. If the first highlighted result isn’t what you want, press the n key (as many times as necessary) to search forward in the document for the same string until you find what you’re looking for.

1.3.2. Info

The man system is great if you happen to know the name of the command or program you’re after. But suppose the command name is the bit that you’re missing. Type info at the command prompt, and you’ll be transported to an environment that is, by Bash standards, downright interactive:

$ info

As you can see from figure 1.3, the content is arranged alphabetically by topic with headings like Basics and Compression. You can use the up and down arrow keys to scroll between lines; and, when you reach a topic of interest, you can press Enter to move to the topic’s page.

Figure 1.3. The first screen of Info’s main menu. Info links may appear different on your system depending on what software you’ve installed.

Let’s suppose that you want to learn more about file permissions. Scroll down through the Basics section until you get to File Permissions, and press Enter. The Menu section of this page indicates that the lines that follow are links to more pages another level down. The u key will take you back up one level, and pressing q will exit Info altogether.

I have the sense that Info isn’t as heavily used in the community as it should be. In fact, I myself have a dark secret to share about Info—I worked with Linux for the better part of a decade before I even noticed it!

By default, the Info system might not be installed on some Linux server distributions. If typing info at the command prompt doesn’t give you the satisfaction you’re seeking, you can install it (on Ubuntu/Debian systems) using sudo apt install info.

1.3.3. The internet

No matter how dumb you may think you are, I can assure you that thousands of Linux administrators with all levels of experience have faced the same kinds of problems and solved them. Many of the solutions were the result of reaching out for help in an online community forum like link:serverfault.com or link:linuxquestions.org/questions.

Of course, you can always post your own questions on those sites, but why bother? Internet search engines do a great job indexing the questions that have already been asked and answered. A well-formed search query can usually get you to what you need much more quickly than starting the whole process over again from scratch.

The trick is knowing how to search intelligently. Typing my server crashed in the search field and hoping for the best probably won’t be all that useful. You obviously need more detail. OK. What kind of server is it: an Apache web server? Did any error messages appear in your browser? Did the crash generate any log entries? It’d probably be a good idea to find out.

Getting error information from system logs

On nearly all modern Linux distributions (with the notable exception of Ubuntu 14.04), you can access all system logs through journalctl:

# journalctl

As you’ll quickly see, running journalctl without any arguments will drown you in a torrent of data. You’ll need to find some way to filter for the information you’re after. Allow me to introduce you to grep:

# journalctl | grep filename.php            1

  • 1 The | (pipe) character uses the output of one command (journalctl, for example) as input for the next (grep).

In this example, I use the vertical line (|) that’s achieved on US keyboard layouts through the Shift- combination. This pipes the output of journalctl to the grep filter, which will print to the screen only those lines that include the string filename.php. I’m assuming, of course, that your web server is running PHP content and that there’s a file named filename.php. Not that I’d ever do that. I usually give mine far more descriptive and useful names like stuff.php.

You can use grep in sequence to narrow your results further. Suppose there were too many journal entries for filename.php, and you realized you only needed the ones that also contain the word error. You could pipe the results of the first operation to a second grep command, filtering for error:

# journalctl | grep filename.php | grep error

In case you’d prefer to see only those lines that don’t contain the word error, you’d add -v (for inverted results):

# journalctl | grep filename.php | grep -v error
Searching the internet

Now imagine that the output you got from journalctl includes this text:

[Fri Oct 12 02:19:44 2017] [error] [client 54.208.59.72]
  Client sent malformed Host header

This might be useful. There’s no point searching the internet for the date stamp or for that particular IP address, but I’ll bet someone else has encountered Client sent malformed Host header.

To cut down on false positives, you might want to enclose the words in quotation marks so your search engine returns only results matching that exact phrase. Another way to minimize false positives is to tell the search engine to ignore pages containing a particular string.

In this rather silly example, you’re searching the internet for a good introduction to writing Linux scripts. You find that, based on most of the results your search engine shows you for writing scripts, someone out there seems to think that you’d rather live in Hollywood. You can solve that problem by excluding pages that contain the word movie:

writing scripts -movie

Summary

  • Just about any Linux command-line operation will make use of some or all of five basic tools: ls, pwd, cd, cat, and less.
  • Linux uses pseudo file systems to expose data on the hardware environment to processes and users.
  • Authorized users can invoke sudo to gain administration permissions for individual commands.
  • There’s a great deal of documentation and other help available through the man system, Info, and online.

Key terms

  • A file system is made up of data files indexed in a way that allows the perception of a directory-based organization.
  • A process is an active instance of a running software program.
  • A disk partition is the logical division of a physical storage device that can be made to work exactly like a standalone device. Partitions are common organizational tools for all modern operating systems.
  • Bash is a command-line user interface for executing system actions.
  • Plain text that is usable for administration purposes is text made up of a limited set of characters and contains no extraneous formatting code.
  • File globbing involves using wildcard characters to refer to multiple files with a single command.
  • Tab completion employs the Tab key to suggest possible completions of a partially typed command.
  • Pseudo file systems are directories containing files with dynamic data automatically generated at or after system boot.

Security best practices

Avoid working on your Linux machine as the root user. Use a regular user account instead, and, when you need to perform administration tasks, use sudo.

Command-line review

  • ls -lh /var/log lists the contents and full, human-friendly details of the /var/log/ directory.
  • cd, by itself, returns you to your home directory.
  • cp file1 newdir copies a file called file1 to the directory named newdir.
  • mv file? /some/other/directory/ moves all files containing the letters file and one more character to the target location.
  • rm -r * deletes all files and directories beneath the current location. Use with great care.
  • man sudo opens the man documentation file on using sudo with commands.

Test yourself

1

Which of the following Linux distributions is best suited for security operations?

  1. OpenSUSE
  2. CentOS
  3. Kali Linux
  4. LXLE

2

Which of the following tools allows you to edit text within a terminal session?

  1. nano
  2. gedit
  3. touch
  4. LibreOffice

3

What does adding the -l argument to the ls command do?

  1. Lists file details
  2. Lists information in a human readable format
  3. Displays only file names
  4. Displays subdirectories recursively

4

Which of the following commands will display your current location within the file system?

  1. touch
  2. pwd
  3. ls -c
  4. cd

5

What does the command cat /etc/group do?

  1. Displays the contents of the /etc/group file within a navigable interface
  2. Copies the /etc/group file to a new, specified location
  3. Updates the last accessed value of the /etc/group file
  4. Prints the contents of the /etc/group file to output (scrolling the contents to the screen)

6

Which of these commands will delete directories containing files and subdirectories?

  1. rmdir myfulldirectory
  2. sudo rmdir myfulldirectory
  3. rm -r myfulldirectory
  4. rm myfulldirectory

7

Assuming there’s no directory named mynewfile, what will the mv myfile mynewfile command do?

  1. Make a copy of the file myfile named mynewfile
  2. Create an empty directory named mynewfile
  3. Create an empty directory named mynewfile and move the myfile file into the new directory
  4. Change the name of myfile to mynewfile

8

Which of the following will delete all files with the word file plus any number of characters in its name?

  1. rm file*
  2. rm file?
  3. rm file.
  4. rm file??

Answer key

1.

c

2.

a

3.

a

4.

b

5.

d

6.

c

7.

d

8.

a

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.46.96