83484.
83484.book Page 2 Monday, September 18, 2006 8:58 AM Linux’s tools for running programs and manipulating files are simple, unglamorous, and powerful. Although you can use GUI tools to drag and drop files, real Linux street smarts requires you to be able to manage your files from the command line by using odd-sounding commands such as ls, cp, ln, and mv. In the first few tasks of this phase, you’ll learn to use some of the most basic of these tools.
83484.book Page 3 Monday, September 18, 2006 8:58 AM Task 1.1: Use Basic Command-Line Features 3 Actually correcting a problem such as the one described here is likely to require the use of Linux’s package management tools. These are described in Phase 3, “Managing Software.” Scope of Task This task is fairly straightforward. It requires little time and effort. You might want to continue experimenting with bash after you’ve completed this task, though.
83484.book Page 4 Monday, September 18, 2006 8:58 AM 4 Phase 1 Working on the Command Line Procedure To complete this task, you must log into your account on the affected workstation and enter several Linux commands. In the process, you’ll learn how to type and edit commands, how to use command completion, and how to use command history. You’ll also learn a few of the more common Linux commands. Logging In The first task is logging into the Linux system.
83484.book Page 5 Monday, September 18, 2006 8:58 AM Task 1.1: Use Basic Command-Line Features 5 For this task, you should log in as an ordinary user. In fact, it’s good practice to always log in as an ordinary user. You can subsequently acquire superuser privileges from your ordinary user account if you need such access (described shortly in “Obtaining Superuser Privileges”).
83484.book Page 6 Monday, September 18, 2006 8:58 AM 6 Phase 1 Working on the Command Line whatis. Unfortunately, the whatis file is too long to fit on a standard 80×25 screen, although it might fit on an extra-large screen or resized xterm. more This command is a simple pager—it displays text a page (that is, a screenful) at a time. When you’re done reading a page, press the spacebar and more displays the next page.
83484.book Page 7 Monday, September 18, 2006 8:58 AM Task 1.1: Use Basic Command-Line Features 7 Using Command Completion Many users find typing commands to be tedious and error prone. For this reason, Linux shells include various tools that can help speed up operations. The first of these is command completion: Type part of a command or (as an option to a command) part of a filename and then press the Tab key. The shell tries to fill in the rest of the command or the filename.
3484.book Page 8 Monday, September 18, 2006 8:58 AM 8 Phase 1 Working on the Command Line Editing Commands Frequently, after finding a command in the history, you want to edit it. The bash shell, like many shells, provides editing features modeled after those of the Emacs editor: Move within the line Press Ctrl+A or Ctrl+E to move the cursor to the start or end of the line, respectively. The left and right arrow keys will move within the line a character at a time.
83484.book Page 9 Monday, September 18, 2006 8:58 AM Task 1.1: Use Basic Command-Line Features 9 you can move from section to section of the documentation for a program. Type info info to learn more about this system. Both man pages and info pages are usually written in a terse style. They’re intended as reference tools, not tutorials; they frequently assume basic familiarity with the command, or at least with Linux generally.
83484.book Page 10 Monday, September 18, 2006 8:58 AM 10 Phase 1 Working on the Command Line Don’t shut down or reboot Linux by pressing the power or reset buttons on the computer’s case. Like most modern OSs, Linux requires a controlled shutdown to avoid damaging the data on its hard disk. If you want to shut down the system, use a shutdown option at a GUI login prompt or type shutdown -h now as root. Replace -h with -r if you want to reboot the computer rather than shut it down.
83484.book Page 11 Monday, September 18, 2006 8:58 AM Task 1.2: Manage Files and Directories 11 Setup You need perform no special setup to do this task; just log into your computer and acquire root privileges. Caveats Because this task is performed as root and uses powerful file-manipulation commands, a potential for devastating errors exists. Certain typos, when entered as root, can obliterate an entire Linux installation.
83484.book Page 12 Monday, September 18, 2006 8:58 AM 12 Phase 1 Working on the Command Line Copying Files Linux’s file-copying command is cp (which is short for copy). In its most basic form, cp copies a single file from one location to another: # cp /etc/fstab /home/project7/ This command copies the /etc/fstab file to the /home/project7 directory; you’ll find the copy there under the same name as the original.
83484.book Page 13 Monday, September 18, 2006 8:58 AM Task 1.2: Manage Files and Directories 13 Depending upon your configuration, you might or might not be prompted before rm deletes each individual file. If you’re prompted and don’t want to be, you can add the -f option; if you’re not prompted but you do want to be, you can add the -i option. The contents of /etc/X11 vary somewhat from one system to another. Thus, you might need to modify these examples on your system.
83484.book Page 14 Monday, September 18, 2006 8:58 AM 14 Phase 1 Working on the Command Line file’s main owner. This is effectively the same as the chgrp command, which works much like chown but accepts a group name alone (without a dot or colon). If you omit the colon or dot and the group name, chown changes only the primary owner of the file or directory. The chown command has several options; consult its man page for details.
83484.book Page 15 Monday, September 18, 2006 8:58 AM Task 1.2: Manage Files and Directories TABLE 1.1 15 Example Permissions and Their Likely Uses (continued) Permission String Octal Code Meaning rwxr-x--- 750 Read and execute permission for the owner and group. The file’s owner also has write permission. Users who are not the file’s owner or members of the group have no access to the file. rwx------ 700 Read, write, and execute permissions for the file’s owner only; all others have no access.
83484.book Page 16 Monday, September 18, 2006 8:58 AM 16 Phase 1 Working on the Command Line This command grants rw-rw---- permissions to the xorg.conf file in /home/project7. The symbolic form of the command represents a series of changes to permissions, using the codes summarized in Table 1.2. TABLE 1.
83484.book Page 17 Monday, September 18, 2006 8:58 AM Task 1.3: Manage Links TABLE 1.3 Examples of Symbolic Permissions with chmod (continued) Command Initial Permissions End Permissions rwxrwxr-x rwxrwx--- chmod g=u report.tex rw-r--r-- rw-rw-r-- chmod g-w,o-rw report.tex rw-rw-rw- rw-r----- chmod o-rwx 17 bigprogram As with many other file-manipulation commands, chmod accepts a -R option to operate recursively on an entire directory tree.
83484.book Page 18 Monday, September 18, 2006 8:58 AM 18 Phase 1 Working on the Command Line Setup You need perform no special setup to perform this task; just log into your computer as the user who owns the files in the /home/project7 directory and type cd /home/project7 to change into that directory. Be sure to complete Task 1.2 before starting this task. Caveats If you perform this task as an ordinary user, the risk to the computer is minimal.
83484.book Page 19 Monday, September 18, 2006 8:58 AM Task 1.3: Manage Links 19 the original name show 2, and so on. (For directories, the second column’s number refers to the number of directories within the specified directory, including pointers to the directory itself and its parent directory.) Current versions of Linux forbid making hard links to directories, but this was possible with some earlier versions of Linux.
83484.book Page 20 Monday, September 18, 2006 8:58 AM 20 Phase 1 Working on the Command Line want the copied fstab file to be accessible under two names; you can delete either of the two hard links with rm: $ rm fstab This example deletes the second link; if sample-file had been deleted instead, you’d break the symbolic link (another-link). You should be aware that some file operations will do odd things with links, and particularly with symbolic links.
83484.book Page 21 Monday, September 18, 2006 8:58 AM Task 1.4: Find Files 21 Duration This task should take half an hour or an hour to complete. Once you’ve mastered these commands, you should be able to search for files in a matter of seconds—although some of these commands may take several minutes to execute. Setup No special setup is required.
83484.book Page 22 Monday, September 18, 2006 8:58 AM 22 Phase 1 Working on the Command Line In addition to the -name criterion, you can search for files in various other ways, such as by permissions (-perm), file size (-size), and owner (-user). The man page for find details these options; consult it for more information. Using locate The locate command is much less flexible than find, but it’s also much faster.
83484.book Page 23 Monday, September 18, 2006 8:58 AM Task 1.5: Edit Files 23 find hits in a slightly different set of directories than is shown here. Don’t be concerned about this.) The whereis program accepts several parameters that modify its behavior in various ways. These are detailed in the program’s man page. Criteria for Completion To complete this task, you should use the three file-location commands to locate the whatis program.
83484.book Page 24 Monday, September 18, 2006 8:58 AM 24 Phase 1 Working on the Command Line Setup In principle, you could use an emergency boot disk, but you’ll probably find it more convenient to use your regular Linux installation. Log into your computer as an ordinary user. You should then copy the /etc/lilo.conf file to a safe temporary location, such as your home directory: $ cp /etc/lilo.conf ~/ If your system lacks a lilo.
83484.book Page 25 Monday, September 18, 2006 8:58 AM Task 1.5: Edit Files 25 If you’re not sure what mode Vi is in, press the Esc key. This will return you to command mode, from which you can re-enter insert mode, if necessary. Unfortunately, terminology surrounding Vi modes is inconsistent at best. Command mode is sometimes referred to as normal mode, and insert mode is sometimes called edit mode or entry mode, for instance. Ex mode is often not described as a mode at all, but as colon commands.
83484.book Page 26 Monday, September 18, 2006 8:58 AM 26 Phase 1 FIGURE 1.2 the program. Working on the Command Line The last line of a Vi display is a status line that shows messages from Adding a new entry to lilo.conf involves duplicating the lines beginning with the image= line and modifying the duplicates. Therefore, the first editing task is to duplicate these four lines. To do this, follow these steps: 1.
83484.book Page 27 Monday, September 18, 2006 8:58 AM Task 1.5: Edit Files 27 Now that you’ve duplicated the necessary lines, you must modify one copy to point to the kernel whose entry was accidentally deleted. To do so, follow these steps: 1. Move the cursor to the v in vmlinuz on the second image= line. You’re about to begin customizing this second stanza. 2. Up until now, you’ve operated Vi in command mode. There are several commands that you can use to enter insert mode.
83484.book Page 28 Monday, September 18, 2006 8:58 AM 28 Phase 1 Working on the Command Line There’s a great deal more depth to Vi than is presented here; the editor is quite capable, and some Linux users are very attached to it. Entire books have been written about Vi. Consult one of these, or a Vi Web page like http://www.vim.org, for more information. Saving Changes To save changes to a file, type :w from command mode.
83484.book Page 29 Monday, September 18, 2006 8:58 AM Task 1.6: Manage Accounts 29 Scenario Personnel changes require you to add an account for a new user (Trevor Brown) and delete an account for an employee who’s leaving (Susan Jones). Changes to the amount of free space on two disks also requires you to move another user’s (Dale Smith’s) files from the /home2 to the /home directory tree. Scope of Task This task involves creating, deleting, and modifying accounts.
83484.book Page 30 Monday, September 18, 2006 8:58 AM 30 Phase 1 Working on the Command Line Understanding the Basics of Account Management Linux and Unix systems have traditionally stored account information in the /etc/passwd file.
83484.book Page 31 Monday, September 18, 2006 8:58 AM Task 1.6: Manage Accounts 31 You can create an account with different defaults by placing appropriate parameters between useradd and the account name. Alternatively, you can create an account with the defaults and then use usermod to change them. Some features you’re particularly likely to want to adjust include: Comment The -c comment parameter passes the comment field for the user.
83484.book Page 32 Monday, September 18, 2006 8:58 AM 32 Phase 1 Working on the Command Line passwd to begin the password-changing process and allow the users to type their own desired passwords: # passwd tbrown New UNIX password: Retype new UNIX password: passwd: password updated successfully You would type passwd tbrown, then let the user type a password (twice). The system doesn’t echo the password for security reasons.
83484.book Page 33 Monday, September 18, 2006 8:58 AM Task 1.7: Use Streams, Pipes, and Redirection 33 Don’t modify a user account when that user is logged in; certain changes are likely to wreak havoc with work the user is doing. Wait for the user to log out, or if a change must be implemented immediately, ask the user to log out. Changing the account’s home directory won’t move the files in the directory. To do that, you must use the cp command.
83484.book Page 34 Monday, September 18, 2006 8:58 AM 34 Phase 1 Working on the Command Line Scenario A user of a Linux computer you administer reports problems with network connections from the Mozilla Firefox browser. As part of your diagnosis of this problem, you want to use some diagnostic commands that produce copious output. In order to do this more easily, you will pipe the output streams from these programs into other programs and redirect the output into files that you can peruse later.
83484.book Page 35 Monday, September 18, 2006 8:58 AM Task 1.7: Use Streams, Pipes, and Redirection 35 This task may be performed as an ordinary user. If you run it as root, you run the usual risks of performing tasks as root. Procedure This task investigates three practical techniques: redirecting output, redirecting input, and piping data between programs. All three techniques rely on the fact that Linux uses input and output streams, so you should first understand a bit of theory.
83484.book Page 36 Monday, September 18, 2006 8:58 AM 36 Phase 1 Working on the Command Line operator (>), which you place after the command and before the name of a file that is to receive the output: $ netstat -p > net-connections.txt The file net-connections.txt now contains the output of the netstat -p command, with one exception (described shortly). You can then open net-connections.txt in a text editor or view it with a program such as less in order to study its contents in greater detail.
83484.book Page 37 Monday, September 18, 2006 8:58 AM Task 1.7: Use Streams, Pipes, and Redirection 37 requires you to enter text either as a command-line option or via standard input. Thus, you’d redirect standard input from the error-messages.txt file. You’d also have to redirect standard output to save the result in a file: $ text2gif < error-messages.txt > error-graphics.gif You can use a graphics program, such as the GIMP, to view the error-graphics.
83484.book Page 38 Monday, September 18, 2006 8:58 AM 38 Phase 1 Working on the Command Line This example relies on subtle order effects: On the command line, standard error is redirected to standard output before standard output is redirected to /dev/null; however, bash interprets these redirections in the opposite order. Thus, this command will send nothing as input to text2gif if you reverse the order of the two redirection operators.
83484.book Page 39 Monday, September 18, 2006 8:58 AM Task 1.8: Manage the Shell Environment 39 Setup To prepare for the task, log into your user account on your Linux system. (You can—and should—perform this task as an ordinary user.) You may perform this task in either a textmode or a GUI login, but in the latter case, you must launch an xterm or similar command prompt window. Caveats Performing this task as root poses the usual risks of accidentally damaging the installation.
83484.book Page 40 Monday, September 18, 2006 8:58 AM 40 Phase 1 Working on the Command Line In most distributions, the system is configured with default prompts that include your username, the computer’s hostname, and the current directory, but details differ. You can learn what your shell prompt is by typing echo $PS1. This command displays the contents of the $PS1 variable.
83484.book Page 41 Monday, September 18, 2006 8:58 AM Task 1.8: Manage the Shell Environment 41 Suppose you don’t want to use Vi as your default editor, though; perhaps you prefer nano. You can adjust your default editor by changing the environment variable: $ export EDITOR="/usr/bin/nano" Check that /usr/bin/nano exists before typing this command. If it doesn’t exist, locate the file’s true location or substitute another editor, such as /usr/ bin/emacs or /usr/bin/jed.
83484.book Page 42 Monday, September 18, 2006 8:58 AM 42 Phase 1 Working on the Command Line condition and try again with another file; it may take a bit of trial and error to locate the correct file. If you like, you can change the global configuration file to affect all users, but you must do so as root. In addition to startup scripts, bash provides shutdown (or logout) scripts. The most common name for this script is ~/.bash_logout, which is a user script.
83484.book Page 43 Monday, September 18, 2006 8:58 AM Task 1.8: Manage the Shell Environment TABLE 1.5 43 Common Environment Variables and Their Meanings (continued) Variable Name Explanation $LD_LIBRARY_PATH A few programs use this environment variable to indicate directories in which library files may be found. It works much like PATH. $PS1 This is the default prompt in bash.
83484.book Page 44 Monday, September 18, 2006 8:58 AM 44 Phase 1 Working on the Command Line To learn what environment variables are set in your particular session, type env. This command officially runs a program in a modified environment; however, when typed alone, it displays the values of all the currently set environment variables. The output of this command is likely to be quite long, so you may want to pipe it through less or redirect the output to a file for later examination.
83484.book Page 45 Monday, September 18, 2006 8:58 AM Task 1.9: Write Basic Scripts 45 Duration This task will take about an hour to complete. Writing real-world scripts can take anywhere from a minute or so up to several hours or even days, depending on the complexity of the script and your own proficiency at script writing. Setup To perform this task, you must log into your computer as an ordinary user. One of the scripts presented in this task requires root access to run.
83484.book Page 46 Monday, September 18, 2006 8:58 AM 46 Phase 1 Working on the Command Line When you’re done writing the shell script, you should modify it so that it’s executable. You do this with the chmod command, as described in Task 1.2. Specifically, you use the +x option to add execute permissions, probably in conjunction with a to add these permissions for all users.
83484.book Page 47 Monday, September 18, 2006 8:58 AM Task 1.9: Write Basic Scripts 47 you can install in Linux as a command—even another script. The following list includes a few commands that are commonly used in scripts: Normal file-manipulation commands The file-manipulation commands, such as ls, mv, cp, and rm, are often used in scripts. You can use these commands to help automate repetitive file maintenance tasks. grep This command is described earlier, in “Piping Data between Programs” in Task 1.
83484.book Page 48 Monday, September 18, 2006 8:58 AM 48 Phase 1 Working on the Command Line Variables that are passed to the script are frequently called parameters. They’re represented by a dollar sign ($) followed by a number from 0 up—$0 stands for the name of the script, $1 is the first parameter to the script, $2 is the second parameter, and so on. To understand how this might be useful, consider the task of adding a user. As described earlier, in Task 1.
83484.book Page 49 Monday, September 18, 2006 8:58 AM Task 1.9: Write Basic Scripts 49 For instance, consider Listing 1.4, which displays the current IP address of the computer on which it runs. This script uses the variable $ip, which is extracted from the output of ifconfig using grep and cut commands. (The trailing backslash on the second line of the script indicates that the following line is a continuation of the preceding line.
83484.book Page 50 Monday, September 18, 2006 8:58 AM 50 Phase 1 Working on the Command Line To better understand the use of conditionals, consider the following code fragment: if [ -s /tmp/tempstuff ] then echo "/tmp/tempstuff found; aborting!" exit fi This fragment causes the script to exit if the file /tmp/tempstuff is present. The then keyword marks the beginning of a series of lines that execute only if the conditional is true, and fi (if backwards) marks the end of the if block.
83484.book Page 51 Monday, September 18, 2006 8:58 AM Task 1.9: Write Basic Scripts 51 while [ condition ] do commands done The until loop is similar in form, but it continues execution for as long as its condition is false—that is, until the condition becomes true. Using Functions A function is a part of a script that performs a specific sub-task and that can be called by name from other parts of the script.
83484.book Page 52 Monday, September 18, 2006 8:58 AM 52 Phase 1 Working on the Command Line If you enter Listing 1.6 and call it safercp, you might use it like this, assuming the file original.txt exists and dest.txt does not: $ ./safercp original.txt dest.txt $ ./safercp original.txt dest.txt Target file exists! Exiting! The first run of the command succeeded because dest.txt did not exist.