You are on page 1of 80

What is UNIX?

UNIX is an operating system which was first developed in the 1960s, and has been
under constant development ever since. By operating system, we mean the suite of
programs which make the computer work. It is a stable, multi-user, multi-tasking
system for servers, desktops and laptops.

UNIX systems also have a graphical user interface (GUI) similar to Microsoft
Windows which provides an easy to use environment. However, knowledge of UNIX
is required for operations which aren't covered by a graphical program, or for when
there is no windows interface available, for example, in a telnet session.

Types of UNIX

There are many different versions of UNIX, although they share


common similarities. The most popular varieties of UNIX are Sun
Solaris, GNU/Linux, and MacOS X.

Here in the School, we use Solaris on our servers and workstations, and Fedora
Linux on the servers and desktop PCs.

The UNIX operating system

The UNIX operating system is made up of three parts; the kernel, the shell and the
programs.

The kernel

The kernel of UNIX is the hub of the operating system: it allocates time and memory
to programs and handles the filestore and communications in response to system
calls.

As an illustration of the way that the shell and the kernel work together, suppose a
user types rm myfile (which has the effect of removing the file myfile). The shell
searches the filestore for the file containing the program rm, and then requests the
kernel, through system calls, to execute the program rm on myfile. When the
process rm myfile has finished running, the shell then returns the UNIX prompt %
to the user, indicating that it is waiting for further commands.

The shell

The shell acts as an interface between the user and the kernel. When a user logs in,
the login program checks the username and password, and then starts another
program called the shell. The shell is a command line interpreter (CLI). It interprets
the commands the user types in and arranges for them to be carried out. The
commands are themselves programs: when they terminate, the shell gives the user
another prompt (% on our systems).

The adept user can customise his/her own shell, and users can use different shells
on the same machine. Staff and students in the school have the tcsh shell by
default.

The tcsh shell has certain features to help the user inputting commands.

Filename Completion - By typing part of the name of a command, filename or


directory and pressing the [Tab] key, the tcsh shell will complete the rest of the
name automatically. If the shell finds more than one name beginning with those
letters you have typed, it will beep, prompting you to type a few more letters before
pressing the tab key again.

History - The shell keeps a list of the commands you have typed in. If you need to
repeat a command, use the cursor keys to scroll up and down the list or type history
for a list of previous commands.

Files and processes

Everything in UNIX is either a file or a process.

A process is an executing program identified by a unique PID (process identifier).

A file is a collection of data. They are created by users using text editors, running
compilers etc.

Examples of files:
• a document (report, essay etc.)
• the text of a program written in some high-level programming language
• instructions comprehensible directly to the machine and incomprehensible to a casual
user, for example, a collection of binary digits (an executable or binary file);
• a directory, containing information about its contents, which may be a mixture of
other directories (subdirectories) and ordinary files.

The Directory Structure

All the files are grouped together in the directory structure. The file-system is
arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is
traditionally called root (written as a slash / )

In the diagram above, we see that the home directory of the undergraduate student
"ee51vn" contains two sub-directories (docs and pics) and a file called
report.doc.

The full path to the file report.doc is "/home/its/ug1/ee51vn/report.doc"

Starting an UNIX terminal

To open an UNIX terminal window, click on the "Terminal" icon from


Applications/Accessories menus.
An UNIX Terminal window will then appear with a % prompt, waiting for you to start
entering commands.
UNIX Tutorial One

1.1 Listing files and directories

ls (list)

When you first login, your current working directory is your home directory. Your
home directory has the same name as your user-name, for example, ee91ab, and it
is where your personal files and subdirectories are saved.

To find out what is in your home directory, type

% ls

The ls command ( lowercase L and lowercase S ) lists the contents of your current
working directory.

There may be no files visible in your home directory, in which case, the UNIX prompt
will be returned. Alternatively, there may already be some files inserted by the
System Administrator when your account was created.

ls does not, in fact, cause all the files in your home directory to be listed, but only
those ones whose name does not begin with a dot (.) Files beginning with a dot (.)
are known as hidden files and usually contain important program configuration
information. They are hidden because you should not change them unless you are
very familiar with UNIX!!!

To list all files in your home directory including those whose names begin with a dot,
type

% ls -a

As you can see, ls -a lists files that are normally hidden.

ls is an example of a command which can take options: -a is an example of an


option. The options change the behaviour of the command. There are online manual
pages that tell you which options a particular command can take, and how each
option modifies the behaviour of the command. (See later in this tutorial)

1.2 Making Directories

mkdir (make directory)

We will now make a subdirectory in your home directory to hold the files you will be
creating and using in the course of this tutorial. To make a subdirectory called
unixstuff in your current working directory type

% mkdir unixstuff

To see the directory you have just created, type


% ls

1.3 Changing to a different directory

cd (change directory)

The command cd directory means change the current working directory to


'directory'. The current working directory may be thought of as the directory you are
in, i.e. your current position in the file-system tree.

To change to the directory you have just made, type

% cd unixstuff

Type ls to see the contents (which should be empty)

Exercise 1a

Make another directory inside the unixstuff directory called backups

1.4 The directories . and ..

Still in the unixstuff directory, type

% ls -a

As you can see, in the unixstuff directory (and in all other directories), there are
two special directories called (.) and (..)

The current directory (.)

In UNIX, (.) means the current directory, so typing

% cd .

NOTE: there is a space between cd and the dot

means stay where you are (the unixstuff directory).


This may not seem very useful at first, but using (.) as the name of the current
directory will save a lot of typing, as we shall see later in the tutorial.

The parent directory (..)

(..) means the parent of the current directory, so typing

% cd ..

will take you one directory up the hierarchy (back to your home directory). Try it
now.

Note: typing cd with no argument always returns you to your home directory. This is
very useful if you are lost in the file system.

1.5 Pathnames

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-
system. For example, to find out the absolute pathname of your home-directory,
type cd to get back to your home-directory and then type

% pwd

The full pathname will look something like this -

/home/its/ug1/ee51vn

which means that ee51vn (your home directory) is in the sub-directory ug1 (the
group directory),which in turn is located in the its sub-directory, which is in the
home sub-directory, which is in the top-level root directory called " / " .
Exercise 1b

Use the commands cd, ls and pwd to explore the file system.

(Remember, if you get lost, type cd by itself to return to your home-directory)

1.6 More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type

% ls unixstuff

to list the conents of your unixstuff directory.

Now type

% ls backups

You will get a message like this -

backups: No such file or directory


The reason is, backups is not in your current working directory. To use a command
on a file (or directory) not in the current working directory (the directory you are
currently in), you must either cd to the correct directory, or specify its full
pathname. To list the contents of your backups directory, you must type

% ls unixstuff/backups

~ (your home directory)

Home directories can also be referred to by the tilde ~ character. It can be used to
specify paths starting at your home directory. So typing

% ls ~/unixstuff

will list the contents of your unixstuff directory, no matter where you currently are in
the file system.

What do you think

% ls ~

would list?

What do you think

% ls ~/..

would list?

Summary

Command Meaning

ls list files and directories

ls -a list all files and directories


mkdir make a directory

cd directory change to named directory

cd change to home-directory

cd ~ change to home-directory

cd .. change to parent directory

pwd display the path of the current directory

UNIX Tutorial Two

2.1 Copying Files

cp (copy)

cp file1 file2 is the command which makes a copy of file1 in the current working
directory and calls it file2

What we are going to do now, is to take a file stored in an open access area of the
file system, and use the cp command to copy it to your unixstuff directory.

First, cd to your unixstuff directory.

% cd ~/unixstuff

Then at the UNIX prompt, type,

% cp /vol/examples/tutorial/science.txt .

Note: Don't forget the dot . at the end. Remember, in UNIX, the dot means the
current directory.

The above command means copy the file science.txt to the current directory,
keeping the name the same.
(Note: The directory /vol/examples/tutorial/ is an area to which everyone in the
school has read and copy access. If you are from outside the University, you can
grab a copy of the file here. Use 'File/Save As..' from the menu bar to save it into
your unixstuff directory.)

Exercise 2a

Create a backup of your science.txt file by copying it to a file called science.bak

2.2 Moving files

mv (move)

mv file1 file2 moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has the effect
of moving rather than copying the file, so you end up with only one file rather than
two.

It can also be used to rename a file, by moving the file to the same directory, but
giving it a different name.

We are now going to move the file science.bak to your backup directory.

First, change directories to your unixstuff directory (can you remember how?). Then,
inside the unixstuff directory, type

% mv science.bak backups/.

Type ls and ls backups to see if it has worked.

2.3 Removing files and directories

rm (remove), rmdir (remove directory)

To delete (remove) a file, use the rm command. As an example, we are going to


create a copy of the science.txt file then delete it.
Inside your unixstuff directory, type

% cp science.txt tempfile.txt
% ls
% rm tempfile.txt
% ls

You can use the rmdir command to remove a directory (make sure it is empty first).
Try to remove the backups directory. You will not be able to since UNIX will not let
you remove a non-empty directory.

Exercise 2b

Create a directory called tempstuff using mkdir , then remove it using the rmdir
command.

2.4 Displaying the contents of a file on the screen

clear (clear screen)

Before you start the next section, you may like to clear the terminal window of the
previous commands so the output of the following commands can be clearly
understood.

At the prompt, type

% clear

This will clear all text and leave you with the % prompt at the top of the window.

cat (concatenate)

The command cat can be used to display the contents of a file on the screen. Type:

% cat science.txt
As you can see, the file is longer than than the size of the window, so it scrolls past
making it unreadable.

less

The command less writes the contents of a file onto the screen a page at a time.
Type

% less science.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to
quit reading. As you can see, less is used in preference to cat for long files.

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then type

% head science.txt

Then type

% head -5 science.txt

What difference did the -5 do to the head command?

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

% tail science.txt
Q. How can you view the last 15 lines of the file?

2.5 Searching the contents of a file

Simple searching using less

Using less, you can search though a text file for a keyword (pattern). For example,
to search through science.txt for the word 'science', type

% less science.txt

then, still in less, type a forward slash [/] followed by the word to search

/science

As you can see, less finds and highlights the keyword. Type [n] to search for the
next occurrence of the word.

grep (don't ask why it is called grep)

grep is one of many standard UNIX utilities. It searches files for specified words or
patterns. First clear the screen, then type

% grep science science.txt

As you can see, grep has printed out each line containg the word science.

Or has it ????

Try typing

% grep Science science.txt

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option, i.e. type


% grep -i science science.txt

To search for a phrase or pattern, you must enclose it in single quotes (the
apostrophe symbol). For example to search for spinning top, type

% grep -i 'spinning top' science.txt

Some of the other options of grep are:

-v display those lines that do NOT match


-n precede each matching line with the line number
-c print only the total count of matched lines

Try some of them and see the different results. Don't forget, you can use more than
one option at a time. For example, the number of lines without the words science or
Science is

% grep -ivc science science.txt

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on
science.txt, type

% wc -w science.txt

To find out how many lines the file has, type

% wc -l science.txt

Summary

Command Meaning

cp file1 file2 copy file1 and call it file2

mv file1 file2 move or rename file1 to file2


rm file remove a file

rmdir directory remove a directory

cat file display a file

less file display a file a page at a time

head file display the first few lines of a file

tail file display the last few lines of a file

grep 'keyword' file search a file for keywords

wc file count number of lines/words/characters in file

UNIX Tutorial Three

3.1 Redirection

Most processes initiated by UNIX commands write to the standard output (that is,
they write to the terminal screen), and many take their input from the standard
input (that is, they read it from the keyboard). There is also the standard error,
where processes write their error messages, by default, to the terminal screen.

We have already seen one use of the cat command to write the contents of a file to
the screen.

Now type cat without specifing a file to read

% cat

Then type a few words on the keyboard and press the [Return] key.

Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the
input.

What has happened?


If you run the cat command without specifing a file to read, it reads the standard
input (the keyboard), and on receiving the 'end of file' (^D), copies it to the
standard output (the screen).

In UNIX, we can redirect both the input and the output of commands.

3.2 Redirecting the Output

We use the > symbol to redirect the output of a command. For example, to create a
file called list1 containing a list of fruit, type

% cat > list1

Then type in the names of some fruit. Press [Return] after each one.

pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}

What happens is the cat command reads the standard input (the keyboard) and the
> redirects the output, which normally goes to the screen, into a file called list1

To read the contents of the file, type

% cat list1

Exercise 3a

Using the above method, create another file called list2 containing the following
fruit: orange, plum, mango, grapefruit. Read the contents of list2

3.2.1 Appending to a file

The form >> appends standard output to a file. So to add more items to the file
list1, type
% cat >> list1

Then type in the names of more fruit

peach
grape
orange
^D (Control D to stop)

To read the contents of the file, type

% cat list1

You should now have two files. One contains six fruit, the other contains four fruit.

We will now use the cat command to join (concatenate) list1 and list2 into a new
file called biglist. Type

% cat list1 list2 > biglist

What this is doing is reading the contents of list1 and list2 in turn, then outputing
the text to the file biglist

To read the contents of the new file, type

% cat biglist

3.3 Redirecting the Input

We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list. Type

% sort

Then type in the names of some animals. Press [Return] after each one.

dog
cat
bird
ape
^D (control d to stop)

The output will be

ape
bird
cat
dog

Using < you can redirect the input to come from a file rather than the keyboard. For
example, to sort the list of fruit, type

% sort < biglist

and the sorted list will be output to the screen.

To output the sorted list to a file, type,

% sort < biglist > slist

Use cat to read the contents of the file slist

3.4 Pipes

To see who is on the system with you, type

% who

One method to get a sorted list of names is to type,

% who > names.txt


% sort < names.txt

This is a bit slow and you have to remember to remove the temporary file called
names when you have finished. What you really want to do is connect the output of
the who command directly to the input of the sort command. This is exactly what
pipes do. The symbol for a pipe is the vertical bar |

For example, typing


% who | sort

will give the same result as above, but quicker and cleaner.

To find out how many users are logged on, type

% who | wc -l

Exercise 3b

Using pipes, display all lines of list1 and list2 containing the letter 'p', and sort the
result.

Answer available here

Summary

Command Meaning

command > file redirect standard output to a file

command >> file append standard output to a file

command < file redirect standard input from a file

command1 | command2 pipe the output of command1 to the input of command2

cat file1 file2 > file0 concatenate file1 and file2 to file0

sort sort data

who list users currently logged in

4.1 Wildcards

The * wildcard

The character * is called a wildcard, and will match against none or more
character(s) in a file (or directory) name. For example, in your unixstuff directory,
type
% ls list*

This will list all files in the current directory starting with list....

Try typing

% ls *list

This will list all files in the current directory ending with ....list

The ? wildcard

The character ? will match exactly one character.


So ?ouse will match files like house and mouse, but not grouse.
Try typing

% ls ?list

4.2 Filename conventions

We should note here that a directory is merely a special type of file. So the rules and
conventions for naming files apply also to directories.

In naming files, characters with special meanings such as / * & % , should be


avoided. Also, avoid using spaces within names. The safest way to name a file is to
use only alphanumeric characters, that is, letters and numbers, together with _
(underscore) and . (dot).

Good filenames Bad filenames

project.txt project

my_big_program.c my big program.c

fred_dave.doc fred & dave.doc


File names conventionally start with a lower-case letter, and may end with a dot
followed by a group of letters indicating the contents of the file. For example, all files
consisting of C code may be named with the ending .c, for example, prog1.c . Then
in order to list all files containing C code in your home directory, you need only type
ls *.c in that directory.

4.3 Getting Help

On-line Manuals

There are on-line manuals which gives information about most commands. The
manual pages tell you which options a particular command can take, and how each
option modifies the behaviour of the command. Type man command to read the
manual page for a particular command.

For example, to find out more about the wc (word count) command, type

% man wc

Alternatively

% whatis wc

gives a one-line description of the command, but omits any information about
options etc.

Apropos

When you are not sure of the exact name of a command,

% apropos keyword

will give you the commands with keyword in their manual page header. For example,
try typing

% apropos copy

Summary
Command Meaning

* match any number of characters

? match one character

man command read the online manual page for a command

whatis command brief description of a command

apropos keyword match commands with keyword in their man pages

UNIX Tutorial Five

5.1 File system security (access rights)

In your unixstuff directory, type

% ls -l (l for long listing!)

You will see that you now get lots of details about the contents of your directory,
similar to the example below.

Each file (and directory) has associated access rights, which may be found by typing
ls -l. Also, ls -lg gives additional information as to which group owns the file
(beng95 in the following example):
-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1

In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, -,


and, occasionally, s or S. If d is present, it will be at the left hand end of the string,
and indicates a directory: otherwise - will be the starting symbol of the string.

The 9 remaining symbols indicate the permissions, or access rights, and are taken as
three groups of 3.

• The left group of 3 gives the file permissions for the user that owns the file (or
directory) (ee51ab in the above example);
• the middle group gives the permissions for the group of people to whom the file (or
directory) belongs (eebeng95 in the above example);
• the rightmost group gives the permissions for all others.

The symbols r, w, etc., have slightly different meanings depending on whether they
refer to a simple file or to a directory.

Access rights on files.

• r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
• w (or -), indicates write permission (or otherwise), that is, the permission (or
otherwise) to change a file
• x (or -), indicates execution permission (or otherwise), that is, the permission to
execute a file, where appropriate

Access rights on directories.

• r allows users to list files in the directory;


• w means that users may delete files from the directory or move files into it;
• x means the right to access files in the directory. This implies that you may read files
in the directory provided you have read permission on the individual files.

So, in order to read a file, you must have execute permission on the directory
containing that file, and hence on any directory containing that directory as a
subdirectory, and so on, up the tree.

Some examples
-rwxrwxrwx a file that everyone can read, write and execute (and delete).

a file that only the owner can read and write - no-one else

-rw------- can read or write and no-one has execution rights (e.g. your

mailbox file).

5.2 Changing access rights

chmod (changing a file mode)

Only the owner of a file can use chmod to change the permissions of a file. The
options of chmod are as follows

Symbol Meaning

u user

g group

o other

a all

r read

w write (and delete)

x execute (and access directory)

+ add permission

- take away permission

For example, to remove read write and execute permissions on the file biglist for
the group and others, type

% chmod go-rwx biglist

This will leave the other permissions unaffected.

To give read and write permissions on the file biglist to all,


% chmod a+rw biglist

Exercise 5a

Try changing access permissions on the file science.txt and on the directory
backups

Use ls -l to check that the permissions have changed.

5.3 Processes and Jobs

A process is an executing program identified by a unique PID (process identifier). To


see information about your processes, with their associated PID and status, type

% ps

A process may be in the foreground, in the background, or be suspended. In general


the shell does not return the UNIX prompt until the current process has finished
executing.

Some processes take a long time to run and hold up the terminal. Backgrounding a
long process has the effect that the UNIX prompt is returned immediately, and other
tasks can be carried out while the original process continues executing.

Running background processes

To background a process, type an & at the end of the command line. For example,
the command sleep waits a given number of seconds before continuing. Type

% sleep 10

This will wait 10 seconds before returning the command prompt %. Until the
command prompt is returned, you can do nothing except wait.

To run sleep in the background, type

% sleep 10 &

[1] 6259
The & runs the job in the background and returns the prompt straight away,
allowing you do run other programs while waiting for that one to finish.

The first line in the above example is typed in by the user; the next line, indicating
job number and PID, is returned by the machine. The user is be notified of a job
number (numbered from 1) enclosed in square brackets, together with a PID and is
notified when a background process is finished. Backgrounding is useful for jobs
which will take a long time to complete.

Backgrounding a current foreground process

At the prompt, type

% sleep 1000

You can suspend the process running in the foreground by typing ^Z, i.e.hold down
the [Ctrl] key and type [z]. Then to put it in the background, type

% bg

Note: do not background programs that require user interaction e.g. vi

5.4 Listing suspended and background processes

When a process is running, backgrounded or suspended, it will be entered onto a list


along with a job number. To examine this list, type

% jobs

An example of a job list could be

[1] Suspended sleep 1000


[2] Running netscape
[3] Running matlab

To restart (foreground) a suspended processes, type

% fg %jobnumber
For example, to restart sleep 1000, type

% fg %1

Typing fg with no job number foregrounds the last suspended process.

5.5 Killing a process

kill (terminate or signal a process)

It is sometimes necessary to kill a process (for example, when an executing program


is in an infinite loop)

To kill a job running in the foreground, type ^C (control c). For example, run

% sleep 100
^C

To kill a suspended or background process, type

% kill %jobnumber

For example, run

% sleep 100 &


% jobs

If it is job number 4, type

% kill %4

To check whether this has worked, examine the job list again to see if the process
has been removed.

ps (process status)

Alternatively, processes can be killed by finding their process numbers (PIDs) and
using kill PID_number
% sleep 1000 &
% ps

PID TT S TIME COMMAND


20077 pts/5 S 0:05 sleep 1000
21563 pts/5 T 0:00 netscape
21873 pts/5 S 0:25 nedit

To kill off the process sleep 1000, type

% kill 20077

and then type ps again to see if it has been removed from the list.

If a process refuses to be killed, uses the -9 option, i.e. type

% kill -9 20077

Note: It is not possible to kill off other users' processes !!!

Summary

Command Meaning

ls -lag list access rights for all files

chmod [options] file change access rights for named file

command & run command in background

^C kill the job running in the foreground

^Z suspend the job running in the foreground

bg background the suspended job

jobs list current jobs

fg %1 foreground job number 1


kill %1 kill job number 1

ps list current processes

kill 26152 kill process number 26152

UNIX Tutorial Six

Other useful UNIX commands

quota

All students are allocated a certain amount of disk space on the file system for their
personal files, usually about 100Mb. If you go over your quota, you are given 7 days
to remove excess files.

To check your current quota and how much of it you have used, type

% quota -v

df

The df command reports on the space left on the file system. For example, to find
out how much space is left on the fileserver, type

% df .

du

The du command outputs the number of kilobyes used by each subdirectory. Useful
if you have gone over quota and you want to find out which directory has the most
files. In your home-directory, type

% du -s *

The -s flag will display only a summary (total size) and the * means all files and
directories.
gzip

This reduces the size of a file, thus freeing valuable disk space. For example, type

% ls -l science.txt

and note the size of the file using ls -l . Then to compress science.txt, type

% gzip science.txt

This will compress the file and place it in a file called science.txt.gz

To see the change in size, type ls -l again.

To expand the file, use the gunzip command.

% gunzip science.txt.gz

zcat

zcat will read gzipped files without needing to uncompress them first.

% zcat science.txt.gz

If the text scrolls too fast for you, pipe the output though less .

% zcat science.txt.gz | less

file

file classifies the named files according to the type of data they contain, for
example ascii (text), pictures, compressed data, etc.. To report on all files in your
home directory, type

% file *

diff
This command compares the contents of two files and displays the differences.
Suppose you have a file called file1 and you edit some part of it and save it as file2.
To see the differences type

% diff file1 file2

Lines beginning with a < denotes file1, while lines beginning with a > denotes file2.

find

This searches through the directories for files and directories with a given name,
date, size, or any other attribute you care to specify. It is a simple command but
with many options - you can read the manual by typing man find.

To search for all fies with the extention .txt, starting at the current directory (.) and
working through all sub-directories, then printing the name of the file to the screen,
type

% find . -name "*.txt" -print

To find files over 1Mb in size, and display the result as a long listing, type

% find . -size +1M -ls

history

The C shell keeps an ordered list of all the commands that you have entered. Each
command is given a number according to the order it was entered.

% history (show command history list)

If you are using the C shell, you can use the exclamation character (!) to recall
commands easily.

% !! (recall last command)

% !-3 (recall third most recent command)

% !5 (recall 5th command in list)


% !grep (recall last command starting with grep)

You can increase the size of the history buffer by typing

% set history=100

UNIX Tutorial Seven

7.1 Compiling UNIX software packages

We have many public domain and commercial software packages installed on our
systems, which are available to all users. However, students are allowed to
download and install small software packages in their own home directory, software
usually only useful to them personally.

There are a number of steps needed to install the software.

• Locate and download the source code (which is usually compressed)


• Unpack the source code
• Compile the code
• Install the resulting executable
• Set paths to the installation directory

Of the above steps, probably the most difficult is the compilation stage.

Compiling Source Code

All high-level language code must be converted into a form the computer
understands. For example, C language source code is converted into a lower-level
language called assembly language. The assembly language code made by the
previous stage is then converted into object code which are fragments of code which
the computer understands directly. The final stage in compiling a program involves
linking the object code to code libraries which contain certain built-in functions. This
final stage produces an executable program.

To do all these steps by hand is complicated and beyond the capability of the
ordinary user. A number of utilities and tools have been developed for programmers
and end-users to simplify these steps.
make and the Makefile

The make command allows programmers to manage large programs or groups of


programs. It aids in developing large programs by keeping track of which portions of
the entire program have been changed, compiling only those parts of the program
which have changed since the last compile.

The make program gets its set of compile rules from a text file called Makefile which
resides in the same directory as the source files. It contains information on how to
compile the software, e.g. the optimisation level, whether to include debugging info
in the executable. It also contains information on where to install the finished
compiled binaries (executables), manual pages, data files, dependent library files,
configuration files, etc.

Some packages require you to edit the Makefile by hand to set the final installation
directory and any other parameters. However, many packages are now being
distributed with the GNU configure utility.

configure

As the number of UNIX variants increased, it became harder to write programs which
could run on all variants. Developers frequently did not have access to every system,
and the characteristics of some systems changed from version to version. The GNU
configure and build system simplifies the building of programs distributed as source
code. All programs are built using a simple, standardised, two step process. The
program builder need not install any special tools in order to build the program.

The configure shell script attempts to guess correct values for various system-
dependent variables used during compilation. It uses those values to create a
Makefile in each directory of the package.

The simplest way to compile a package is:

1. cd to the directory containing the package's source code.


2. Type ./configure to configure the package for your system.
3. Type make to compile the package.
4. Optionally, type make check to run any self-tests that come with the package.
5. Type make install to install the programs and any data files and documentation.
6. Optionally, type make clean to remove the program binaries and object files from the
source code directory

The configure utility supports a wide variety of options. You can usually use the
--help option to get a list of interesting options for a particular configure script.

The only generic options you are likely to use are the --prefix and --exec-prefix
options. These options are used to specify the installation directories.

The directory named by the --prefix option will hold machine independent files such
as documentation, data and configuration files.

The directory named by the --exec-prefix option, (which is normally a subdirectory


of the --prefix directory), will hold machine dependent files such as executables.

7.2 Downloading source code

For this example, we will download a piece of free software that converts between
different units of measurements.

First create a download directory

% mkdir download

Download the software here and save it to your new download directory.

7.3 Extracting the source code

Go into your download directory and list the contents.

% cd download
% ls -l

As you can see, the filename ends in tar.gz. The tar command turns several files and
directories into one single tar file. This is then compressed using the gzip command
(to create a tar.gz file).

First unzip the file using the gunzip command. This will create a .tar file.
% gunzip units-1.74.tar.gz

Then extract the contents of the tar file.

% tar -xvf units-1.74.tar

Again, list the contents of the download directory, then go to the units-1.74 sub-
directory.

% cd units-1.74

7.4 Configuring and creating the Makefile

The first thing to do is carefully read the README and INSTALL text files (use the
less command). These contain important information on how to compile and run the
software.

The units package uses the GNU configure system to compile the source code. We
will need to specify the installation directory, since the default will be the main
system area which you will not have write permissions for. We need to create an
install directory in your home directory.

% mkdir ~/units174

Then run the configure utility setting the installation path to this.

% ./configure --prefix=$HOME/units174

NOTE: The $HOME variable is an example of an environment variable. The value of


$HOME is the path to your home directory. Just type

% echo $HOME

to show the contents of this variable. We will learn more about environment
variables in a later chapter.
If configure has run correctly, it will have created a Makefile with all necessary
options. You can view the Makefile if you wish (use the less command), but do not
edit the contents of this.

7.5 Building the package

Now you can go ahead and build the package by running the make command.

% make

After a minute or two (depending on the speed of the computer), the executables
will be created. You can check to see everything compiled successfully by typing

% make check

If everything is okay, you can now install the package.

% make install

This will install the files into the ~/units174 directory you created earlier.

7.6 Running the software

You are now ready to run the software (assuming everything worked).

% cd ~/units174

If you list the contents of the units directory, you will see a number of
subdirectories.

bin The binary executables

info GNU info formatted documentation

man Man pages

share Shared data files

To run the program, change to the bin directory and type


% ./units

As an example, convert 6 feet to metres.

You have: 6 feet


You want: metres

* 1.8288

If you get the answer 1.8288, congratulations, it worked.

To view what units it can convert between, view the data file in the share directory
(the list is quite comprehensive).

To read the full documentation, change into the info directory and type

% info --file=units.info

7.7 Stripping unnecessary code

When a piece of software is being developed, it is useful for the programmer to


include debugging information into the resulting executable. This way, if there are
problems encountered when running the executable, the programmer can load the
executable into a debugging software package and track down any software bugs.

This is useful for the programmer, but unnecessary for the user. We can assume that
the package, once finished and available for download has already been tested and
debugged. However, when we compiled the software above, debugging information
was still compiled into the final executable. Since it is unlikey that we are going to
need this debugging information, we can strip it out of the final executable. One of
the advantages of this is a much smaller executable, which should run slightly faster.

What we are going to do is look at the before and after size of the binary file. First
change into the bin directory of the units installation directory.

% cd ~/units174/bin
% ls -l
As you can see, the file is over 100 kbytes in size. You can get more information on
the type of file by using the file command.

% file units

units: ELF 32-bit LSB executable, Intel 80386, version 1,


dynamically linked (uses shared libs), not stripped

To strip all the debug and line numbering information out of the binary file, use the
strip command

% strip units
% ls -l

As you can see, the file is now 36 kbytes - a third of its original size. Two thirds of
the binary file was debug code!!!

Check the file information again.

% file units

units: ELF 32-bit LSB executable, Intel 80386, version 1,


dynamically linked (uses shared libs), stripped

Sometimes you can use the make command to install pre-stripped copies of all the
binary files when you install the package. Instead of typing make install, simply
type make install-strip

UNIX Tutorial Eight

8.1 UNIX Variables

Variables are a way of passing information from the shell to programs when you run
them. Programs look "in the environment" for particular variables and if they are
found will use the values stored. Some are set by the system, others by you, yet
others by the shell, or any program that loads another program.

Standard UNIX variables are split into two categories, environment variables and
shell variables. In broad terms, shell variables apply only to the current instance of
the shell and are used to set short-term working conditions; environment variables
have a farther reaching significance, and those set at login are valid for the duration
of the session. By convention, environment variables have UPPER CASE and shell
variables have lower case names.

8.2 Environment Variables

An example of an environment variable is the OSTYPE variable. The value of this is


the current operating system you are using. Type

% echo $OSTYPE

More examples of environment variables are

• USER (your login name)


• HOME (the path name of your home directory)
• HOST (the name of the computer you are using)
• ARCH (the architecture of the computers processor)
• DISPLAY (the name of the computer screen to display X windows)
• PRINTER (the default printer to send print jobs)
• PATH (the directories the shell should search to find a command)

Finding out the current values of these variables.

ENVIRONMENT variables are set using the setenv command, displayed using the
printenv or env commands, and unset using the unsetenv command.

To show all values of these variables, type

% printenv | less

8.3 Shell Variables

An example of a shell variable is the history variable. The value of this is how many
shell commands to save, allow the user to scroll back through all the commands they
have previously entered. Type

% echo $history

More examples of shell variables are


• cwd (your current working directory)
• home (the path name of your home directory)
• path (the directories the shell should search to find a command)
• prompt (the text string used to prompt for interactive commands shell your login
shell)

Finding out the current values of these variables.

SHELL variables are both set and displayed using the set command. They can be
unset by using the unset command.

To show all values of these variables, type

% set | less

So what is the difference between PATH and path ?

In general, environment and shell variables that have the same name (apart from
the case) are distinct and independent, except for possibly having the same initial
values. There are, however, exceptions.

Each time the shell variables home, user and term are changed, the corresponding
environment variables HOME, USER and TERM receive the same values. However,
altering the environment variables has no effect on the corresponding shell
variables.

PATH and path specify directories to search for commands and programs. Both
variables always represent the same directory list, and altering either automatically
causes the other to be changed.

8.4 Using and setting variables

Each time you login to a UNIX host, the system looks in your home directory for
initialisation files. Information in these files is used to set up your working
environment. The C and TC shells uses two files called .login and .cshrc (note that
both file names begin with a dot).

At login the C shell first reads .cshrc followed by .login


.login is to set conditions which will apply to the whole session and to perform
actions that are relevant only at login.

.cshrc is used to set conditions and perform actions specific to the shell and to each
invocation of it.

The guidelines are to set ENVIRONMENT variables in the .login file and SHELL
variables in the .cshrc file.

WARNING: NEVER put commands that run graphical displays (e.g. a web browser)
in your .cshrc or .login file.

8.5 Setting shell variables in the .cshrc file

For example, to change the number of shell commands saved in the history list, you
need to set the shell variable history. It is set to 100 by default, but you can
increase this if you wish.

% set history = 200

Check this has worked by typing

% echo $history

However, this has only set the variable for the lifetime of the current shell. If you
open a new xterm window, it will only have the default history value set. To
PERMANENTLY set the value of history, you will need to add the set command to
the .cshrc file.

First open the .cshrc file in a text editor. An easy, user-friendly editor to use is
nedit.

% nedit ~/.cshrc

Add the following line AFTER the list of other commands.

set history = 200


Save the file and force the shell to reread its .cshrc file buy using the shell source
command.

% source .cshrc

Check this has worked by typing

% echo $history

8.6 Setting the path

When you type a command, your path (or PATH) variable defines in which directories
the shell will look to find the command you typed. If the system returns a message
saying "command: Command not found", this indicates that either the command
doesn't exist at all on the system or it is simply not in your path.

For example, to run units, you either need to directly specify the units path
(~/units174/bin/units), or you need to have the directory ~/units174/bin in
your path.

You can add it to the end of your existing path (the $path represents this) by
issuing the command:

% set path = ($path ~/units174/bin)

Test that this worked by trying to run units in any directory other that where units is
actually located.

% cd
% units

To add this path PERMANENTLY, add the following line to your .cshrc AFTER the list
of other commands.

set path = ($path ~/units174/bin)


3.1 Synopsis
The following chapter will cover the basic commands and functionality of the FreeBSD
operating system. Much of this material is relevant for any UNIX®-like operating
system. Feel free to skim over this chapter if you are familiar with the material. If you are
new to FreeBSD, then you will definitely want to read through this chapter carefully.

After reading this chapter, you will know:

• How to use the “virtual consoles” of FreeBSD.


• How UNIX file permissions work along with understanding file flags in
FreeBSD.
• The default FreeBSD file system layout.
• The FreeBSD disk organization.
• How to mount and unmount file systems.
• What processes, daemons, and signals are.
• What a shell is, and how to change your default login environment.
• How to use basic text editors.
• What devices and device nodes are.
• What binary format is used under FreeBSD.
• How to read manual pages for more information.

3.2 Virtual Consoles and Terminals


FreeBSD can be used in various ways. One of them is typing commands to a text
terminal. A lot of the flexibility and power of a UNIX® operating system is readily
available at your hands when using FreeBSD this way. This section describes what
“terminals” and “consoles” are, and how you can use them in FreeBSD.

3.2.1 The Console


If you have not configured FreeBSD to automatically start a graphical environment
during startup, the system will present you with a login prompt after it boots, right after
the startup scripts finish running. You will see something similar to:
Additional ABI support:.

Local package initialization:.

Additional TCP options:.

Fri Sep 20 13:01:06 EEST 2002

FreeBSD/i386 (pc3.example.org) (ttyv0)

login:

The messages might be a bit different on your system, but you will see something similar.
The last two lines are what we are interested in right now. The second last line reads:

FreeBSD/i386 (pc3.example.org) (ttyv0)

This line contains some bits of information about the system you have just booted. You
are looking at a “FreeBSD” console, running on an Intel or compatible processor of the
x86 architecture[1]. The name of this machine (every UNIX machine has a name) is
pc3.example.org, and you are now looking at its system console--the ttyv0 terminal.

Finally, the last line is always:

login:

This is the part where you are supposed to type in your “username” to log into FreeBSD.
The next section describes how you can do this.

3.2.2 Logging into FreeBSD


FreeBSD is a multiuser, multiprocessing system. This is the formal description that is
usually given to a system that can be used by many different people, who simultaneously
run a lot of programs on a single machine.

Every multiuser system needs some way to distinguish one “user” from the rest. In
FreeBSD (and all the UNIX-like operating systems), this is accomplished by requiring
that every user must “log into” the system before being able to run programs. Every user
has a unique name (the “username”) and a personal, secret key (the “password”).
FreeBSD will ask for these two before allowing a user to run any programs.

Right after FreeBSD boots and finishes running its startup scripts[2], it will present you
with a prompt and ask for a valid username:
login:

For the sake of this example, let us assume that your username is john. Type john at this
prompt and press Enter. You should then be presented with a prompt to enter a
“password”:

login: john

Password:

Type in john's password now, and press Enter. The password is not echoed! You need
not worry about this right now. Suffice it to say that it is done for security reasons.

If you have typed your password correctly, you should by now be logged into FreeBSD
and ready to try out all the available commands.

You should see the MOTD or message of the day followed by a command prompt (a #, $,
or % character). This indicates you have successfully logged into FreeBSD.

3.2.3 Multiple Consoles


Running UNIX commands in one console is fine, but FreeBSD can run many programs at
once. Having one console where commands can be typed would be a bit of a waste when
an operating system like FreeBSD can run dozens of programs at the same time. This is
where “virtual consoles” can be very helpful.

FreeBSD can be configured to present you with many different virtual consoles. You can
switch from one of them to any other virtual console by pressing a couple of keys on your
keyboard. Each console has its own different output channel, and FreeBSD takes care of
properly redirecting keyboard input and monitor output as you switch from one virtual
console to the next.

Special key combinations have been reserved by FreeBSD for switching consoles[3].
You can use Alt-F1, Alt-F2, through Alt-F8 to switch to a different virtual console in
FreeBSD.

As you are switching from one console to the next, FreeBSD takes care of saving and
restoring the screen output. The result is an “illusion” of having multiple “virtual” screens
and keyboards that you can use to type commands for FreeBSD to run. The programs that
you launch on one virtual console do not stop running when that console is not visible.
They continue running when you have switched to a different virtual console.

3.2.4 The /etc/ttys File


The default configuration of FreeBSD will start up with eight virtual consoles. This is not
a hardwired setting though, and you can easily customize your installation to boot with
more or fewer virtual consoles. The number and settings of the virtual consoles are
configured in the /etc/ttys file.

You can use the /etc/ttys file to configure the virtual consoles of FreeBSD. Each
uncommented line in this file (lines that do not start with a # character) contains settings
for a single terminal or virtual console. The default version of this file that ships with
FreeBSD configures nine virtual consoles, and enables eight of them. They are the lines
that start with ttyv:

# name getty type status comments

ttyv0 "/usr/libexec/getty Pc" cons25 on secure

# Virtual terminals

ttyv1 "/usr/libexec/getty Pc" cons25 on secure

ttyv2 "/usr/libexec/getty Pc" cons25 on secure

ttyv3 "/usr/libexec/getty Pc" cons25 on secure

ttyv4 "/usr/libexec/getty Pc" cons25 on secure

ttyv5 "/usr/libexec/getty Pc" cons25 on secure

ttyv6 "/usr/libexec/getty Pc" cons25 on secure

ttyv7 "/usr/libexec/getty Pc" cons25 on secure

ttyv8 "/usr/X11R6/bin/xdm -nodaemon" xterm off secure

For a detailed description of every column in this file and all the options you can use to
set things up for the virtual consoles, consult the ttys(5) manual page.

3.2.5 Single User Mode Console


A detailed description of what “single user mode” is can be found in Section 12.6.2. It is
worth noting that there is only one console when you are running FreeBSD in single user
mode. There are no virtual consoles available. The settings of the single user mode
console can also be found in the /etc/ttys file. Look for the line that starts with
console:

# name getty type status comments

# If console is marked "insecure", then init will ask for the root
password

# when going to single-user mode.


console none unknown off secure

Note: As the comments above the console line indicate, you can edit this line and
change secure to insecure. If you do that, when FreeBSD boots into single user mode,
it will still ask for the root password.

Be careful when changing this to insecure. If you ever forget the root password,
booting into single user mode is a bit involved. It is still possible, but it might be a bit
hard for someone who is not very comfortable with the FreeBSD booting process and the
programs involved.

3.2.6 Changing Console Video Modes


The FreeBSD console default video mode may be adjusted to 1024x768, 1280x1024, or
any other size supported by your graphics chip and monitor. To use a different video
mode, you first must recompile your kernel and include two additional options:

options VESA

options SC_PIXEL_MODE

Once the kernel has been recompiled with these two options, you can then determine
what video modes are supported by your hardware by using the vidcontrol(1) utility. To
get a list of supported video modes issue the following:

# vidcontrol -i mode

The output of this command is a list of video modes that are supported by your hardware.
You can then choose to use a new video mode by passing it to vidcontrol(1) in a root
console:

# vidcontrol MODE_279

If the new video mode is acceptable, it can be permanently set on boot by setting it in the
/etc/rc.conf file:

allscreens_flags="MODE_279"

Notes
[1] This is what i386 means. Note that even if you are not running FreeBSD on an Intel
386 CPU, this is going to be i386. It is not the type of your processor, but the
processor “architecture” that is shown here.
[2] Startup scripts are programs that are run automatically by FreeBSD when booting.
Their main function is to set things up for everything else to run, and start any
services that you have configured to run in the background doing useful things.
[3] A fairly technical and accurate description of all the details of the FreeBSD console
and keyboard drivers can be found in the manual pages of syscons(4), atkbd(4),
vidcontrol(1) and kbdcontrol(1). We will not expand on the details here, but the
interested reader can always consult the manual pages for a more detailed and
thorough explanation of how things work.

.3 Permissions
FreeBSD, being a direct descendant of BSD UNIX®, is based on several key UNIX
concepts. The first and most pronounced is that FreeBSD is a multi-user operating
system. The system can handle several users all working simultaneously on completely
unrelated tasks. The system is responsible for properly sharing and managing requests for
hardware devices, peripherals, memory, and CPU time fairly to each user.

Because the system is capable of supporting multiple users, everything the system
manages has a set of permissions governing who can read, write, and execute the
resource. These permissions are stored as three octets broken into three pieces, one for
the owner of the file, one for the group that the file belongs to, and one for everyone else.
This numerical representation works like this:

Value Permission Directory Listing


0 No read, no write, no execute ---
1 No read, no write, execute --x
2 No read, write, no execute -w-
3 No read, write, execute -wx
4 Read, no write, no execute r--
5 Read, no write, execute r-x
6 Read, write, no execute rw-
7 Read, write, execute rwx

You can use the -l command line argument to ls(1) to view a long directory listing that
includes a column with information about a file's permissions for the owner, group, and
everyone else. For example, a ls -l in an arbitrary directory may show:

% ls -l

total 530

-rw-r--r-- 1 root wheel 512 Sep 5 12:31 myfile

-rw-r--r-- 1 root wheel 512 Sep 5 12:31 otherfile

-rw-r--r-- 1 root wheel 7680 Sep 5 12:31 email.txt

...
Here is how the first column of ls -l is broken up:

-rw-r--r--

The first (leftmost) character tells if this file is a regular file, a directory, a special
character device, a socket, or any other special pseudo-file device. In this case, the -
indicates a regular file. The next three characters, rw- in this example, give the
permissions for the owner of the file. The next three characters, r--, give the permissions
for the group that the file belongs to. The final three characters, r--, give the permissions
for the rest of the world. A dash means that the permission is turned off. In the case of
this file, the permissions are set so the owner can read and write to the file, the group can
read the file, and the rest of the world can only read the file. According to the table
above, the permissions for this file would be 644, where each digit represents the three
parts of the file's permission.

This is all well and good, but how does the system control permissions on devices?
FreeBSD actually treats most hardware devices as a file that programs can open, read,
and write data to just like any other file. These special device files are stored on the /dev
directory.

Directories are also treated as files. They have read, write, and execute permissions. The
executable bit for a directory has a slightly different meaning than that of files. When a
directory is marked executable, it means it can be traversed into, that is, it is possible to
“cd” (change directory) into it. This also means that within the directory it is possible to
access files whose names are known (subject, of course, to the permissions on the files
themselves).

In particular, in order to perform a directory listing, read permission must be set on the
directory, whilst to delete a file that one knows the name of, it is necessary to have write
and execute permissions to the directory containing the file.

There are more permission bits, but they are primarily used in special circumstances such
as setuid binaries and sticky directories. If you want more information on file permissions
and how to set them, be sure to look at the chmod(1) manual page.

3.3.1 Symbolic Permissions


Contributed by Tom Rhodes.

Symbolic permissions, sometimes referred to as symbolic expressions, use characters in


place of octal values to assign permissions to files or directories. Symbolic expressions
use the syntax of (who) (action) (permissions), where the following values are available:

Option Letter Represents


(who) u User
(who) g Group owner
Option Letter Represents
(who) o Other
(who) a All (“world”)
(action) + Adding permissions
(action) - Removing permissions
(action) = Explicitly set permissions
(permissions) r Read
(permissions) w Write
(permissions) x Execute
(permissions) t Sticky bit
(permissions) s Set UID or GID

These values are used with the chmod(1) command just like before, but with letters. For
an example, you could use the following command to block other users from accessing
FILE:

% chmod go= FILE

A comma separated list can be provided when more than one set of changes to a file must
be made. For example the following command will remove the group and “world” write
permission on FILE, then it adds the execute permissions for everyone:

% chmod go-w,a+x FILE

3.3.2 FreeBSD File Flags


Contributed by Tom Rhodes.

In addition to file permissions discussed previously, FreeBSD supports the use of “file
flags.” These flags add an additional level of security and control over files, but not
directories.

These file flags add an additional level of control over files, helping to ensure that in
some cases not even the root can remove or alter files.

File flags are altered by using the chflags(1) utility, using a simple interface. For
example, to enable the system undeletable flag on the file file1, issue the following
command:

# chflags sunlink file1

And to disable the system undeletable flag, simply issue the previous command with “no”
in front of the sunlink. Observe:

# chflags nosunlink file1


To view the flags of this file, use the ls(1) command with the -lo flags:

# ls -lo file1

The output should look like the following:

-rw-r--r-- 1 trhodes trhodes sunlnk 0 Mar 1 05:54 file1

Several flags may only added or removed to files by the root user. In other cases, the file
owner may set these flags. It is recommended that administrators read over the chflags(1)
and chflags(2) manual pages for more information.

3.3.3 The setuid, setgid, and sticky Permissions


Contributed by Tom Rhodes.

Other than the permissions already discussed, there are three other specific settings that
all administrators should know about. They are the setuid, setgid and sticky
permissions.

These settings are important for some UNIX operations as they provide functionality not
normally granted to normal users. To understand them, the difference between the real
user ID and effective user ID must also be noted.

The real user ID is the UID who owns or starts the process. The effective UID is the user
ID the process runs as. As an example, the passwd(1) utility runs with the real user ID as
the user changing their password; however, to manipulate the password database, it runs
as the effective ID of the root user. This is what allows normal users to change their
passwords without seeing a “Permission Denied” error.

Note: The nosuid mount(8) option will cause these binaries to silently fail. That is, they
will fail to execute without ever alerting the user. That option is also not completely
reliable as a nosuid wrapper may be able to circumvent it; according to the mount(8)
manual page.

The setuid permission may be set by prefixing a permission set with the number four (4)
as shown in the following example:

# chmod 4755 suidexample.sh

The permissions on the suidexample.sh file should now look like the following:

-rwsr-xr-x 1 trhodes trhodes 63 Aug 29 06:36 suidexample.sh


It should be noticeable from this example that an s is now part of the permission set
designated for the file owner, replacing the executable bit. This allows utilities which
need elevated permissions, such as passwd.

To view this in real time, open two terminals. On one, start the passwd process as a
normal user. While it waits for a new password, check the process table and look at the
user information of the passwd command.

In terminal A:

Changing local password for trhodes

Old Password:

In terminal B:

# ps aux | grep passwd

trhodes 5232 0.0 0.2 3420 1608 0 R+ 2:10AM 0:00.00 grep


passwd

root 5211 0.0 0.2 3620 1724 2 I+ 2:09AM 0:00.01 passwd

As stated above, the passwd is run by a normal user, but is using the effective UID of
root.

The setgid permission performs the same function as the setuid permission; except that
it alters the group settings. When an application or utility is ran with this setting, it will be
granted the permissions based on the group that owns the file, not the user who started
the process.

To set the setgid permission on a file, provide the chmod command with a leading two
(2) as in the following example:

# chmod 2755 sgidexample.sh

The new setting may be viewed as before, notice the s is now in the field designated for
the group permission settings:

-rwxr-sr-x 1 trhodes trhodes 44 Aug 31 01:49 sgidexample.sh

Note: In these examples, even though the shell script in question is an executable file, it
will not run with a different EUID or effective user ID. This is because shell scripts may
not access the setuid(2) system calls.
The first two special permission bits we discussed (the setuid and setgid permission
bits) may lower system security, by allowing for elevated permissions. There is a third
special permission bit that can strengthen the security of a system: the sticky bit.

The sticky bit, when set on a directory, allows file deletion only by the file owner.
This permission set is useful to prevent file deletion in public directories, such as /tmp,
by users who do not own the file. To utilize this permission, prefix the permission with a
one (1). For example:

# chmod 1777 /tmp

Now, it is possible to see the effect by using the ls command:

# ls -al / | grep tmp

drwxrwxrwt 10 root wheel 512 Aug 31 01:49 tmp

The sticky bit permission is distinguishable from the t at the very end of the set.

3.4 Directory Structure


The FreeBSD directory hierarchy is fundamental to obtaining an overall understanding of
the system. The most important concept to grasp is that of the root directory, “/”. This
directory is the first one mounted at boot time and it contains the base system necessary
to prepare the operating system for multi-user operation. The root directory also contains
mount points for other file systems that are mounted during the transition to multi-user
operation.

A mount point is a directory where additional file systems can be grafted onto a parent
file system (usually the root file system). This is further described in Section 3.5.
Standard mount points include /usr, /var, /tmp, /mnt, and /cdrom. These directories
are usually referenced to entries in the file /etc/fstab. /etc/fstab is a table of various
file systems and mount points for reference by the system. Most of the file systems in
/etc/fstab are mounted automatically at boot time from the script rc(8) unless they
contain the noauto option. Details can be found in Section 3.6.1.

A complete description of the file system hierarchy is available in hier(7). For now, a
brief overview of the most common directories will suffice.

Directory Description
/ Root directory of the file system.
/bin/
User utilities fundamental to both single-user and multi-user
environments.
/boot/
Programs and configuration files used during operating system
bootstrap.
Directory Description
/boot/defaults/Default bootstrapping configuration files; see loader.conf(5).
/dev/ Device nodes; see intro(4).
/etc/ System configuration files and scripts.
/etc/defaults/ Default system configuration files; see rc(8).
/etc/mail/ Configuration files for mail transport agents such as sendmail(8).
/etc/namedb/ named configuration files; see named(8).

/etc/periodic/
Scripts that are run daily, weekly, and monthly, via cron(8); see
periodic(8).
/etc/ppp/ ppp configuration files; see ppp(8).

/mnt/
Empty directory commonly used by system administrators as a
temporary mount point.
/proc/ Process file system; see procfs(5), mount_procfs(8).
/rescue/ Statically linked programs for emergency recovery; see rescue(8).
/root/ Home directory for the root account.
/sbin/
System programs and administration utilities fundamental to both
single-user and multi-user environments.
Temporary files. The contents of /tmp are usually NOT preserved
/tmp/
across a system reboot. A memory-based file system is often mounted
at /tmp. This can be automated using the tmpmfs-related variables of
rc.conf(5) (or with an entry in /etc/fstab; see mdmfs(8)).
/usr/ The majority of user utilities and applications.
/usr/bin/ Common utilities, programming tools, and applications.
/usr/include/ Standard C include files.
/usr/lib/ Archive libraries.
/usr/libdata/ Miscellaneous utility data files.
/usr/libexec/ System daemons & system utilities (executed by other programs).
Local executables, libraries, etc. Also used as the default destination
for the FreeBSD ports framework. Within /usr/local, the general
/usr/local/
layout sketched out by hier(7) for /usr should be used. Exceptions are
the man directory, which is directly under /usr/local rather than
under /usr/local/share, and the ports documentation is in
share/doc/port.

/usr/obj/
Architecture-specific target tree produced by building the /usr/src
tree.
/usr/ports/ The FreeBSD Ports Collection (optional).
/usr/sbin/ System daemons & system utilities (executed by users).
/usr/share/ Architecture-independent files.
/usr/src/ BSD and/or local source files.
/usr/X11R6/ X11R6 distribution executables, libraries, etc (optional).
/var/ Multi-purpose log, temporary, transient, and spool files. A memory-
based file system is sometimes mounted at /var. This can be
automated using the varmfs-related variables of rc.conf(5) (or with an
Directory Description
entry in /etc/fstab; see mdmfs(8)).
/var/log/ Miscellaneous system log files.
/var/mail/ User mailbox files.
/var/spool/ Miscellaneous printer and mail system spooling directories.
/var/tmp/
Temporary files. The files are usually preserved across a system
reboot, unless /var is a memory-based file system.
/var/yp/ NIS maps.

3.5 Disk Organization


The smallest unit of organization that FreeBSD uses to find files is the filename.
Filenames are case-sensitive, which means that readme.txt and README.TXT are two
separate files. FreeBSD does not use the extension (.txt) of a file to determine whether
the file is a program, or a document, or some other form of data.

Files are stored in directories. A directory may contain no files, or it may contain many
hundreds of files. A directory can also contain other directories, allowing you to build up
a hierarchy of directories within one another. This makes it much easier to organize your
data.

Files and directories are referenced by giving the file or directory name, followed by a
forward slash, /, followed by any other directory names that are necessary. If you have
directory foo, which contains directory bar, which contains the file readme.txt, then
the full name, or path to the file is foo/bar/readme.txt.

Directories and files are stored in a file system. Each file system contains exactly one
directory at the very top level, called the root directory for that file system. This root
directory can then contain other directories.

So far this is probably similar to any other operating system you may have used. There
are a few differences; for example, MS-DOS® uses \ to separate file and directory
names, while Mac OS® uses :.

FreeBSD does not use drive letters, or other drive names in the path. You would not write
c:/foo/bar/readme.txt on FreeBSD.

Instead, one file system is designated the root file system. The root file system's root
directory is referred to as /. Every other file system is then mounted under the root file
system. No matter how many disks you have on your FreeBSD system, every directory
appears to be part of the same disk.
Suppose you have three file systems, called A, B, and C. Each file system has one root
directory, which contains two other directories, called A1, A2 (and likewise B1, B2 and C1,
C2).

Call A the root file system. If you used the ls command to view the contents of this
directory you would see two subdirectories, A1 and A2. The directory tree looks like this:

A file system must be mounted on to a directory in another file system. So now suppose
that you mount file system B on to the directory A1. The root directory of B replaces A1,
and the directories in B appear accordingly:

Any files that are in the B1 or B2 directories can be reached with the path /A1/B1 or
/A1/B2 as necessary. Any files that were in /A1 have been temporarily hidden. They will
reappear if B is unmounted from A.

If B had been mounted on A2 then the diagram would look like this:
and the paths would be /A2/B1 and /A2/B2 respectively.

File systems can be mounted on top of one another. Continuing the last example, the C
file system could be mounted on top of the B1 directory in the B file system, leading to
this arrangement:

Or C could be mounted directly on to the A file system, under the A1 directory:


If you are familiar with MS-DOS, this is similar, although not identical, to the join
command.

This is not normally something you need to concern yourself with. Typically you create
file systems when installing FreeBSD and decide where to mount them, and then never
change them unless you add a new disk.

It is entirely possible to have one large root file system, and not need to create any others.
There are some drawbacks to this approach, and one advantage.

Benefits of Multiple File Systems

• Different file systems can have different mount options. For example, with
careful planning, the root file system can be mounted read-only, making it
impossible for you to inadvertently delete or edit a critical file. Separating
user-writable file systems, such as /home, from other file systems also
allows them to be mounted nosuid; this option prevents the suid/guid bits
on executables stored on the file system from taking effect, possibly
improving security.
• FreeBSD automatically optimizes the layout of files on a file system,
depending on how the file system is being used. So a file system that
contains many small files that are written frequently will have a different
optimization to one that contains fewer, larger files. By having one big file
system this optimization breaks down.
• FreeBSD's file systems are very robust should you lose power. However, a
power loss at a critical point could still damage the structure of the file
system. By splitting your data over multiple file systems it is more likely
that the system will still come up, making it easier for you to restore from
backup as necessary.
Benefit of a Single File System

• Filesystems are a fixed size. If you create a file system when you
install FreeBSD and give it a specific size, you may later discover
that you need to make the partition bigger. This is not easily
accomplished without backing up, recreating the file system with
the new size, and then restoring the backed up data.

Important: FreeBSD features the growfs(8) command,


which makes it possible to increase the size of file system on
the fly, removing this limitation.

File systems are contained in partitions. This does not have the same
meaning as the common usage of the term partition (for example, MS-
DOS partition), because of FreeBSD's UNIX® heritage. Each partition is
identified by a letter from a through to h. Each partition can contain only
one file system, which means that file systems are often described by
either their typical mount point in the file system hierarchy, or the letter of
the partition they are contained in.

FreeBSD also uses disk space for swap space. Swap space provides
FreeBSD with virtual memory. This allows your computer to behave as
though it has much more memory than it actually does. When FreeBSD
runs out of memory it moves some of the data that is not currently being
used to the swap space, and moves it back in (moving something else out)
when it needs it.

Some partitions have certain conventions associated with them.

Partition Convention
a Normally contains the root file system
b Normally contains swap space
Normally the same size as the enclosing slice. This allows utilities that need to
c work on the entire slice (for example, a bad block scanner) to work on the c
partition. You would not normally create a file system on this partition.
d
Partition d used to have a special meaning associated with it, although that is
now gone and d may work as any normal partition.

Each partition-that-contains-a-file-system is stored in what FreeBSD calls


a slice. Slice is FreeBSD's term for what the common call partitions, and
again, this is because of FreeBSD's UNIX background. Slices are
numbered, starting at 1, through to 4.

Slice numbers follow the device name, prefixed with an s, starting at 1. So


“da0s1” is the first slice on the first SCSI drive. There can only be four
physical slices on a disk, but you can have logical slices inside physical
slices of the appropriate type. These extended slices are numbered starting
at 5, so “ad0s5” is the first extended slice on the first IDE disk. These
devices are used by file systems that expect to occupy a slice.

Slices, “dangerously dedicated” physical drives, and other drives contain


partitions, which are represented as letters from a to h. This letter is
appended to the device name, so “da0a” is the a partition on the first da
drive, which is “dangerously dedicated”. “ad1s3e” is the fifth partition in
the third slice of the second IDE disk drive.

Finally, each disk on the system is identified. A disk name starts with a
code that indicates the type of disk, and then a number, indicating which
disk it is. Unlike slices, disk numbering starts at 0. Common codes that
you will see are listed in Table 3-1.

When referring to a partition FreeBSD requires that you also name the
slice and disk that contains the partition, and when referring to a slice you
must also refer to the disk name. Thus, you refer to a partition by listing
the disk name, s, the slice number, and then the partition letter. Examples
are shown in Example 3-1.

Example 3-2 shows a conceptual model of the disk layout that should help
make things clearer.

In order to install FreeBSD you must first configure the disk slices, then
create partitions within the slice you will use for FreeBSD, and then create
a file system (or swap space) in each partition, and decide where that file
system will be mounted.

Table 3-1. Disk Device Codes

Code Meaning
ad ATAPI (IDE) disk
da SCSI direct access disk
acd ATAPI (IDE) CDROM
cd SCSI CDROM
fd Floppy disk

Example 3-1. Sample Disk, Slice, and Partition Names

Name Meaning
ad0s1a The first partition (a) on the first slice (s1) on the first IDE disk (ad0).
da1s2e The fifth partition (e) on the second slice (s2) on the second SCSI disk (da1).

Example 3-2. Conceptual Model of a Disk


This diagram shows FreeBSD's view of the first IDE disk attached to the
system. Assume that the disk is 4 GB in size, and contains two 2 GB slices
(MS-DOS partitions). The first slice contains a MS-DOS disk, C:, and the
second slice contains a FreeBSD installation. This example FreeBSD
installation has three data partitions, and a swap partition.

The three partitions will each hold a file system. Partition a will be used
for the root file system, e for the /var directory hierarchy, and f for the
/usr directory hierarchy.
3.6 Mounting and Unmounting File Systems
The file system is best visualized as a tree, rooted, as it were, at /. /dev, /usr, and the
other directories in the root directory are branches, which may have their own branches,
such as /usr/local, and so on.
There are various reasons to house some of these directories on separate file systems.
/var contains the directories log/, spool/, and various types of temporary files, and as
such, may get filled up. Filling up the root file system is not a good idea, so splitting /var
from / is often favorable.

Another common reason to contain certain directory trees on other file systems is if they
are to be housed on separate physical disks, or are separate virtual disks, such as Network
File System mounts, or CDROM drives.

3.6.1 The fstab File


During the boot process, file systems listed in /etc/fstab are automatically mounted
(unless they are listed with the noauto option).

The /etc/fstab file contains a list of lines of the following format:

device /mount-point fstype options dumpfreq passno

device

A device name (which should exist), as explained in Section 18.2.

mount-point

A directory (which should exist), on which to mount the file system.

fstype

The file system type to pass to mount(8). The default FreeBSD file system is ufs.

options

Either rw for read-write file systems, or ro for read-only file systems, followed by
any other options that may be needed. A common option is noauto for file
systems not normally mounted during the boot sequence. Other options are listed
in the mount(8) manual page.

dumpfreq

This is used by dump(8) to determine which file systems require dumping. If the
field is missing, a value of zero is assumed.

passno

This determines the order in which file systems should be checked. File systems
that should be skipped should have their passno set to zero. The root file system
(which needs to be checked before everything else) should have its passno set to
one, and other file systems' passno should be set to values greater than one. If
more than one file systems have the same passno then fsck(8) will attempt to
check file systems in parallel if possible.

Consult the fstab(5) manual page for more information on the format of the /etc/fstab
file and the options it contains.

3.6.2 The mount Command


The mount(8) command is what is ultimately used to mount file systems.

In its most basic form, you use:

# mount device mountpoint

There are plenty of options, as mentioned in the mount(8) manual page, but the most
common are:

Mount Options

-a

Mount all the file systems listed in /etc/fstab. Except those marked as
“noauto”, excluded by the -t flag, or those that are already mounted.

-d

Do everything except for the actual mount system call. This option is useful in
conjunction with the -v flag to determine what mount(8) is actually trying to do.

-f

Force the mount of an unclean file system (dangerous), or forces the revocation of
write access when downgrading a file system's mount status from read-write to
read-only.

-r

Mount the file system read-only. This is identical to using the ro (rdonly for
FreeBSD versions older than 5.2) argument to the -o option.

-t fstype

Mount the given file system as the given file system type, or mount only file
systems of the given type, if given the -a option.
“ufs” is the default file system type.

-u

Update mount options on the file system.

-v

Be verbose.

-w

Mount the file system read-write.

The -o option takes a comma-separated list of the options, including the following:

noexec

Do not allow execution of binaries on this file system. This is also a useful
security option.

nosuid

Do not interpret setuid or setgid flags on the file system. This is also a useful
security option.

3.6.3 The umount Command


The umount(8) command takes, as a parameter, one of a mountpoint, a device name, or
the -a or -A option.

All forms take -f to force unmounting, and -v for verbosity. Be warned that -f is not
generally a good idea. Forcibly unmounting file systems might crash the computer or
damage data on the file system.

-a and -A are used to unmount all mounted file systems, possibly modified by the file
system types listed after -t. -A, however, does not attempt to unmount the root file
system.

3.7 Processes
FreeBSD is a multi-tasking operating system. This means that it seems as though more
than one program is running at once. Each program running at any one time is called a
process. Every command you run will start at least one new process, and there are a
number of system processes that run all the time, keeping the system functional.
Each process is uniquely identified by a number called a process ID, or PID, and, like
files, each process also has one owner and group. The owner and group information is
used to determine what files and devices the process can open, using the file permissions
discussed earlier. Most processes also have a parent process. The parent process is the
process that started them. For example, if you are typing commands to the shell then the
shell is a process, and any commands you run are also processes. Each process you run in
this way will have your shell as its parent process. The exception to this is a special
process called init(8). init is always the first process, so its PID is always 1. init is
started automatically by the kernel when FreeBSD starts.

Two commands are particularly useful to see the processes on the system, ps(1) and
top(1). The ps command is used to show a static list of the currently running processes,
and can show their PID, how much memory they are using, the command line they were
started with, and so on. The top command displays all the running processes, and updates
the display every few seconds, so that you can interactively see what your computer is
doing.

By default, ps only shows you the commands that are running and are owned by you. For
example:

% ps

PID TT STAT TIME COMMAND

298 p0 Ss 0:01.10 tcsh

7078 p0 S 2:40.88 xemacs mdoc.xsl (xemacs-21.1.14)

37393 p0 I 0:03.11 xemacs freebsd.dsl (xemacs-21.1.14)

48630 p0 S 2:50.89 /usr/local/lib/netscape-linux/navigator-


linux-4.77.bi

48730 p0 IW 0:00.00 (dns helper) (navigator-linux-)

72210 p0 R+ 0:00.00 ps

390 p1 Is 0:01.14 tcsh

7059 p2 Is+ 1:36.18 /usr/local/bin/mutt -y

6688 p3 IWs 0:00.00 tcsh

10735 p4 IWs 0:00.00 tcsh

20256 p5 IWs 0:00.00 tcsh

262 v0 IWs 0:00.00 -tcsh (tcsh)

270 v0 IW+ 0:00.00 /bin/sh /usr/X11R6/bin/startx -- -bpp 16

280 v0 IW+ 0:00.00 xinit /home/nik/.xinitrc -- -bpp 16

284 v0 IW 0:00.00 /bin/sh /home/nik/.xinitrc

285 v0 S 0:38.45 /usr/X11R6/bin/sawfish


As you can see in this example, the output from ps(1) is organized into a number of
columns. PID is the process ID discussed earlier. PIDs are assigned starting from 1, go up
to 99999, and wrap around back to the beginning when you run out (a PID is not
reassigned if it is already in use). The TT column shows the tty the program is running on,
and can safely be ignored for the moment. STAT shows the program's state, and again, can
be safely ignored. TIME is the amount of time the program has been running on the CPU--
this is usually not the elapsed time since you started the program, as most programs spend
a lot of time waiting for things to happen before they need to spend time on the CPU.
Finally, COMMAND is the command line that was used to run the program.

ps(1) supports a number of different options to change the information that is displayed.
One of the most useful sets is auxww. a displays information about all the running
processes, not just your own. u displays the username of the process' owner, as well as
memory usage. x displays information about daemon processes, and ww causes ps(1) to
display the full command line for each process, rather than truncating it once it gets too
long to fit on the screen.

The output from top(1) is similar. A sample session looks like this:

% top

last pid: 72257; load averages: 0.13, 0.09, 0.03 up 0+13:38:33


22:39:10

47 processes: 1 running, 46 sleeping

CPU states: 12.6% user, 0.0% nice, 7.8% system, 0.0% interrupt,
79.7% idle

Mem: 36M Active, 5256K Inact, 13M Wired, 6312K Cache, 15M Buf, 408K Free

Swap: 256M Total, 38M Used, 217M Free, 15% Inuse

PID USERNAME PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND

72257 nik 28 0 1960K 1044K RUN 0:00 14.86% 1.42% top

7078 nik 2 0 15280K 10960K select 2:54 0.88% 0.88%


xemacs-21.1.14

281 nik 2 0 18636K 7112K select 5:36 0.73% 0.73%


XF86_SVGA

296 nik 2 0 3240K 1644K select 0:12 0.05% 0.05% xterm

48630 nik 2 0 29816K 9148K select 3:18 0.00% 0.00%


navigator-linu

175 root 2 0 924K 252K select 1:41 0.00% 0.00% syslogd

7059 nik 2 0 7260K 4644K poll 1:38 0.00% 0.00% mutt

...
The output is split into two sections. The header (the first five lines) shows the PID of the
last process to run, the system load averages (which are a measure of how busy the
system is), the system uptime (time since the last reboot) and the current time. The other
figures in the header relate to how many processes are running (47 in this case), how
much memory and swap space has been taken up, and how much time the system is
spending in different CPU states.

Below that are a series of columns containing similar information to the output from
ps(1). As before you can see the PID, the username, the amount of CPU time taken, and
the command that was run. top(1) also defaults to showing you the amount of memory
space taken by the process. This is split into two columns, one for total size, and one for
resident size--total size is how much memory the application has needed, and the resident
size is how much it is actually using at the moment. In this example you can see that
Netscape® has required almost 30 MB of RAM, but is currently only using 9 MB.

top(1) automatically updates this display every two seconds; this can be changed with the
s option.

3.8 Daemons, Signals, and Killing Processes


When you run an editor it is easy to control the editor, tell it to load files, and so on. You
can do this because the editor provides facilities to do so, and because the editor is
attached to a terminal. Some programs are not designed to be run with continuous user
input, and so they disconnect from the terminal at the first opportunity. For example, a
web server spends all day responding to web requests, it normally does not need any
input from you. Programs that transport email from site to site are another example of this
class of application.

We call these programs daemons. Daemons were characters in Greek mythology: neither
good or evil, they were little attendant spirits that, by and large, did useful things for
mankind, much like the web servers and mail servers of today do useful things. This is
why the BSD mascot has, for a long time, been the cheerful-looking daemon with
sneakers and a pitchfork.

There is a convention to name programs that normally run as daemons with a trailing “d”.
BIND is the Berkeley Internet Name Domain, but the actual program that executes is
called named; the Apache web server program is called httpd; the line printer spooling
daemon is lpd and so on. This is a convention, not a hard and fast rule; for example, the
main mail daemon for the Sendmail application is called sendmail, and not maild, as
you might imagine.

Sometimes you will need to communicate with a daemon process. One way to do so is to
send it (or any other running process), what is known as a signal. There are a number of
different signals that you can send--some of them have a specific meaning, others are
interpreted by the application, and the application's documentation will tell you how that
application interprets signals. You can only send a signal to a process that you own. If
you send a signal to someone else's process with kill(1) or kill(2), permission will be
denied. The exception to this is the root user, who can send signals to everyone's
processes.

FreeBSD will also send applications signals in some cases. If an application is badly
written, and tries to access memory that it is not supposed to, FreeBSD sends the process
the Segmentation Violation signal (SIGSEGV). If an application has used the alarm(3)
system call to be alerted after a period of time has elapsed then it will be sent the Alarm
signal (SIGALRM), and so on.

Two signals can be used to stop a process, SIGTERM and SIGKILL. SIGTERM is the polite
way to kill a process; the process can catch the signal, realize that you want it to shut
down, close any log files it may have open, and generally finish whatever it is doing at
the time before shutting down. In some cases a process may even ignore SIGTERM if it is
in the middle of some task that can not be interrupted.

SIGKILL can not be ignored by a process. This is the “I do not care what you are doing,
stop right now” signal. If you send SIGKILL to a process then FreeBSD will stop that
process there and then[1].

The other signals you might want to use are SIGHUP, SIGUSR1, and SIGUSR2. These are
general purpose signals, and different applications will do different things when they are
sent.

Suppose that you have changed your web server's configuration file--you would like to
tell the web server to re-read its configuration. You could stop and restart httpd, but this
would result in a brief outage period on your web server, which may be undesirable.
Most daemons are written to respond to the SIGHUP signal by re-reading their
configuration file. So instead of killing and restarting httpd you would send it the
SIGHUP signal. Because there is no standard way to respond to these signals, different
daemons will have different behavior, so be sure and read the documentation for the
daemon in question.

Signals are sent using the kill(1) command, as this example shows.

Sending a Signal to a Process

This example shows how to send a signal to inetd(8). The inetd configuration file is
/etc/inetd.conf, and inetd will re-read this configuration file when it is sent SIGHUP.

1. Find the process ID of the process you want to send the signal to.
Do this using ps(1) and grep(1). The grep(1) command is used to search
through output, looking for the string you specify. This command is run as
a normal user, and inetd(8) is run as root, so the ax options must be given
to ps(1).
2. % ps -ax | grep inetd

3. 198 ?? IWs 0:00.00 inetd -wW

So the inetd(8) PID is 198. In some cases the grep inetd command might
also appear in this output. This is because of the way ps(1) has to find the
list of running processes.

4. Use kill(1) to send the signal. Because inetd(8) is being run by


root you must use su(1) to become root first.

5. % su

6. Password:

7. # /bin/kill -s HUP 198

In common with most UNIX® commands, kill(1) will not print any output
if it is successful. If you send a signal to a process that you do not own then
you will see “kill: PID: Operation not permitted”. If you mistype
the PID you will either send the signal to the wrong process, which could
be bad, or, if you are lucky, you will have sent the signal to a PID that is
not currently in use, and you will see “kill: PID: No such process”.

Why Use /bin/kill?: Many shells provide the kill command as


a built in command; that is, the shell will send the signal directly,
rather than running /bin/kill. This can be very useful, but
different shells have a different syntax for specifying the name of
the signal to send. Rather than try to learn all of them, it can be
simpler just to use the /bin/kill ... command directly.

Sending other signals is very similar, just substitute TERM or KILL in the command
line as necessary.

Important: Killing random process on the system can be a bad idea. In


particular, init(8), process ID 1, is very special. Running /bin/kill -s
KILL 1 is a quick way to shutdown your system. Always double check the
arguments you run kill(1) with before you press Return.

Notes
[1] Not quite true--there are a few things that can not be interrupted. For example, if the
process is trying to read from a file that is on another computer on the network, and
the other computer has gone away for some reason (been turned off, or the network
has a fault), then the process is said to be “uninterruptible”. Eventually the process
will time out, typically after two minutes. As soon as this time out occurs the process
will be killed.
3.9 Shells
In FreeBSD, a lot of everyday work is done in a command line interface called a shell. A
shell's main job is to take commands from the input channel and execute them. A lot of
shells also have built in functions to help with everyday tasks such as file management,
file globbing, command line editing, command macros, and environment variables.
FreeBSD comes with a set of shells, such as sh, the Bourne Shell, and tcsh, the
improved C-shell. Many other shells are available from the FreeBSD Ports Collection,
such as zsh and bash.

Which shell do you use? It is really a matter of taste. If you are a C programmer you
might feel more comfortable with a C-like shell such as tcsh. If you have come from
Linux or are new to a UNIX® command line interface you might try bash. The point is
that each shell has unique properties that may or may not work with your preferred
working environment, and that you have a choice of what shell to use.

One common feature in a shell is filename completion. Given the typing of the first few
letters of a command or filename, you can usually have the shell automatically complete
the rest of the command or filename by hitting the Tab key on the keyboard. Here is an
example. Suppose you have two files called foobar and foo.bar. You want to delete
foo.bar. So what you would type on the keyboard is: rm fo[Tab].[Tab].

The shell would print out rm foo[BEEP].bar.

The [BEEP] is the console bell, which is the shell telling me it was unable to totally
complete the filename because there is more than one match. Both foobar and foo.bar
start with fo, but it was able to complete to foo. If you type in ., then hit Tab again, the
shell would be able to fill in the rest of the filename for you.

Another feature of the shell is the use of environment variables. Environment variables
are a variable/key pair stored in the shell's environment space. This space can be read by
any program invoked by the shell, and thus contains a lot of program configuration. Here
is a list of common environment variables and what they mean:

Variable Description
USER Current logged in user's name.
PATH Colon-separated list of directories to search for binaries.
DISPLAY Network name of the X11 display to connect to, if available.
SHELL The current shell.
TERM
The name of the user's type of terminal. Used to determine the capabilities of
the terminal.
TERMCAP
Database entry of the terminal escape codes to perform various terminal
functions.
Variable Description
OSTYPE Type of operating system. e.g., FreeBSD.
MACHTYPE The CPU architecture that the system is running on.
EDITOR The user's preferred text editor.
PAGER The user's preferred text pager.
MANPATH Colon-separated list of directories to search for manual pages.

Setting an environment variable differs somewhat from shell to shell. For example, in the
C-Style shells such as tcsh and csh, you would use setenv to set environment variables.
Under Bourne shells such as sh and bash, you would use export to set your current
environment variables. For example, to set or modify the EDITOR environment variable,
under csh or tcsh a command like this would set EDITOR to /usr/local/bin/emacs:

% setenv EDITOR /usr/local/bin/emacs

Under Bourne shells:

% export EDITOR="/usr/local/bin/emacs"

You can also make most shells expand the environment variable by placing a $ character
in front of it on the command line. For example, echo $TERM would print out whatever
$TERM is set to, because the shell expands $TERM and passes it on to echo.

Shells treat a lot of special characters, called meta-characters as special representations of


data. The most common one is the * character, which represents any number of
characters in a filename. These special meta-characters can be used to do filename
globbing. For example, typing in echo * is almost the same as typing in ls because the
shell takes all the files that match * and puts them on the command line for echo to see.

To prevent the shell from interpreting these special characters, they can be escaped from
the shell by putting a backslash (\) character in front of them. echo $TERM prints
whatever your terminal is set to. echo \$TERM prints $TERM as is.

3.9.1 Changing Your Shell


The easiest way to change your shell is to use the chsh command. Running chsh will
place you into the editor that is in your EDITOR environment variable; if it is not set, you
will be placed in vi. Change the “Shell:” line accordingly.

You can also give chsh the -s option; this will set your shell for you, without requiring
you to enter an editor. For example, if you wanted to change your shell to bash, the
following should do the trick:

% chsh -s /usr/local/bin/bash
Note: The shell that you wish to use must be present in the /etc/shells file. If you have
installed a shell from the ports collection, then this should have been done for you
already. If you installed the shell by hand, you must do this.

For example, if you installed bash by hand and placed it into /usr/local/bin, you
would want to:

# echo "/usr/local/bin/bash" >> /etc/shells

Then rerun chsh.

3.10 Text Editors


A lot of configuration in FreeBSD is done by editing text files. Because of this, it would
be a good idea to become familiar with a text editor. FreeBSD comes with a few as part
of the base system, and many more are available in the Ports Collection.

The easiest and simplest editor to learn is an editor called ee, which stands for easy
editor. To start ee, one would type at the command line ee filename where filename is
the name of the file to be edited. For example, to edit /etc/rc.conf, type in ee
/etc/rc.conf. Once inside of ee, all of the commands for manipulating the editor's
functions are listed at the top of the display. The caret ^ character represents the Ctrl key
on the keyboard, so ^e expands to the key combination Ctrl+e. To leave ee, hit the Esc
key, then choose leave editor. The editor will prompt you to save any changes if the file
has been modified.

FreeBSD also comes with more powerful text editors such as vi as part of the base
system, while other editors, like Emacs and vim, are part of the FreeBSD Ports
Collection (editors/emacs and editors/vim). These editors offer much more
functionality and power at the expense of being a little more complicated to learn.
However if you plan on doing a lot of text editing, learning a more powerful editor such
as vim or Emacs will save you much more time in the long run.

Many applications which modify files or require typed input will automatically open a
text editor. To alter the default editor used, set the EDITOR environment variable. See
shells section for more details.

3.11 Devices and Device Nodes


A device is a term used mostly for hardware-related activities in a system, including
disks, printers, graphics cards, and keyboards. When FreeBSD boots, the majority of
what FreeBSD displays are devices being detected. You can look through the boot
messages again by viewing /var/run/dmesg.boot.

For example, acd0 is the first IDE CDROM drive, while kbd0 represents the keyboard.

Most of these devices in a UNIX® operating system must be accessed through special
files called device nodes, which are located in the /dev directory.

3.11.1 Creating Device Nodes


When adding a new device to your system, or compiling in support for additional
devices, new device nodes must be created.

3.11.1.1 DEVFS (DEVice File System)

The device file system, or DEVFS, provides access to kernel's device namespace in the
global file system namespace. Instead of having to create and modify device nodes,
DEVFS maintains this particular file system for you.

See the devfs(5) manual page for more information.

3.12 Binary Formats


To understand why FreeBSD uses the elf(5) format, you must first know a little about the
three currently “dominant” executable formats for UNIX®:

• a.out(5)

The oldest and “classic” UNIX object format. It uses a short and compact
header with a magic number at the beginning that is often used to
characterize the format (see a.out(5) for more details). It contains three
loaded segments: .text, .data, and .bss plus a symbol table and a string table.

• COFF

The SVR3 object format. The header now comprises a section table, so you
can have more than just .text, .data, and .bss sections.

• elf(5)

The successor to COFF, featuring multiple sections and 32-bit or 64-bit


possible values. One major drawback: ELF was also designed with the
assumption that there would be only one ABI per system architecture. That
assumption is actually quite incorrect, and not even in the commercial
SYSV world (which has at least three ABIs: SVR4, Solaris, SCO) does it
hold true.

FreeBSD tries to work around this problem somewhat by providing a utility


for branding a known ELF executable with information about the ABI it is
compliant with. See the manual page for brandelf(1) for more information.

FreeBSD comes from the “classic” camp and used the a.out(5) format, a
technology tried and proven through many generations of BSD releases, until the
beginning of the 3.X branch. Though it was possible to build and run native ELF
binaries (and kernels) on a FreeBSD system for some time before that, FreeBSD
initially resisted the “push” to switch to ELF as the default format. Why? Well,
when the Linux camp made their painful transition to ELF, it was not so much to
flee the a.out executable format as it was their inflexible jump-table based
shared library mechanism, which made the construction of shared libraries very
difficult for vendors and developers alike. Since the ELF tools available offered a
solution to the shared library problem and were generally seen as “the way
forward” anyway, the migration cost was accepted as necessary and the transition
made. FreeBSD's shared library mechanism is based more closely on Sun's
SunOS™ style shared library mechanism and, as such, is very easy to use.

So, why are there so many different formats?

Back in the dim, dark past, there was simple hardware. This simple hardware
supported a simple, small system. a.out was completely adequate for the job of
representing binaries on this simple system (a PDP-11). As people ported UNIX
from this simple system, they retained the a.out format because it was sufficient
for the early ports of UNIX to architectures like the Motorola 68k, VAXen, etc.

Then some bright hardware engineer decided that if he could force software to do
some sleazy tricks, then he would be able to shave a few gates off the design and
allow his CPU core to run faster. While it was made to work with this new kind of
hardware (known these days as RISC), a.out was ill-suited for this hardware, so
many formats were developed to get to a better performance from this hardware
than the limited, simple a.out format could offer. Things like COFF, ECOFF,
and a few obscure others were invented and their limitations explored before
things seemed to settle on ELF.

In addition, program sizes were getting huge and disks (and physical memory)
were still relatively small so the concept of a shared library was born. The VM
system also became more sophisticated. While each one of these advancements
was done using the a.out format, its usefulness was stretched more and more
with each new feature. In addition, people wanted to dynamically load things at
run time, or to junk parts of their program after the init code had run to save in
core memory and swap space. Languages became more sophisticated and people
wanted code called before main automatically. Lots of hacks were done to the
a.out format to allow all of these things to happen, and they basically worked for
a time. In time, a.out was not up to handling all these problems without an ever
increasing overhead in code and complexity. While ELF solved many of these
problems, it would be painful to switch from the system that basically worked. So
ELF had to wait until it was more painful to remain with a.out than it was to
migrate to ELF.

However, as time passed, the build tools that FreeBSD derived their build tools
from (the assembler and loader especially) evolved in two parallel trees. The
FreeBSD tree added shared libraries and fixed some bugs. The GNU folks that
originally wrote these programs rewrote them and added simpler support for
building cross compilers, plugging in different formats at will, and so on. Since
many people wanted to build cross compilers targeting FreeBSD, they were out of
luck since the older sources that FreeBSD had for as and ld were not up to the
task. The new GNU tools chain (binutils) does support cross compiling, ELF,
shared libraries, C++ extensions, etc. In addition, many vendors are releasing ELF
binaries, and it is a good thing for FreeBSD to run them.

ELF is more expressive than a.out and allows more extensibility in the base
system. The ELF tools are better maintained, and offer cross compilation support,
which is important to many people. ELF may be a little slower than a.out, but
trying to measure it can be difficult. There are also numerous details that are
different between the two in how they map pages, handle init code, etc. None of
these are very important, but they are differences. In time support for a.out will
be moved out of the GENERIC kernel, and eventually removed from the kernel
once the need to run legacy a.out programs is past.

3.13 For More Information


3.13.1 Manual Pages
The most comprehensive documentation on FreeBSD is in the form of manual pages.
Nearly every program on the system comes with a short reference manual explaining the
basic operation and various arguments. These manuals can be viewed with the man
command. Use of the man command is simple:

% man command

command is the name of the command you wish to learn about. For example, to learn
more about ls command type:

% man ls

The online manual is divided up into numbered sections:


1. User commands.
2. System calls and error numbers.
3. Functions in the C libraries.
4. Device drivers.
5. File formats.
6. Games and other diversions.
7. Miscellaneous information.
8. System maintenance and operation commands.
9. Kernel developers.

In some cases, the same topic may appear in more than one section of the online
manual. For example, there is a chmod user command and a chmod() system call.
In this case, you can tell the man command which one you want by specifying the
section:

% man 1 chmod

This will display the manual page for the user command chmod. References to a
particular section of the online manual are traditionally placed in parenthesis in
written documentation, so chmod(1) refers to the chmod user command and
chmod(2) refers to the system call.

This is fine if you know the name of the command and simply wish to know how
to use it, but what if you cannot recall the command name? You can use man to
search for keywords in the command descriptions by using the -k switch:

% man -k mail

With this command you will be presented with a list of commands that have the
keyword “mail” in their descriptions. This is actually functionally equivalent to
using the apropos command.

So, you are looking at all those fancy commands in /usr/bin but do not have the
faintest idea what most of them actually do? Simply do:

% cd /usr/bin

% man -f *

or

% cd /usr/bin

% whatis *

which does the same thing.


3.13.2 GNU Info Files
FreeBSD includes many applications and utilities produced by the Free Software
Foundation (FSF). In addition to manual pages, these programs come with more
extensive hypertext documents called info files which can be viewed with the
info command or, if you installed emacs, the info mode of emacs.

To use the info(1) command, simply type:

% info

For a brief introduction, type h. For a quick command reference, type ?.

You might also like