Week 1

Notes 1 : Basic Usage of the CS/CEE Department Server

References:

The purpose of this is to learn how to use the command-line environment used for CMPS 2010 assignments. You will learn how to login, logout, change your password, create a file, view files, and use the email client. In addition, create and manage directories, view and manipulate file permissions, compile a program, and manage your processes (executables).

Read the entire contents of this page before beginning the lab work

INTRODUCTION
The CEE/CS uses a Linux server as the programming environment for the CMPS 2010 and 2020 sequence. This server uses a text-based interface called a command-line interface (CLI). In a CLI, you type commands to accomplish tasks instead of clicking on icons. This course will introduce you to the basic commands you will need in the programming sequence.

The department server is called Odin. It is accessible over the Internet by using the domain name 'odin.cs.csubak.edu' and an appropriate client to connect to Odin's CLI (called a terminal program). The terminal program will present you with a command-line prompt (such as the $ or > symbols). You can type commands at the prompt to initiate tasks.

Here are some examples of what a command-line prompt might look like (your prompt by default will look like the first two, but with your username before the @ symbol):
rowdyrunner@odin:~$
rowdyrunner@odin:~/cs2010$
(/home/stu/rowdyrunner/)>
(/home/stu/rowdyrunner/cs2010/)>

Typography Conventions

These typography conventions will be used throughout the semester to indicate how to properly create a command to type at the command-line prompt.

  • Items in fixed font are commands you should type in the terminal program to accomplish the task. Phrases after # or // are comments on the command and should NOT be typed.
    Example: man ssh // This gives the man page on the ssh command. You would just type man ssh for the command.
  • In a command, any phrase inside <angle-brackets> indicates a field that you MUST fill in for the command to work. When you give the field, you do NOT use angle-brackets, you just give the required item. For example, the phrase <filename> in the command less <filename> means you must give a filename for the less command to work, such as less lab1.cpp
  • In a command, any phrase inside [square-brackets] indicates an optional field that you can choose to use for the command. Again, when you give the field, you do not use the brackets. For example, the command ls -l [filename] can be correctly given as either ls -l or with a filename such as ls -l lab1.cpp

HOW TO LOGIN AND LOGOUT

You should have received an account sheet with your username and initial password for Odin at the start of the seminar. If you do not have an account sheet, see the instructor or a class assistant.

To log in, you must connect over the Internet using the SSH protocol, which encrypts your Internet traffic so your data cannot be easily intercepted. You can log in from any place that has an SSH client and Internet connection. The department lab machines already have an SSH client installed, as do most of the campus computers in the basement of the library. You can also install an SSH client on your home machine or smart phone to connect to Odin from off campus.

A popular program for Windows is Putty. You can download Putty from the department downloads page. Putty is a self-contained executable, which means it does not have to be installed. Just download it to a convenient location (such as your desktop) and click to launch it. Mac OS X should already have an SSH client installed which is very similar to the SSH client used on the department lab machines. There are no official recommendations for smart phone SSH clients at this time, but be sure to check reviews for any app that you are considering to be sure that the app is trusted before using it to login.

To connect to Odin from the department Linux machines or Mac OS X, follow these instructions:

  1. Open the terminal application (the icon on the department lab machines that looks like a small, blank monitor or the Terminal program on Mac OS X).
  2. On the department machines, type the command ssh <username>@odin at the prompt. Enter your username in all lowercase and without the angle-brackets. From off campus or the campus wireless network, you will need to use the full domain name, so the command is ssh <username>@odin.cs.csubak.edu
  3. On the department machines, type the command ssh <username>@odin at the prompt. Enter your username in all lowercase and without the angle-brackets. From off campus or the campus wireless network, you will need to use the full domain name, so the command is ssh <username>@odin.cs.csubak.edu

Connecting from Putty on Windows is slightly different. Putty does not actually present you with a prompt to type the ssh command. Instead, it does the ssh command for you after you fill out a connection dialog entry. To connect from Putty on Windows, do the following:

  1. Double-click Putty to open it. This will bring up the connection dialog box for you to fill in.
  2. In the connection dialog box, set the Host Name (or IP address) field to: odin.cs.csubak.edu
  3. In the connection dialog box, set the Protocol field to: ssh
  4. In the connection dialog box, verify that the Port field is 22. (Optional: once verified, you can save the connection information by typing a name for the connection in the lower saved session area and pressing Save)
  5. Press Open to establish the connection.
  6. Enter your account username in all lowercase exactly as given on the account sheet. DO NOT type the command ssh <username>@odin.cs.csubak.edu. JUST give your username. You have already told Putty to do the SSH command to Odin when you filled out the connection dialog box, so you just need to give your username now.
  7. Enter your password (case sensitive) as given on your account sheet or as you set it (if you opt to change your password later in the lab).

Always make sure to logoff when you are done using the department server for the day. If you forget to logoff when you are using the department lab machines, the next student to use that machine can access your account, which means they can do anything to your account (including deleting all of your files). To logout, type the command exit or CTRL+d. Do NOT just click the x in the corner of the terminal program to close the terminal without first typing the exit command. If you close the terminal program while you are in the CLI email client, you might essentially lock yourself out of the email client. And if you close the terminal program while in the file editor, you will create a swap file that may or may not have the latest version of your program in it.

HOW TO PICK A SECURE PASSWORD

The first command you will perform in the lab is to change your password from the default password to one you have chosen. You want a password you can remember and yet one that is secure. A secure password does not contain ANY word that can be looked up in a dictionary (hello99 is not secure), should contain at least one uppercase letter and one number (preferably, it would also include symbols like ! # = and so on), and should not be used for ANY of your other accounts.

There are many ways to pick a secure password. One trick is the following: Pick a line in your favorite song or a well-known phrase. Take the first letters of every word in that line/phrase. Change one letter to a number and one letter to uppercase in a way that is easy to remember. For example, "the first noel the angels did say" becomes "t1ntAds" (where first is the number 1 and angels is capitalized). You now have a password that is easy to remember but difficult to crack.

Another option is to go here: http://www.cs.csubak.edu/cgi-bin/passwordgen.cgi This utility will generate a secure and easy to remember password for you using a technique called "pronounceable passwords". Select one word from the list that you can easily remember. If you don't like any of the words, reload the page for a new list.

You can also use a password vault program on your smart phone, but be aware that not all faculty members will let you use your smart phone in class, so if you create a long, random password that you can't remember without your phone, you might have problems.

The command to change your password is passwd It will prompt you for your current password, which must be entered exactly. You will then be prompted to enter your new password twice. If the two entries do not match exactly (such as a typo), the command will fail and you will have to try again. If the command succeeds, it will just return you to the prompt without giving any message -> no returned message means successful.

HOW TO ACCESS HELP FILES

All the basic commands covered in this class have help files on them. These are called the "manpages" because they are associated with the man command. To start viewing a man page, give the following command: man <command> This will bring up one or more pages of information on the command. For multi-page information, use the space bar (or page down) key to go forward a page and the 'b' key (or page up) to go back a page. To exit the manpage viewer, hit the 'q' key.

USING A TEXT EDITOR

All of your programs for CMPS 2010 will be created by writing a simple text file that contains your source code. The easiest way to do this is to issue a text editor command to start a text editor program. On Odin, the best text editor for creating your source code is the vi or vim (visual editor improved) text editor.

This editor may seem a little intimidating at first, but it has many benefits, including color-coded syntax highlighting and automatic code indentation. All students are strongly encouraged to learn basic vi/vim commands. Students may also want to look into the intermediate vim commands given in the vi Reference Card, as these can make the coding experience faster.

The primary concept to grasp with vi/vim is the concept of modes. vi/vim has three main modes: insert mode, command mode and last-line mode. Insert mode is where you can add new content to the file, such as typing a new line of code. Command and last-line mode allow you to edit the file, such as deleting a word, saving the changes, and exiting vi/vim. All commands given in command mode execute as soon as you finish typing the command (without hitting enter). All last-line commands require you to hit enter for the command to execute. By default, vi/vim starts in command mode. Before you can make any changes, you have to enter insert mode. In order to save the changes, you have to leave insert mode and go back to command mode. Everyone should learn these basic commands as covered below:

To start vi/vim, issue the following command (note that on Odin vi actually starts vim): vi <filename> Note: Filenames on Linux/UNIX do not have spaces in them, so make sure the filename is all letters, numbers and underscore characters.

To enter into insert mode, hit the i key. You can then type what you wish to type and it will appear on the screen. You may also alter the contents of the file, such as deleting portions or using the arrow keys to navigate to another part of the file to alter it. Insert mode in vim works similarly to pico (but be aware in older systems the arrow keys will not work in insert mode). While you are in insert mode, the word '-- INSERT --' will appear at the bottom of your screen.

To go back to command mode, hit the ESC key. Notice that the word '-- INSERT --' disappears, this indicates that you're back in command mode. If in doubt, hit ESC again. Think of the ESC key as your rescue button. If you can't remember what mode you are in, hit ESC. ESC will cancel any partial commands (but not any completed commands) and take you back to command mode. Anything you entered in insert mode will be preserved in vim's working buffer and will still be displayed on the screen.

To undo the last change, go back to command mode and hit the u key. The last change is everything done in the last insert mode session or the last command executed while in command or last-line mode. To redo the last change, give the command CTRL-R from command mode. This is helpful when you accidently hit the u key instead of the i key.

To save the file, go back to command mode and give the last-line command :w and hit the enter key. When you hit the colon key, a colon will appear at the very last line of your window. Then when you type w, it will appear at the bottom of the screen. Hit enter to execute the save command and the last line will be updated with information about the file.

Sometimes you will be given a source code example on an assignment that you need to copy into your source code file and modify. When using vi, you must first tell vi that you are pasting code, or it will automatically indent the code in an odd fashion.

To enable copy/paste from the mouse, give the following last-line vi commands. You cannot be in insert mode when giving these commands, so be sure to hit ESC first:

  • :set paste - Give this command before entering insert mode to enable correct pasting. This disables automatic indenting so the text will be pasted correctly.
  • :set nopaste - Give this command after pasting in insert mode (hit ESC to go back to command mode). This will enable automatic indenting when you go back into insert mode.

To exit vim, go back to command mode and give the last-line command :q and hit enter. This will return you to Odin's command-line prompt. This command will fail if there are any unsaved changes. If you accidently made changes and you want to quit WITHOUT saving the changes, give the last-line command :q! and hit enter.

HOW TO USE PINE FOR EMAIL

Your Odin email address is used by Computer Science faculty to communicate with you. You will also use your email account to submit assignments to the instructor. This is a full-fledged email account, so you can also use it to email anyone on the Internet. Odin is the primary email exchange server for the cs.csubak.edu domain. Thus, your Odin email address is: <username>@cs.csubak.edu

The supported email client on Odin is pine (also called alpine). Pine is a text-only email client that works over X, telnet and ssh. Pine also supports Internet protocols for mailing binary attachments (MIME). Like vi/vim, pine uses commands. Unlike vi/vim, pine commands are NOT case sensitive, so you can use the lowercase or uppercase letter for any command. When you first start pine, you will be given a greeting message. Hit the E key to exit the greeting message.

The most commonly used commands in pine are:

  • C - Compose a new email message
  • L - List your mail folders
  • I - Go in to your inbox folder
  • M - Go to the main menu
  • Q - Quit pine and go back to the prompt
  • Enter Key - Select the currently highlighted item
  • Arrow Keys - Move the cursor to highlight another item
  • , or < or Right Arrow Key - Go back one level (e.g. from an email back to your inbox)

To compose an email with an attachment, follow these steps:

  1. Hit the C key to enter the compose message screen.
  2. Enter the recipient's email address in the To: field.
  3. If you do NOT want to attach a file, go to step 8. Otherwise, to attach a file, hit CTRL-J (indicated as ^J in the pine help listing at the bottom of the screen) to enter the attachment menu.
  4. Next hit CTRL-T to open the file browser.
  5. Use the arrow keys to highlight the file. If the file is in a directory, use the arrow keys to highlight the directory, then hit enter to go in to the directory.
  6. Once the file is highlighted, hit the enter key twice to attach it to the email message.
  7. Repeat steps 3 - 6 for each file you wish to attach.
  8. Arrow down to the Subject: field and enter a useful subject (if you are emailing an assignment to class, use the subject line your instructor told you to use).
  9. Arrow down to the Message Text area and type your message.
  10. When ready to send, hit CTRL-X and then Y to confirm. If you instead wish to throw away the email message WITHOUT sending it, hit CTRL-C and then C to confirm, and the message will be deleted.

To read your new email, follow these steps:

  1. Hit the I key to go to your inbox.
  2. Use the arrow keys to highlight the message you wish to read.
  3. Hit the enter key to read the message.

Read these instructions for more information on using pine. There will also be a demonstration on the projector. You should practice sending emails to a student at a neighboring computer as you will be using pine to submit your assignments in most CMPS 2010 sections.

Pine hint: If you are emailing another Odin user, you do not have to type their entire email address in the To: field. Just type their username and hit enter. If you have typed their username correctly, their full name will come up. This is a good habit to get into when submitting your assignments to make sure that you do not typo the instructor's name.

BASIC FILE MANIPULATION

The next commands we will learn are for viewing and manipulating files. There will be a demonstration of these commands on the projector, so be sure to pay attention and follow along.

The commands for file manipulation are:

ls Lists all the files in your current directory
ls -a [filename] Lists hidden files (filenames beginning with . are hidden, such as .bashrc)
ls -l [filename] Gives detailed file information, such as size and permissions
ls -al [filename] Gives detailed file information, such as size and permissions, including hidden files
cat <filename> Prints the contents of a file with no paging (e.g. prints it all at once, even if the file is too big to display on your current screen size)
less <filename> Prints the contents of a file with paging (like how the manpages display information)
cp <source> <destination> Copies the source file to the destination file, leaving the source file intact (so you will have two copies of the file).
cp <source> <clone_new_name> Copies the source file to a cloned newly named file, leaving the source file intact (so you will have two copies of the file).
mv <source> <destination> Moves the source file to the destination dir
mv <current_name> <new_name> Renames a file from the current filename to the new filename.
rm <filename> Deletes a file. Note: there is NO undelete command, so be VERY SURE you want to delete a file before using this command.

BASIC DIRECTORY MANIPULATION

You can create directories and subdirectories to organize your files, just as you can create folders in a graphical user interface (GUI). It is recommended that you create a directory for each class and store all your files for that class in that directory to keep your files organized.

When you first login, you are automatically put into your "home" directory, which is your user-specific file space on Odin. Odin does not remember what directory you were in when you logged off, so if you want to store all of your class files in a specific directory, you have to remember to go to that directory every time you logon.

The following commands are used to create and manipulate directories, and also to move data between directories. A demonstration of these commands will be given on the projector:

pwd Prints the name of the directory you are currently in.
mkdir <dir_name> Create a directory with the given name.
rmdir <dir_name> Remove the directory with the given name. The directory must be empty for this command to work.
ls [-al] <dir_name> List the files contained in the given directory name, optionally with the dot file and detailed listing options.
ls -ld <dir_name> Display the long listing for the directory itself (instead of its contents).
cd <dir_name> Change your current directory to the given directory name. All new files created with vi will be saved in this directory.
cd Change your current directory to your home directory.
cp <filename> <directory> Copy the given filename into the given directory.
mv <dir_name> <new_name> Rename a directory from the current directory name to a new directory name.
mv <filename> <dir_name> Move (not copy) the given filename into the given directory name.

UNDERSTANDING FILE PERMISSIONS

Since Unix is a multiuser operating systems, all files and directories have permission flags to control access. Permission flags are set for three entities: the file's owner, a group of users, and the rest of the world. Permission flags control whether the entity can read (r), write (w), or execute (x) the resource.

Your Odin account is in group 'student'. If you give the command ls -al in one of your directories you will view the permissions set for files and subdirectories in that directory. It will resemble the following:

drwxr-xr-x  2 demostudent student 4096 2010-09-08 23:14 doc/
-rw-r--r--  1 demostudent student 5609 2009-09-29 22:26 notes
The first part is the permission string. The first letter in the permission string is the file type (d for directory, - for a normal file). The next three letters are the owner's permissions (rwx and rw- in the above example). The next three letters are the group's permissions (r-x and r-- above). The last three letters are the world's permissions (r-x and r-- above).

The default permissions are "drwxr-xr-x" for directories and "-rw-r--r--" for normal files. This means the owner has full permissions (read/write for files, read/write/execute for directories). NOTE: You must have execute permissions on directories in order to list their contents. The group and world have no write permissions (indicated by a - where the write permission would be), but do have read permissions for normal files and read/execute permissions for directories. This means everyone can read the file and look at the directory, but only the owner can update its contents.

You can think of each permission set (rwx) as a 3-digit binary number where a 1 means to turn the permission on and a 0 means to turn the permission off. Thus a 5, which is 101 in binary, will turn on read and execute permission and turn off write permission. A 7, which is 111 in binary, will flip all permissions on. A 2 (010 in binary) will turn on write permission only and a 1 (001 in binary) will turn on execute permission only.

The command to change permissions is chmod (change mode). For instance, to change permission on a file named myfile.txt to give you (owner) rwx (read,write,execute), group r-x (read and execute), and world r-- (read only) do this:
chmod 754 myfile.txt
You can change the permissions on your directories to read only for you (make it "private") by giving the name of the directory as the second argument:
chmod 700 doc
It is STRONGLY RECOMMENDED that you create a directory for each course and make that directory private. Then be sure to change into that directory using the cd command before creating any source code files for your CMPS 2010 assignments. This will ensure that your work remains private, so that other students cannot copy your work. The commands to do so are as follows:
mkdir cs2010
chmod 700 cs2010
Make sure to change directories into your course directory every time you log in (cd cs2010). Odin does not remember what directory you were in last so you must use the cd command on every login.

Any file you leave in your home directory will be readable by others unless you do the chmod command for that file or move the file into a private directory.

PROCESSES AND HOW TO MANAGE THEM

A process is an executing program under Unix/Linux. Each process has an owner (generally the owner of the executable file) and a unique process ID known as the PID. As the owner of a process, you can tell Odin to stop the process, which is knowing as "killing" the process. You can only kill processes that you own, so don't be concerned that a mistake might crash the server (see man kill). As you begin writing programs, you often need to use the kill command to kill a program stuck in an infinite loop or a frozen shell process.

The first way one can control a process is to send a control character to the currently running process (the foreground process). For example, if your program is stuck in an infinite loop, you can try to send the control character for kill. Control characters are initiated by holding the CTRL key and hitting another character, such as CTRL-C (written as ^C). The hat (^)is used to represent the CTRL key. The common control characters are:
^C sends a kill signal to the foreground process
^Z suspends the foreground process
^D sends an EOF signal to the foreground process

Some processes can ignore the control signals, so the control characters will not always work. If the control character does not work, you will need to use the kill [-9] <PID> command. In order to use this command, you first need to discover the PID for your out of control process. To do this, you will use the ps command.

The ps command has many options that affect which processes it shows you. By default, it will just show you the processes for your current login session. But if one login is stuck in an infinite loop, you will need to start a second login session to kill the stuck process. To view all of your processes, use one of the following commands:
ps x // Shows all processes owned by you
ps ux // Shows detailed information for your processes
ps -ef | grep <username> // Filters all processes, looking for your username
The last command makes use of the pipe character (|) to redirect the output of the ps command into the grep command, which filters the data and just displays the entries that match the given username. You might also want to redirect the output of ps into a file (particularly if you have a lot of files) so you can use a text editor to view them. Let's say we want to redirect ps ux into a file called process_list.txt. The command to do that is:
ps ux > process_list.txt
Once you have the list of processes, you need to look for the line that contains your runaway process. The number towards the start of the line (under the PID column) is the PID for the process. We can then give that to the kill command. For example, if the PID is 12345, the kill command would be one of the following:
kill 12345 // Politely request that the process exit. Can be ignored.
kill -9 12345 // Demand that the process exit immediately. Not ignored.
Note that your login shell is actually a process, so you can use the kill command to kill your other login sessions. This can be a useful failsafe if you forget to logout and realize it when you're at home. You can login, find the PID of your idle session on campus and kill it to force a logoff.

One final note on processes. Foreground processes can be sent to the background when you want to keep using the shell prompt for other tasks. This is useful when the program does not need user interaction. To start a program in the background, append an ampersand (&) to the command: cat & // Start cat in the background . Make sure that the program can complete in the background. Otherwise, it will sit there and do nothing as it waits for the user to type something. To see all your current background processes, type the command jobs. To bring a background process back to the foreground (so you can type something that it is expecting), use the command fg <job_number>. Job number is listed in the output of the jobs command and is NOT the PID.


Top