Professional Documents
Culture Documents
Quick Tour
Table of Contents
1. Logging In ................................................................................................................... 4 Discussion .............................................................................................................. 4 Starting Linux Sessions ..................................................................................... 4 Virtual Consoles .............................................................................................. 4 The X Graphical Environment ............................................................................ 5 Terminals in the Graphical environment ............................................................... 6 Logging out from the graphical environment ......................................................... 8 Using the who command to determine who's on the system. ..................................... 9 Logging into a machine over the network ............................................................. 9 Examples .............................................................................................................. 10 Example 1. Using virtual consoles ...................................................................... 10 Online Exercises .................................................................................................... 11 Online Exercise 1. Using multiple virtual consoles ................................................. 11 Specification .......................................................................................... 11 Deliverables ........................................................................................... 12 Questions .............................................................................................................. 12 2. The Kernel, Programs, and Processes ............................................................................... 14 Discussion ............................................................................................................. 14 The Linux Kernel ........................................................................................... 14 Programs ....................................................................................................... 15 Processes ...................................................................................................... 15 Shells ........................................................................................................... 16 The ps command ............................................................................................ 16 Questions .............................................................................................................. 17 The kernel, programs, and processes .................................................................. 17 3. Examining the Filesystem .............................................................................................. 19 Discussion ............................................................................................................. 19 The Naming of Files and Directories .................................................................. 19 Listing directory contents with ls ....................................................................... 19 Viewing the contents of a file with cat ............................................................... 20 Redirecting command output to files .................................................................. 20 Permissions, and a user's home directory. ............................................................ 21 Examples .............................................................................................................. 21 Example 1. Redirecting command output to a file. ................................................. 21 Online Exercises .................................................................................................... 22 Online Exercise 1. The ls Command ................................................................... 22 Specification .......................................................................................... 22 Deliverables ........................................................................................... 22 Possible Solution .................................................................................... 22 Questions .............................................................................................................. 22 Multiple Choice Questions. .............................................................................. 22 4. Running Commands ..................................................................................................... 24 Discussion ............................................................................................................. 24 Command Line Grammar ................................................................................. 24 Commands .................................................................................................... 24 Command Line Switches ................................................................................. 25 Short Command Line Switches ................................................................. 25 Multiple Short Command Line Switches ..................................................... 25 Long Command Line Switches .................................................................. 26 Arguments ..................................................................................................... 26 Getting Help: Usage ........................................................................................ 27
Quick Tour
Examples .............................................................................................................. 27 Example 1. Learning to Use the cat Command ...................................................... 27 Questions .............................................................................................................. 29 Command Line Syntax .................................................................................... 29 5. Managing Terminals ..................................................................................................... 32 Discussion ............................................................................................................. 32 Controlling Terminals ...................................................................................... 32 Resetting Terminals ........................................................................................ 32 Terminal control sequences .............................................................................. 32 Identifying terminals ....................................................................................... 35 Tab Completion ............................................................................................. 36 Examples .............................................................................................................. 37 Example 1. Resetting the terminal after viewing a binary file. ................................... 37 Example 2. Canceling a command ...................................................................... 37 Online Exercises .................................................................................................... 38 Online Exercise 1. Suspending terminal output ...................................................... 38 Specification .......................................................................................... 38 Deliverables ........................................................................................... 38 Cleaning Up .......................................................................................... 38 Online Exercise 2. Sorting your socks. ................................................................ 38 Specification .......................................................................................... 38 Deliverables ........................................................................................... 39 Questions .............................................................................................................. 39 6. Getting Help ............................................................................................................... 42 Discussion ............................................................................................................. 42 Getting Help .................................................................................................. 42 Obtaining Usages with -h, -?, and --help ............................................................. 42 Man Pages .................................................................................................... 42 The less Pager ....................................................................................... 43 Man Chapters ........................................................................................ 43 Keyword Searches, and the -a Switch. ........................................................ 43 Info Pages ..................................................................................................... 44 The /usr/share/doc Directory ............................................................................. 44 Red Hat Documentation Guides ........................................................................ 45 The Linux Documentation Project ..................................................................... 45 yelp: The Gnome Help Browser ........................................................................ 46 Questions .............................................................................................................. 48
Chapter 1. Logging In
Key Concepts
In Linux, user sessions are started by "logging in" to the machine. To login, you must have already been provided a username and password. If a Linux machine is running an X server, users may login using the graphical interface. Users may login on any of six virtual consoles. The CTRL+ALT+F2 through CTRL+ALT+F6 key sequences switch between the six virtual consoles. The CTRL+ALT+F1 key sequence switches to the default X server. If a Linux machine supports it, users can login in over the network using a remote shell. The who command lists which users are currently logged in, and how they logged on.
Discussion
Starting Linux Sessions
The Unix operating system, which was originally developed in the early 1970s, predates the days of personal computers. Originally, many users would simultaneously use Unix running on a mainframe computer. Rather than each user having a personal computer, they would use what is (these days) commonly called a "dumb terminal" - really just a keyboard and text monitor, connected to the mainframe computer with a serial line connection. In order to identify oneself to the Unix operating system, a user would first have to "log on" by providing a userid and password. Although Linux (and other versions of Unix) has grown to take advantage of affordable personal computers, it has not lost the core concepts of users, passwords, logging on, and text based terminals. This lesson will describe various ways to start an interactive session with a Linux system.
Virtual Consoles
Red Hat Enterprise Linux provides six "virtual consoles" that are available to users physically at the computer. The virtual consoles act like "dumb terminals". The virtual consoles are accessed using the CTRL+ALT+F2 through CTRL+ALT+F6 key sequences, with one virtual console mapped to each of the first 6 function keys. (If you are in a graphical environment, you should know that CTRL+ALT+F1 will return you to it before trying to switch to a virtual console.) When starting a session on a Linux machine using a virtual console, the screen will look something like the following:
Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64 station login:
Logging In
To login, a user types their username, with no spaces, and hits the RETURN key. Next, the user is prompted for their password, which is not echoed to the screen as the user types it, again followed by RETURN. Once successfully logged in, the user is greeted with a shell prompt.
Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64 station login: elvis Password: [elvis@station elvis]$
The rest of this course is about what can be done from the shell prompt. For now, we are simply going to learn how to end a session, by typing "exit", and hitting RETURN. The virtual console should return to the original login screen. Why would users want to use a virtual console instead of the graphical environment? First, the machine might not have enough memory or hardware to support the graphical environment, and the virtual consoles might be all that are available. Even on modern machines, however, the virtual consoles are often a much quicker and more efficient interface for experienced users. Because they are implemented by the Linux kernel directly, they are also useful in debugging systems where the graphics might not be working. Lastly, for network servers that usually spend their lives in locked closets, administrators often want to avoid the complexity that the graphical interface adds to the system.
Logging In
A user logs on by clicking on their username then typing their password followed by RETURN. If their username is not listed, the user can click "other" then log on by typing their username, followed by RETURN, and their password, again followed by RETURN.
Logging In
Experienced Linux users often prefer the versatility and power of the command line interface over graphical applications. Akin to a virtual console, the terminal provides a shell command line interface. Because the user has already logged in to start the graphical session, he does not need to login again when opening new terminal.
Logging In
When finished, the gnome-terminal can be closed by typing the exit command (followed by RETURN), or left clicking in the "go away box" found in the upper right-hand corner of the terminal.
Logging In
The first column lists the users who have logged on, and the last few columns the time that they logged on. With a little experience, the second column tells where the user logged on. For the first two users, elvis, and root, tty2 and tty3 refers to virtual consoles number 2 and 3, respectively. The first entry for blondie on tty1 with the :0, refers to the X server itself running on the first virtual console, and pts/0 refers to the first terminal opened within the X server. Without getting too bogged down in the details, we can see that elvis is logged on the second virtual console, root on the third, and blondie has logged in using the graphical environment.
Logging In
virtual console. In the following example, elvis will login to a virtual console on the local machine, called station.redhat.com. He will then use ssh to login to the remote machine nimbus.example.com, potentially halfway around the world. Pay careful attention to the shell prompt in the following example. Because Linux users are often "hopping" from machine to machine using remote shells, the prompt has been designed to help the user keep straight what machine the shell is running on.
Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64 station login: elvis Password: Last login: Thu Apr 3 13:03:06 from hedwig [elvis@station elvis]$ who elvis tty2 May 3 07:48 [elvis@station elvis]$ ssh elvis@nimbus.example.com elvis@nimbus.example.com's password: Last login: Thu May 1 17:38:43 2003 from station.redhat.com [elvis@nimbus elvis]$ who elvis pts/1 May 3 11:59 (station.redhat.com) [elvis@nimbus elvis]$ exit Connection to nimbus.example.com closed. [elvis@station elvis]$ exit
Note that when elvis ran the who command on the remote machine, it reported the machine that he logged in from, namely station.redhat.com. There are ssh clients for operating systems other than Linux. For example, the Open Source PuTTY [http://www.chiark.greenend.org.uk/~sgtatham/putty/] application can be installed on almost any windows machine, and used to open a shell into a remote Linux server.
Examples
Using virtual consoles
In the following example, a user logs on the second virtual console as the user elvis, but then realizes his system is mis-configured. In order to fix the misconfiguration, he is going to switch to the third virtual console, and log in as the administrative user root and fix the problem. He will then exit, switch back to the second virtual console, and be on his way.
Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64 station login: elvis Password: Last login: Mon May 5 15:07:16 on tty2 [elvis@station elvis]$ cat /tmp/README cat: /tmp/README: Permission denied [elvis@station elvis]$ who elvis tty2 May 5 15:54
At this point, elvis has tried to read the file /tmp/README, but found out the file's permissions did not let him. Because the user knows the password to the root (administrator's) account on the machine, he can fix the problem by switching to another virtual console (using the CTRL+ALT+F3 key sequence), logging in as root, and changing the file's permissions.
10
Logging In
Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64 station login: root Password: Last login: Mon May [root@station root]# root tty3 elvis tty2 [root@station root]# [root@station root]#
5 15:54:18 on tty6 who May 5 15:55 May 5 15:54 chmod go+r /tmp/README exit
Note that the output of the who command shows the user logged on as elvis on the second virtual console, and logged on as root on the third. The user now switches back to the second virtual console using CTRL+ALT+F2, and picks up where he left off.
[elvis@station elvis]$ cat /tmp/README GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software ...
Online Exercises
Using multiple virtual consoles
Lab Exercise
Objective: Learn to manage multiple virtual consoles Estimated Time: 10 mins.
Specification
Your machine should have been configured with multiple accounts, all with the same password. If your username is elvis, for example, you should also have accounts named elvis_a, elvis_b, and elvis_c, all with the same password. For this exercise, you should log into virtual consoles 2-5, using each one of your accounts in order. Virtual Console 2 3 4 5 Username username username_a username_b username_c
11
Logging In
If you have completed the exercise correctly, you should be able to run the who command from any terminal, and see output similar to the following:
[elvis@staion elvis]$ who elvis_a tty3 May elvis tty2 May elvis_b tty4 May elvis_c tty5 May 5 5 5 5 16:18 16:18 16:18 16:18
Deliverables
1. 1. 4 active login sessions, one on each of the first 4 text only virtual consoles.
Questions
1. a. b. c. d. 2. a. b. c. d. 3. a. b. c. d. 4. a. b. c. Which of the following best describes why Linux is referred to as a multiuser operating system? Multiple users can use the same machine simultaneously, using multiple network connections and dumb terminals. Multiple users can use the same machine, but only one at a time. Many people contributed to the development of the Linux operating system. Linux is not referred to as a multiuser operating system.
Which of the following is not an advantage of using virtual consoles instead of a graphical interface? Virtual consoles often respond more quickly than graphical environments. Virtual consoles operate at the kernel level, and so can be used to debug the system when higher level components are mis-configured. Virtual consoles are very intuitive, and help is available in an obvious manner. Graphical environments add a lot of complexity to a system, and can make them harder to maintain.
From the fourth virtual console, which key sequence(s) will take you to the second virtual console? CTRL+ALT+2 CTRL+ALT+F2 CTRL+2 (using the numeric keypad) None of the above.
From the X graphical environment, which key sequence(s) will take you to the second virtual console? ALT+F2 CTRL+ALT+F2 Both a and b.
12
Logging In
d. 5.
If a user has logged into the X graphical environment, but desires to use a terminal command line interface, how might she go about obtaining a terminal? a. b. c. d. Using the CTRL+ALT+F3 key-sequence to switch to a virtual console. Clicking the terminal icon that appears in the bottom panel. Right click in the desktop, and choose "Open in Terminal" from the pop-up menu. Both a and c
6. a.
What does the who command report? The users who have logged onto the machine since midnight. The users who are currently logged onto the machine, and where they logged on from. The users who are currently logged onto the machine, and how much time they have remaining before their session expires. The command is used to lookup users from an Internet database of all users.
b. c. d. 7. a. b. c. d.
Which of the following is NOT required in order to open a remote shell using ssh? The remote machine must be running the ssh service. You must know the username and password of an account on the machine. The local machine must be a Linux machine. You must know the hostname or IP address of the remote machine.
13
Discussion
The Linux Kernel
The Linux kernel is an operating system, just as Windows, MacOS, and OS/2 are operating systems. The operating system is an environment in which programs run, providing mechanisms for programs to interact with the computer hardware. The kernel lets programs know when a keyboard key was struck, when the mouse moved, or when a packet destined for the program arrived over the network. The kernel allows programs to access the harddrive, perhaps to read a configuration files or store data. Just as it would be difficult to explain what a language is to people who have grown up only knowing about one language, it is often difficult to explain what an operating system is to people who are new to computers, or who have only used one operating system. The operating system operates at such a low level, that users don't interact with it directly, and often don't realize that the programs they are running are using it.
14
Programs
Programs are files that are stored on a computer's harddrive. A program is a series of very basic instructions, that tell the operating system what to do and when to do it in a very detailed and specific manner. The following might be the instructions written down in the file /usr/bin/gcalctool, the GNOME calculator program. "While the user isn't doing anything, blink the cursor. When the user hits a key, if it's a number, write it to the screen. If it's not a number, ignore it, unless it's the return key. If it's the return key, add the previous number to the total, and print the total." Of course, the instructions are not written in English, or any other human readable language. Instead, a computer, at the lowest level, only knows how to perform a small number of tasks, say 256 of them. Imagine that each of these tasks were numbered, task number 0 through task number 255. Programs really look more like the following instructions: "Do task 23. If the result is greater than 0, do task 45. Otherwise, do task number 82. Take the result, and store it into memory." Because the tasks that a computer can do vary from one type of CPU to another, and because different operating systems can be thought of as numbering the tasks differently, programs that are compiled for one type of operating system will generally not run on other operating systems.
Processes
When a user instructs the kernel to run a program, the kernel reads the instructions found in the program file, and loads them into memory. It then begins performing the instructions. The copy of the program running in memory is called a process. Note that the same program can be loaded into memory and run more than once, so that at any point the kernel might be running several processes which are instances of the same program. Linux, like most modern operating systems, is a multitasking operating system. This means that the kernel gives the appearance of running multiple processes simultaneously. In reality, the kernel is running a single
15
process for a brief amount of time, usually on the order of 50 milliseconds. It then swaps that process out, and swaps in another one for its 50 milliseconds of action. Eventually, the kernel works its way through all of the processes and starts over again. The rapid switching from process to process gives the users of the machine the impression that all of the processes are running simultaneously.
Shells
Linux users often use a special type of program, called a shell, to interact with the kernel. The shell runs within a terminal, issuing a prompt and waiting to be told what to do. A user then asks the shell to run a program by typing in the name of the program. In the following example, the user elvis asks the shell to run a simple calendar program called cal. The shell dutifully executes the program, and reports the program's output back to the terminal:
[elvis@station elvis]$ cal September 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [elvis@station elvis]$
Because users use a shell to perform almost every task on the system, the shell is an important and sophisticated program. Much of this course will be devoted to learning how to use the shell to perform tasks effectively. When the user asks the shell to run a program, the user is said to be specifying a command for the shell. The shell is often referred to as a Command Line Interface.
The ps command
The ps command is commonly used to list processes running on a system. The ps command, without arguments, shows all of the processes that were started from a single terminal (or terminal window in a graphical environment). In the following example, elvis discovers that his terminal currently has two processes running: his bash shell, and the ps command itself.
[elvis@station elvis]$ ps PID TTY TIME CMD 1378 pts/1 00:00:00 bash 1418 pts/1 00:00:00 ps
The first column shows the process's PID, or Process ID. Every process running on the system is identified by a unique PID. The second column identifies elvis's terminal, and the third column reports how much CPU time the process has consumed. The ps aux command shows a detailed list of all processes that are currently running on the system. For now, we will not go into the details of what "aux" stands for, just consider it black magic to be memorized. Neither will we give a detailed discussion of all of the following fields. All of this will be addressed in a following workbook. Simply realize that at any given moment, there are usually a large number of processes running on a Linux system. (Note that in the following output, many lines have been removed and replaced with "...").
[elvis@station elvis]$ ps aux USER PID %CPU %MEM VSZ root 1 0.0 0.1 19328 root 2 0.0 0.0 0 root 3 0.0 0.0 0 RSS 1476 0 0 TTY ? ? ? STAT Ss S S START 08:44 08:44 08:44 TIME 0:00 0:00 0:00 COMMAND /sbin/init [kthreadd] [migration/0]
16
... root rpc dbus root root root rpcuser root root root root root ... root root postfix root root root root root root root ... elvis elvis
1124 1166 1191 1202 1207 1234 1237 1244 1275 1282 1292 1317 1415 1495 1502 1506 1514 1525 1542 1555 1562 1564 3599 3628
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0
0.3 248680 0.0 18952 0.2 98024 0.5 97408 0.2 55832 0.1 9100 0.1 23124 0.0 39328 0.0 0 0.0 29440 0.3 189016 0.0 4068 0.1 63944 0.3 78544 0.3 78692 0.4 262256 0.1 117168 0.0 21412 0.0 4048 0.2 121468 0.0 4052 0.0 4052 0.1 108324 0.1 108048
3392 856 2072 5152 2296 1520 1160 600 0 540 3288 628 1072 3148 3156 4440 1340 464 316 2060 520 516
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tty2 tty3
08:44 08:44 08:44 08:44 08:44 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 08:45 12:53 12:55
0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00
/sbin/rsyslogd rpcbind dbus-daemon NetworkManager /usr/sbin/modem /sbin/dhclient -d -4 -sf /us rpc.statd /usr/sbin/wpa_supplicant -c [rpciod/0] rpc.idmapd cupsd -C /etc/cups/cupsd.conf /usr/sbin/acpid /usr/sbin/sshd /usr/libexec/postfix/master qmgr -l -t fifo -u /usr/sbin/abrtd crond /usr/sbin/atd /usr/bin/rhsmcertd 240 /usr/sbin/gdm-binary -nodaemon /sbin/mingetty /dev/tty2 /sbin/mingetty /dev/tty3
Questions
The kernel, programs, and processes
1. a. b. c. d. 2. a. b. c. d. 3. a. b. c. d. Which of the following is not considered an operating system? Red Hat Enterprise Linux Windows XP Microsoft Office Mac OS X
Which of the following is not true for the Linux operating system? Multiple processes appear to be running at the same time. Only one instance of any given program may be running as a process. Programs are stored as files in the filesystem. Only one instance of the kernel may be running at any given time.
Which of the following is a true statement? Only the root user may run processes from a shell. The shell is a process that is commonly used to execute other processes. The shell is the kernel component that interacts directly with hardware. Only one instance of a shell may be running as a process.
17
4. a.
Which of the following is the default shell for Red Hat Enterprise Linux? /bin/bash /bin/tcsh /bin/zsh /bin/sh
b. c. d. 5. a. b. c. d.
Which of the following is the command for listing processes? ps lsps lps ls
18
Discussion
The Naming of Files and Directories
In Linux, information and programs are stored on disks as files. Files are grouped into directories, which can contain files and other directories. (Other operating systems often refer to directories as "folders"). This hierarchy of directories containing directories is often referred to as the "directory tree". The base of the directory tree is a directory named simply /, and referred to as "the root directory". Files within the root directory can be referred to as /filename. In Red Hat Enterprise Linux, the root directory contains mostly other directories, such as /etc. Files within these subdirectories may be referred to as / etc/filename, and the process continues for each new level of subdirectories. For example, the file network found in the directory sysconfig, which is in turn found in the directory etc, which is in the root directory /, can be referred to as /etc/sysconfig/network. Obviously, starting at the root directory every time you refer to a file is a lot of work. Fortunately, Linux provides an easier way. Every process, including a user's shell, uses a "current working directory" for context. Files in a process's current working directory can be referred to as simply filename, without the leading slash. Files in subdirectories of the current working directory can be referred to as dirname/ filename, again without the leading slash. For example, if a process's current working directory were / etc, the network file referred to above could be referred to as sysconfig/network. If the working directory was /etc/sysconfig then the file could simply be referred to as network. In summary, there are always two ways to refer to a file. File references relative to the root directory always start with a leading /, and are called absolute references. File references relative to the current working directory start with something other than a /, and are referred to as relative references.
19
The ls command, when called without arguments (i.e., without specifying a directory), lists the contents of the shell's current working directory. If using a color terminal, the ls command also colorizes the filenames to help distinguish which of the directory contents are regular files (white), and which are directories (blue). The ls command is a very flexible command that can provide a lot of different information. It will be discussed in more detail in later lessons.
For now, don't worry about what the contents mean, just realize that the cat command displayed the entire contents of this 5 line file. Note that if you ask cat to display a very long file, or a binary (non text) file, cat will happily comply. There are more sophisticated commands for browsing large files, a screen full at a time, which will be introduced later.
The output of the ls /etc/sysconfig/rhn command was not displayed on the terminal, but instead placed into the newly created file lsout.txt. elvis next takes a ls of his current working directory, and sees the newly created file. He then uses the cat command to observe the contents of the file. In "Unix speak", elvis "redirected the output from the ls command to the file lsout.txt".
20
The user elvis has encountered another common Unix concept, that of file ownerships and permissions. elvis tried to create the new file /etc/lsout.txt, but elvis does not have permissions to create files in the /etc directory. By default, in Red Hat Enterprise Linux, users are not able to create files just anywhere. In fact, there are only a few places where files can be created. Every user has a home directory, where they can create new files (and new subdirectories). Fortunately, when user's log in to a Linux session, their shell uses their home directory as its current working directory. By default in Red Hat Enterprise Linux, a user's home directory is named /home/username, where username is replaced with the user's username. Upcoming workbooks will explore the filesystem and permissions in much more detail. For now, just realize users are generally only allowed to create files in their home directory.
Examples
Redirecting command output to a file.
The user prince wants to use the cal command to store a calendar of the current month into the file calendar.txt.
[prince@station prince]$ cal September 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [prince@station prince]$ cal > calendar.txt [prince@station prince]$ ls calendar.txt [prince@station prince]$ cat calendar.txt September 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
prince first views the output of the cal command directly. prince runs the cal command again, redirecting the output to the file calendar.txt prince confirms that the new file was created by listing the contents of his current working directory (in this case, his home directory). prince examines the contents of the new file, to confirm that it does contain the output of the cal command.
21
Online Exercises
The ls Command
Lab Exercise
Objective: Redirect command output into a newly created file. Estimated Time: 5 mins.
Specification
Use the ls command to list the contents of the /etc directory, redirecting the output of the command into a file called lsetc.txt in your home directory.
Deliverables
1. 1. The file lsetc.txt in your home directory, which contains the output of the command ls /etc.
Possible Solution
The following command provides a possible solution to this exercise.
[student@station student]$ ls /etc > lsetc.txt
Questions
Multiple Choice Questions.
1. a. b. c. d. 2. Assuming the shell's current working directory is /home/elvis, which of the following would refer to the file /home/elvis/Mail/sent? Mail/sent /Mail/sent sent /sent
As the user elvis, whose shell's current working directory is /home/elvis, which of the following would refer to the file received, which is in the directory Mail, which is in the home directory? a. b. c. d. /home/elvis/Mail/received Mail/received received both A and B.
3.
Which of the following commands would list the files in the /usr/lib directory?
22
a. b. c. d. 4.
Which of the following commands would successfully redirect the output of the cal to the file lsout.txt? a. b. c. d. lsout.txt > cal cal ==> lsout.txt cal > lsout.txt cal } lsout.txt
5. a.
For the user elvis, with /home/elvis for a home directory, which of the following commands would succeed on a default Red Hat Enterprise Linux installation? ls /etc > /etc/lsetc.txt /etc/lsetc.txt < ls /etc ls /etc > /home/elvis/lsetc.txt /home/elvis/lsetc.txt < ls /etc
b. c. d.
23
Discussion
Command Line Grammar
While the command line interface has many advantages, including efficiency and flexibility, simplicity is not one of them. Fortunately, there are conventions that (almost) all programs follow. A little time spent learning these conventions can make learning new programs much easier. If commands entered on the command line were compared to English sentences, the commands could be thought of as having verbs, adverbs, and direct objects. The verb is the command to run, the adverbs are the various command line switches that can be used to modify the behavior of the command (quietly, or verbosely), and any remaining words are the direct objects (what the command is supposed to act on). Again, as in languages, there are irregularities, and for almost every rule mentioned, there will be exceptions.
Commands
The first word on any command is generally the name of a program that lives as a file somewhere in the filesystem. 1 For example, the previous lessons used the ps and who commands. If we want to know which file contains these programs, there is a program called which which can help us. The which command, followed by the name of another command, will tell you "which" file is being run.
[elvis@station elvis]$ which ps /bin/ps [elvis@station elvis]$ which who /usr/bin/who
When running a command, the shell process asks the kernel to execute the specified program as a separate process, and arranges the output (or, more correctly, the standard out stream) of the process to be written to the terminal. The shell then pauses until the command's process terminates. Once the command finishes, the shell prints another prompt, and waits to be told what to do next.
1
Exceptions to this rule include shell internals, shell aliases, and shell functions, which will be handled in the bash shell workbook.
24
Running Commands
The first ls command simply lists the contents of the directory. The second ls -s command, which includes the -s command line switch, gives the sizes of the contents as well. The third ls -l command gives a "long" listing, including all kinds of details about the files, such as ownerships, permissions, and modify times. For now, don't worry about all of the detailed output. This will be covered in an upcoming workbook on using the filesystem. Just note how command line switches are used to modify the basic behavior of the ls command.
In this case, the word 40 is not considered an argument to the ls command, but instead it is an argument to the -w command line switch. (How wide should the output be? 40 characters.) Command line switch arguments simply follow the command line switch. How do you know which command line switches take arguments and which don't? The short answer is through experience, but we will soon mention ways to find help as well.
25
Running Commands
introduces a new -r switch for the ls command, which reverses the sorting order. Note how it is used along with the -s and -w switches.
[elvis@station elvis]$ ls -s -w total 132 4 X11R6 0 local 4 0 tmp 4 libexec 4 4 src 40 lib 4 8 share 4 kerberos 40 8 sbin 8 include 40 -r /usr/ games etc dict bin
Often, when using multiple command line switches, users will take advantage of a shortcut that lets all of the switches be "piled together" onto one hyphen (-), as in the following example.
[elvis@station elvis]$ ls -srw 40 /usr/ total 132 4 X11R6 0 local 4 games 0 tmp 4 libexec 4 etc 4 src 40 lib 4 dict 8 share 4 kerberos 40 bin 8 sbin 8 include
All of the single letter switches that don't take an argument, in this case -s and -r, can be piled together, sharing a single -. If a switch does take an argument, such as -w 40, it can only share a hyphen if it comes last. That way, its argument can be specified next on the command line.
0 tmp 4 X11R6
When long command line switches take an argument, the syntax differs slightly as well. Rather than the argument following the switch as a separate word, the argument is bunched together with the long switch, separated by an =, as in --width=40. Note also, that short and long command line switches can be mixed.
[elvis@station elvis]$ ls --width=40 --size -r total 132 4 X11R6 0 local 4 games 0 tmp 4 libexec 4 etc 4 src 40 lib 4 dict 8 share 4 kerberos 40 bin 8 sbin 8 include /usr/
Arguments
Compared to command line switches, arguments are easy. Whatever words are left over on the command line, after the command name and after any command line switches, are called arguments to the command. What a command expects as arguments, or if it expects any at all, depend on the command. For example, the ls command, if given arguments, will treat the arguments as the files or directories to be listed. The ps command expects no arguments. The cal command takes from zero to two, a possible month and a possible year to generate the calendar for. Learning what arguments a program expects, and what it does with its arguments, is part of learning how to use that command.
26
Running Commands
Some things to note in the usage message: Optional elements are enclosed in square brackets ([ and ]). Here, the usage message refers to multiple short command line switches using the shortcut notation. Note that ls supports both the short (-s) and long (--size) forms of this command line switch. Here is the -w or --width command line switch, which requires an argument. Usage messages do not provide a complete reference for the command, but merely provide enough information to refresh memories. More ways to find help will be discussed in a later lesson in this workbook.
Examples
Learning to Use the cat Command
The user madonna had a friend tell her that the cat command is used to look at the contents of files. She has never used the command before, and is interested in learning how to use it. She starts by examining the command's usage message.
[madonna@station madonna]$ cat --help
27
Running Commands
Usage: cat [OPTION] [FILE]... Concatenate FILE(s), or standard input, to standard output. -A, -b, -e -E, -n, -s, -t -T, -u -v, --show-all --number-nonblank equivalent to -vET number nonblank output lines equivalent to -vE --show-ends display $ at end of each line --number number all output lines --squeeze-blank never more than one single blank line equivalent to -vT --show-tabs display TAB characters as ^I (ignored) --show-nonprinting use ^ and M- notation, except for LFD and TAB --help display this help and exit --version output version information and exit
With no FILE, or when FILE is -, read standard input. Report bugs to <bug-coreutils@gnu.org>.
She doesn't yet understand all of the usage message, such as the references to standard input and standard output, but she can understand enough of the first line to understand that the cat command expects filenames for arguments. She tries to display the contents of the file /etc/anacrontab.
[madonna@station madonna]$ cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 1 7 30 65 70 75 cron.daily cron.weekly cron.monthly run-parts /etc/cron.daily run-parts /etc/cron.weekly run-parts /etc/cron.monthly
Looking at some of the usage messages command line switches, she notes that the -n command line switch numbers the output lines. She tries out this switch.
[madonna@station madonna]$ cat -n /etc/anacrontab 1 # /etc/anacrontab: configuration file for anacron 2 3 # See anacron(8) and anacrontab(5) for details. 4 5 SHELL=/bin/sh 6 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 7 8 1 65 cron.daily run-parts /etc/cron.daily 9 7 70 cron.weekly run-parts /etc/cron.weekly 10 30 75 cron.monthly run-parts /etc/cron.monthly
From the output, it's now easy to see that this file contains 10 lines, or to refer to line number 6. She suspects that the spacing between the words in lines 8 through 10 is made up of tab characters instead of spaces. Noting from the usage message that the -t command line switch will replace any tab characters with ^I, she tries to confirm her suspicion.
[madonna@station madonna]$ cat -t /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 1^I65^Icron.daily^I^Irun-parts /etc/cron.daily 7^I70^Icron.weekly^I^Irun-parts /etc/cron.weekly
28
Running Commands
30^I75^Icron.monthly^I^Irun-parts /etc/cron.monthly
She now notes from the usage that the -A command line switch is "equivalent to -vET", which she takes to be the shortcut combination of the short command line switches -v, -E, and -T. She tries both out, to see if she's right.
[madonna@station madonna]$ cat -A /etc/anacrontab # /etc/anacrontab: configuration file for anacron$ $ # See anacron(8) and anacrontab(5) for details.$ $ SHELL=/bin/sh$ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin$ $ 1^I65^Icron.daily^I^Irun-parts /etc/cron.daily$ 7^I70^Icron.weekly^I^Irun-parts /etc/cron.weekly$ 30^I75^Icron.monthly^I^Irun-parts /etc/cron.monthly$ [madonna@station madonna]$ cat -vET /etc/anacrontab # /etc/anacrontab: configuration file for anacron$ $ # See anacron(8) and anacrontab(5) for details.$ $ SHELL=/bin/sh$ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin$ $ 1^I65^Icron.daily^I^Irun-parts /etc/cron.daily$ 7^I70^Icron.weekly^I^Irun-parts /etc/cron.weekly$ 30^I75^Icron.monthly^I^Irun-parts /etc/cron.monthly$
Seeing the identical output, she decides she has interpreted the usage message correctly.
Questions
Command Line Syntax
The touch command is used to update timestamps on specified files. Use the following usage and sample invocations for the command touch to answer the next 6 questions.
[madonna@station madonna]$ which touch /bin/touch [madonna@station madonna]$ touch --help Usage: touch [OPTION]... FILE... Update the access and modification times of each FILE to the current time. Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, --no-create do not create any files -d, --date=STRING parse STRING and use it instead of current time -f (ignored) -m change only the modification time -r, --reference=FILE use this file's times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time --time=WORD set time given by WORD: access atime use (same as -a) modify mtime (same as -m) --help display this help and exit --version output version information and exit Note that the -d and -t options accept different time-date formats. If a FILE is -, touch standard output. [madonna@station madonna]$ touch -r /etc/services touch: file arguments missing
29
Running Commands
Try `touch --help' for more information. [madonna@station madonna]$ tooch /etc/services -bash: tooch: command not found [madonna@station madonna]$ touch -k /etc/services touch: invalid option -- k Try `touch --help' for more information. [madonna@station madonna]$ touch -r /etc/services /tmp/foo
1. a.
Which of the following would be a legitimate invocation of the touch command? touch -k /etc/services touch -ac /etc/services touch -nocreate /etc/services touch -t
b. c. d. 2. a. b. c. d. 3. a. b. c. d. 4. a. b. c. d. 5. a. b. c. d.
Which of the following would be a legitimate invocation of the touch command? touch -frm /etc/services touch --cm /etc/services touch --no-create -a /etc/services touch
Which best describes why madonna got the "file arguments missing" error message when running touch -r /etc/services? The -r command line switch requires an argument, which madonna didn't supply. The file /etc/services does not exist. The touch command could not be found on the system. -r is not a supported command line switch.
Which best describes why madonna got the "command not found" error message when running tooch /etc/services? The file /etc/services does not exist. madonna misspelled the command name, and no command named tooch could be found. The touch command requires a mandatory -a command line switch. madonna does not have the right permissions to execute the command.
Which best describes why madonna got the "invalid option" error message when running touch -k /etc/services? The file /etc/services does not exist. madonna misspelled the command name, and no command named touch could be found. The touch command requires a mandatory -a command line switch. The touch command doesn't support the -k command line switch.
30
Running Commands
6.
When madonna ran the command touch -r /etc/services /tmp/foo, what best describes the role of the word /etc/services? a. b. c. d. The word serves as an argument to the touch command. The word serves as an argument to the -r command line switch. The word serves as the name of the command to run. The word is misplaced, and caused the command to exit in failure.
7. a.
When a user runs the command who from the command line, which of the following best describes what happens? The shell asks the kernel to execute the contents of the file /usr/bin/who as a separate process, displaying the process's output on the terminal. The shell makes the who system call, prompting the Linux kernel for the output directly. The shell exits, and is replaced by the who process. When the who process terminates, it replaces itself with a new shell process. When long command line switches were introduced, which best describes why they were preceded with a double hyphen (such as --size) instead of a single hyphen (such as -size)? a. b. c. d. The creators of long command line switches liked to type a lot. The word -size could be interpreted as a shortcut for the -s -i -z -e short command line switches, instead of a single long command line switch. A single hyphen already implied a mandatory command line switch argument. The creators of long command line switches liked to make things complicated.
b. c. 8.
31
Discussion
Controlling Terminals
The bash shell, and the terminal it runs on, are the primary tools used by experienced Linux users. Later in this course, an entire Workbook will be devoted to using the bash shell's powerful features. This lesson introduces some features shared by terminals in general, and one feature of the bash shell that is too useful to delay until later, tab completion.
Resetting Terminals
When terminals display information, they usually display one byte at a time. When computers are handling human readable text, the characters are usually encoded as an integer byte value using the ASCII format. While a byte can encode up to 256 different values, only a small number of these (about 100) are used for text characters and punctuation. The others are used to encode control sequences (see below) and other special conditions. Sometimes, users accidentally (or intentionally) ask a terminal to display data that is not encoded as ASCII text, such as an image or an executable. The terminal does the best job it can, displaying characters for ASCII text, but what often appears to be random garbage for bytes that refer to control characters. Even worse, the terminal can end up in an alternate graphics mode, so that even what should be normal ASCII text looks unreadable. In these situations, the reset command can restore the terminal to sane behavior. Usually, when you'd like to use this command, you won't even be able to see yourself type it. However, if at an unreadable bash prompt, blindly typing reset and followed by the RETURN key will usually restore the terminal to sane behavior.
32
Managing Terminals
CTRL+C is probably the most useful of the above sequences, coming to the rescue whenever a user is feeling "JUST MAKE IT STOP!" When using the bash shell, typing CTRL+C will terminate the currently running process, and return you to the bash prompt. The /dev/zero device node is a pseudo device that, when read, returns an infinite number of (binary) zeros. The /dev/null device node is a pseudo device that throws away any information written to it. The following command, therefore, would run forever, reading zeros and throwing them away. Someone stuck in this situation could use CTRL+C to cancel the command.
[elvis@station elvis]$ cat /dev/zero > /dev/null (... user types CTRL-C ...) [elvis@station elvis]$
CTRL+D
As will be discovered in a later workbook, many Unix commands read their input directly from the keyboard. Unfortunately for new Unix users, it's not always obvious how to tell the command when to stop listening. The answer is CTRL+D. This control sequence is used to send an "End of Transmission" (usually interpreted as "End of File") to the listening process. The wc command is an example of a command that, when not given a filename as an argument, will count the number of lines, words, and characters that a user types in from the keyboard. The user signals the end of the input with CTRL+D.
[elvis@station elvis]$ wc polly wants a cracker polly wants a cracker polly wants a cracker (... user types CTRL-D ...)
33
Managing Terminals
3 12 66 [elvis@station elvis]$
The wc command dutifully reports that the user typed 3 lines, as 12 words, using 66 characters. (While this doesn't seem so useful now, many more uses will be found for wc in a later workbook.) What would have happened if the user had typed CTRL+C instead? The wc command would have been canceled, not reporting any output. CTRL+Z CTRL+Z is used to suspend a program, which can later be restored with the fg ("foreground") command. While in the middle of an ftp session, elvis wants to determine his shell's current working directory. He suspends the ftp session with CTRL+Z, runs the pwd command from the shell, and then restores the ftp session with fg.
[elvis@station elvis]$ ftp ftp.redhat.com Trying 66.187.232.51... Connected to ftp.redhat.com (66.187.232.51). 220 Red Hat FTP server ready. All transfers are logged. (FTP) Name (ftp.redhat.com:elvis): anonymous 331 Please specify the password. Password: 230 Login successful. Have fun. Remote system type is UNIX. Using binary mode to transfer files. ftp> (... user types CTRL-Z ...) [1]+ Stopped ftp ftp.redhat.com [elvis@station elvis]$ pwd /home/elvis [elvis@station elvis]$ fg ftp> ftp.redhat.com ftp> ls 227 Entering Passive Mode (66,187,232,51,67,212) 150 Here comes the directory listing. drwxr-xr-x 6 ftp ftp 4096 May 24 01:33 pub 226 Directory send OK. ...
Managing processes in this manner is a fairly complicated subject, which will be discussed in detail in a later Workbook. For now, just know that CTRL+Z suspends, and fg resumes. CTRL+U CTRL+U is commonly used to "wipe" the current line. If you have so mangled a bash command line that you just want to start over, type CTRL+U. Also, CTRL+U is very helpful when you mess up while entering a password. Because your characters are usually not echoed back to you, its often hard to know what you've type. Just type CTRL+U and start over. CTRL+H serves the exact same role as the BACKSPACE key. On some terminals, however, the BACKSPACE key is incorrectly mapped, and doesn't function correctly. CTRL+H will almost always work in these situations. CTRL+L was traditionally used to tell a line printer to eject the current page and start on the next one. For online terminals, it has been repurposed for redrawing screens. For many screen based programs, including the vi and nano editors, if the screen ever becomes mangled because of unexpected messages, a CTRL+L will cause the program to refresh the screen, redrawing it correctly. For the bash shell, CTRL+L causes bash to clear the screen, but preserve the current command line.
CTRL+H
CTRL+L
34
Managing Terminals
CTRL+Q
CTRL+Q "thaws" a screen (causing it to become active again) after it has been "frozen" with a CTRL+S. Although it is only useful after a CTRL+S has been used, it is introduced first (as the more useful command) because almost no one intentionally uses CTRL+S. Instead, new Unix users will sometimes accidentally type a CTRL+S, and not understand why their terminal has gone unresponsive. If your terminal ever seems dead, try typing a CTRL+Q and see if that fixes it. See CTRL+Q above. Almost never used. (People use the RETURN key instead.) The only real use is to make the terminal beep, impressing your friends with your vast reservoirs of Unix knowledge.
Identifying terminals
Dating back from it's origins as a terminal server, Unix is obsessed with the concept of a terminal. Processes are grouped into "sessions" based on which terminal they were started from. While somewhat outdated, knowing how to recognize terminals can help in tracking the origins of processes. As with most other devices, programs interact with terminals at a low level through device nodes, found within the /dev directory. For example, communication with the first virtual console uses the device node /dev/tty1. Most processes keep a record of which terminal they were started from, and user's login sessions are usually associated with the terminal they are using. When identifying terminals in these situations, the terminal is referred to by its device node, for example, tty1. In the following excerpt, elvis is listing the current users on a machine with who, and listing the current processes running from his terminal with ps. In both cases, the terminal is specified in the column "TTY".
[elvis@station root tty2 elvis tty3 blondie tty1 blondie pts/0 blondie pts/1 blondie pts/2 blondie pts/3 [elvis@station PID TTY 4384 tty3 4427 tty3 elvis]$ who Jun 21 10:12 Jun 21 16:50 Jun 21 10:13 :0 Jun 21 16:43 (:0.0) Jun 21 10:14 (:0.0) Jun 21 10:31 (:0.0) Jun 21 10:39 (:0.0) elvis]$ ps TIME CMD 00:00:00 bash 00:00:00 ps
Many different devices are treated by Linux as a terminal, including virtual consoles, serial line connected VT100 terminals, modems, etc. The following table lists some of the common terminal names that are conventionally used for some common terminal devices.
pts/n
Pseudo-terminal
35
Managing Terminals
Name :0
Device X server
How used The X server is not really a terminal. When a user logs in using the X graphical environment, their terminal may be listed as the X server itself.
Tab Completion
Tab completion is not really a feature of terminals, but of the bash shell that is usually run within them. Tab completion could well be the most helpful topic within this lesson. When typing the names of commands and files, a lot of time and effort can be saved by learning how to make good use of the TAB key. When typing the name of a command as the first word of a bash command line, start typing the command you would like to run, but at some point before you finish the word, stop and hit the TAB key. One of three things will happen. 1. The rest of the command you were beginning to type magically appears. 2. Part of the command you were typing magically appears, but then bash stops and beeps at you. 3. bash just beeps at you. The bash shell does the best job that it can in selecting the command you were starting to type from the list of all possible commands. In the first case, there is only one command that started out with the letters that you typed, so bash was able to figure out exactly what command you were starting, and finished it for you. You're done. In the second and third cases, more than one command started out with the letters you were typing. bash completed the command as far as possible, but stopped with a beep to let you know that you still have some choices to make. In order to see the selection of commands that bash has narrowed it down to, hit the TAB key a second time. bash will list all commands that start with what you have typed. Supply enough of the remainder of the command to make it unique, and hit TAB again. bash will finish the command off for you. An example is in order. The user elvis wants to enable Unicode mode for his terminal. Don't worry about what Unicode mode is, or why he wants to do it. Just know that the command that enables Unicode mode is called unicode_start, and the command that disables it is called unicode_stop. He begins typing the command, and hits the TAB key.
[elvis@station elvis]$ unic<TAB>
bash expands the word to unicode_st, and beeps. In order to see the list of possible completions, elvis hits the TAB key twice.
[elvis@station elvis]$ unicode_st<TAB><TAB> unicode_start unicode_stop
bash returns with a list of two commands that both start unicode_st. Seeing that the command that elvis wants to run is the only one that starts out unicode_sta, elvis types an a, and hits TAB again.
[elvis@station elvis]$ unicode_sta<TAB>
bash completes the command, and positions the cursor so that its ready to begin typing any arguments or command line switches for the command.
[elvis@station rha030]$ unicode_start
36
Managing Terminals
Not only does bash complete the first word against available commands, but all other words on the command line can be completed against files in the filesystem. For example, suppose madonna wanted to examine the file /etc/updatedb.conf. Rather than typing out the entire file name, she could type the first part, and hit TAB.
[madonna@station madonna]$ cat /etc/upd<TAB>
Because /etc/updatedb.conf is the only file that starts out /etc/upd, bash is able to complete the file.
[madonna@station madonna]$ cat /etc/updatedb.conf PRUNEFS = "auto afs iso9660 sfs udf" PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/spool/cups /var/spool/squid /var/tmp"
As you proceed through the course, a little time spent exploring tab completion can save a lot of time and effort down the road. After a while, using the TAB key to save typing becomes second nature. After the end of a busy day, the left little finger can hurt because it spent the day pounding the TAB key.
Examples
Resetting the terminal after viewing a binary file.
The user blondie accidentally uses the cat command to view an executable file, /bin/arch. Because the file is a compiled executable, it contains bytes that aren't meant to be displayed on a terminal, and sends her terminal into haywire behavior.
[blondie@station blondie]$ cat /bin/arch ELF??414 (44???4????/lib/ld-linux.so.2GNU???y??6,?????aaa????aac????-?????[???
In order to reset her terminal, blondie blindly types the reset command, and her terminal is restored to a sane condition.
[blondie@station blondie]$
Canceling a command
The user prince thought it would be interesting to recursively list the contents of his filesystem, starting with the root directory /.
[prince@station prince]$ ls -R / /: bin data etc initrd lost+found boot dev home lib misc /bin: arch ash ash.static aumix-minimal awk basename bash bash2 ...
mnt opt
proc rha
root RPMS
sbin tmp
usr var
web
After a few minutes watching files go by, he feels like he's seen enough. He cancels the command by typing a CTRL+C. The command terminates, and he is returned to the bash prompt.
[prince@station prince]$
37
Managing Terminals
Online Exercises
Suspending terminal output
Lab Exercise
Objective: Learn to manage terminal output for long running commands. Estimated Time: 5 mins.
Specification
1. Like prince in the example, take a recursive listing of the root directory of the filesystem, using the command ls -R /. 2. While the output is flowing by, freeze your terminal using the CTRL+S control sequence. 3. Thaw the terminal using the CTRL+Q control sequence. You should be able to alternately freeze and thaw the streaming output by using these control sequences. 4. With the output from the command still flowing, suspend the process with the CTRL+Z control sequence. 5. Confirm your lab with the process suspended.
Deliverables
1. 1. A stopped (suspended) ls -R / command.
Cleaning Up
After you have finished confirming your deliverable, restore the suspended process to the foreground with the fg command. Now cancel it with the CTRL+C control sequence.
Specification
You would like to generated a list of (alphabetically) sorted socks in the file sorted_socks.txt in your home directory. 1. Run the command sort > sorted_socks.txt in your home directory. The command should not immediately terminate, but instead wait for input from the keyboard. 2. Type in several types of socks, such as "yellow socks", "dirty socks", "cool winnie the pooh socks", etc. Separate each type of sock with a new line by using the RETURN key.
38
Managing Terminals
3. After listing several socks, terminate the list with the CTRL+D control sequence. You should now have a file that contains a list of sorted socks. 4. Exit your shell, so that your history gets saved.
[student@station student]$ sort > sorted_socks.txt yellow socks dirty socks cool winnie the pooh socks (... type CTRL-D ...) [student@station student]$ cat sorted_socks.txt cool winnie the pooh socks dirty socks yellow socks
Deliverables
1. 1. A file in your home directory called sorted_socks.txt, which contains a list of sorted socks, and a .bash_history file that contains the command sort > sorted_socks.txt.
Questions
1. a. b. c. d. e. 2. a. b. c. d. e. 3. Which of the following commands can restore a terminal to sane behavior? sanetty reload start-unicode reset sane Which of the following control sequences can be used to suspend a command? CTRL+C CTRL+D CTRL+Q CTRL+U CTRL+Z If you walk up to a terminal that seems completely non-responsive, which control sequence might restore activity? a. b. c. d. CTRL+C CTRL+D CTRL+Q CTRL+U
39
Managing Terminals
e. 4.
CTRL+Z If you suspect that you have mistyped while entering a password, which control sequence would allow you to start over?
a. b. c. d. e. 5.
CTRL+C CTRL+D CTRL+Q CTRL+U CTRL+Z If you want to cancel a command that is generating too much output, which control sequence would be most helpful?
a. b. c. d. e. 6.
CTRL+C CTRL+D CTRL+L CTRL+S CTRL+U If you want to freeze the terminal output, so that you can resume it later, which control sequence would be most helpful?
a. b. c. d. e. 7. a. b. c. d. e.
CTRL+C CTRL+D CTRL+L CTRL+S CTRL+U Which control sequence causes bash to clear the screen? CTRL+C CTRL+D CTRL+L CTRL+S CTRL+U
Use the output from the who command to answer the following questions.
[prince@station prince]$ who blondie tty2 Apr 21 22:07 elvis tty3 Apr 21 22:07 elvis tty4 Apr 21 22:07
40
Managing Terminals
21 21 21 21 21 21
8. a.
How has the user blondie logged onto the Enterprise Linux machine? Using the Login Manager in the X graphical environment. A virtual console. A serial line connected terminal. A ssh network connection The login method cannot be determined with the information provided. How has the user prince logged onto the Enterprise Linux machine? a. b. c. d. e. Using the Login Manager in the X graphical environment. A virtual console. A serial line connected terminal. A ssh network connection The login method cannot be determined with the information provided. Which user logged on as the administrative user root? a. b. c. d. e. blondie elvis madonna prince It cannot be determined with the information provided.
b. c. d. e. 9.
10.
41
Discussion
Getting Help
Unix, and Linux in particular, has a tradition that manuals and documentation should not be kept on a bookshelf, but found within the system, in easy reach of the system's users. No one remembers every command line switch to the ls command, so most commands provide terse summaries called "usages" when invoked with the appropriate command line switches. More complete reference information for most commands can be found in structured "man" pages and "info" pages. In Red Hat Enterprise Linux, the /usr/share/doc directory contains less structured documentation specific to a particular package. Lastly, a wealth of guides and tutorials can be found at the Linux Documentation Project, or in Red Hat Enterprise Linux documentation packages.
Man Pages
Manual pages, more often abbreviated "man pages", are the traditional source of reference information of Unix systems. A documentation page for most commands, file formats, programming calls, and general topics can be viewed using the man command. For example, man ls generates documentation for the ls command.
42
Getting Help
Man Chapters
Man pages are organized into eight standard chapters, as itemized in the following tables. Some pages share identical names in different chapters. For example, pages exist for both the passwd command, found in chapter one on user commands, but also the /etc/passwd file, found in chapter five on file formats. Unfortunately for the user trying to find documentation on the /etc/passwd file format, man passwd only displays the first page it finds, in this case the entry from chapter one. In order to view the man page from chapter five, the chapter must be explicitly specified, as in man 5 passwd.
In Unix, references to man pages customarily include the chapter number in parentheses after the name of the page, such as passwd(1) or passwd(5). Each chapter contains an introductory page called intro, so the command man 5 intro would produce an introduction to chapter 5.
43
Getting Help
... chpasswd (8) - update password file in batch gpasswd (1) - administer the /etc/group file hesiod_free_passwd [hesiod_getpwnam] (3) - Hesiod functions for retrieving passwd information hesiod_getpwnam (3) - Hesiod functions for retrieving passwd information hesiod_getpwuid [hesiod_getpwnam] (3) - Hesiod functions for retrieving passwd information htpasswd (1) - Create and update user authentication files ldappasswd (1) - change the password of an LDAP entry lppasswd (1) - add, change, or delete digest passwords pam_localuser (8) - require users to be listed in /etc/passwd passwd (1) - update a user's authentication tokens(s) passwd (5) - password file passwd [sslpasswd] (1ssl) - compute password hashes saslpasswd (8) - set a user's sasl password smbpasswd (5) - The Samba encrypted password file smbpasswd (8) - change a user's SMB password ...
madonna got more than she bargained for, but included in the output is a reference to the password file, and the fact that the information is found in chapter 5 of the man pages. Now that madonna knows the right chapter, she pulls up the appropriate page with man 5 passwd. As another approach, madonna could have used the -a command line switch, which tells the man command to view all relevant pages, in order.
Info Pages
Man pages are usually designed to provide reference information, not tutorials or help on general context. Many more complicated commands are more fully documented in hyperlinked "info" pages. Info pages predate the days of web browsers and clickable links. Info pages are viewed using either the traditional info command, or Red Hat Enterprise Linux provides a similar command with an easier interface called pinfo. Info pages are primarily used by software developed by the GNU project [http://www.gnu.org]. The command pinfo, without arguments, will list a table of contents for all installed info pages. Basic navigation is similar to the less pager. Links among the info pages can be traversed using the four arrow keys, as outlined in the table below.
44
Getting Help
documented. By allowing users to use software early, they can help influence the ongoing design of the software for the better. Red Hat Enterprise Linux embraces this philosophy, and will include useful, stable software, even if the software is not formally documented in man pages or info pages. Often, documentation about how to use newly developed products might be a simple text file called README hastily thrown together by the developer. Red Hat Enterprise Linux includes such unstructured, but often still helpful, documentation, within the /usr/share/doc directory, organized by the Red Hat package that owns the software. Your mileage will vary about how useful the documentation is for any given package. For example, elvis discovers that the informal documentation provided for the evince document viewer is rather skimpy, consisting primarily of the developers' unstructured ChangeLog and a terse README.
[elvis@station elvis]$ ls -s /usr/share/doc/evince-0.6.0/ total 308 300 ChangeLog 8 README
In contrast, a wealth of information about configuring the complicated file sharing service samba can be found under its /usr/share/doc directory, including subdirectories containing documentation in a variety of formats, and PDF copies of three texts on the subject.
[elvis@station ~]$ ls /usr/share/doc/samba-3.0.23c/ autofs libsmbclient README COPYING Manifest registry history misc REVISION htmldocs printer-accounting Roadmap LDAP printing Samba3-ByExample.pdf Samba3-Developers-Guide.pdf Samba3-HOWTO.pdf THANKS WHATSNEW.txt
HOWTOs
45
Getting Help
GUIDES
Guides provide more in depth coverage of broad topics, such as System Administration, or even Linux Kernel Module Programming.
While the documentation is not specific to the Red Hat Enterprise Linux distribution, much of the information is still useful and relevant.
While yelp provides the primary documentation for many GNOME specific graphical applications, including the Nautilus File Manager and GNOME applets, there are also subtle entries referring to "Manual Pages" and "GNU Info Pages" at the bottom of the table of contents. These provide a "clickable" front end to the man pages and info pages, respectfully, and the wealth of information contained therein.
46
Getting Help
Additionally, the Red Hat Release Notes, mentioned previously when introducing the documentation found at www.redhat.com [http://docs.redhat.com], can be accessed by choosing the nearby "Documentation" selection from the System menu.
Figure 6.3. Accessing The Red Hat Enterprise Linux Release Notes
47
Getting Help
Questions
1. a. b. c. d. e. 2. a. b. c. d. e. 3. Which of the following is not a way to obtain help for the ls command? help ls ls --help man ls pinfo ls All are legitimate ways of obtaining help. When viewing a man page, which key causes the pager to quit? Z TAB q RETURN SPACE Which chapter of the man pages would contain documentation on the /etc/group configuration file? a. b. c. d. e. 4. a. b. c. d. e. 5. 1 4 5 7 8 Which chapter of the man pages would contain documentation on the groups command? 1 4 5 7 8 Which chapter of the man pages would contain documentation on filename suffixes, and the type of file that they imply? a. b. 1 4
48
Getting Help
c. d. e. 6.
5 7 8 In the table listing man page chapters, chapter 6 was omitted. What does this chapter contain information on?
a. b. c. d. e. 7.
Unix history hardware devices networking protocols graphical applications games Which of the following commands would generate a keyword search of the man pages for the word sleep?
a. b. c. d. e. 8. a. b. c. d. e. 9.
mankey sleep man --key sleep man -key sleep keyword sleep None of the above commands would work. Informally structured documentation is found where in a Red Hat Enterprise Linux distribution? Red Hat Enterprise Linux only includes formally structured documentation. /usr/share/doc /usr/doc /usr/doc/share /tmp/doc/ Which of the following interfaces to the info pages allows someone to navigate links with mouse clicks?
a. b. c. d. 10. a.
The pinfo command. The info command. The yelp application. A and C
What type of help can be viewed with the GNOME Help Browser, yelp? documentation for GNOME graphical applications
49
Getting Help
b. c. d. e.
50