Linux Tutorial Souri
Linux Tutorial Souri
Anubhav Seth
Souri Guha
Table of Contents
1 Introduction
5 Viewing Files
5.1 Text Files
5.2 Other File Types
6 Online Help
6.1 Online Manual
6.2 Apropos
6.3 Info
6.4 The Gnome Help Browser
8 Security
8.1 Changing your Password
8.2 File Permissions
9 Processes
9.1 Listing Processes
9.2 Ending a Process
9.3 Running a Process in the Background
11 Compiling Programs
11.1 Compiling From the Shell
11.2 Compiling From Within Emacs
11.3 Using Make
11.4 Debugging
12 Networking
12.1 World Wide Web
12.2 Remote Login
12.3 Remote X Windows
12.4 Novell Access
2.1 Logging In
If you sit down at a machine and the screen is black, wiggle the mouse a little. The screen saver will disappear and a login
screen will appear. Just type your username in the appropriate box, hit Enter, enter your password, and hit Enter again.
When you are ready, click on the Panel button that looks like a monitor (or sometimes, a footprint). This will bring up a terminal
window into which you can type Linux commands. The program running in the terminal window that accepts these commands
is called a shell . The default shell is called bash (the GNU Bourne-Again SHell). When you start the shell, you only see a
prompt like:
razr@localhost>
The prompt indicates that the shell is ready to accept your commands. In this example, the prompt indicates that user razr is
logged into the computer named localhost . As you learn more about Linux, you will find that many things can be accomplished
either by “pointing and clicking” with the mouse or by typing commands in a shell. While using the mouse is often more
convenient, shell commands can give you greater control over an operation. They are also often more convenient for performing
some quick file operations. We will cover many shell commands later in this tutorial.
pwd Enter
in the terminal window. (From now on, we will omit the Enter after commands; you will need to hit this key after every
command you enter.) You should see something like
razr@localhost> pwd
/home/razr
to indicate that your are currently in your home directory. (The subdirectory razr will be, of course, replaced by your user
name.) Whenever you create or refer to a file using a relative pathname, the pathname of the working directory is simply
prepended to get the correct absolute pathname.
You can also specify pathnames that go “up” in the tree by using the special symbol “ .. ” which refers to the parent directory
of the current directory. For example, in Figure 1, if the current working directory were /bin , then the relative pathname
../home/lisa/hello.cc would refer to the file hello.cc in the subdirectory lisa .
You can use the cd command to change your working directory. For example, to change your working directory to the parent
directory, type
cd ..
in the the terminal window. To confirm the change, type pwd . You will now see:
razr@localhost> pwd
/home
To change back to your home directory, type
cd razr
(replacing razr with your username, of course). Here is a shortcut: typing cd by itself will always return you to your working
directory from anywhere.
total 27
drwx------ 70 joe joe 6144 Jul 30 13:02 ./
drwxr-xr-x 102 root root 2048 Mar 26 09:23 ../
-rw-r--r-- 1 joe joe 1067 Aug 16 1999 .Xdefaults
-rw-r--r-- 1 joe joe 1100 Mar 27 16:27 .bash_profile
-rw-r--r-- 1 joe joe 1625 Jan 12 2001 .bashrc
-rw-r--r-- 1 joe joe 4581 Feb 1 16:17 .emacs
drwxr-xr-x 12 joe joe 1536 Jul 23 10:31 .gnome/
The first line gives the total amount of disk storage used by the files in this directory, in disk blocks. Each additional line in the
display gives information about one file or subdirectory in the directory. The first character in the first column in the file listing
tells you what type of file you are looking at. A - means that this is a normal file, a d means that this is actually a subdirectory,
and a l means that this is a symbolic link (more on links in Section 7.6). The rest of the characters in the first column give
information about file permissions. We will talk about this more in Section 8.2. The second column gives the number of hard
links to this file or directory (again, more in Section 7.6). The third columns tells who the owner of the file or directory is.
Every file in a Linux filesystem has an owner who can decide who has access to that file. In this case, joe owns every file in
his home directory except the parent directory. The fourth column indicates the group of each file or directory. Groups can be
used to give access to a file to a group of specific users. The fifth column gives the size of the file or directory in bytes. The
sixth column gives the time the file was last modified. Finally, the seventh column gives the name of the file or directory.
4.3 Wildcard Characters
Sometimes it is convenient to specify a group of files or directories as the argument to a command. If the names of these files
are similar in some way, wildcard characters may be used. There are two main wildcard characters: a * can be substituted for
any string of 0 or more characters and a ? can be substituted for exactly one character. For example, if you wanted to list all
files in the working directory that start with prog , you could type
ls prog*
in the shell. If you wanted all the files with a .cc extension, you could type
ls *.cc
in the shell. If you wanted all the files with names like prog1.cc , prog2.cc , etc., but not prog.cc , you could type
ls prog?.cc
in the shell.
Wildcard characters can be used anywhere you would normally use a file or directory name.
The first line indicates that the disk with symbolic name 5 /dev/hda8 is mounted at the root directory. The second line
indicates that another disk with symbolic name /dev/hda1 is mounted at subdirectory /boot . The third line is similar.
The disk in the fourth line is actually a subdirectory on a disk on another networked computer ( sunshine ). The /home
directory on every computer in the lab points to this same directory on sunshine . This is how you are able to access your
home directory (which is actually on sunshine ) the same way from every computer! The protocol that allows this seamless
mounting of remote disks is called the Network File System (NFS). The fifth line is similar to the fourth, and the sixth line
represents the CD-ROM drive, which is mounted at /mnt/cdrom .
5 Viewing Files
5.1 Text Files
The easiest way to view the contents of a file is by using the cat command. The name of the command sounds odd at first, but
it will make more sense when we revisit it in Section 7.7. For example, to view the file named /etc/passwd , simply type
cat /etc/passwd
in the shell. (This file contains some encrypted passwords for this machine.) The cat command is really only good for looking
at small files, since the entire file is displayed without stopping. To view longer files, it is better to use the more command. For
example, try to view the file again by typing
more /etc/passwd
in the shell. Notice that, this time, the command stops after one page is displayed. To view the next line, hit Enter . To view
the next page, hit the space bar. To move backward one page, type b . To exit, type q . If you want to search for something
while in more , type / followed by what you want to find and then hit Enter .
6 Online Help
The commands we have covered so far are just a tiny portion of those available. So how do you find out about these commands
and remember all those you have learned? You don’t; you use the online manuals instead!
man ls
from a shell. The manual page is displayed using more , so hit the space bar to see each page. If you want to find out more about
man itself, type man man . There are manual pages on most Linux shell commands and on most standard C/C++ functions
and Linux system calls.
6.2 Apropos
If you’re looking for how to do something, but don’t know the command yet, try the apropos command. For example, if you
want to display a new clock window, try typing apropos clock . A list of matching manual sections will be listed. There is
an stunning amount of software installed on these machines. Exploring is the only way to found out about it.
Referring to this symbolic link (with cat , for instance) will always refer to the real file /home/joe/my/misc/stuff/friends .
You can also use symbolic links with directories.
Linux actually supports two different kinds of links: hard and symbolic (or soft ). A hard link is a direct link to a lower level
operating system maintained entry for a file on disk, and is less exible than a symbolic link. Using the ln command without
the -s option creates a hard link instead of a symbolic link.
7.9 Searching for Files
7.9.1 Searching by File Name
If you need to access a specific file in the filesystem and you do not know where it is located, you can use the find command
to find it. For example, the command
find . -name paper.txt -print
will search for the file paper.txt starting in the current working directory ( . ). The command
find /bin -name ma* -print
will search for all files that begin with the characters ma , starting in the directory /bin . In each case, the absolute pathname of
each matching file is printed. Although the syntax of the find command looks strange, it gets the job done. There are many
other ways to search for files with find . Consult the online manual (Section 6) for more information.
Sometimes it is useful to be able to search for a file based on the file’s content instead of its name. The grep command can be
used to do just this. For example, the command
grep example ./*
will search all files in the current directory for the string example . You can also use wildcards (and, in general, regular
expressions) with grep . For example, the command
grep ex*le ./*
will search all files in the current directory for strings starting with ex and ending with le .
8 Security
8.1 Changing your Password
It is always a good idea to change your password periodically. To do so, use the yppasswd command. The yp at the beginning
of the command is short for “yellow pages”, the old name of the Network Information Service (NIS), the system that we use to
maintain passwords on our network. When you issue the yppasswd command, you will prompted for both your old password
and your new password.
When you choose a new password, it is important to choose something that is easy for you to remember, yet hard for others
(including automatic “cracker” software) to guess. Use a combination of lower and upper case letters, punctuation, and digits
in your password.
Recall that the first character in the first column ( - ) indicates that the file is a normal file. The next 9 characters indicate the
access permissions for the file. The 9 characters are divided into 3 groups of 3. Each group indicates whether read (r), write
(w), and execute (x) permission is granted to a particular group of users. The first group, corresponding to the first 3 characters,
is the “user” (or file owner), the second group is the file “group”, and the third group is “other”, which consists of all users on
the system. So the line above indicates that the owner has read, write, and execute permission for the file and everyone else has
only read permission.
When the file is actually a directory, read and write permissions refer to the ability to read the contents of the directory and
create new entries in the directory. Executable permission means that one can search in the directory (or list its contents with
ls ), but not read from or write to it.
The chmod command is used to change the access permissions of a file or a directory. There are two ways to specify permis-
sions for a file with chmod . The first is to use a symbolic mode representation of the permissions. In this mode, you use some
combination of u for user, g for group, o for other, or a for all to specify which of these group(s) of users’ permissions to
modify. Then use + to add a permission or - to take away a permission. Lastly, specify the permission(s) to add/subtract: r for
read, w for write, or x for execute. For example, suppose the current working directory contained the following files:
9 Processes
Linux, like most modern operating system, is multitasking , which means that it can execute many programs simultaneously. A
program that is currently executing is called a process . A Linux system consists of several dozen active processes at any time.
Some of these processes are system processes that perform important “behind the scenes” tasks and some are user processes
corresponding to programs like Netscape or Emacs.
This tells you that you are running 2 processes and the commands ( CMD ) used to start them were bash (your shell) and ps
(the process you just started when you entered ps ). The first column in the list, headed by PID , gives the process ID for each
process, an integer used to uniquely identify each process. The next column, TTY , gives the terminal name to which the process
is attached. The third column, TIME , gives the amount of time the process has been running for (or the amount of time it has
been running since it was last sleeping or stopped).
Using ps by itself only gives information about processes that you own, which, unless you are a system administrator, is usually
all the information you need. If you want information about all the processes running on the system, you can issue the
ps aux
command. This command combines 3 options (the normal dash before options is not necessary) which, together, give informa-
tion about all processes that have users have attached to them. Here is a (significantly trimmed) example of what this output
might look like:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 1324 76 ? S Jul23 0:12 init [5]
root 2 0.0 0.0 0 0 ? SW Jul23 0:21 [kflushd]
root 3 0.0 0.0 0 0 ? SW Jul23 0:03 [kupdate]
root 4 0.0 0.0 0 0 ? SW Jul23 0:00 [kpiod]
root 5 0.0 0.0 0 0 ? SW Jul23 0:14 [kswapd]
root 6 0.0 0.0 0 0 ? SW< Jul23 0:00 [mdrecoveryd]
root 415 0.0 0.1 1548 160 ? S Jul23 0:02 syslogd -m 0
rpc 440 0.0 0.1 1472 252 ? S Jul23 0:00 portmap
root 456 0.0 0.0 0 0 ? SW Jul23 0:00 [lockd]
root 500 0.0 0.2 5744 272 ? S Jul23 0:00 [ypbind]
nobody 566 0.0 0.0 7616 24 ? S Jul23 0:00 [identd]
.
.
.
root 1006 14.7 24.7 62796 31612 ? R Jul23 53:50 /etc/X11/X
joe 7892 0.0 1.2 7580 1564 ? S 14:15 0:00 gnome-session
joe 7957 0.0 1.3 4700 1700 ? S 14:15 1:39 sawmill
This listing gives a lot of information about the 100 or so processes that are probably currently executing on the system. For
example, you can gather that the computer was rebooted last on July 23. You can see that X windows ( /etc/X11/X ) is using
almost 25% of the computer’s memory at the moment and 15% of the CPU time. You can also view the current status ( STAT )
of each of the processes in the system. An R in this column means that the processing is currently “runnable” which, as you
will learn when you take Operating Systems, means that the process is either currently running or in the ready queue, waiting
to gain access to the processor. An S indicates that the process is “sleeping” (currently not waiting to use the processor). A
W means that, in addition to sleeping, the process has been “swapped out” (not currently resident in memory but on the disk
instead). Lastly, the < marks a process that is given higher priority than normal while waiting for the CPU.
10.2 Emacs
Although you may use whichever text editor you prefer, we recommend that you learn Emacs. Emacs is an extremely powerful
and exible program, capable of doing much more than just editing text. There exist fervent Emacs aficionados that prefer to
do all of their work through Emacs; they rarely ever type anything into a terminal window! However, Emacs can also be used
in a very intuitive manner, just like any other text editor you may have used in the past.
There are two versions of Emacs available. One version, simply called emacs , works either in text mode in a terminal window
or with a GUI in its own window. Which mode is invoked depends upon whether you are logged in remotely or locally. The
other version, xemacs , only has a GUI, but with more extensive menus and a nicer scroll bar.
In Figure 2, you will find a table of some of the more commonly used Emacs key combinations. If you wish to learn more
advanced features, you are encouraged to browse the detailed, easy-to-use help facility available within the application. There
is also an Emacs book in the department office that we recommend you consult, as appropriate.
11 Compiling Programs
The standard UNIX compilers are the GNU compilers, distributed as part of the GNU project. The C++ compiler is called g++
and the C compiler is called gcc . You can learn more about g++ later by looking at the man pages. (There’s a lot there!) For
now, we’ll just look at the basics.
# A simple makefile
This file states that the executable sort is dependent on the object files sort.o and list.o . If either of these object files
change, the command g++ sort.o list.o -o sort should be used to recreate (link) sort from the object files. The
next two groups show how to create the object files. The first states that sort.o should be recreated (compiled using g++ ) if
sort.cc or sort.h changes. (The -c option to g++ tells the compiler to only compile, creating object files, and not to link
them together.) The third group does a similar thing for list.o . So, you can see that if, for example, list.h is modified,
list.o and then sort will need to be recreated but sort.o will not.
To use this makefile, it should be placed in the same directory as the source files and named Makefile . Then, execute
make sort
to compile and link everything.
Here is a more complicated example of a makefile:
CC = g++
LD = g++
INCDIR = -I../
CFLAGS = -g -c $(INCDIR)
LDFLAGS =
all: sender
clean:
rm *.o
In this makefile, you see the use of macros like CC (the name of the compiler) and LD (the name of the linker). Macros can
sometimes simplify a makefile and make it easier to modify. For example, if we wanted to change the compiler we could
just change the definition of the CC macro instead of changing each and every command list. The INCDIR , CFLAGS , and
LDFLAGS macros contain an option giving additional directories in which to look for header files, compiler options, and linker
options, respectively.
The all target can be followed by the default target(s) you want to create if no target is specified on the command line. In this
case, if make is given on the command line by itself, the target sender will be created. The clean target at the end can be
used to remove unneeded object files from the directory.
12 Networking
Linux was designed from the start to be a net-centric operating system. For the most part, network resources can be used
in Linux seamlessly. Many times, you do not even realize you are using the network. We saw two examples of this earlier:
remotely stored passwords with NIS and accessing your home directory via NFS.
Linux is complete with all the standard network tools including Netscape Navigator, FTP, Telnet, etc.
Simply answer yes and you will be prompted for your password. To end your ssh session, type exit .
xhost +remote-host
in a local shell, where remote-host is the name of the remote host. Then log in to the remote host on which you want to
start the application. Once you are logged in, you have to specify that applications you start should be displayed on your local
machine instead of on the remote one. To do this, you need to set the DISPLAY environment variable:
DISPLAY=local-host:0
The value local-host is the name of the host running the X server (the one at which you are sitting). The :0 is the display
number. Now you are ready to execute a client application. For example, executing netscape now on the remote host will
display the Netscape application on your screen.
umask 077
The first line in the file above is a comment, which is ignored by the shell. The next seven lines assign values to a variety of
environment variables . There are two types of variables in bash : environment variables and user-defined variables . The values
of environment variables are used by the shell and programs executed from the shell to customize their execution. User-defined
variables are used as temporary locations for data during a shell script. You can enter the command
printenv
to see the values of all currently set environment variables. The value of an environment variable can be set by using an
assignment statement like those above. For example, the statement
PS1="\u@\h> "
sets your shell prompt to the form user@host> . The special character \u stands for your user name and the special character
\h stands for the machine’s host name. Similarly, in the statement
PS1="\w$ "
the \w is a special character that stands for the current working directory. So this command changes your prompt to the name
of the working directory followed by a $ and a space.
The PATH variable contains a list of directories (separated by colons) in which the shell will look for programs whose names
you type on the command line. If you need to execute a common program by typing its absolute pathname on the command
line, add the directory to this list and you can simply type the program name from now on. Notice the mention of $PATH at
the beginning of the list. When a variable name is prefixed with $ , the value of the variable is returned. So, in this case, we are
appending the list of directories to the current value of PATH . The CDPATH variable contains a list of directories in which the
shell looks for relative pathnames specified with the cd command. The PRINTER variable contains the name of your default
printer. The USER variable contains your user name. The syntax on the right hand side of the assignment is for command
substitution . This means that the command inside the $(command) construct is executed and the result is assigned to the
variable. In this case, the id -un command returns your user name. The HOSTNAME variable contains the host name of the
machine. The /bin/hostname command gets this information and the result is stored in the variable.
After the variable assignments is the export command. This command causes the variables that follow to be copied by the
shell to every program executed from the shell. For example, if we did not export PRINTER here, the default printer name
would not be known to the lpr command later.
The umask command specifies the default access permissions for new files that you create. The bit mask argument of umask
specifies which permissions should be turned off when a new file is created. The default permissions are obtained by subtracting
the argument of umask from 666 for files and 777 for directories. In this case, the default permissions are set to 600
( rw------- ) for files and 700 ( rwx------ ) for directories.
The last part of the file is an example of a conditional construct. This particular statement tests to see if the file ˜/.bashrc
exists and if so, executes it. See one of the references in the bibliography for more information about shell programming if you
are curious about how this works.
In your .bashrc file, you will find a number of alias commands. Aliases are shorthand for commonly used commands.
For example, the command
Special Symbols
. the current directory
.. the parent directory
˜ your home directory
/ separator between directories in a pathname
* wildcard in a filename that matches any sequence of characters
? wildcard in a filename that matches any single character
& run a process in the background
Example: emacs & runs emacs in the background, freeing up the terminal.
Account Management
yppasswd Change your password on the NIS server.
ypchfn Change your full name on the NIS server.
ypchsh Change your default shell on the NIS server.
Applications
abiword a word processor
acroread Adobe Acrobat Reader
emacs , xemacs a very well endowed text editor.
exmh an electronic mail client
gedit a simple text editor
gimp the GNU image manipulation program (similar to PhotoShop)
glimmer a code-oriented text editor
gnp the gnotepad+ text editor
gnumeric a spreadsheet program
gv file display postscript file file
kedit , kwrite KDE text editors
kword , kspread ,
kpresenter ,
killustrator the KOffice office application suite
latex a typesetting program used by mathematicians and scientists (see references below)
nedit a text editor
netscape the web browser (and e-mail client, etc.)
soffice StarOffice is an office application suite, complete with a word processor, spreadsheet, HTML editor,
etc. It is Microsoft Office compatible.
To use StarOffice, you must first run usr/local/office52/program/setup from your
home directory. This will install about 2 MB of stuff in a directory called office52 . After
installation, you can run StarOffice by executing soffice in the office52 subdirectory.
File Permissions
chmod mode files Set access mode (permissions) for files . There are two ways to specify permissions for a file.
1. First, use some combination of u for user, g for group, o for other, or a for all to specify
which of these group(s) of users’ permissions to modify. Then use + to add a permission or -
to take away a permission. Lastly, specify the permission(s) to add/subtract: r for read, w for
write, or x for execute.
Example: chmod go-rw * takes away read and write permission from group and other for all
files in the current directory.
2. Think of the rwx for a group ( u , g , or o ) as a representation of a 3 bit binary number (0 – 7
in decimal). If a permission is set, that bit is a 1; otherwise it is a 0. Use the 3 decimal digits for
the 3 groups as the permission.
Example: chmod 744 * gives all permissions to user, and only read permission to group and
other.
Help
apropos [-w wildcard] keyword Search manual pages for keyword .
info [topic] Display help on command .
man [section] [topic] Display manual page on command in section .
Miscellaneous
cal [[month] year] Display a calendar for a month or year. By default, display the current month.
clear Clear the terminal screen.
date Display current date and time.
xclock a graphical analog clock
Networking
finger name@host Lookup a user by name on machine host .
ftp host Initiate file transfer protocol with remote machine host .
host [host] Convert host name to IP address, or vice versa, using default name server.
hostname [-d] Display host name of this machine. The -d option displays domain instead.
ncpmount -S server -U user dir Mount NetWare server server on directory dir as user user .
ncpumount dir Unmount the NetWare server mounted on dir .
ssh [host] Remotely log in to the machine host securely.
telnet [host] Remotely log in to the machine host .
rusers Query users logged onto systems on local network.
Printing
lpq [-Pprinter] Display contents of default printer queue or queue of printer .
lpr [-Pprinter] [files] Print files to default printer or printer .
lprm [-Pprinter] [job nums] Remove jobs with numbers nums from a printer queue.
Process Management
kill [-9] pid End process with process id pid . The -9 option kills the process unconditionally.
nice command Execute command with lower than normal priority (to be “nice”).
ps [aux] Display information about currently executing processes. The aux options show a lot of
information about all processes in the system.
top Display the top processes with respect to CPU utilization. Hit q to quit.
Programming
ddd Data Display Debugger (you must compile with the -g option to use the debugger on your program)
gcc the GNU C compiler
g++ the GNU C++ compiler
gdb the GNU command line debugger
xxgdb X front end to GDB