TLUG | Guides / Docs > CSSLUG Last Modified: 2004-05-14 11:02

CS Students Linux User's Guide


Author: Derick Swanepoel (derick AT up.ac.za)
Version: 1.3.1
Last modified: Fri May 14 11:02:15 SAST 2004
CHANGELOG

Table of Contents

1. Introduction
1.1 About This Guide
1.2 Logging In and Out

2. Linux Commands & Tools - An Introduction to the Terminal
2.1 Why the Terminal? (!)
2.2 Some Differences from DOS/Windows (!)
2.3 Terminal Basics
2.4 Basic Commands
2.4.1 ls – Directory Listing
2.4.2 cd – Change Directory
2.4.3 cp & mv – Copying and Moving Files
2.4.4 rm – Deleting Files
2.4.5 mkdir & rmdir – Creating and Removing Directories
2.4.6 man – Getting Help From the Man Pages
2.4.7 passwd – Changing Your Password
2.4.8 tar – Archiving/"Zipping" Files
2.4.9 du – Disk Usage
2.4.10 chmod – Changing File Access Permissions
2.4.11 cat & less – Displaying Files
2.4.12 locate & whereis – Finding Files and Commands
2.5 Working With Stiffies, CDs & USB Devices (!)
2.6 Running Programs From the Terminal
2.6.1 Programs in the Current Directory
2.6.2 Elementary Job Control (+)
2.7 Process Management
2.7.1 ps & top – Listing the Running Processes
2.7.2 kill & killall – Sending Signals to a Process

3. Your Linux Account
3.1 Managing Your Home Directory - on Quotas and Housekeeping (!)
3.2 Accessing Your Account Remotely (+)
3.2.1 Secure Shell (SSH)
3.2.2 Secure Copy (scp)

4. Compiling Programs
4.1 The GNU C Compiler and C/C++ (!)
4.1.1 Discussion
4.1.2 Filename Extensions and Execute Permissions
4.1.3 Other Compilers
4.1.4 GCC 3 vs. 2
4.1.5 Using GCC
4.2 Writing a Makefile
4.3 Some Notes About Java (+)

5. The Graphical User Interface (GUI) (+)
5.1 The X Window System
5.2 The Window Manager

6. So Many Browsers & Editors! Suggestions on Which Programs to Use
6.1 Text Editors
6.2 Browsers
6.3 Window Managers
6.4 Terminals

7. When Things Go Wrong (+)
7.1 Killing Misbehaving Processes
7.2 Recovering From a Total Lock-Up of the GUI


Other guides available:
   + Installing and Maintaining Linux at Home (Important!)
   + Working With USB Devices



1. Introduction

1.1 About This Guide

This purpose of this guide is to introduce you to Linux and teach you the basics of what you need to know in order to use it effectively. The topics are organised more or less in order of importance and what you will encounter as you start learning Linux. To make this guide as simple as possible and to enable you to learn Linux quickly, the more advanced topics are kept separate.

Please note that this guide is continually being updated and improved. You can check back here occasionally and look at the version number / last modified date at the top of this page to see if there's anything new. Clicking on the CHANGELOG link at the top will show you exactly what has changed.

Some conventions used in this guide:

1.2 Logging In and Out

Logging in doesn't require much explanation. Just remember that usernames and passwords are case-sensitive. The Linux login screen also allows you to choose a window manager, but for now you can leave it at "default" which will load KDE. For more information on window managers, see section 5.3 Window Managers.

Regardless of which window manager you chose, it should have some sort of "Start menu" containing a "Logout" option. You can also press Ctrl+Alt+Del and select "Logout". (In KDE, pressing Shift+Ctrl+Alt+Del will log you out without asking for confirmation.)

To reboot a machine, choose "Shutdown..." on the login screen and then choose "Reboot". If you happen to be logged in you can select "Reboot" from KDE's logout menu too.

IMPORTANT! Never, ever reboot a machine that is still alive with the reset button (this goes for Linux and Windows) unless there's absolutely no other way of shutting it down. Terminating an operating system without allowing it to shut down cleanly can damage the filesystem (the ext3 filesystem commonly used on Linux handle unclean shutdowns better, but it is still bad, so don't do it). In Linux it is almost never necessary to reboot when something went wrong – in most cases you can just restart the GUI (X Window System). See section 7.1 When Things Go Wrong.

Back to Top


2. Linux Commands & Tools - An Introduction to the Terminal

2.1 Why the Terminal? (!)

If you grew up in Windows (like most people using computers today), you may find the concept of typing commands at a prompt rather strange and old-fashioned. But contrary to popular belief, typing commands at a command line prompt can often get things done faster than point-n-clicking around with a mouse. Since you'll be compiling and running your programs from inside a terminal anyway, it makes sense to use the same interface for managing files too. As you'll see, the Linux shell is extremely powerful – infinitely more than the DOS or Windows' shell. Almost everything you'll need to do is possible in the GUI too, but you are strongly encouraged to use a terminal.

Of course, it's your choice whether to take the time to learn the necessary Linux commands, or to point-n-click your way through everything the way you've gotten used to in Windows. It requires a somewhat different mindset, but when you grasp the power of the Linux command line you'll understand why it's considered to be so useful and an efficient way of performing a variety of tasks.

You can start up a terminal by clicking the button on the taskbar. If you're working in KDE, this will start up Konsole. For more information about different terminals, check out section 5.4 Terminals.

You'll be presented with a command prompt that looks something like this:

[s2123456@b038pc042 s2123456]$

Your username appears before the @, the hostname of the computer after that and finally the name of the current directory (which changes as you navigate to other directories). Type exit (or press Ctrl+D) to get out of the terminal. The shell (the program that presents you with the command prompt and executes your commands) most commonly used on Linux is called "bash" (Bourne-Again Shell). As I've mentioned, its capabilities are vast – entire books have been written on using the shell and scripting in it.

Back to Top


2.2 Some Differences from DOS/Windows (!)

Back to Top


2.3 Terminal Basics

Back to Top


2.4 Basic Commands

2.4.1 ls – Directory Listing

Description:
ls is used to list the contents of a directory. The files are colour-coded according to their types:
Blue
Green
Red
Purple
Cyan
Yellow
White
directories
executable files (e.g. programs, shell scripts)
archives / zip-files
images, audio & video files
symbolic links
device files
everything else

Options:
-lLong listing: Display files in a detailed format, showing permissions, owner, group, file size and date. Typing ll is the same as ls -l and will also give a long listing.
-aShow all files: Include hidden files in the listing (files that start with a dot are "hidden" – they aren't displayed by default unless you use the -a option.)
-hShow file sizes in human readable format: This option is used with the -l option to show file sizes in KB or MB instead of bytes.

Note: All commands in Linux allow you to combine options, for example ls -lah will list all files in long format using human readable sizes.

Back to Top


2.4.2 cd – Change Directory

Description:
cd works just like in DOS, except that there must be a space between cd and the directory name. And remember that the pathname separators are slashes (/).

Typing cd .. will change to the previous level in the directory tree; cd ../.. goes back two levels. ".." denotes the parent directory and "." the current directory. So typing cd . will change to the directory you're currently in (which isn't much of a change ;-)
Typing cd ~ takes you back to your home directory. The tilde character (~) is a special character – whenever the shell sees it, it substitutes the tilde with the path of your home directory. Actually, typing just cd and nothing else will also take you to your home directory. cd - will take you back to the previous directory you were in.

Examples:

[derick@ysbeer derick]$ cd cos213
[derick@ysbeer cos213]$ cd /tmp
[derick@ysbeer tmp]$ cd ~/stuff
[derick@ysbeer stuff]$ pwd
/home/derick/stuff
[derick@ysbeer stuff]$ cd -
[derick@ysbeer tmp]$

The pwd command used in the example shows you what your Present Working Directory is, which is useful if you've lost track of where you are.

Back to Top


2.4.3 cp & mv – Copying and Moving Files

Description:
cp and mv work more or less the same. You give them a list of source files and a destination. mv is also used to rename files.

Note that you can use wildcards, for example: cp *.txt somewhere.

Options:
-rRecursive: Copy directories recursively, i.e. an entire directory tree. (Note: This option is only available for cp.)
-uUpdate: Only copy/move files that don't already exist at the destination, or that are newer than existing destination files.
-gProgress: Show a progress bar if the operation is going to take a long time.

Examples:

Copy mozilla1280.jpg to the images directory
# cp mozilla1280.jpg images/
Copy scite_src and all its subdirectories to /tmp
# cp -r scite_src/ /tmp
Move Makefile and all .c and .h files to the cos213 directory
# mv Makefile *.c *.h cos213/
Move a file from /tmp to the current directory
# mv /tmp/d4x-2.02-2.i386.rpm .
Rename a file
# mv foo.pl rename.pl

Note that tab completion comes in really handy for the filename in the second last example. Just imagine typing out something like quake.x11-1.0-i386-unknown-linux2.0.tar.gz or How To Write Unmaintainable Code.html (for which you'd have to escape the spaces with a backslash or put the name in quotes.)

Back to Top


2.4.4 rm – Deleting Files

Description:
The rm command removes files.

Options:
-rRecursive: Delete a directory and all its subdirectories recursively, i.e. an entire directory tree.
-fForce: Don't ask for confirmation.

Examples:
Say goodbye to the "tmp" directory:

# rm -rf tmp/

Back to Top


2.4.5 mkdir & rmdir – Creating and Removing Directories

Description:
These commands require no explanation. Note that rmdir will only delete a directory if it is empty. The easiest way to delete a directory and all its contents is to use rm -rf dirname, as in the example for rm.

Back to Top


2.4.6 man – Getting Help From the Man Pages

The Linux manual pages (commonly called just "man pages") are a collection of documents that describe all Linux commands and programs and their options.

  • To display the man page for ls, type man ls.
  • You can use the cursor keys and PgUp/PgDn to move around.
  • Press "q" to quit.
  • To search for text, press "/" (slash), enter something to search for and press ENTER. All occurrences of the text you searched for will be highlighted. You can press "n" to move to the next occurrence and "N" for the previous.

You can also browse man pages using KDE's Konqueror webbrowser. Simply enter man:/ls in the address bar to get the man page for ls. Entering just man:/ will display a list of man page sections, and clicking on one will display a list of man pages available in that section.

Back to Top


2.4.7 passwd – Changing Your Password

To change your password, simply type passwd. You will be asked for your current password, then your new password and again your new password to make sure you entered it correctly.

IMPORTANT: To change your password in the labs, use yppasswd. This is necessary because your account exists on the server kanagawa and not on the local computer you work on.

You can also use KDE's password changing tool: Use K Menu -> Settings -> Change Password.

IMPORTANT: Don't choose a lame password. passwd will warn you if you attempt to choose a weak password (like a dictionary word or name). Don't write it down and don't let anyone watch you type it in. A password cracker is run daily on the server to see if anyone has chosen a weak password and users whose passwords were easily cracked will be notified. Read passwd's man page for more information and suggestions.

Back to Top


2.4.8 tar – Archiving/"Zipping" Files

Description:
tar is used to create archives of files and directories. In essence it does what WinZip does. tar can create different kinds of archives, for example: A plain tar-file (which is just an uncompressed archive, e.g. "tapebackup.tar"), a gzip-compressed archive (which has the extension ".tar.gz" or ".tgz", e.g. "scite150.tar.gz") and a bzip2-compressed archive (which has the extension ".tar.bz2" or ".bzip2", e.g. "MPlayer-0.90rc3.tar.bz2"). Note that filename extensions don't really matter – they're only there to help you and certain programs to know what type a file is without having to look at its contents. Compressed archives made with tar are often called "tarballs".

Usage:
tar [options] archive-name filename1 [filename2, ...]

Note that the filenames may contain wildcards or be directories. You also don't need to use a "-" to specify the options, so tar czf works the same as tar -czf.

You are expected to submit your pracs and assignments as gzip-archives, so most of the time you'd use tar as follows:
tar -czf somefile.tgz file1 file2 ...

Options:
-cCreate an archive
-xExtract files from the archive
-tList files in the archive
-zSpecify that the archive is a gzip-archive
-jSpecify that the archive is a bzip2-archive
-f filenameSpecify the filename of the archive
-vVerbose: List the files being processed
-C dirExtract files to the specified directory

Examples:

Create a gzip-archive named "prac1.tgz" containing the files "prac1.C" and "Makefile"
# tar -czf prac1.tgz prac1.C Makefile

Create an archive of the "cos213" directory and all its subdirectories
# tar -czf cos213-backup.tgz cos213/

Archive all .C and .h files and the images/ directory, and list the files being archived
# tar -czvf proj-0.7-beta.tgz *.C *.h images/

Extract an archive
# tar -xzf NVIDIA_GLX-1.0-4191.tar.gz

Extract a specified file (README) from an archive
# tar -xzf NVIDIA_GLX-1.0-4191.tar.gz README

Extract a bzip2 archive to a specified directory (/tmp)
# tar -xjf feather.bzip2 -C /tmp

List the files in an archive (the -v option gives more details)
# tar -tzvf gentoo-portage.tgz

Back to Top


2.4.9 du – Disk Usage

Description:
The du command shows how much space is used by files/directories.

It is important to check your disk space usage with du regularly so you can clean up your home directory before you exceed your quota and are prevented from logging in.

Options:
-hDisplay sizes in human readable format (e.g. in KB, MB, etc.)
-sSummarise: Display only a total for each directory, instead of the size of every file

Examples:

Display the disk usage of the "mplayer" directory and its files
# du -h mplayer/
Display your home directory's disk usage (without displaying all the file sizes)
# du -hs ~
Display the current directory's disk usage
# du -hs

Back to Top


2.4.10 chmod – Changing File Access Permissions

An Introduction to File Access Permissions:
Here's an excerpt from a long file listing (done with ll in my home directory):

permissions     user     group       size    date/time  filename    
-rwxr-xr-x    1 derick   users         152 Feb 11 11:14 arg.pl
-r-x------    1 root     root         1277 Feb  6 09:16 beep.sh
-rw-r--r--    1 derick   users           5 Feb 11  2003 cos301.txt
drwxr-xr-x    2 derick   users        4096 Feb  7 08:08 csslug
The first column contains the file access permissions. If the first character is a 'd' then the entry is a directory. The next 9 characters indicate read, write and execute permissions for three groups of users: The owner of the file, other users in the file's group and all other users. The third column shows who a file belongs to and the fourth column shows what group the file is part of (usually this is the group to which the user belongs).

As you can see, all the files belong to me except beep.sh which belongs to root. To explain the access permissions, lets take the first file (arg.pl) as an example. The first group of permissions (rwx) indicates that I have read, write and execute permissions on the file. This means I can view the contents of the file, I can modify it and since it's a Perl script, I can also execute it. The second group of permissions indicates that other users in the file's group (the users group) can read and execute the file, but they cannot write to it (or delete it). The same goes for the third group of permissions applicable to all other users on the system.

The second file (beep.sh) can only be read and executed by root. All other users have no permissions to access that file. (Keep in mind that no matter who owns a file and what its permissions are, root can always access or delete it, or change its owner and permissions.) The third file (cos301.txt) is readable and writable to me, but only readable to everyone else. This is the default when files are created. You may wonder whether this means that other users can read your files. That depends on the permissions on your home directory. To check the permissions on your home dir, type ll -d ~. For example:

[derick@quantumcow derick]$ ll -d ~
drwx------   28 derick   users        4096 Feb 11 20:05 /home/derick
The access permissions indicate that I've got read, write and execute access to my home directory, but nobody else does. This means only I can access it and the files and subdirectories in it. Important: Directories must always have both read and execute permissions set to be accessible.

Description and Usage of chmod:
chmod changes the file access permissions of the specified file(s). Its options can be pretty complex and I'll only describe its basic usage:

chmod [user_type][operator][permissions] file1 ...
user_type is one or more of the following letters:
u: the user who owns the file
g: other users in the file's group
o: other users not in the file's group
a: all users

If the user type is omitted the permissions are applied to all users.

operator is one of the following:
+: adds the specified permissions to the existing permissions of the file
-: removes the specified permissions
=: causes the specified permissions to be the only permissions the file has

permissions is one or more of the letters r, w and x (which have already been explained).

chmod has the -R option that will recursively change permissions on files and directories.

Examples:

Remove execute permissions for all users
# chmod a-x screenshot1.png
Remove read and write permissions for everyone but yourself
# chmod go-rw world_domination_plan.txt
Same as the previous command, just simpler (sets rw permissions for the user only)
# chmod u=rw world_domination_plan.txt
Grant read access to everyone
# chmod +r Snarfer.java
Grant read/write access to everyone on all files and dirs in cos213proj/
# chmod -R +rw cos213proj/

Important Note: When copying files from disks or Windows FAT partitions, the permissions are always set to "rwx" for everyone ("r-x" for read-only files and files on CDs). This is because the FAT filesystem used on disks and non-NTFS Windows partitions don't support permissions or ownership (there is essentially no file-level security on a FAT-based Windows). This isn't really a problem, but you may want to chmod -x such files that aren't actually executable.

Back to Top


2.4.11 cat & less – Displaying Files

Description:
cat and less can be used to view files. The easiest way to view a file is to say: cat filename.
You can use the -n option to number each line, e.g.: cat -n foo.C

For longer files you can use less (which works more or less like more :-). less shows a screenful of the file at a time and you can move up and down with the cursor keys and PgUp/PgDn. To quit viewing the file press "q". You can also press "/" (slash) and type in a word to search for just like in the man pages (in fact, the man pages are displayed using less). Pressing "<" skips to the beginning of the file and ">" to the end.

Back to Top


2.4.12 locate & whereis – Finding Files and Commands

locate can be used to find files whose names contain a specified string. For example:

[derick@quantumcow derick]$ locate foobar
/home/derick/images/screenshots/Windows goes foobar again.jpg
/mnt/c/Projects/GovGaz/cd/autorun-foobar.inf
The -i option can be used to search case insensitively.

whereis is used to search for Linux commands (specifically, it locates the binary, source, and manual page files for a command). For example:

[derick@quantumcow derick]$ whereis whoami
whoami: /usr/bin/whoami /usr/share/man/man1/whoami.1.gz
           [binary]               [man page]

[root@quantumcow root]# whereis mybitches
mybitches:

Clearly, there are none of the latter on my system ;-)
As you can see in the last example, when you search for a command that doesn't exist there are no results.

Back to Top


2.5 Working With Stiffies, CDs & USB Devices (!)

Description:
As mentioned in the section about differences between Linux and Windows, the contents of removable media like stiffies, CDs and USB flash disks are not automatically visible through drive letters and need to be "mounted" first. When a disk is mounted onto a directory (usually one of the directories in /mnt), it becomes logically part of the directory structure and you can access the files on it just like any other on the hard disk. A disk that has been mounted must always be unmounted. Don't worry if this doesn't make sense to you. (BTW, it doesn't seem like there's any agreement on whether to call 3.5" disks stiffies or floppies, but either way "floppy" refers to a normal 1.44MB disk and not the old 360K / 1.2MB disk ;-)

However, since several users found the concept of mounting too foreign and forgot to unmount after they had used a stiffy, it has been decided that users should use an easier and more fool-proof method to access stiffies. If you're used to mounting stiffies then feel free to continue the Unix tradition, but always remember to unmount. If you don't, your data may not be written to the stiffy. The fool-proof solution is called mtools and is described below:

You have three options when it comes to accessing floppies / CDs:

  1. Use mtools:
    The mtools commands allow you work with floppies like in DOS, meaning you don't have to mount/unmount them and you access them through the drive letter "a:". The mtools commands are just like the DOS commands, except they all start with "m": mcd, mdir, mcopy, mdel, mmove, mren, mmd, mrd. To see a complete list of mtools commands, type mtools in a terminal.

    For example, to copy a file: mcopy cos213-prac1.tgz a:

  2. Keep the age old Unix tradition of mounting/unmounting disks yourself:
    To mount a floppy: mount /mnt/floppy
    To mount a CD: mount /mnt/cdrom
    To mount a USB flash disk: mount /mnt/usb
    After mounting, a floppy is accessed through the directory /mnt/floppy and a CD through /mnt/cdrom. (These directories are called "mount points".) The argument given to mount specifies the mount point of the device containing the disk to be mounted. Alternatively, you can also specify the device to mount instead of the mount point, e.g. mount /dev/fd0 or mount /dev/cdrom. USB storage devices are emulated as SCSI disks and are usually called mount /dev/sda1 or mount /dev/sdb1.

    After you've finished reading from or writing to a disk, it must be unmounted:
    Floppy: umount /mnt/floppy
    CD: umount /mnt/cdrom
    USB: umount /mnt/usb

  3. Point-n-click in KDE:
    In KDE, you can mount/unmount a floppy by right-clicking on the floppy icon on the desktop and choosing "Mount" (or "Unmount"). A little green triangle will appear on the icon indicating that the device is mounted. The same goes for CDs and flash disks. From there on you can use a terminal or the file manager to manage files like normal. Remember to unmount!

Important:

Note: For more information about USB devices, see the "Working With USB Devices" guide.

Back to Top


2.6 Running Programs From the Terminal

2.6.1 Programs in the Current Directory

Also mentioned in the section about differences between Linux and Windows is the fact that the current directory is not in the path. So what does this mean? This means that if you want to run a program in the current directory, you need to prefix it with ./

To repeat the example used previously, if I want to run a program called "prac1" in my cos213 directory, I would type:

[derick@ysbeer cos213]$ ./prac1

At first glance this might seem awkward and pointless, but there are good reasons which I won't explain in this guide for the sake of brevity. If you were really desperate, you could edit your shell's config file and put the current directory in the path yourself. However, you should get used to dot-slashing.

Back to Top


2.6.2 Elementary Job Control (+)

Unix supported multitasking before graphical user interfaces were even dreamed of. You could run several programs (or "jobs") at the same time on the same console and switch between them using a feature called job control. The simplest and most common use of job control is running a program, putting it in the background and continuing to use the shell. Here's an example:

Suppose you run an application like kghostview (to view PDF files) from within a terminal:

[derick@ysbeer derick]$ kghostview
  
kghostview pops up, and when you look at the terminal you'll see the cursor but no prompt. In doesn't respond to input until you close kghostview. However, you can suspend kghostview by pressing Ctrl+Z in the terminal, which gives control back to you:
[1]+  Stopped                 kghostview
[derick@ysbeer derick]$
When you look at kghostview now it is suspended and doesn't respond. Now type bg in the terminal to allow kghostview to continue running in the background:
[derick@ysbeer derick]$ bg
[1]+ kghostview &
[derick@ysbeer derick]$
The ampersand (&) indicates that kghostview is running in the background and you can continue using it like normal.

If you know beforehand that you want to run a program in the background and continue working in the terminal, you can add an ampersand (&) to the end of the command line:

[derick@ysbeer derick]$ kghostview &
[1] 1932
[derick@ysbeer derick]$
The line that's printed out tells you that this is the first program started in the background ([1]) and what the process ID of that program is (1932). This will obviously be different on your system.

You can do the same with programs running inside the terminal, for example the vi editor:

  1. Type vi in a terminal. You'll be presented with the intro screen.
  2. Press Ctrl+Z to suspend it.
  3. Type bg to allow vi to continue running in the background. (This isn't strictly necessary for something like vi because it doesn't need to actually do something in the background, but for other programs that do processing or whatever it is required.)
  4. You can now go on and use the terminal, and whenever you want to switch back to vi you type fg to bring it back to the foreground.

Back to Top


2.7 Process Management

2.7.1 ps & top – Listing the Running Processes

Description:
ps lists the processes running on the system. top displays a continually updated list of processes (press "q" to quit top).

Options (ps):
-aDisplay all processes, including those of other users.
-uDisplay user-oriented format. Basically this displays information like username of owner, CPU and memory usage, resident stack size, etc. for each process.
-xDon't worry about what this option does, just use it because ps doesn't give much output without it.
-fThe "forest" option: Display the process listing as an ASCII-art tree.

There are many other options, but you can check out the man page for more details.

One of the most useful ways to use ps is ps -aux. In fact, this is probably the only way you'll need to use it unless you actually understand why you'd want to do otherwise. You could also try ps -auxf which nicely displays the process hierarchy in tree format.

When running top, press "h" for help and "q" to quit.

Back to Top


2.7.2 kill & killall – Sending Signals to a Process

Description:
kill and killall are used to send signals to processes. By default the TERM signal is sent which will terminate a process (unless the process "catches" that signal, in which case it may be necessary to use the KILL signal which cannot be caught.) The most common use of kill and killall is to, well, kill processes that have stopped responding or are misbehaving. killall differs from kill in that you specify the name of the process you want to nuke, and it will nuke all processes with that name. Signals are not used only for killing, but also for inter-process communication where one process can to some extent control another by sending different kinds of signals.

Usage:
kill [-s signal] process_id ...
killall [-s signal] process_name ...

In order to find out what the process ID (PID) or name is of the program that you want to kill, you need to use ps as described above. A list of signal names can be found in /usr/include/asm/signal.h, but you probably won't need to use any other than the default TERM signal.

Examples:

Send the TERM signal to process 1984 and hope it dies...
# kill 1984

kill -9: Use with caution. You don't just throw around the KILL (9) signal for fun. It kills without mercy and without giving a process any opportunity to exit gracefully.
# kill -9 10372

Kill all instances of bash (including the one you're typing in)
# killall bash
Send the HUP signal to all instances of artsd
# killall -s HUP artsd

Back to Top


3. Your Linux Account

3.1 Managing Your Home Directory - on Quotas and Housekeeping (!)

Your home directory resides on a server called kanagawa (kanagawa.up.ac.za, to be precise), along with more than 1000 other users' home directories. Disk space is limited and therefore each user has a disk quota of 40 MB. More accurately, your "soft limit" is 40 MB and your "hard limit" is 45 MB. This means that when your disk usage exceeds 40 MB, you will be warned that your quota is full. When you exceed 45 MB you will not be allowed to log in normally and will only be able to use a restricted set of programs for getting your disk usage within bounds. You don't want this to happen because it will waste your time.

It is your responsibility to keep your home directory clean. You should check your disk usage periodically (every week or so) – see section 2.4.8 du - Disk Usage.
Here are some tips for managing your home directory:

Back to Top


3.2 Accessing Your Account Remotely (+)

3.2.1 Secure Shell (SSH)

You can access your account on kanagawa from any computer with a secure shell (SSH) client. For example, if you have an Internet connection at home you could log in on kanagawa with your account using SSH. Suppose I'm at home and want to edit some files on my home directory on kanagawa:

[derick@quantumcow derick]$ ssh dswanepoel@kanagawa.up.ac.za
dswanepoel@kanagawa's password: [Enter password]
Last login: Thu Feb  6 16:02:46 2003 from roadkill.cs.up.ac.za
[dswanepoel@kanagawa dswanepoel]$
Now I'm logged in on kanagawa (notice how the prompt has changed) and I can edit files and run programs just as if I was sitting in the lab. Type exit to log out.

When you are logged in via secure shell, all transmissions are encrypted. It is also possible to run GUI applications over an SSH connection (when using a dialup connection from home it's not very practical, but on the university's network it works fine). When you run a GUI application like SciTE via SSH, it actually runs on the remote system but the window is rendered on your local system. (This is unlike running a program over a Windows network where the executable is simply loaded into memory and runs on your local system.) In fact, we did our entire COS213 project remotely on another machine on campus. Don't try running GUI applications remotely on kanagawa, however. Being a server, it doesn't have the X Window System or any GUI apps for that matter (it's just not necessary).

There are SSH clients for Windows too, so you can still access your account from home if you can't install Linux there. PuTTY is a nice Windows SSH client, which you can get at www.chiark.greenend.org.uk/~sgtatham/putty/download.htm. Note that you can't run GUI apps remotely on Windows (unless you've got an X server for Windows, but that's just plain wrong...)

Back to Top


3.2.2 Secure Copy (scp)

Description:
Secure copy (scp) allows you to copy files between computers on a network. In other words, you can copy files from your home directory on kanagawa to your computer at home and vice versa. This is extremely useful for when you forgot to copy a practical's source code on a disk to take home (or if your disk has died a tragic premature death). It's a much easier way to transfer files between varsity and home than e-mail or unreliable disks. There is a Windows client called "pscp" which is part of the collection of PuTTY tools.

Usage:
scp [options] [[user@]host1:]file1 [...] [[user@]host2:]file2

scp makes various assumptions if you omit certain parameters.

  • Username: Use the current user's username.
  • Hostname: Assume the file is on the local system.
  • Full path to filename on remote host: Assume the filename or partial path is in the user's home directory.
  • Destination path/filename: Copy the file to the user's home directory.

Options:
-rRecursively copy entire directories
-CEnable compression

Examples:

Copy a file from my local system to the /tmp directory on physlab7 using the root user:
# scp linux-2.6.1.tar.bz2 root@physlab7.up.ac.za:/tmp

Copy a file from my home directory on kanagawa to the current directory (note that because I don't specify the remote path to the file, scp assumes it's in my home dir on kanagawa):
# scp derick@kanagawa.up.ac.za:world_domination_plan.txt .

Copy a file from my local system to kanagawa. Since the remote path doesn't start with a "/" it is assumed to be a subdirectory of derick's home directory:
# scp proj-0.7-lumberjack.tgz derick@kanagawa.up.ac.za:cos213/

Copy some files to baobab. Since the remote username is omitted it is assumed to be the current user, and since the remote path is omitted it is assumed to be that user's home directory on baobab:
# scp *.jpg *.png baobab.up.ac.za:

Copy the entire apps dir on 137.215.45.107 using user derick to root's home dir on andromeda. Note that there are 3 different machines involved: The one I issue the command from, the source and the destination:
# scp -r derick@137.215.45.107:/tmp/apps root@andromeda.up.ac.za:

Back to Top


4. Compiling Programs

4.1 The GNU C Compiler and C/C++

4.1.1 Discussion

The compiler(s) used on Linux are part of the well-established GNU Compiler Collection (GCC). This includes gcc (for compiling C programs) and g++ (for C++ programs) which are the compilers you'll be using. (For the rest of this guide I'll refer to both these compilers as "gcc".) gcc compiles the source code of your programs into object files (which have a .o extension) and then links them together into an executable file (also called a "binary").

4.1.2 Filename Extensions and Execute Permissions

Note that executables on Linux don't have a .exe extension – in fact, they generally don't have any extension. Linux does not identify files by their extensions (like Windows) but by their contents, so it doesn't really matter what you call a file. As long as a file is executable, you can call it something.blah and it will still run when you type ./something.blah. However, if an executable does not have execute permissions it won't run. You can see whether a file has execute permissions by doing a directory listing. If the name is green, then it has execute permissions. (If you do a long listing (ls -l) the permissions column should contain an 'x'.) If for some reason a program does not have execute permissions, you can give it those permissions using the chmod command, e.g.: chmod +x program. When you compile a program with gcc it automatically gives it execute permissions.

Extensions of Source Files:

C source files usually have a .c extension. Extensions for C++ source files vary, but the most commonly used ones are .C, .cpp or .cxx. (The unofficial standard for COS213 is to use .C) See gcc's manpage for more details.

4.1.3 Other Compilers

Since your programs will be compiled with gcc when they're marked, you are seriously discouraged from using any other compiler. Stay away from Cygwin's version of gcc - unless it's impossible to install Linux on your computer. Your programs may compile under Cygwin but there's no guarantee it will run correctly. Even a simple COS213 assignment that works perfectly in Linux can segfault and do weird things in Cygwin, resulting in students losing marks and other atrocities. Don't even think about coding in Windows and compiling with Borland or Microsoft's compiler – they don't produce Linux binaries so programs compiled with them won't even run on Linux.

4.1.4 GCC 3 vs. 2

The recently (2002) released version 3 of gcc implements the changes made to the ISO C standard, and thus breaks some backwards compatibility with previous versions of gcc. This means that programs that compiled with gcc 2.x may not necessarily compile with gcc 3.x. The following are the two most important changes to be aware of:

  • Deprecated headers: In the past certain header files such as iostream.h, tree.h, etc. were included using
    #include <iostream.h>
    In gcc 3.x the .h extension is dropped for these headers, so the above statement would become
    #include <iostream>
    The list of files that this applies to is located in gcc's "backward" directory, which differs between Linux distributions. On Red Hat it is /usr/include/c++/3.x.x/backward/, on Gentoo it is /usr/lib/gcc-lib/i686-pc-linux-gnu/3.x.x/include/g++-v3/backward/. (Doing a locate backward should give you its location no matter what distro you're using.) Either way, gcc will print a nice, long warning whenever you include one of these header files with the .h extension. Ultimately, you would typically only use the iostream header for functions like cout.

  • Namespaces: If you've included iostream as specified above and you get errors like `cout' undeclared, you probably forgot to use the appropriate namespace. If you use cout and related iostream functions you should have the statement using namespace std; after the includes.

To see what version of gcc you have, type gcc --version

Back to Top


4.1.5 Using GCC

Syntax:
The following is the most common way of using gcc:
C:   gcc <options> inputfiles -o outputfile
C++: g++ <options> inputfiles -o outputfile

where inputfiles are source and/or object files and outputfile is the name of the executable to be produced. It doesn't matter in which order you specify the options, input files and output file. Here is a very simple example:

g++ matrix.C -o matrix

Options:
-o filenameSpecify the name of the output file, which will be an executable unless the -c option is used.
-cCompile the source file(s) but don't link them into an executable. This will produce an object file (.o) for each source file.
-OnOptimise. The value of n controls the level of optimisation, which ranges from 0 to 3. Compiling your program with -O3 will compile it with the highest level of optimisation. -Os optimises for size (makes the program as small as possible). -O0 disables optimisation. (Note: The -O option is "minus Oh", not "minus zero".)
-wDon't display warning messages
-WallEnable all warning messages (a good option to use to make sure your code doesn't do potentially dangerous or questionable things).
-gGenerate debugging information. This is necessary when you want to debug your program with a debugger like gdb or ddd. (It's generally a good idea not to use optimisation when debugging a program, unless you like being confused.)
-MMOutput the dependencies for the given source file (doesn't compile anything). This is very useful for when you're creating a Makefile.

For more information (especially on the topics of optimisation and warnings) see gcc's manpage.
For an explanation of the purpose of the -c option see section 4.2 Writing a Makefile.

Back to Top


4.2 Writing a Makefile

Discussed in a separate section -- by Jaco Kroon.

Back to Top


4.3 Some Notes About Java (+)

Back to Top


5. The Graphical User Interface (GUI) (+)

5.1 The X Window System

The Graphical User Interface (GUI) of Linux is called the "X Window System" or simply "X". On top of X runs the window manager which is responsible for drawing the windows and buttons and handling user input. In very over-simplified terms, the user interacts with the window manager using the mouse and keyboard, the window manager interacts with X and X interacts with the Linux kernel. Both the window manager and X are user-space applications – they are not part of the kernel as is the case with Windows. This necessarily makes them slightly slower, but with the advantage of increased flexibility and overall stability of the system. If the window manager or X crashes it seldom affects the kernel, and since they're just normal programs they can be restarted without rebooting the system.

Back to Top


5.2 The Window Manager

As mentioned above, a window manager is an application that draws the windows, desktop and so on. The window manager determines the look of the window decorations, buttons and icons. Usually it has a "taskbar" and a sort of "Start menu" and it handles switching between applications and desktops. This section describes some of the features of the KDE window manager, most of which are common to almost all Linux window managers. I'm using KDE as an example because in my experience it's the most effective in allowing Linux to be a powerful desktop operating system and is the easiest transition from Windows to Linux.

Multiple Desktops

Almost all Linux window managers allow you to have more than one desktop. Usually there are four "virtual desktops" (KDE lets you to have up to 16), so when one of them becomes cluttered with windows you can switch to a new, clean one. On the taskbar you should see a grid with four blocks, numbered 1 to 4. Clicking on one of them switches to that desktop. In KDE you can also switch to the next or previous desktop by pressing Ctrl+Alt+Right or Ctrl+Alt+Left. Pressing Tab while holding Ctrl will cycle through the desktops from 1 to 4.

Window Options

Right-clicking on the titlebar of a window (or its entry in the taskbar) brings up a menu of window options. Among these options are the following useful functions:

Copying and Pasting

Linux allows you to paste selected text with the middle mouse button. Any text you select with the mouse is automatically copied to the clipboard and can be pasted by clicking the middle mouse button (or by clicking the left and right buttons together). This goes for absolutely any text, anywhere. You can paste text from a terminal into a text editor, or from a browser to a terminal, for example. In KDE you will find a little clipboard applet in the system tray that keeps track of the last few selections. If you right-click on it you can choose a previous selection from its history to paste.

Locking the Screen

If you need to leave your workstation for a moment and you're concerned about privacy and security (which you should be, especially in the Informatorium), it's a good idea to lock the screen. In KDE you can do this by pressing Alt+Ctrl+L. This will start up a screensaver which will prompt for your password when interrupted.

IMPORTANT! Don't leave your workstation locked for a lengthy period of time. Going on lunch for an hour with your workstation locked is inconsiderate towards the other users of the labs. If you stay away too long some equally inconsiderate person will come and reboot it into Windows anyway...

Back to Top


6. So Many Browsers & Editors! Suggestions on Which Programs to Use

One of Linux's distinguishing features is the freedom and choice it provides. There are usually several different applications of the same kind available, whether it be text editors, browsers, mail clients, MP3 players or terminals. This vast diversity may be confusing if you are new to Linux and the open source world, especially if you are used to Windows where most decisions are made for you. This section aims to provide some guidance in choosing certain programs from the host of possibilities. Obviously it's subjective to some extent, and remember that the choice is always yours. Use whatever works best for you.

6.1 Text Editors

SciTE

If you don't already have a preferred text editor, I suggest you check out SciTE. In fact, I suggest you try it out anyway since it really is one of the very best Linux text editors. SciTE's features include:

There also happens to be a Windows version of SciTE. SciTE is included on the CS 2003 CD and is installed on Linux and Windows in the labs.
Check out the SciTE page for installation instructions, screenshots and a command reference.

SciTE's homepage: www.scintilla.org/SciTE.html
New versions can be obtained from www.scintilla.org/SciTEDownload.html.
New versions customised for the labs can be obtained from TLUG's download page.

Kate

Kate is the KDE Advanced Text Editor. It has many of the same features as SciTE, like extensive syntax highlighting (including support for Quake, Wolfenstein and Half-Life config files!), and quite a few of its own neat features like a built-in terminal emulator, project management plugin, file list and a plugin to open a file's corresponding .h/.cpp counterpart. Kate has pretty good integration with KDE (a very strong feature of KDE that is continually improving) – in fact, it is a "KPart" which can be embedded in the Konqueror browser and KMail e-mail client.

Kate lacks code completion (for now) and is perhaps somewhat slower than the other editors mentioned, but it's very configurable and has a good configuration interface which is missing in SciTE.

vi and Emacs

These are the two heavyweight editors in the Unix/Linux world. The following quote describes them well:

Real Programmers consider "what you see is what you get" to be just as bad a concept in Text Editors as it is in women. No, the Real Programmer wants a "you asked for it, you got it" text editor -- complicated, cryptic, powerful, unforgiving, dangerous.

vi is just like that, Emacs perhaps a little less, but both are extremely powerful. There's probably no-one who can harness the full power of vi and Emacs.

Advantages of vi:

Advantages of Emacs:
Emacs has many of the advantages of vi: It's pretty standard too, fast, has a graphical counterpart and has extensive syntax highlighting. Emacs is more than a text editor, however. Some people say, "Emacs is a great operating system. It just doesn't have all the neat programs." It truly has everything, and the kitchen sink. You can read your mail, browse the Web, etc. in Emacs. It even has a built-in psychiatrist. Some regard this as an advantage, others feel it is bloated. Ultimately, you decide.

Disadvantages of vi and Emacs:
To the advanced user there are essentially none. For the Linux newbie who knows only Windows these editors can be a bit too intimidating (as the quote says: complicated, cryptic, unforgiving and dangerous). The biggest hurdle to overcome is the collection of (sometimes strange and arbitrary) editor commands that need to be learned.

Back to Top


6.2 Browsers

The choice of a browser is a touchy subject. No browser is perfect, but at least you have a choice, so where one is inadequate another may be more appropriate.

Konqueror

Konqueror is KDE's browser / file manager (sort of like Windows' Explorer). It's well on its way to becoming one of the mainstream browsers. Personally speaking it is my first choice as a browser. Konqueror does a very good job at rendering webpages, supports tabs, allows you to reject cookies, manage pop-up windows and it's fairly fast. (And since KDE 3.1 it no longer has problems with the broken JavaScript on Virtual Campus). Konqueror is one of the most developed and continually improving parts of KDE. Apple uses Konqueror's rendering engine in their Safari browser.

Mozilla

Mozilla is one of the best browsers, in the same league as Opera and Internet Explorer. Mozilla renders pages well, is consistent and stable and supports tabbed browsing. One can also disable images and cookies from the menu (Tasks -> Privacy & Security -> Image Manager -> Block Images from this Site, for example), and specify what JavaScript is allowed to do and what not (i.e. disable pop-up windows). One disadvantage is that the interface is somewhat sluggish and it takes quite a while to start up. Mozilla is also available for Windows.

Opera

Opera is a great browser too. It is renowned for being the fastest browser on earth and it sure does live up to its reputation. Opera also supports tabbed browsing, allows you to prevent images from displaying with the click of a button and gives you control over what JavaScript is allowed to do just like Mozilla. Opera includes a very useful Google search bar in the address bar and can be heavily customised. It is also available for Windows (version 7 is a serious candidate for replacing IE...)

So what's the catch with Opera? It's not free. If it's not registered it displays ads in the toolbar, but if you can live with that then it's a great browser.

Lynx

No Linux installation is complete without a text-mode browser! Lynx is a fully-featured web browser that operates entirely in text-mode. It obviously doesn't display images, but it allows one to browse a webpage, follow links and download files perfectly well. It even supports frames. You can even read your mail on UP's webmail interface through Lynx!
(Try it: lynx http://student.up.ac.za/email).

You'll probably never need to use Lynx as a normal user. I mentioned it more as an interesting fact. (Although I know someone that got more than halfway through booking a flight ticket in Lynx, but he certainly isn't a normal user.) However, if you ever admin a system remotely and it doesn't have an X server, you'll be thankful for a text-mode browser!

Back to Top


6.3 Window Managers

KDE

KDE is not just a window manager but an entire "desktop environment". It includes most of the applications and utilities needed for daily use (and a fair amount of more exotic ones, too). In section 5.2 The Window Manager some features of KDE were described. As mentioned there, using KDE will probably be the easiest transition from Windows to Linux. I would strongly recommend it to the Linux newbie. KDE is the youngest of the most common Linux graphical environments, but it is also surely the most highly developed. It has become an established desktop environment with a full-featured set of applications ranging from configuration tools to an IDE for application development to an office suite. In fact, the German government has begun using KMail as an e-mail client.

Some may argue that KDE is bloated and slow (and they may not be entirely wrong), but it is continually improving and offers a flexibility and modularity that no other window manager does. KDE is one of the most significant factors in making Linux enabling Linux to be a full-featured desktop OS with each new version showing definite speed, stability and feature improvements.

Gnome

Gnome is another established desktop environment with very much the same functionality and power as KDE. It's a little less like Windows than KDE though, with its own look & feel and paradigms. Gnome and KDE are more or less equally popular on Linux desktops.

IceWM

The best thing about IceWM is that it's lightning fast. It has a lean and mean interface without needless bells and whistles. It lacks a decent menu and configuration system, but these exist as add-on modules.

EvilWM

EvilWM can be summarised as "the border around an xterm and a bunch of keystrokes". It's installed in the labs just for fun – masochistic fun, I might add :-) EvilWM is the ultimate of minimalism: no desktop, no icons, no taskbar – not even window title bars. Just a one pixel border around windows, and a collection of shortcuts to launch xterms and manipulate windows.

Back to Top


6.4 Terminals

ATerm

ATerm is a very fast, lightweight terminal. This and the fact that it supports transparency is basically what makes it my personal favourite. You'll have to read the man page to change the configuration options, however. The following command will launch ATerm with some nice options:
aterm -fg white -bg black -sr -sl 1000 -tr -sh 35 -geometry 90x28
(You can right-click on the K Menu in KDE and choose "Menu Editor" to add an entry for ATerm using this command, if there isn't one already.)

Konsole

Konsole is KDE's terminal. It has lots of functionality, includes transparency and has useful configuration dialogs. The major drawback about Konsole is that it's rather slow.

xterm

xterm is X's original terminal. Like ATerm, it is small and fast, and you'll need to read the man page if you want to configure it. xterm is the default terminal in IceWM.

Back to Top


7. When Things Go Wrong (+)

One of Linux's strengths is it's ability to recover from problems, failures and crashes. There's a saying that goes: "Linux – because rebooting is for adding new hardware." It is not uncommon for Linux machines to run for weeks, months, sometimes even more than a year without the need for a reboot. One of the things that make this possible is the extensive control Linux gives you over your programs' processes. This section describes some of the ways you can avoid having to reboot when things go wrong.

7.1 Killing Misbehaving Processes

ps and kill

When you find that a program has hung or is generally misbehaving, you can use the ps and kill commands to kill it (see section 2.7 Process Management). Say for example I have ssh'ed to kanagawa and for some reason it has stopped responding. First I will use ps to determine the ssh process's ID, and then use kill to terminate it:

# ps -aux
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
...
derick   14993  0.0  0.4  3164 1604 pts2     S    20:46   0:00 ssh kanagawa
...

# kill 14993
If a process does not die after killing it, you can try to use kill -9. However, as mentioned before, kill -9 must be used sparingly. It doesn't allow a process to terminate gracefully and clean up after itself.

top

The top program is also a very useful tool for finding misbehaving processes and terminating them. top displays a continually updated list of processes, which are by default sorted by CPU usage. When a process gets out of hand and starts eating up all of the CPU power, it will be displayed at the top of the listing. You can kill the process by pressing 'k', entering its process ID and pressing enter again (which selects the default TERM signal (15) to be sent to the process). Once again, if the process stubbornly refuses to die, follow the same procedure but enter 9 at the last step to send the KILL signal.

KDE System Guard

Lastly, your graphical alternative to these programs is something like KDE's System Guard. To launch KDE System Guard, press Ctrl+Esc or run it from a terminal with ksysguard. You may have to click on the "User%" heading to sort processes by CPU usage.

Tip: There is a quick way to kill graphical applications. Pressing Alt+Ctrl+Esc will change the cursor into the infamous and deadly "Skull and Crossbones ™", with which you can kill anything just by clicking on it. Two warnings are in order, however: Firstly, killing with The Skull may kill a program's window but not necessarily its process. It's a good idea to use ps to make sure the process also terminated. Secondly, hand-eye coordination is important because misclicks are fatal. The Skull asks no questions.

Processes That Won't Die

Sometimes you may come across processes that seem immune even against kill -9. These processes are usually in an "uninterruptable I/O wait" state, indicated by a 'D' in ps's STAT column. If you've tried to terminate such a process and it just won't go away, you'll have to wait for the Linux kernel to take care of it when it comes out of its uninterruptible state. Something else to note is that the kernel implements an "Out Of Memory Killer" that will promptly terminate any process that causes the machine to run out of memory.

Back to Top


7.2 Recovering From a Total Lock-Up of the GUI

In the rare and unfortunate case that everything stops responding and you cannot even reach a terminal to see what's going on, there is still hope. Graphical User Interfaces in Linux are separated from the kernel and from other non-graphical programs, so they can be terminated separately when misbehaving. In the event that KDE or the underlying X Window System stops responding, you can switch out of graphical mode to text mode and log in on one of the virtual terminals. Pressing Alt+Ctrl+F1 will take you to the first virtual terminal.

Once logged in on a virtual terminal you can do anything, except run graphical applications. From here you can try terminating the offending program. To switch back to graphical mode, press Alt+Ctrl+F7. There are six virtual terminals you can log in on (simultaneously, if you need to), accessed with Alt+Ctrl+F1,F2,F3... and so on. If killing one process doesn't help, keep on killing processes until things go back to normal.

What to do when it doesn't work:

If you find yourself in one of the following situations: "I'm running out of processes to kill and things are still locked up!" or "I can't even get to a virtual terminal with Alt+Ctrl+F1!", then it is time to resort to more drastic measures. You can terminate the X Window System by pressing Alt+Ctrl+Backspace. IMPORTANT: This will immediately kill ALL your processes, including KDE, and you will lose any unsaved work. When X is killed, it will be restarted and you'll be returned to the login screen.

NOTE: Use this method responsibly. Do NOT kill X when someone is logged in and has locked the workstation while going away for a while. Only when it is clear that a person has "abandoned" the workstation and there are no other workstations to use are you allowed to terminate his/her session like this. If you find anyone who has terminated your session, please report them to the techteam (or at least report the time at which you found them logged in so they can be identified and addressed).

Back to Top


 

This guide was written by Derick Swanepoel (derick AT up.ac.za)
Comments and suggestions are welcome.

If you have Linux questions / problems, there is a Tuks Linux Users Group (TLUG) that can assist you. You can subscribe to the TLUG mailing list by sending an e-mail to majordomo@cs.up.ac.za with the following line as the body of the message:

subscribe tlug


There is also lots of active discussion on the TLUG forums at tlugforums.up.ac.za

Visit TLUG's website at tlug.up.ac.za

Have a nice day.