Chapter 2. Using Essential Tools

Image

The following topics are covered in this chapter:

The following RHCSA exam objectives are covered in this chapter:

  • Use input-output redirection (>, >>, |, 2>, etc.)

  • Create and edit text files

  • Locate, read, and use system documentation including man, info, and files in /usr/share/doc

This chapter is dedicated to coverage of the basic Linux skills that everyone should have before attempting to take the RHCSA exam.

“Do I Know This Already?” Quiz

The “Do I Know This Already?” quiz allows you to assess whether you should read this entire chapter thoroughly or jump to the “Exam Preparation Tasks” section. If you are in doubt about your answers to these questions or your own assessment of your knowledge of the topics, read the entire chapter. Table 2-1 lists the major headings in this chapter and their corresponding “Do I Know This Already?” quiz questions. You can find the answers in Appendix A, “Answers to the ‘Do I Know This Already?’ Quizzes and ‘Review Questions.’

Table 2-1 “Do I Know This Already?” Section-to-Question Mapping

Foundation Topics Section

Questions

Basic Shell Skills

1, 3, 4–7

Editing Files with vim

8

Understanding the Shell Environment

2, 9

Finding Help

10

1. Which of the following commands enables you to redirect standard output as well as standard error to a file?

a. 1&2> file

b. > file 2>&1

c. >1&2 file

d. 1>2& file

2. You are looking for a variable that is set in a Bash login shell for all users. Which of the following files is the most likely location where this variable is set? (Choose two.)

a. /etc/profile

b. /etc/bashrc

c. ~/.bash_profile

d. ~/.bashrc

3. A user has created a script with the name myscript. He tries to run it using the command myscript, but it is not started. The user has verified that the script permissions are set as executable. Which of the following is the most likely explanation?

a. An internal command is preventing the startup of the script.

b. Users are not allowed to run scripts.

c. The directory that contains the script is not in the PATH.

d. The script does not have appropriate permissions.

4. You need the output of the command ls to be used as input for the less command. Which of the following examples will do that for you?

a. ls > less

b. ls >> less

c. ls >| less

d. ls | less

5. A user wants to remove her complete history. Which of the following approaches would do that?

a. Remove the ~/.bash_history file and type history -c.

b. Type history -c.

c. Remove the ~/.bash_history file.

d. Type history -c and close the current shell.

6. Which of the following is not a valid method to repeat a command from history?

a. Press Ctrl-r and start typing a part of the command.

b. Type ! followed by the first letters in the command.

c. Type ! followed by the number of the command as listed in history.

d. Press Ctrl-x followed by the number in history.

7. For which of the following items can Bash completion be used?

a. Commands

b. Files

c. Variables

d. All of the above

8. Which of the following commands enables you to replace every occurrence of old with new in a text file that is opened with vi?

a. :%s/old/new/g

b. :%r/old/new/

c. :s/old/new/g

d. r:/old/new

9. Which approach works best if during the login process you want to show a message to all users who have just logged in to a shell session on your server?

a. Put the message in /etc/issue.

b. Put the message in /etc/motd.

c. Put the message in /etc/profile.

d. Put the message in /etc/bashrc.

10. You are using man -k user, but you get the message “nothing appropriate.” Which of the following solutions is most likely to fix this for you?

a. Type updatedb to update the mandb database.

b. Type makewhatis to update the mandb database.

c. Type mandb to update the mandb database.

d. Use man -K, not man -k.

Foundation Topics

Basic Shell Skills

The shell is the default working environment for a Linux administrator. It is the environment where users and administrators enter commands that are executed by the operating system. Different shells for Linux are available, but Bash is the most common shell. So when we are talking about “the shell” in this book, we are actually talking about the Bash shell. This chapter provides an overview of some of the items that you will encounter when working with the shell.

Understanding Commands

Working with the shell is all about working with command syntax. Typically, command syntax has three basic parts: the command, its options, and its arguments.

The command is the command itself, such as ls. This command shows a list of files in the current directory. To modify the behavior of the command, you can use options. Options are a part of the program code, and they modify what the command is doing. For instance, when you use the -l option with the ls command, a long listing of filenames and properties is displayed.

The word argument is a bit confusing. Generally speaking, it refers to anything that the command addresses, so anything you put after the command is an argument (including the options). Apart from the options that can be used as an argument, commands can have other arguments as well, which serve as a target to the command.

Let’s have a look at an example: the command ls -l /etc. This command has two different arguments: -l and /etc. The first argument is an option, modifying the behavior of the command. The second argument is a target, specifying where the command should do its work. You’ll find these three elements in nearly all commands you’ll be working with in a Linux environment.

Executing Commands

The purpose of the Linux shell is to provide an environment in which commands can be executed. The shell takes care of interpreting the command that a user has entered correctly. To do this, the shell makes a distinction between three kinds of commands:

  • Aliases

  • Internal commands

  • External commands

An alias is a command that a user can define as needed. Some aliases are provided by default; type alias on the command line to get an overview. To define an alias, use alias newcommand='oldcommand', as in the default alias ll='ls -l --color=auto' that has already been created on your system. Aliases are executed before anything else. So, if you have an alias with the name ll but also a command with the name ll, the alias will always take precedence, unless a complete pathname is used.

An internal command is a command that is a part of the shell itself and, as such, doesn’t have to be loaded from disk separately. An external command is a command that exists as an executable file on the disk of the computer. Because it has to be read from disk the first time it is used, it is a bit slower. When a user executes a command, the shell first looks to determine whether it is an internal command; if it is not, it looks for an executable file with a name that matches the command on disk. To find out whether a command is a Bash internal or an executable file on disk, you can use the type command.

To look up external commands, use the $PATH variable. This variable defines a list of directories that is searched for a matching filename when a user enters a command. To find out which exact command the shell will be using, you can use the which command. For instance, type which ls to find out where the shell will get the ls command from. An even stronger command is type. This command will also work on internal commands and aliases.

You should notice that for security reasons the current directory is not in the $PATH variable and Linux does not look in the current directory to see whether a specific command is available from that directory. That is why you need to start a command that is in the current directory but nowhere in the $PATH by including ./ in front of it. The dot stands for the current directory, and by running it as ./, you tell Bash to look for the command in the current directory. Although running commands this way is not very common, you will have to do it to run scripts that you’ve created in your current directory.

The $PATH variable can be set for specific users, but in general, most users will be using the same $PATH variable. The only exception to this is the user root, who needs access to specific administration commands. In Exercise 2-1, you learn some of the basics about working with commands.

Exercise 2-1 Using Internal and External Commands from the Shell

  1. Authenticate as the user who you created in Chapter 1, “Installing Red Hat Enterprise Linux” when installing your server.

  2. Type time ls. This executes the ls command where the Bash internal time shows information about the time it took to complete this command.

  3. Type which time. This shows the filename /usr/bin/time that was found in the $PATH variable.

  4. Type echo $PATH to show the contents of the $PATH variable. You can see that /usr/bin is included in the list, but because there also is an internal command time, the time command from the path will not be executed unless you tell the shell specifically to do so—the command in step 2 has executed the internal command for you because of command precedence.

  5. Type /usr/bin/time ls to run the /usr/bin/time command when executing ls. You’ll notice that the output differs completely. Ignore the meaning of the output; we’ll get back to that later. What matters for now is that you realize that these are really two different commands.

I/O Redirection

By default, when a command is executed, it shows its results on the screen of the computer you are working on. The computer monitor is used as the standard destination for output, which is also referred to as the STDOUT. The shell also has default destinations to send error messages to and to accept input. Table 2-2 gives an overview of all three.

Table 2-2 Standard Input, Output, and Error Overview

Name

Default Destination

Use in Redirection

File Descriptor Number

STDIN

Computer keyboard

< (same as 0<)

0

STDOUT

Computer monitor

> (same as 1>)

1

STDERR

Computer monitor

2>

2

So if you run a command, that command would expect input from the keyboard, and it would normally send its output to the monitor of your computer without making a distinction between normal output and errors. Some commands, however, are started in the background and not from a current terminal session, so these commands do not have a monitor or console session to send their output to, and they do not listen to keyboard input to accept their standard input. That is where redirection comes in handy. Redirection is also useful if you want to work with input from an alternative location, such as a file.

Programs started from the command line have no idea what they are reading from or writing to. They just read from what the Linux kernel calls file descriptor 0 if they want to read from standard input, and they write to file descriptor number 1 to display non-error output (also known as “standard output”) and to file descriptor 2 if they have error messages to be output. By default, these file descriptors are connected to the keyboard and the screen. If you use redirection symbols such as <, >, and |, the shell connects the file descriptors to files or other commands. Let’s first look at < and >. Later we discuss pipes (the | symbol). Table 2-3 shows the most common redirectors that are used from the Bash shell.

Table 2-3 Common Bash Redirectors

Redirector

Explanation

> (same as 1>)

Redirects STDOUT. If redirection is to a file, the current contents of that file are overwritten.

>> (same as 1>>)

Redirects STDOUT. If output is written to a file, the output is appended to that file.

2>

Redirects STDERR.

2>&1

Redirects STDERR to the same destination as STDOUT. Notice that this has to be used in combination with normal output redirection, as in ls whuhiu > errout 2>&1.

< (same as 0<)

Redirects STDIN.

In I/O redirection, files can be used to replace the default STDIN, STDOUT, and STDERR. You can also redirect to device files. A device file on Linux is a file that is used to access specific hardware. Your hard disk, for instance, can be referred to as /dev/sda, the console of your server is known as /dev/console or /dev/tty1, and if you want to discard a command’s output, you can redirect to /dev/null. Note that to access most device files, you need to be root.

Using Pipes

Whereas an I/O redirector is used as an alternative for keyboard and computer monitor, a pipe can be used to catch the output of one command and use that as input for a second command. If a user runs the command ls, for instance, the output of the command is shown onscreen, because the screen is the default STDOUT. If the user uses ls | less, the commands ls and less are started in parallel. The standard output of the ls command is connected to the standard input of less. Everything that ls writes to the standard output will become available for read from standard input in less. The result is that the output of ls is shown in the less pager, where the user can browse up and down through the results easily.

As a Linux administrator, you’ll use pipes a lot. Using pipes makes Linux a flexible operating system; by combining multiple commands using pipes, you can create “super” commands that make almost anything possible. In Exercise 2-2, you use I/O redirectors and pipes.

Exercise 2-2 Using I/O Redirection and Pipes

  1. Open a shell as user user and type cd without any arguments. This ensures that the home directory of this user is the current directory while working on this exercise. Type pwd to verify this.

  2. Type ls. You’ll see the results onscreen.

  3. Type ls > /dev/null. This redirects the STDOUT to the null device, with the result that you will not see it.

  4. Type ls ilwehgi > /dev/null. This command shows a “no such file or directory” message onscreen. You see the message because it is not STDOUT, but rather an error message that is written to STDERR.

  5. Type ls ilwehgi 2> /dev/null. Now you will no longer see the error message.

  6. Type ls ilwehgi Documents 2> /dev/null. This shows the name of the Documents folder in your home directory while hiding the error message.

  7. Type ls ilwehgi Documents 2> /dev/null > output. In this command, you still write the error message to /dev/null while sending STDOUT to a file with the name output that will be created in your home directory.

  8. Type cat output to show the contents of this file.

  9. Type echo hello > output. This overwrites the contents of the output file.

  10. Type ls >> output. This appends the result of the ls command to the output file.

  11. Type ls -R /. This shows a long list of files and folders scrolling over your computer monitor. (You might want to press Ctrl-C to stop [or wait some time]).

  12. Type ls -R | less. This shows the same result, but in the pager less, where you can scroll up and down using the arrow keys on your keyboard.

  13. Type q to close less. This will also end the ls program.

  14. Type ls > /dev/tty1. This gives an error message because you are executing the command as an ordinary user and ordinary users cannot address device files directly (unless you were logged in to tty1). Only the user root has permission to write to device files directly.

History

A convenient feature of the Bash shell is the Bash history. Bash is configured by default to keep the last 1,000 commands a user used (and if the shell session is never closed, the exact number can grow well beyond that). When a shell session is closed, the history of that session is updated to the history file. The name of this file is .bash_ history, and it is created in the home directory of the user who started a specific shell session. Notice that the history file is closed only when the shell session is closed; until that moment, all commands in the history are kept in memory.

The history feature makes it easy to repeat complex commands. There are several ways of working with history:

  • Type history to show a list of all commands in the Bash history.

  • Press Ctrl-r to open the prompt from which you can do backward searches in commands that you have previously used. Just type a string and Bash will look backward in the command history for any command containing that string as the command name or one of its arguments. Press Ctrl-r again to repeat the last backward search.

  • Type !number to execute a command with a specific number from history.

  • Type !sometext to execute the last command that starts with sometext. Notice that this is a potentially dangerous command because the command that was found is executed immediately!

In some cases it might be necessary to wipe the Bash history. This is useful, for instance, if you’ve typed a password in clear text by accident. If that happens, you can type history -c to clear the current history. Commands from this session won’t be written to the history file when you exit the current session. If you want to remove the complete Bash history, type history -w immediately after using history -c.

Exercise 2-3 guides you through some history features.

Exercise 2-3 Working with History

  1. Make sure that you have opened a shell as user student.

  2. Type history to get an overview of commands that you have previously used.

  3. Type some commands, such as the following:

    ls
    pwd
    cat /etc/hosts
    ls -l

    The goal is to fill the history a bit.

  4. Open a second terminal on your server by right-clicking the graphical desktop and selecting the Open in Terminal menu option.

  5. Type history from this second terminal window. Notice that you do not see the commands that you just typed in the other terminal. That is because the history file has not been updated yet.

  6. From the first terminal session, press Ctrl-r. From the prompt that opens now, type ls. You’ll see the last ls command you used. Press Ctrl-r again. You’ll now see that you are looking backward and that the previous ls command is highlighted. Press Enter to execute it.

  7. Type history | grep cat. The grep command searches the history output for any commands that contain the text cat. Note the command number of one of the cat commands you have previously used.

  8. Type !nn, where nn is replaced by the number you noted in step 7. You’ll see that the last cat command is repeated.

  9. Close this terminal by typing exit.

  10. From the remaining terminal window, type history -c. This wipes all history that is currently in memory. Close this terminal session as well.

  11. Open a new terminal session and type history. It may be a bit unexpected, but you’ll see a list of commands anyway. That is because history -c clears the in-memory history, but it does not remove the .bash_history file in your home directory.

Bash Completion

Another useful feature of the Bash shell is command-line completion. This feature helps you in finding the command you need, and it also works on variables and filenames. If you have installed the bash-completion software package, it works for some of the more complex commands as well.

Bash completion is useful when working with commands. Just type the beginning of a command and press the Tab key on your computer’s keyboard. If there is only one option for completion, Bash will complete the command automatically for you. If there are several options, you need to press Tab once more to get an overview of all the available options. In Exercise 2-4, you learn how to work with these great features.

Exercise 2-4 Using Bash Completion

  1. Still from a user shell, type gd and press Tab. You’ll see that nothing happens.

  2. Press Tab again. Bash now shows a short list of all commands that start with the letters gd.

  3. To make it clear to Bash what you want, type i (so that your prompt at this point shows the command gdi). Press Tab again. Bash now knows what you want and opens gdisk for you. Press Enter to close the prompt that was just opened.

  4. Use cd /etc to go to the /etc directory.

  5. Type cat pas and press Tab. Because there is one file only that starts with pas, Bash knows what to do and automatically completes the filename. Press Enter to execute the command.

Editing Files with vim

Managing Linux often means working with files. Most things that are configured on Linux are configured through files. To complete administrative tasks, you often need to change the contents of a configuration file with a text editor.

Over the years, many text editors have been created for Linux. One editor really matters, though, and that is vi. Even if some other text editors are easier to use, vi is the only text editor that is always available. That is why as a Linux administrator you need to know how to work with vi. One common alternative is vim, or “vi improved”; it is a complete rewrite of vi with a lot of enhancements that make working with vi easier, such as syntax highlighting for many configuration files, which makes it easy to recognize typing errors that you have made. Everything that you learn in this section about vim works on vi as well.

An important concept when working with vim is that it uses different modes. Two of them are particularly important: command mode and input mode. These modes often cause confusion because in command mode you can just enter a command and you cannot change the contents of a text file. To change the contents of a text file, you need to get to input mode.

The challenge when working with vim is the vast number of commands that are available. Some people have even produced vim cheat sheets, listing all available commands. Do not use them. Instead, focus on the minimal number of commands that are really important. Table 2-4 summarizes the most essential vim commands. Use these (and only these) and you’ll do fine on the RHCSA exam.

Tip

Do not try to work with as many commands as possible when working with vim. Just use a minimal set of commands and use them often. You’ll see; you’ll get used to these commands and remember them on the exam. Also, you may like the vimtutor command. (Use yum install vim-enhanced to install it.) This command opens a vim tutorial that has you work through some nice additional exercises.

Key topic

Table 2-4 vim Essential Commands

vim Command

Explanation

Esc

Switches from input mode to command mode. Press this key before typing any command.

i, a

Switches from command mode to input mode at (i) or after (a) the current cursor position.

o

Opens a new line below the current cursor position and goes to input mode.

:wq

Writes the current file and quits.

:q!

Quits the file without applying any changes. The ! forces the command to do its work. Only add the ! if you really know what you are doing.

:w filename

Writes the current file with a new filename.

dd

Deletes the current line.

yy

Copies the current line.

p

Pastes the current selection.

v

Enters visual mode, which allows you to select a block of text using the arrow keys. Use d to cut the selection or y to copy it.

u

Undoes the last command. Repeat as often as necessary.

Ctrl-r

Redoes the last undo.

gg

Goes to the first line in the document.

G

Goes to the last line in the document.

/text

Searches for text from the current cursor position forward.

?text

Searches for text from the current cursor position backward.

^

Goes to the first position in the current line.

$

Goes to the last position in the current line.

!ls

Adds the output of ls (or any other command) in the current file.

:%s/old/new/g

Replaces all occurrences of old with new.

Note that you know the most essential commands for working with vim. Exercise 2-5 gives you the opportunity to test them.

Exercise 2-5 vim Practice

  1. Type vim ~/testfile. This starts vim and opens a file with the name testfile in ~, which represents your current home directory.

  2. Type i to enter input mode and then type the following text:

    cow
    sheep
    ox
    chicken
    snake
    fish
    oxygen
  3. Press Esc to get back to command mode and type :w to write the file using the same filename.

  4. Type :3 to go to line number 3.

  5. Type dd to delete this line.

  6. Type dd again to delete another line.

  7. Type u to undo the last deletion.

  8. Type o to open a new line.

  9. Enter some more text at the current cursor position:

    tree
    farm
  10. Press Esc to get back into command mode.

  11. Type :%s/ox/OX/g.

  12. Type :wq to write the file and quit. If for some reason that does not work, use :wq!.

Understanding the Shell Environment

When you are working from a shell, an environment is created to ensure that all that is happening is happening the right way. This environment consists of variables that define the user environment, such as the $PATH variable discussed earlier. In this section, you get a brief overview of the shell environment and how it is created.

Understanding Variables

The Linux shell environment consists of many variables. Variables are fixed names that can be assigned dynamic values. An example of a variable is $LANG, which in my shell is set to en_US.UTF-8. This value (which may differ on your system) ensures that I can work in the English language using settings that are common in the English language (think of how date and time are displayed).

The advantage for scripts and programs of working with variables is that the program only has to use the name of the variable without taking interest in the specific value that is assigned to the variable. Because different users have different needs, the variables that are set in a user environment will differ. To get an overview of the current variables defined in your shell environment, type the env command, which will show environment variables. Example 2-1 shows some lines of the output of this command.

Example 2-1 Displaying the Current Environment

[user@server1 ~]$ env
MAIL=/var/spool/mail/user
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/
  user/.local/bin:/home/user/bin
PWD=/home/user
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/user
LOGNAME=user
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/bin/env
OLDPWD=/etc

As you can see from Example 2-1, to define a variable, you type the name of the variable, followed by an equal sign (=) and the value that is assigned to the specific variable. To read the value of a variable, you can use the echo command (among others), followed by the name of the variable, as in echo $PATH, which reads the current value of the $PATH variable and prints that on the STDOUT. For now, you do not have to know much more about variables. You can read about more advanced use of variables in Chapter 19, “An Introduction to Bash Shell Scripting.”

Recognizing Environment Configuration Files

When a user logs in, an environment is created for that user automatically. This happens based on four different configuration files where some script code can be specified and where variables can be defined for use by one specific user:

  • /etc/profile: This is the generic file that is processed by all users upon login.

  • /etc/bashrc: This file is processed when subshells are started.

  • ~/.bash_profile: In this file, user-specific login shell variables can be defined.

  • ~/.bashrc: In this user-specific file, subshell variables can be defined.

As you have seen, in these files a distinction is made between a login shell and a subshell. A login shell is the first shell that is opened for a user after the user has logged in. From the login shell, a user may run scripts, which will start a subshell of that login shell. Bash allows for the creation of a different environment in the login shell and in the subshell, but to make sure the same settings are used in all shells, it’s a good idea to include subshell settings in the login shell as well.

Using /etc/motd and /etc/issue

To display messages during the login process, Bash uses the /etc/motd and the /etc/issue files. Messages in /etc/motd display after a user has successfully logged in to a shell. (Note that users in a graphical environment do not see its contents after a graphical login.) Using /etc/motd can be a convenient way for system administrators to inform users about an issue or a security policy, for example.

Another way to send information to users is by using /etc/issue. The contents of this file display before the user logs in. This provides an excellent means of specifying specific login instructions to users who are not logged in yet.

In Exercise 2-6, you can review the topics that have been discussed in this section.

Exercise 2-6 Managing the Shell Environment

  1. Open a shell in which you are user user.

  2. Type echo $LANG to show the contents of the variable that sets your system keyboard and language settings.

  3. Type ls --help. You’ll see that help about the ls command is displayed in the current language settings of your computer.

  4. Type LANG=es_ES.UTF-8. This temporarily sets the language variable to Spanish.

  5. Type ls --help again. You’ll see that now the ls help text is displayed in Spanish.

  6. Type exit to close your terminal window. Because you have not changed the contents of any of the previously mentioned files, when you open a new shell, the original value of the LANG variable will be used.

  7. Open a shell as user again.

  8. Verify the current value of the LANG variable by typing echo $LANG.

  9. Type vim .bashrc to open the .bashrc configuration file.

  10. In this file, add the line COLOR=red to set a variable with the name COLOR and assign it the value red. Notice that this variable doesn’t really change anything on your system; it just sets a variable.

  11. Close the user shell and open a new user shell.

  12. Verify that the variable COLOR has been set, by using echo $COLOR. Because the .bashrc file is included in the login procedure, the variable is set after logging in.

Finding Help

On an average Linux system, hundreds of commands are available—way too many to ever be able to remember all of them, which is why using the help resources on your computer is so very important. The man command is the most important resource for getting help about command syntax and usage. Apart from that, you can show a compact list of command options using command --help.

Using --help

The quickest way to get an overview of how to use a command is by running the command with the --help option. Nearly all commands will display a usage summary when using this option. The list of options that is shown in this way is of use mainly when you already have a generic understanding of how to use the command and need a quick overview of options available with the command—it doesn’t give detailed information that will help users who don’t know the command yet.

Tip

Nearly all commands provide a short overview of help when the option --help is used. Some commands do not honor that option and consider it erroneous. Fortunately, these commands will be so friendly as to show an error message, displaying valid options with the command, which effectively means that you’ll get what you needed anyway.

Using man

When using the Linux command line, you will at some point consult man pages. The man command is what makes working from the command line doable. If you do not know how a command is used, the man page of that command will provide valuable insight. This section covers a few man essentials.

To start with, the most important parts of the man page in general are at the bottom of the man page. Here you’ll find two important sections: In many cases there are examples; if there are no examples, there is always a “See Also” section. The topics you find here are related man pages, which is useful if you have just not hit the right man page. To get to the bottom of the man page as fast as possible, use the G command. You can also type /example to search the man page for any examples. Figure 2-1 shows what the end of a man page may look like.

A screenshot shows the end of the results displayed when the command 'man' is used. The commands to create different types of LV are listed. Also given are keywords groups. A few of the keywords are: lvm, lvm.conf, and lvmconfig.

FIGURE 2-1 Sample man Page Contents

Finding the Right man Page

To find information in man pages, you can search the mandb database by using apropos or man -k. If the database is current, getting access to the information you need is easy. Just type man -k, followed by the keyword you want to search for. This command looks in the summary of all man pages that are stored in the mandb database. Example 2-2 shows a partial result of this command.

Example 2-2 Searching man Pages with man –k

[root@server1 ~]# man -k partition
addpart (8)       - simple wrapper around the "add partition" ioctl
cfdisk (8)        - display or manipulate disk partition table
cgdisk (8)        - Curses-based GUID partition table (GPT)
                      manipulator
delpart (8)       - simple wrapper around the "del partition" ioctl
fdisk (8)         - manipulate disk partition table
fixparts (8)      - MBR partition table repair utility
gdisk (8)         - Interactive GUID partition table (GPT) manipulator
iostat (1)        - Report Central Processing Unit (CPU) statistics
                      and in...
kpartx (8)        - Create device maps from partition tables
mpartition (1)    - partition an MSDOS hard disk
os-prober (1)     - Discover bootable partitions on the local system
partprobe (8)     - inform the OS of partition table changes
partx (8)         - tell the Linux kernel about the presence and
                      numbering...
pvcreate (8)      - initialize a disk or partition for use by LVM
pvresize (8)      - resize a disk or partition in use by LVM2
resizepart (8)    - simple wrapper around the "resize partition" ioctl
sfdisk (8)        - partition table manipulator for Linux
sgdisk           (- Command-line GUID partition table (GPT)
                      manipulator fo...
systemd-efi-boot-generator (8) - Generator for automatically mounting
                                   the EFI...
systemd-gpt-auto-generator (8) - Generator for automatically
                                   discovering and ..

Based on the information that man -k is giving you, you can probably identify the man page that you need to access to do whatever you want to accomplish. Be aware, however, that man -k is not perfect; it searches only the short summary of each command that is installed. If your keyword is not in the summary, you’ll find nothing and get a “nothing appropriate” error message.

Tip

Instead of using man -k, you can use the apropos command, which is equivalent to man -k.

When using man -k to find specific information from the man pages, you’ll sometimes get a load of information. If that happens, it might help to filter down the results a bit by using the grep command. But if you want to do that, it is important that you know what you are looking for.

Man pages are categorized in different sections. The most relevant sections for system administrators are as follows:

Key topic
  • 1: Executable programs or shell commands

  • 5: File formats and conventions

  • 8: System administration commands

There are also sections that provide in-depth details about your Linux system, such as the sections about system calls and library calls. When using man -k, you’ll get results from all of these sections. To limit the results that display, it makes sense to use grep to show only those sections that are relevant for what you need. So, if you are looking for the configuration file that has something to do with passwords, use man -k password | grep 5, or if you are looking for the command that an administrator would use to create partitions, use man -k partition | grep 8.

Another useful man option is -f. The command man -f <somecommand> displays a short description of the item as found in the mandb database. This may help you when deciding whether this man page contains the information you are looking for.

Updating mandb

As previously mentioned, when using the man -k command, the mandb database is consulted. This database is automatically created through a cron scheduled job. Occasionally, you might look for something that should obviously be documented but all you get is the message “nothing appropriate.” If that happens, you might need to update the mandb database. Doing that is easy: Just run the mandb command as root without any arguments. It will see whether new man pages have been installed and update the mandb database accordingly.

Tip

Do not try to memorize all the commands that you need to accomplish specific tasks. Instead, memorize how to find these commands and find which man page to read to get more information about the command. In Exercise 2-7, you’ll see how that works.

Assume that you are looking for a command, using man -k, but all you get is the message “nothing appropriate” and you do not remember how to fix it. Exercise 2-7 shows what you can do in such cases.

Exercise 2-7 Using man -k

  1. Because man -k does not give the expected result, it makes sense to look in the man page for the man command for additional information about man -k. Type man man to open the man page of man. Once in the man page, type /-k to look for a description of the -k option. Type n a few times until you get to the line that describes the option. You’ll see that man -k is equivalent to apropos and that you can read the man page of apropos for more details. So type q to exit this man page.

  2. Type man apropos and read the first paragraphs of the description. You’ll see that the database searched by apropos is updated by the mandb program.

  3. Type man mandb. This man page explains how to run mandb to update the mandb database. As you’ll read, all you need to do is type mandb, which does the work for you.

  4. Type mandb to update the mandb database. Notice that you won’t see many man pages being added if the mandb database was already quite accurate.

Using info

Apart from the information that you’ll find in man pages, another system provides help about command usage. This is the info system. Most commands are documented in man pages, but some commands have their main documentation in the info system and only show a short usage summary in the man page. If that is the case, the “See Also” section of the man page of that command will tell you that “The full documentation for...is maintained as a Texinfo manual.” You then can read the Info page using the command pinfo or info. Both commands work, but in pinfo, special items such as menu items are clearly indicated, which is why using pinfo is easier.

When working with info, take a look at the top line of the viewer. This shows the current position in the info document. Particularly interesting are the Up, Next, and Previous indicators, which tell you how to navigate. Info pages are organized like web pages, which means that they are organized in a hierarchical way. To browse through that hierarchy, type n to go to the next page, p to go to the previous page, or u to move up in the hierarchy.

In an info page, you’ll also find menus. Each item that is marked with an asterisk (*) is a menu item. Use the arrow keys to select a specific menu item. This brings you down one level. To get back up again, type u. This brings you back to the original starting point in the pinfo hierarchy. Figure 2-2 shows what an info page looks like.

A screenshot shows the result displayed when using the command 'pinfo.' A description is given under the heading "Introduction," and a link to report bugs- "<bug-coreutils@gnu.org>" is also mentioned.

FIGURE 2-2 Getting More Command Usage Information Using pinfo

Exercise 2-8 shows an example of such a command, and in this exercise you learn how to get the information out of the info page.

Exercise 2-8 Using info

  1. Type man ls. Type G to go to the end of the man page and look at the “See Also” section. It tells you that the full documentation for ls is maintained as a Texinfo manual. Quit the man page by pressing q.

  2. Type pinfo coreutils 'ls invocation'. This shows the information about ls usage in the pinfo page. Read through it and press q when done.

Using /usr/share/doc Documentation Files

A third source of information consists of files that are sometimes copied to the /usr/share/doc directory. This happens in particular for services and larger systems that are a bit more complicated. You will not typically find much information about a command like ls, but some services do provide useful information in /usr/share/doc.

Some services store very useful information in this directory, like rsyslog, bind, Kerberos, and OpenSSL. For some services, even example files are included. One example of these services is VDO, which is covered in more detail in Chapter 15, “Managing Advanced Storage.”

Summary

In this chapter, you read about essential Linux administration tasks. You learned about some of the important shell basics, such as I/O redirection, working with history, and management of the environment. You also learned how to edit text files with the vim editor. In the last part of this chapter, you learned how to find information using man and related commands.

Exam Preparation Tasks

As mentioned in the section “How to Use This Book” in the Introduction, you have several choices for exam preparation: the end-of-chapter labs; the memory tables in Appendix B; Chapter 26, “Final Preparation”; and the practice exams.

Review All Key Topics

Review the most important topics in the chapter, noted with the Key Topic icon in the outer margin of the page. Table 2-5 lists a reference of these key topics and the page number on which each is found.

Key topic

Table 2-5 Key Topics for Chapter 2

Key Topic Element

Description

Page

Table 2-4

vim essential commands

40

List

Significant sections in man

47

Complete Tables and Lists from Memory

Print a copy of Appendix B, “Memory Tables” (found on the companion website), or at least the section for this chapter, and complete the tables and lists from memory. Appendix C, “Memory Tables Answer Key,” includes completed tables and lists to check your work.

Define Key Terms

Define the following key terms from this chapter and check your answers in the glossary:

shell

bash

internal command

external command

$PATH

variable

STDIN

STDOUT

STDERR

file descriptor

pipe

redirect

device files

environment

login shell

subshell

Review Questions

The questions that follow are meant to help you test your knowledge of concepts and terminology and the breadth of your knowledge. You can find the answers to these questions in Appendix A.

1. What is a variable?

2. Which command enables you to find the correct man page based on keyword usage?

3. Which file do you need to change if you want a variable to be set for every shell that is started?

4. When analyzing how to use a command, you read that the documentation is maintained with the Techinfo system. How can you read the information?

5. What is the name of the file where Bash stores its history?

6. Which command enables you to update the database that contains man keywords?

7. How can you undo the last modification you have applied in vim?

8. What can you add to a command to make sure that it does not show an error message, assuming that you do not care about the information that is in the error messages either?

9. How do you read the current contents of the $PATH variable?

10. How do you repeat the last command you used that contains the string dog somewhere in the command?

End-of-Chapter Lab

You have now learned about some of the most important basic skills that a Linux administrator should have. In this section, you apply these skills by doing an end-of-chapter lab.

Lab 2.1

1. Modify your shell environment so that on every subshell that is started, a variable is set. The name of the variable should be COLOR, and the value should be set to red. Verify that it is working.

2. Use the appropriate tools to find the command that you can use to set the system time 1 minute ahead.

3. From your home directory, type the command ls -al wergihl * and ensure that errors as well as regular output are redirected to a file with the name /tmp/lsoutput.

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

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