Lab 03: Remote Login and UNIX Shell Commands

Objectives

In this lab, you will learn how to use UNIX commands to navigate through the file system of a remote Unix computer and create files there.

You will be using the web to publish parts of your completed assignments (homeworks, labs, and projects) throughout the quarter. Therefore, you must know how to upload files to your web directory on dante. This will be a useful skill throughout your college career here, so take the time to learn how to do it now.

UNIX is an operating system like Microsoft Windows, Mac OSX and Linux. An operating system allows you to run programs on a computer. Unix is often used for server systems like dante, and so it is useful to have some knowledge about how to navigate through the file system and create simple text files.

Key Words

UNIX, operating system, remote login, SSH, shell, bash, pwd, ls, cd, home directory, /, ., .., parent directory, mkdir, pico, more, cp, mv, rm, rmdir, chmod, exit.

Preparation

You should have completed the objectives in the previous labs before doing this lab. If you have trouble with logging on to the remote system dante, go back and review the previous labs (Lab 01 and Lab 02).

Remote Login

To upload and download your files to/from a remote computer, you must log in to the remote computer. For this lab, you will use a program named SSH Secure Shell to login to dante.

SSH stands for Secure SHell and is both a program and a standardized method (protocol) for communicating between computers in a secure/encrypted manner. The SSH client (the application that runs on your desktop machine) is installed in the UW labs. It is also available for installation on your own machine as part of the UWICK kit available from the UW (on CD at the bookstore or online).

  1. Start SSH (SSH Secure Shell) SSH icon or SFTP (SSH Secure File Transfer) SFTP icon.
  2. Click on the Quick Connect icon Quick Connect.
  3. A window named Connect to Remote Host will pop up. For the host name, enter dante.u.washington.edu and for the user name, enter your UW NetID. If your UW NetID was smith, you would enter the following:

    SSH connect dialog

    Enter your password in the next pop-up window to finish connecting to dante.

  4. If you connected using SFTP, you need to open a shell by clicking on its icon SSH icon.
  5. If you connected using SSH or opened a shell, then you will see the welcome menu for dante.
  6. Press S to start a shell in which you can use UNIX commands.

A shell is a program in which you type commands to tell a computer what to do. In Windows, the MS-DOS or Command Prompt is Microsoft's version of a shell. In Mac OSX, the Terminal provides the shell (which is in fact a Unix shell similar to the one on dante).

Use UNIX Commands

To do anything in a shell, you must use commands. This section explains how to use UNIX commands in a UNIX shell to make a computer do things for you.

There are several different shells that can be used in UNIX. When you first logged in to dante, a shell named psh (pronounced: p-shell) displayed the welcome menu. When you pressed on S, you started a shell named csh (pronounced: c-shell).

  1. Begin by changing your shell to bash (pronounced: bash), since bash has certain default features that make it easier for you to navigate through directories. Type the command bash and press the enter key. The prompt will now change from dantexx% to bash-2.01$. See the example below. (Bold text indicates what you type, followed by the enter key.)

    Note, nothing may change as the default shell will probably be bash. If you want to know what your current shell is, you can type: echo $SHELL
    dante04% bash
    bash-2.01$ 
    

    A prompt, or command prompt, is a short line of text that a shell displays when it is ready for you to enter a command. Some example prompts are dante04%, bash-2.01$, and C:\WINDOWS>.

  2. If you type a command that does not exist, bash will complain with command not found and give you the prompt again so that you can type another command. Try the following:
    bash-2.01$ BASH
    bash: BASH: command not found
    bash-2.01$ 
    

    All UNIX commands are case sensitive. This means that typing bash is not the same as typing Bash, BASH, or baSH.

  3. Some commands are useful for finding where you are in the directory structure and for showing what is in a directory. The command pwd shows you your present work directory, that is, the directory you are currently located in. The command ls will list the contents of your present work directory (or other directories that are specified after ls). The command cd (change directory) will change the present work directory to the one you specifiy. Typing cd by itself will change you back to your home directory. Try using the same commands as the user smith (you will see your UW NetID instead of smith and the actual contents of your files and directories may differ):
    bash-2.01$ pwd
    /···/smith
    bash-2.01$ ls
    public_html
    bash-2.01$ cd public_html
    bash-2.01$ pwd
    /···/smith/public_html
    bash-2.01$ ls
    fit100     index.html
    bash-2.01$ cd fit100
    bash-2.01$ pwd
    /···/smith/public_html/fit100
    bash-2.01$ ls
    lab1
    bash-2.01$
    

    Note that Unix uses / (forward slash) to separate the directories in a pathname. Windows uses the \ (back slash) instead , e.g., C:\WINNT\Temp.

  4. You must use a / (with no spaces before or after the slash) to separate the name of a directory from its subdirectory (and subsubdirectory, etc).

    bash-2.01$ cd public_html/fit100
    bash-2.01$ pwd
    /···/smith/public_html/fit100
    bash-2.01$ ls
    lab1
    bash-2.01$
    

    The command cd public_html/fit100 means to change the present work directory to fit100, which is a subdirectory of public_html (fit100 is located in/under public_html).

  5. There are certain directories that always exist. They are named . (dot) and ..(dot dot). However, all file names and directory names beginning with a dot are hidden. You can see hidden files and directories by adding the flag -a (for "all") to the ls command. To see more information about each file and directory, you can use the -l flag (lowercase L for "long") with ls. You can specify the switches separately or one right after the other. Try the following:
    bash-2.01$ ls -a
    .     ..    lab1
    bash-2.01$ ls -l
    total 8
    drwxr-sr-x   2 smith   www          512 Oct  5 11:44 lab1
    bash-2.01$ ls -a -l
    total 24
    drwxr-sr-x   3 smith   www          512 Oct  5 11:44 .
    drwxr-s---   6 smith   www          512 Oct  5 11:44 ..
    drwxr-sr-x   2 smith   www          512 Oct  5 11:44 lab1
    bash-2.01$ ls -al
    total 24
    drwxr-sr-x   3 smith   www          512 Oct  5 11:44 .
    drwxr-s---   6 smith   www          512 Oct  5 11:44 ..
    drwxr-sr-x   2 smith   www          512 Oct  5 11:44 lab1
    bash-2.01$
    

    The directory names . (dot) .. (dot dot) always exist in a directory. The . refers to the directory itself and .. to its parent directory.

  6. To create/make a directory, you can use the command mkdir (make directory). Make a directory named lab3 in your fit100 directory and then change/go to the tmp directory. An example follows:
    bash-2.01$ mkdir lab3
    bash-2.01$ mkdir tmp
    bash-2.01$ ls
    lab1  lab2  lab3  tmp
    bash-2.01$ cd lab3
    bash-2.01$ pwd
    /···/smith/public_html/fit100/lab3
    bash-2.01$ ls
    bash-2.01$ ls -al
    total 16
    drwxr-sr-x   2 smith   www          512 Oct  5 11:48 .
    drwxr-sr-x   4 smith   www          512 Oct  5 11:48 ..
    bash-2.01$
    
  7. Now check which directory you are currently in and then change/go up one directory level using cd and the .. directory name:
    bash-2.01$ pwd
    /···/smith/public_html/fit100/lab2
    bash-2.01$ cd ..
    bash-2.01$ pwd
    /···/smith/public_html/fit100
    bash-2.01$
    
  8. Now change back to your home directory using a cd by itself.

    bash-2.01$ cd
    bash-2.01$ pwd
    /···/smith
    bash-2.01$ 
    

Create and Edit Files with pico

To create and edit text files on a UNIX computer, you can use the text editing program named pico. Examples of other text editing programs for UNIX computers are emacs and vi. Examples of text editing programs for Windows are Notepad jEdit and Emacs.

pico functions sort of like the compose window in Pine. (Pine was covered in Lab 01.) Unfortunately, the commands are not exactly the same, so you have to pay careful attention to the list at the bottom of the screen.

  1. You start pico by typing its name. If you include another word after its name (e.g., notes.txt), pico will try to open a file with that name. If the file does not exist, an empty file will be opened with the words New file at the bottom of the pico window.
    bash-2.01$ pwd
    /···/smith/
    bash-2.01$ cd public_html/fit100/lab3
    /···/smith/
    bash-2.01$ pwd
    /···/smith/public_html/fit100/lab3
    bash-2.01$ pico notes.txt
    

    pico window

    If your SSH window is narrower than 78 columns wide, pico may not display properly. Increase the window width to at least 78 or 80 columns by dragging on the lower right corner of the window. A typical normal window size is 80x25.

  2. Type some text into the pico window.
  3. At the bottom of the window you should see a list of possible commands for pico. You activate a command by pressing its shortcut, which is the Ctrl key followed by a single letter. For example, ^O means to press Ctrl and the letter O to WriteOut (aka: save to a file).

    Save what you just wrote in a file named notes.txt. Enter the file name, if it is not already shown, and press the Enter key. pico responds by telling you the number of rows you saved to the file, e.g., Wrote 6 lines (the sixth line in the example is an empty line at the end of the file, that the cursor is located on).

    pico window

  4. Exit pico and check to see if your newly created file exists. You can see the contents of a file by using the more command followed by the name of the file you would like to view.
    bash-2.01$ ls
    notes.txt
    bash-2.01$ more notes.txt
    Mary had a little lamb,
    little lamb,  
    little lamb.  
    Mary had a little lamb.
    Its fleece was white as snow.
    
    bash-2.01$
    

More Unix commands

  1. You can copy files and directories using the cp (copy) command. To use this command, you write cp followed by the name of what you would like to copy (the source) followed by a new name (the destination), i.e., cp source destination.

    bash-2.01$ ls
    notes.txt
    bash-2.01$ cp notes.txt notes2.txt
    bash-2.01$ ls
    notes.txt    notes2.txt
    
  2. You can rename a file using the mv (move) command. To use this command, you write mv followed by the name of the existing file (the source) followed by the new name (the destination), i.e., mv source destination.

    bash-2.01$ ls
    notes.txt    notes2.txt
    bash-2.01$ mv notes2.txt sample.txt
    bash-2.01$ ls
    notes.txt    sample.txt
    
  3. You can also move a file to another directory using the mv (move) command. To do this, you write mv followed by the name of the existing file (the source) followed by the name of the directory to move it to (the destination), i.e., mv source destination.

    bash-2.01$ ls
    notes.txt    sample.txt
    bash-2.01$ mv sample.txt ../tmp
    bash-2.01$ ls
    notes.txt
    bash-2.01$ ls ../tmp
    sample.txt
    
  4. To remove/delete a file, you use the rm (remove) command. Note that you can not remove the two special dot directories: . and ...
    NOTE: Anything that you remove/delete with rm is permanently removed and can not be recovered!
  5. To remove/delete directories, you use the rmdir (remove directory) command. You can only remove empty directories, that is, directories that contain no files or subdirectories (except for the two special dot directories: . and ..). Note that you can not remove a directory that is your present work directory.
    NOTE: Anything that you remove/delete with rmdir is permanently removed and can not be recovered!
  6. Make sure that all of your files can be read from a web browser. Open a web browser and use it to go to your fit100 directory, e.g., http://students.washington.edu/smith/fit100/. You should see your lab2 directory. Select it. Now you should be able to see all of your files.

    If you are unable to see any files/directories in your fit100 directory or it it says Forbidden, then you need to change your access permissions. Use the shell to go to your fit100 directory and run the chmod (change mode) command. Note that the chmod command below uses a capital R in the flag and a capital X followed by a single dot.

    bash-2.01$ cd
    bash-2.01$ cd public_html/fit100/
    bash-2.01$ chmod -R a+rx .
    bash-2.01$ 
    

    If you are interested in knowing how chmod works, read its manual by running man chmod. This command will be discussed in more detail later.

  7. Now that you are done working with the shell, exit all shells by using the command exit.
    bash-2.01$ exit
    exit
    dante04% exit
    
    (select 'L' from the menu to logout of your remote login shell)
    

    The first exit is for exiting bash. The next exit is for exiting csh. The last L is for exiting psh.

Check List

______I understand the keywords for this lab and can give examples of what they mean.
______I can remotely login to and use a UNIX shell on dante.
______I know how to use the UNIX commands bash, pwd, ls, cd, mkdir, pico, more, cp, mv, chmod, and exit.
______I know what the . and .. directories are.
______I can create and edit files with pico.

You do not need to submit anything when you have completed this lab.

Help & Resources