Chapter 1. Getting Started

Before you can use Unix, a system staff person has to set up a Unix account for you. The account is identified by your username, which is usually a single word or an abbreviation. Think of this account as your office—it’s your place in the Unix environment. Other users may also be at work on the same system. At many sites, there will be a whole network of Unix computers. So in addition to knowing your username, you may also need to know the hostname (name) of the computer that has your account. Alternatively, your account may be shared between all computers on the local network, and you may be able to log into any of them.

Once you’ve logged in to your account, you’ll interact with your system by typing commands at a command line, to a program called a shell. You’ll get acquainted with the shell, enter a few commands, and see how to handle common problems. To finish your Unix session, you’ll log out.

Each user communicates with the computer from a terminal. To get into the Unix environment, you first connect to the Unix computer. (Your terminal is probably already connected to a computer.[1] But Unix systems also let you log into other computers across a network. In this case, log into your local computer first, then use a remote login command to connect to the remote computer. See Section 6.1 in Chapter 6.)

After connecting your terminal, if needed, you start a session by logging in to your Unix account. To log in, you need your username and a password. Logging in does two things: it identifies which user is in a session, and it tells the computer that you’re ready to start work. When you’ve finished, log out—and, if necessary, disconnect from the Unix computer.

Warning

If someone else has your username and password, they probably can log into your account and do anything you can. They can read private information, corrupt or delete important files, send email messages as if they came from you, and more. If your computer is connected to a network—the Internet or a local network inside your organization—intruders may also be able to log in without sitting at your keyboard! See Section 6.1 in Chapter 6 for one explanation of one way this can be done.

Anyone may be able to get your username—it’s usually part of your email address, for instance. Your password is what keeps others from logging in as you. Don’t leave your password anywhere around your computer. Don’t give your password to anyone who asks you for it unless you’re sure they’ll preserve your account security. Also don’t send your password by email; it can be stored, unprotected, on other systems and on backup tapes, where other people may find it and then break into your account.

If you suspect that someone is using your account, ask system staff for advice. If you can’t do that, setting a new password may help; see Section 3.6 in Chapter 3.

Unix systems are case sensitive. Most usernames, commands, and filenames use lowercase letters (though good passwords use a mixture of lower- and uppercase letters). Before you log in, be sure your CAPS LOCK key is off.

The process of making yourself known to the computer system and getting to your Unix account is called logging in. If you’ve connected to the Unix host from another operating system, you may have been logged into Unix automatically; in this case, you should be able to run Unix programs, as shown later in this chapter in Section 1.1.4 and Section 1.1.5. Otherwise, before you can start work, you must connect your terminal or terminal window to the computer you need (as in the previous section) and identify yourself to the Unix system.

There are generally two ways to log in: graphically and nongraphically. If your screen has a window or windows floating in it, something like Figure 2-2A, you probably need to log in graphically, as explained by Section 2.2.1 in Chapter 2.

Otherwise, to log in nongraphically, enter your username (usually your name or initials) and your private password. The password does not appear as you enter it.

When you have logged in successfully, you’ll get some system messages and finally the shell prompt (where you can enter Unix commands). A successful login to the system named nutshell could look like Example 1-1.

In this example, the system messages include a maintenance notice, a “fortune,” and the date. Although this example doesn’t show it, you may be asked for your terminal type, accounting or chargeback information, and so on. The last line to appear is the Unix shell prompt. When you reach this point, you’re logged in to your account and can use Unix commands.

Instead of a shell prompt, you may get a menu of choices (“email,” “news,” and so on). If one choice is something like “shell prompt” or “command prompt,” select it. Then you’ll be able to follow descriptions and examples in this book.

The messages you see at login time differ from system to system and day to day. Shell prompts can also differ. Examples in this book use the currency sign $ as a prompt.

Let’s summarize logging in nongraphically, step by step:

The system checks your account name and password, and if they’re correct, logs you in to your account.

Nothing seemed to happen after I logged in.

Wait a minute, since the system may just be slow. If you still get nothing, ask other users if they have the same problem.

The system says “login incorrect.”

If you have a choice of computer to log into (as we explained at the start of this chapter in Section 1.1), check that you’re connected to the right computer. If you have accounts on several computers, be sure you’re using the correct username and password for this computer. Otherwise, try logging in again, taking care to enter the username and password correctly. Be sure to type your username at the “login:” prompt and your password at the “password:” prompt. Backspacing may not work while entering either of these; if you make a mistake, use RETURN to get a new “login:” prompt and try again. Also make sure to use the exact combination of upper- and lowercase letters your password contains.

If you still fail after trying to log in a few more times, check with the person who created your account to confirm your username and password.

All letters are in UPPERCASE and/or have backslashes (\) before them.

You probably entered your username in uppercase letters. Type exit at the shell prompt and log in again.

Shells in a Window System

If you’re using a window system, as described in Chapter 2, get a shell by opening a terminal window—if you don’t already have a terminal window open or iconified (minimized) somewhere, that is. (Figure 2-1 shows an example, but yours may look different; the important thing is that the window have a shell prompt in it.) Check your menus and icons for a command with “terminal” or “term” in its name, or a picture of a blank terminal (like a TV screen) in its icon; one common program is xterm.

Entering a Command Line

Entering a command line at the shell prompt tells the computer what to do. Each command line includes the name of a Unix program. When you press RETURN, the shell interprets your command line and executes the program.

The first word that you type at a shell prompt is always a Unix command (or program name). Like most things in Unix, program names are case sensitive; if the program name is lowercase (and most are), you must type it in lowercase. Some simple command lines have just one word, which is the program name. For more information, see Section 1.2, later in this chapter.

Correcting a Command Line

What if you make a mistake in a command line? Suppose you typed dare instead of date and pressed the RETURN key before you realized your mistake. The shell will give you an error message:

$ dare
dare: command not found
$

Don’t be too concerned about getting error messages. Sometimes you’ll get an error even if it appears that you typed the command correctly. This can be caused by typing control characters that are invisible on the screen. Once the prompt returns, reenter your command.

As we said earlier (in Section 1.1.7) most modern shells let you recall previous commands and edit command lines. If you’ll do a lot of work at the shell prompt, it’s worth learning these handy techniques. They take more time to learn than we can spend here, though—except to mention that, on those shells, the left-arrow and right-arrow keys may move your cursor along the command line to the point where you want to make a change. Here, let’s concentrate on simple methods that work with all shells.

If you see a mistake before you press RETURN, you can use the erase character to erase and correct the mistake.

The erase character differs from system to system and from account to account, and can be customized. The most common erase characters are:

CTRL-H is called a control character. To type a control character (for example, CTRL-H), hold down the CTRL key, then press the letter “h.” In the text, we will write control characters as CTRL-H, but in the examples, we will use the standard notation: ^H. This is not the same as pressing the ^ (caret) key, letting go, and then typing an H!

The key labeled DEL may be used as the interrupt character instead of the erase character. (It’s labeled DELETE or RUBOUT on some terminals.) This key is used to interrupt or cancel a command, and can be used in many (but not all) cases when you want to quit what you’re doing. Another character often programmed to do the same thing is CTRL-C.

Other common control characters are:

Find the erase and interrupt characters for your account and write them here:

_______ Backspace and erase a character

_______ Interrupt a program

Logging Out

To end a Unix session, you must log out. You should not end a session by just turning off your terminal!

If you’re using a window system, first close open windows and then close the window system; see Section 2.9 in Chapter 2 for more information. If you logged in graphically, that should end your login session. But, if you logged in nongraphically before you started the window system, closing the window system should take you back to a shell prompt (where you originally typed xinit or startx). In that case, use the following steps to finish logging out.

If you aren’t currently using a window system, you can log out by entering the command exit at a shell prompt. (In many cases, the command logout will also work.) Depending on your shell, you may also be able to log out simply by typing CTRL-D.

What happens next depends on the place from which you’ve logged in: if your terminal is connected directly to the computer, the “login:” prompt should appear on the screen. Otherwise, if you were connected to a remote computer, the shell prompt from your local computer should reappear on your screen. (That is, you’re still logged in to your local computer.) Repeat the process if you want to log out from the local computer.

After you’ve logged out, you can turn off your terminal or leave it on for the next user. But, if the power switch for your terminal is the same as the power switch for the whole Unix computer system, do not simply turn off that power switch! Ask a local expert for help with shutting down your Unix system safely.

Unix command lines can be simple, one-word entries such as the date command. They can also be more complex; you may need to type more than the command or program name.[4]

A Unix command may or may not have arguments. An argument can be an option or a filename. The general format for Unix command lines is:

There isn’t a single set of rules for writing Unix commands and arguments, but you can use these general rules in most cases:

Unix has a lot of commands! Don’t try to memorize all of them. In fact, you’ll probably need to know just a few commands and their options. As time goes on, you’ll learn these commands and the best way to use them for your job. We cover some useful Unix commands in later chapters. This book’s quick reference card has quick reminders.

Let’s look at a sample Unix command. The ls program displays a list of files. You can use it with or without options and arguments. If you enter:

$ ls

you’ll see a list of filenames. But if you enter:

$ ls -l

there’ll be an entire line of information for each file. The –l option (a dash and a lowercase letter “L”) changes the normal ls output to a long format. You can also get information about a particular file by using its name as the second argument. For example, to find out about a file called chap1, enter:

$ ls -l chap1

Many Unix commands have more than one option. For instance, ls has the –a (all) option for listing hidden files. You can use multiple options in either of these ways:

$ ls -a -l
$ ls -al

You must type one space between the command name and the dash that introduces the options. If you enter ls–al, the shell will say “ls–al: command not found.”

The best way to get used to Unix is to enter some commands. To run a command, type the command and then press the RETURN key. Remember that almost all Unix commands are typed in lowercase.

Get today’s date.

Enter date

List logged-in users.

Enter who

Obtain more information about users.

Enter who -u or finger or w

Find out who is at your terminal.

Enter who am i

Enter two commands in the same line.

Enter who am i;date

Mistype a command.

Enter woh

In this session, you’ve tried several simple commands and seen the results on the screen.

When you use a program, you’ll want to know how to control it. How can you tell it what job you want done? Do you give instructions before the program starts, or after it’s started? There are three general ways to give commands on a Unix system, three different kinds of programs. It’s good to be aware of them.

  1. Some Unix programs work only with a window system. For instance, when you type netscape at a shell prompt (or click a button or choose the command from a menu), the Netscape web browser starts. It opens one or more windows on your screen. The program has its own way to receive your commands—through menus and buttons on its windows, for instance.

  2. You’ve also seen (previously, in Section 1.2) Unix commands that you enter at a shell prompt. These programs work in a window system (from a terminal window) or from any terminal. Control those programs from the Unix command line—that is, by typing options and arguments from a shell prompt before you start the program running. After you start the program, wait for it to finish; you generally don’t interact with it.

  3. Some Unix programs that work in terminals have commands of their own. (If you’d like some examples, see Section 3.2 in Chapter 3 and Section 4.3.2 in Chapter 4.) These programs may accept options and arguments on their command line. But, once you start the program, it prints its own prompt and/or menus and it understands its own commands; it takes instructions from your keyboard, which weren’t given on its command line.

    For instance, if you enter pine at a shell prompt, you’ll see a new prompt from the pine program. Enter Pine commands to handle email messages. When you enter the special command q to quit the pine program, pine will stop prompting you. Then you’ll get another shell prompt, where you can enter other Unix commands.

The Unresponsive Terminal

During your Unix session (while you’re logged in), your terminal may not respond when you type a command, or the display on your screen may stop at an unusual place. That’s called a “hung” or “frozen” terminal or session.

(Note that most of the techniques in this section apply to terminal windows in a window system, but not to nonterminal windows such as a web browser. In Chapter 2, Section 2.7 should help with windows in general.)

A session can hang for several reasons. For instance, the connection between your terminal and the computer can get too busy; your terminal has to wait its turn. (Other users or computers probably share the same connection.) In that case, your session starts by itself in a few moments. You should not try to “un-hang” the session by entering extra commands because those commands will all take effect after the connection resumes.

If the system doesn’t respond for quite a while (how long that is depends on your individual situation; ask other users about their experiences), the following solutions usually work. Try the following steps in the order shown until the system responds:

  1. Press the RETURN key once.

    You may have typed text at a prompt (for example, a command line at a shell prompt) but haven’t yet pressed RETURN to say that you’re done typing and your text should be interpreted.

  2. If you can type commands, but nothing happens when you press RETURN, try pressing LINEFEED or typing CTRL-J. If this works, your terminal needs resetting to fix the RETURN key. Some systems have a reset command that you can run by typing CTRL-J reset CTRL-J. If this doesn’t work, you may need to log out and log back in or turn your terminal off and on again. (But, before you turn off your terminal, read the notes earlier and later in this chapter about turning off the power.)

  3. If your shell has job control (see Chapter 7), type CTRL-Z.

    This suspends a program that may be running and gives you another shell prompt. Now you can enter the jobs command to find the program’s name, then restart the program with fg or terminate it with kill.

  4. Use your interrupt key (found earlier in this chapter in Section 1.1.8—typically DELETE or CTRL-C).

    This interrupts a program that may be running. (Unless a program is run in the background, as described in Section 7.1 in Chapter 7, the shell waits for it to finish before giving a new prompt. A long-running program may thus appear to hang the terminal.) If this doesn’t work the first time, try it once more; doing it more than twice usually won’t help.

  5. Type CTRL-Q.

    If output has been stopped with CTRL-S, this will restart it. (Note that some systems will automatically issue CTRL-S if they need to pause output; this character may not have been typed from the keyboard.)

  6. Check that the NO SCROLL key (if you have one) is not locked or toggled on.

    This key stops the screen display from scrolling upward. If your keyboard has a NO SCROLL key that can be toggled on and off by pressing it over and over, keep track of how many times you’ve pressed it as you try to free yourself. If it doesn’t seem to help, be sure you’ve pressed it an even number of times; this leaves the key in the same state it was when you started.

  7. Check the physical connection from the terminal to the system.

  8. Type CTRL-D once at the beginning of a new line.

    Some programs (such as mail) expect text from the user. A program may be waiting for an end-of-input character from you to tell it that you’ve finished entering text. Typing CTRL-D may cause you to log out, so you should try this only as a last resort.

  9. If you’re using a window system, close (terminate) the window you’re using and open a new one. See Section 2.7 in Chapter 2.

    Otherwise, turn your terminal off, wait ten seconds or so, then turn it on again. This may also log you out, but it may not; your old login session could still be running. You can check for old processes and terminate them (as explained in Chapter 7 in Section 7.2 and in Section 7.3)—although this isn’t an easy thing for a beginner to do, so you might want help.



[1] Some terminals can connect to many computers through a kind of switchboard called a port contender or data switch. On these terminals, start by telling the port contender which computer you want to connect to.

[2] To find out which shell you’re using, run the commands echo $SHELL and ps $$. (See Section 1.1.6, later in this chapter.) The answer, something like bash or /bin/bash, is your shell’s name or pathname.

[3] This can happen if you’re using a window system that was started by the superuser when the system was rebooted. Or maybe your prompt has been customized to end with # when you aren’t the superuser.

[4] The command can be the name of a Unix program (such as date), or it can be a command that’s built into the shell (such as exit). You probably don’t need to worry about this! You can read more precise definitions of these terms and others in Glossary.