You are on page 1of 68

Linux

Kathiravelu Ganeshan
Senior Academic Staff Member
School of Computing and Information Technology
Unitec New Zealand

Linux
Kathiravelu Ganeshan
Senior Academic Staff Member
School of Computing and Information Technology
Unitec New Zealand

In memory of my father, Iyampillai Kathiravelu

ISBN 978-0-473-12925-5
First published: December 2007
Copyright 2007 Kathiravelu Ganeshan
This publication may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopy, recording or any information storage and retrieval system
provided the source is acknowledged.

Published by Kathiravelu Ganeshan


6 McQuoid Place
Glenfield
Auckland
New Zealand
Tel: +64 9 443 4689
Mobile: +64 21 063 61 61
Email: ganeshan17@hotmail.com
Email: ganeshan@ihug.co.nz
Websites:
http://ganeshanpublications.blogspot.com
http://cvganeshan.blogspot.com
http://homepages.ihug.co.nz/~ganeshan
Copies of this book are available from the publisher.
All comments on this book will be most appreciated.

PREFACE

This book was written especially for my students in the School of Computing and Information
Technology, Unitec New Zealand and is available free as a pdf file.

ACKNOWLEDGEMENTS
I wish to acknowledge the support and encouragement given by,
Associate Professor Alison Young, Head, School of Computing and Information Technology,
Unitec, New Zealand,
Dr Donald Koh, Associate Head of School, School of Computing and Information Technology,
Unitec, New Zealand, and,
Hira Sathu, Academic Leader, Technical Infrastructure Group, School of Computing and
Information Technology, Unitec, New Zealand, who also kindly checked through the first draft and
made some valuable suggestions.

In the past few semesters, a number of my students requested me and encouraged me to write this
book. Several students also gave some feedback on the draft versions of this book. Notable among
these students are (in alphabetical order):
Aparna Badve, Babu Sankariah, Chhorn Moung, Dakshai Chauhan, David Dewani, Deepak
Manchanda, Dilip Pai, Dmitry Pantileev, Marama Glen-Mokai, Mark Piwari, Natesh Marappa,
Oliver Furneaux, Parvin Karan, Ping Han, Placidus Fernandez, Sandesh Kothare, Sean Van Der
Vlugt, Smita Dwivedi, and Thomas Laurenson.

CHAPTERS
1. Introduction

2. Basic Commands

11

3. Directories

15

4. Files

17

5. Mounting USB and DVD File Systems

19

6. An Editor Called vi

21

7. Adding Users and Setting Passwords

23

8. Scripts

25

9. Adding Groups and Assigning Users to Groups

29

10. Improving Your Script

31

11. A Taste of awk

35

12. Arguments Again

37

13. Variables

39

14. Package Management RPMs

41

15. Networking 1: SSH

43

16. Networking 2: DHCP

45

17. Networking 3: DNS 1

49

18. Networking 4: DNS 2

53

19. Networking 5: NFS

57

20. Networking 6: Dynamic DNS

59

21. Networking 7: FTP

63

22. Networking 8: Apache Web Server

65

CHAPTER 1 INTRODUCTION
Why Linux?
Linux is one of the two major operating systems in the world, today. It is the most used operating
system on servers. It is gaining a respectable share of the desktop market. Linux is, arguably, the
most stable, and the most secure operating system available. As the source code of Linux is freely
available, several countries with security concerns are switching to Linux. Several countries with
large populations are also embracing Linux, because it is free.
I use Linux, because, in addition to the benefits mentioned above, it gives me total, fast and easy
access to the hardware. This is important to me in controlling my robots and other devices. I also
like the advantages of using the command line mode. Some versions of Linux are less than fifty
megabytes in size, and these can do most of the jobs that are currently done by operating systems
that take up more than two gigabytes. Needless to say, the smaller systems are faster too.
Above all, I have fun using Linux.
But, Wait, there's More
Linux is also an operating system that gives you access to lots of free, open source, applications
software, including Open Office, great games and the Gimp. This book is available in odt, pdf, doc,
txt and talking book formats. All these versions were prepared using only legally free, open source
software such as, Open Office and the Gimp on a Linux computer.
What do You Need, to Use this Book Effectively?
Although, you are more than welcome to use this book without any conditions, you would benefit
greatly if you have a computer that runs Linux on which you can practice the commands, and
exercises, as you read or listen. I would also suggest that, as you try the commands, and exercises,
you make notes.
Running Linux on Your Computer
Ideally, you should install a version of Linux on your computer's hard drive. However, if due care is
not taken during installation, you may wipe out any operating systems that may already be on your
computer and more importantly, your data and other files. This risk is present whenever you install
an operating system. Before you start any installation, make copies of all your data and other files.
I have written this book especially for use with the Fedora Core 6 version of Linux. You can
download Fedora Core 6, legally, for free, from the Internet, but it will take several hours. It is a
good idea to find a friend, or neighbor, who studies, or works in, serious computing. Often, they
9

will have a DVD, or a set of CDs, that you may be able to borrow. Note that people who use Linux
are mostly friendly and helpful.
I would recommend that you install Fedora Core 6 on your computer. The installation is
straightforward just make sure you do not select the 'Delete all partitions.....' option during the
installation process, if you have Windows, or other operating system, installed on your computer.
Make copies of all your data and other files before you start the installation.
During the installation, write down the password, you entered for the root user, and keep it safely.
After installation, when the computer restarts, you will have to hit the Enter key to get a menu that
will let you select between Linux and Windows. Often, the menu option for Windows will show up
as Other, unless you changed the name to Windows, during the install process. Do not panic if
something does not look right ask someone who knows, or use the Internet. Two URLs you may
find very useful are linuxforums.org and linuxquestions.org.
Using Linux without Installing to the Hard Drive
If for some reason, you do not wish to install Linux on your hard drive, you can run Linux on your
computer, booting from either a CD, or a USB stick. You have two options. The first option is the
one I recommend. You can install Fedora Core 6 on a USB Flash Drive. A 4GB, that costs around
NZ$ 50.00 should be sufficient.
The second, less preferred option, is to use other versions of Linux. There are several versions of
Linux that run off a CD, or USB. Knoppix and Ubuntu are two popular ones.
You can also try DSL, acronym for Damn Small Linux, or Puppy Linux. Both will run off a CD, or
USB stick. DSL needs a mere fifty MB and there are versions that run inside Windows or Linux.
DSL is very powerful.
Some of the commands and exercises in this book may not work on Linux distributions other than
Fedora Core 6.

10

CHAPTER 2 BASIC COMMANDS


Booting into Linux
If you have installed Fedora Core 6, on a computer that also has Windows installed, pressing the
Enter key, a few seconds after you switch on your computer, will bring up a menu, that lets you
select between Windows, which may show up as Other, and Linux. If you have only Linux
installed, the system will automatically boot into Linux, and you need not press any keys.
Either way, the computer will boot into the the Graphical User Interface (GUI) mode. I would like
you to enter root for username and then enter the password that you used as root user password,
when you installed Linux. This is the password that I asked you to write down during the
installation process.
GUI, or Command Line Mode?
Unlike Windows, you can run Linux in two modes. In GUI mode, Linux is very much like
Windows. However, the command line mode is the one that I like to use while teaching Linux, and
you will soon see why. Just get over the initial reluctance to leave the GUI environment. I know, it
is hard to give up the mouse.
If you are using Fedora Core 6, pressing the Ctrl-Alt-F1 key will bring up another terminal in
command line mode. Log in, again, with username root and the password you entered when you
installed Fedora.
If you are using Knoppix, DSL, Puppy, Ubuntu, or other version of Linux, open a terminal under
the GUI.
Some Basic Commands
Now that you are in a terminal, let us try some essential, basic commands.
Enter ls at the prompt and see the output. The ls command lists the files and directories in the
current directory.
You want to know the name of the current directory? Try the pwd command.
Now try the following commands, in the given order:
date
dmesg
finger
history
who
11

whoami
top
Unlike the other commands, the top command does not get back to the prompt, but keeps on
running. How do you quit this command? Try hitting q on the keyboard. Remember, q for quit.
Now you know some commands, but are not sure exactly what they do. Try the man command.
The man command brings up manual pages on other commands. Manual pages, often called man
pages, are the documentation or help facility in Linux.
Try the following command:
man ls
man ls is the manual page for the ls command. After reading the first few lines of this manual page,
use the down arrow key on your keyboard to scroll down. Looks like there is a lot of help. Do not
worry - you need not memorize all of this man page, but need to remember only a small portion of
it. No-one can, and does, remember all the man pages for all the commands that Linux has.
How do you get out of the man page? Remember, q for quit.
Now that you know how to use the man pages, try the following:
man pwd
man date
man dmesg
man finger
man who
man whoami
man top
Time to revisit the ls command. Try the following commands:
ls -l
ls -a
ls -A
Now use man ls to find out how the -l, -a and -A modify the output of the ls command.
The -l, -a and -A are called options, or switches, and as you can see in the manual page, the ls
command has many options.
Commands can be used with more than one switch. Try the following:
ls -a -l
ls -l -a
ls -l -A
There is another way of using commands with more than one switch. Try the following:
ls -al
ls -la
ls -lat
12

ls -C
ls -lC
ls -Cl
In the last command, does the l take precedence over the C, or is it the other way around? Find out
by trying out the commands.
Preventing RSI / OOUS
One way of reducing the risk of Repetitive Stress Injury, sometimes called Occupational Overuse
Syndrome is to minimize the number of keystrokes we execute. Linux helps by providing several
means of repeating commands user earlier, without having to retype the commands. For example,
the up and down arrow keys can be used to scan through the recently used commands and use these
again without having to retype these, by simply hitting the enter key. Try hitting the up and down
arrow keys and repeating a command used earlier.
Exercises
What does the man command do?
What does the ls command do?
What are options or switches?
Use the manual pages to find out what the following commands do and then try using these:
cd
cp
ps
Shutting Down the Linux Machine
Use halt -p to shutdown your computer.
You may want to explore using the shutdown command instead. Read the man pages for shutdown
and see if you can successfully use the shutdown command to shut down your Linux system.

13

14

CHAPTER 3 DIRECTORIES
Start your computer and log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Directories
Directories can be thought of as folders that can contain other directories and files.
The Linux file system starts at the root, denoted by a forward slash (/). Do not confuse the root user
with the root directory. Enter the following:
cd /
The above command takes you to the root directory.
Now, if you enter ls you will see the directories under the root directory. One of these will be
named, root. This is the home directory of user root.
root, root and root
Let us pause for a moment and sort out the confusion caused by the use of the word, root to name
three different things. The root, or top, of the Linux file system is called the root directory and
represented by a forward slash. The superuser, or the administrator, of the Linux operating system
is called the root user. The home directory of the root user is /root a directory named root under
the root directory represented by a forward slash.
Changing Directories
Entering cd /home will take you to the /home directory. Try it. This is a directory named, home
under the root directory. Remember, the root directory is denoted by a forward slash. Files and
directories of individual users are usually stored under the /home directory. Use the pwd command
to find the name of your current working directory.
Now go back to the root directory by entering cd / and try the pwd command.
Making New Directories
Go to the /home directory by entering cd /home. Enter mkdir tom to create a new directory with
the name, tom. This directory will be under the directory named home which is under the root
directory represented by a forward slash.
The absolute path name of the directory you just created is /home/tom. Let us assume that you
want to change to this directory and your current working directory is /home. You can do this by
15

entering cd tom. Try this. In well maintained systems, this will be the home directory of a user with
username tom.
If you want to create two directories named sales and reports in tom's home directory, you can do
so by entering mkdir sales and mkdir reports, provided you are currently in the directory
/home/tom. Use cd /home/tom to first make /home/tom your current directory and then use mkdir
sales to create the sales directory.
Alternatively, you could have created the sales and reports directories under /home/tom
irrespective of your current working directory and without first moving to the /home/tom directory
by entering mkdir /home/tom/sales and mkdir /home/tom/reports.
Now move to the /bin directory by entering cd /bin and use the pwd command to confirm that /bin
is your current working directory. Create the reports directory under /home/tom by entering mkdir
/home/tom/reports.
Now make the /home/tom directory, your current directory by entering cd /home/tom and use ls to
confirm that you have the sales and reports directories here.
The absolute path names of the last two directories you created are /home/tom/sales and
/home/tom/reports.
Exercises
Create the directory /home/sue.
Create the directories /home/sue/sales and /home/sue/reports.
Make /sbin your current directory and without changing directories, create the directories
/home/sue/memos and /home/tom/email.
Use the man pages to find out more ways in which the cd command can be used. Some of the things
you should try are:
cd ..
cd ../..
cd ~
Note that the wave like character ~ is called a tilde.
After trying the cd command with various options, use the pwd command to compare the results
with what you expected.
When finished, shutdown your machine.
Have you figured out how to use the shutdown command?

16

CHAPTER 4 FILES
Start your Linux system, log in as root user, press Ctrl-Alt-F1 and log in again as root user. Create a
new directory called scarp under the /home/sue directory by entering mkdir /home/sue/scrap and
make this your working directory by entering cd /home/sue/scrap.
Creating Files
Let us create a simple text file by typing cat > file2 and hitting the Enter key. The computer now
waits for us to type in the contents of the file. Type a few lines with some words in it the lines you
type in need not make any sense we will be deleting this file soon. When you have typed at least
five lines, hit Ctrl-d. The lines you typed will be saved under a file named file2 in your current
working directory. Use the ls command to confirm this.
View the contents of this file using cat file2 - note that the greater than sign is not used when we
want to view the file.
Create another file named file3 using cat > file3, typing in some lines and entering Ctrl-d. View
the file using cat file3.
Copying Files
Use cp file2 file2bak to make a copy of the file named file2 and store the copy with the name
file2bak.
Not all Arguments are Switches
In cp file2 file2bak, the command used is cp and the names of the files, file2 and file2bak are
called arguments. They are not called switches as was the case with the -a and -l in ls -a -l. In
the case of commands like ls -a -t, the -a and the -l can be called, arguments, switches or options.
In other words, not all arguments are switches.
Deleting Files
Let us do some tidying up, deleting the files, file2, file2bak and file3. First let us check that they are
there using ls. Delete these one by one using commands of the form, rm filename.
Exercises
Make a directory called myscrap under /home/tom and make this your working directory.
17

Create a text file named text2 using cat.


Make a copy of text2 and store it as text2old.
Make a copy of text2old and save the copy as file2.txt.
What is the difference between cat > text2 and cat text2 ?
Find out the difference between cat > text2 and cat >> text2 by trying these out.
Tidy up your directory using the rm command.
When finished, shutdown your computer.

18

CHAPTER 5 MOUNTING USB AND DVD FILE SYSTEMS


Switch on you computer, log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Using USB and DVD
It is often necessary to make use of file systems on devices such as USB disks and DVDs. Although
some GUI desktop environments such as GNOME automatically mount these systems and provide
a Windows Explorer like File Manager, power users should know how to mount these in commandline mode. Linux has a default /mnt directory which is the recommended location for mounting
other file systems.
I suggest that you create the directories /mnt/usb1, /mnt/usb2, and /mnt/dvd.
To mount the first USB drive, insert the drive in any available USB slot, wait a few seconds, and
then enter,
mount /dev/sda1 /mnt/usb1.
If the file system mounts successfully, it becomes part of the / file system and you can access it
under /mnt/usb1. For example, cp /home/pat/fileAA /mnt/usb1/patfile will copy fileAA from the
/home/pat directory to the USB drive.
You should always unmount a file system before removing the physical media. Enter,
umount /mnt/usb1
to unmount the above. Note that the command is umount and not unmount.
To mount a second USB drive, use,
mount /dev/sdb1 /mnt/usb2,
and to mount a DVD, enter,
mount /dev/dvd /mnt/dvd.
Remember to unmount using umount.
Exercices
Practice mounting and unmounting USB and DVD disks.
Copy files to and from the USB disks.
Make directories on the USB drive.
Copy files form the DVD disk to the USB disk.
Copy files form the DVD disk to the hard disk.
19

20

CHAPTER 6 AN EDITOR CALLED vi


Switch on you computer, log in as root user, press Ctrl-Alt-F1 and log in again as root user and
make /home/sue/scrap your working directory.
Using vi
Enter vi file8.
This will open the file editor called vi. Hit the i on the keyboard. This will put you in the INSERT
mode of vi. You will see a INSERT at the bottom left of the display. Type in a few lines the
lines do not have to make any sense we will soon delete these. Do your best not to touch the
mouse. When you have typed in at least five lines, hit the Esc key on the keyboard. The INSERT
will disappear from the bottom of the screen. Enter a : (colon) not a ; (semicolon) - you need to
use the Shift key to get a : instead of a ;. If you have done this right, you will see the : at the bottom
left corner of the screen. Enter x to save the file and exit the editor.
Using vi you can get into some unexpected situations, if you hit the wrong keys vi is a very
powerful editor. If you are lost, or see some weird stuff on the screen, try hitting the Esc key, then
the : and then q! - note that the q followed by the exclamation mark, forces the vi editor to quit
without saving any work you may have done. Sometimes, you may find that forcing a quit without
saving is better than saving the extra stuff you may have generated accidentally.
You can also start vi without giving any file names at the start. Enter vi to start the editor. Then hit
the i key and type in a few lines. Then hit the Esc key followed by the : and then enter w file1. The
w file1 will save your file as file1 and you can continue to work on your editor.
To save the same file under another name, say file2, hit : and then enter w file2.
Remember, :w writes the file with the name already given, :w filename writes the contents to a file
named filename, :x writes the file and exits the editor, :q quits without complaining if the work has
already been saved, and :q! forces quit without saving.
As I said, earlier, vi is a powerful editor with many commands. For now, the little we have learned
so far will do.
Exercises
Create a number of text files using the vi editor. Keep your directories clean by deleting any
unwanted files.
Create a file named xfile5 using cat > xfile5. The file needs a few lines that may, or may not make
sense. Open the file using vi. Save the file as xfile5.bak. Hit the Esc key, then : and then enter set
21

number to bing up line numbering inside vi. You will find line numbers very useful later, when
you do scripting.
Replace a single character in a file with another, using vi, but without being in the INSERT-mode. Hint: Use r instead of i.
When finished, shutdown your computer.

22

CHAPTER 7 ADDING USERS AND SETTING PASSWORDS


Start your computer and log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Adding A User
Although, you have the option to run Linux in single user mode if and when necessary, Linux is a
multi-user system and Systems Administrators often have to add new users to the system. They use
the useradd command to add new users. Read the man pages for this command.
Add a user with username ganeshan to your system. You can do this by entering useradd
ganeshan. If this does not work, try /usr/sbin/useradd ganeshan.
Setting Passwords
When you are logged in as root user, you can use the passwd command to set, or reset users'
passwords. Entering passwd ganeshan will ask for a new password for user ganeshan. Enter
ganeshan for the new password and confirm it by typing it again when asked for.
The root user has Special Privileges
Use Ctrl-Alt-F2 to open a new terminal and log in with username ganeshan and password
ganeshan.
Try switching between the two terminals by using Ctrl-Alt-F1 and Ctrl-Alt-F2 alternately.
When you are in the second terminal, logged in as user ganeshan try adding a new user with
username bob by entering useradd bob. Does the system let you do this? Try to figure out why you
are able to add users and set their passwords when you are logged in as root user but not when you
are logged in as user ganeshan.
Exercises
Add a user with username henry.
As user ganeshan try the following commands in the given order:
shutdown
/sbin/shutdown
/sbin/shutdown now
Switch to terminal 1 using Ctrl-Alt-F1. Use whoami to confirm that you are logged in as user root
and try the three shutdown commands above, in the given order.
23

24

CHAPTER 8 SCRIPTS
Start your computer and log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Automating Tasks
Computing is about automating things and making life better for everyone. If we use the technology
and resources we have today in a sensible way, none of us need to work more than two days in a
week, there will be no global warming, waiting for surgery, wars or terrorism. But then, we are the
only intelligent species in the world, and perhaps, the universe. We would go towards an oncoming
tsunami and gather fish while all the other land-based species would seek higher ground. Let us not
digress, but go back to automation.
Imagine yourself to be a System Administrator, looking after the computer system of a small
business. A new employee joins the company and you have to add this employee as a user on your
Linux system. Are you able to do this? If not, go back and use the earlier chapters to ensure that
you can do this.
Let us say your company grown to become a medium-sized one. Every now and then it hires a few
staff, and you have to add them as users on your system. What would you do? Would you add them
one by one?
Let us write a script that will partially automate this task.
Create the directory /root/scripts using mkdir /root/scripts and make this your working directory
by entering cd /root/scripts.
Use vi myadduser.sh to edit a file called myadduser.sh using the vi editor.
Hit i to get to the --INSERT-- mode.
Type in the following:
cat userlist | while read xyz
do
useradd $xyz
done
Hit the Esc key, then the : key and then x.
Using vi create another file called userlist and in this file type in the following:
pat
bob
tom
tom2
sue
25

Sue
JohnDoe
Use ls to check that these two files are there in your current working directory.
Now enter chmod +x myadduser.sh and then ./myadduser.sh and study the output displayed on
the screen. The script named myadduser.sh has read the usernames given in the file named
userlist and tried to add these users on the system. If it encountered usernames that were already on
the system, it did not add these usernames but printed out a relevant message. The other users were
added to the system.
Check that this has been done by trying to log in under these usernames in other terminals. You can
open more terminals using Ctrl-Alt-F2, Ctrl-Alt-F3... up to Ctrl-Alt-F6 in Fedora Core 6.
Remember to set passwords for the newly added users, by using the passwd command with the
usernames as arguments. I suggest that you use their usernames as passwords too for now.
Every time some new users need to be added to the system, all you need to do is edit the userlist
file to contain just the new usernames to be added and run the script myadduser.sh.
Let us study the script in detail.
cat userlist | while read xyz
do
useradd $xyz
done
We know that cat userlist will display the contents of the file userlist to the monitor. In this script
we use a pipe represented by the | character on the keyboard to send the output of the cat
command to the next command. The while is used in conjunction with the do and done. The format
is as follows:
while something is true
do
do something
done
An equivalent in the real world would be:
while your are alive and able to
do
enjoy your life
do something good
smile
help someone
have fun
done
In the Linux script, the read reads in a value into the variable named xyz.

26

The $xyz is the value of the variable named xyz.


We know that the command useradd adds users which means that the line,
useradd $xyz
adds the user with username $xyz, where $xyz is the value of the variable xyz.
In summary, the cat command opens and outputs the file named userlist and the | sends this
output to the next command. The read command reads in the next word and stores it in a variable
named xyz. While the read is able to find a word to assign to the variable named xyz, the script
does the useradd on the value supplied by $xyz.
When read no longer finds a word coming from the output of cat, the script terminates.
The chmod +x myadduser.sh makes the script executable. The ./myadduser.sh executes the
script. We will discuss these at a later stage.
Do not worry if you do not understand all this in a few minutes. Most people need some time to
figure this out.
Exercises
Make sure you are able to create the script above and run it successfully with new lists of users
entered in the userlist file.
When finished, shutdown your computer.

27

28

CHAPTER 9 ADDING GROUPS AND ASSIGNING USERS TO GROUPS


Start your computer and log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Adding Groups
If you are the Systems Administrator in a large organisation, you will need to create different
groups and then assign users to these groups. Let us say that you want to create a group called
engineering. Knowing that we use a command called useradd to add users, can you guess the
name of the command that is used to add groups?
Let us create a group called engineering by entering groupadd engineering.
Assigning Users to Groups
There are two ways you can do this. If you are creating new users, then you can use the useradd
command with the -g option to specify the user's initial login group. Use man useradd and find
out more.
For example, useradd tim -g engineering will add a username tim and make engineering his
initial login group.
If the username already exists on the system, then you can use the usermod command to change the
user's initial login group. For example, usermod -g engineering tom will make engineering the
initial login group for username tom.
Exercises
Create a group called gamers.
Add a new user with username kim with initial login group engineering.
Make engineering the initial login group for user bob.
Use the man pages to find out more about the usermod command and then assign gamers as a
supplementary group to users whose usernames are bob, tom, tim and Sue.
Create a group called geeks.
Create a username geek007 whose initial login group will be geeks and supplementary groups
gamers and engineering.
Conserve power, shutdown your machine properly unless you are going to use it soon.
29

30

CHAPTER 10 IMPROVING YOUR SCRIPT


Start your computer and log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Stage 1
Let us say that you not only want to add several users, but also want to assign an initial login group
to each of them as well as keep their full names on file. You can use the script given below. Use vi
to modify your old myadduser.sh to look like this.
cat userlist | while read uname gname fname
do
useradd -g $gname -c $fname $uname
done
Use vi to edit your existing userlist file to look like the following:
hclark politics Helen Clark
jkey politics John Key
dbrash banking Don Brash
kis politics Kim Il Sung
Run the script by entering ./myadduser.sh.
Note how read assigns the first word in every line to the variable uname, the second word to the
variable gname, and all the remaining words on the line to the last variable, fname.
Can you figure out how this script works? Use the man page to check the options used with the
useradd.

Stage 2
Let us improve the script one more step. Say, we want to print out a message every time a user has
been added successfully.
Add a couple more user details in your userlist file, modify your myadduser.sh file to look like the
following and execute it.
cat userlist | while read uname gname fname
do
useradd -g $gname -c $fname $uname

31

if [ $? == 0 ]; then
echo $fname added with username $uname and login group $gname .
fi
done
The if - then - fi is what we have added in this stage. Let us look at this bit of the script in detail.
The [ ] is used to test if the value of $? is equal to 0. The $? returns the exit status of the last
executed command in this case, the useradd command. Any command that successfully executes,
sets the value of $? to 0. This means that every time the useradd command executes successfully, it
sets $? to 0 thus setting [ $? == 0 ] to true, which results in the echo command being executed.
To figure out some uses of the echo command try the following.
echo Hi, My name is
mname=Paul
echo Hi, My name is $mname .
Also, try echo 'Hi, My name is $mname .' Is there a difference in the output?
Stage 3
Let us now append the output of the echo command to a file named report5. We can do this by
editing the line in the myuseradd.sh file from,
echo $fname added with username $uname and login group $gname .
to
echo $fname added with username $uname and login group $gname . >> report5
The >> redirects the output of the echo command to a file named report5 appending the output to
any contents already in the file. If a single > is used instead of the >> then the file is overwritten
and any content that may have been there previously would be lost. Try both options and see how
they affect the contents of the report5 file after you run myuseradd.sh.
Stage 4
Let us generate another report that will provide us information on the usernames that were not
added a sort of an error report. Edit the if fi part of the myuseradd.sh to look like the
following.
if [ $? == 0 ]; then
echo $fname added with username $uname and login group $gname . >> report5
else
echo Error adding user with username $uname . >> errreport
fi
In this stage, I have introduced the else part of the if then else fi.

32

Stage 5
Let us now add the sha-bang line to the script. This line is the first line in a script and tells the
script which shell to use. The default shell in Linux is, more often than not, the bash shell and our
script is written for the bash shell. We will discuss shells in detail at a later stage. Following good
programming practice, you may want to add comments to the script. You can add as much as you
like of comments as long as the lines are preceded by a # (hash symbol). The sha-bang line is read
to determine the shell that needs to be used and after that the sha-bang line is also treated as a
comment.
#!/bin/bash
cat userlist | while read uname gname fname
do
useradd -g $gname -c $fname $uname
if [ $? == 0 ]; then
echo $fname added with username $uname and login group $gname . >> report5
else
echo Error adding user with username $uname . >> errreport
fi
done
Exercises
Make sure you can run the above scripts with a few different versions of userlist files. Add
comments to the script.
Write, and use, a script called mydelusers.sh to automatically delete users, and their home
directories, for users in your userlist files.

33

34

CHAPTER 11 A TASTE OF awk


Start your computer and log in as root user, press Ctrl-Alt-F1 and log in again as root user.
Converting Files and Delimiters
Let us say you have a file, named file1, that has records in the format shown below. The fields are
delimited, or separated by : (colons).
paul:it:Paul Smith:Programmer of the year
brian:sports:Brian Lara:Cricketer of the year
To read this file, and write the contents to a file, named file2 with the fields separated by ;
(semicolons), you can use,
awk -F: '{print $1 "," $2 "," $3 "," $4}' file1 > file2
Try this command, making up your own file1 and then look at your file2 and make sure it works.
In your file2 the fields will be delimited by commas.
Exercises
Write the command that you will use to extract only the first and third fields in the file1 you created
and write these to a file named, file3 with the fields separated by spaces.
Can you see how this command can be used in a script?
Conserve power, shutdown your machine properly unless you are going to use it soon.

35

36

CHAPTER 12 ARGUMENTS AGAIN


In the last few chapters, we had files like userlist and file1, the names of which were written into
the scripts. Instead of hard coding these filenames into the script, we can pass filenames as
command line arguments.
When we use commands like cp file1 file2, these values are stored in special, pre-defined system
variables. In the case of cp file1 file2, the cp is stored in $0, the file1 stored in $1, and the file2
stored in $3.
The script myuseradd.sh can now be modified to look like the following.
#!/bin/bash
cat $1 | while read uname gname fname
do
useradd -g $gname -c $fname

$uname

if [ $? == 0 ]; then
echo $fname added with username $uname and login group $gname . >> $2
else
echo Error adding user with username $uname . >> $3
fi
done
When this version of myuseradd.sh is used, we need to specify the files that are to be used. A
sample execution may look like,
./myuseradd.sh userlist45 successfuladds45 errorreport45.
Exercises
Try using command line arguments with your myadduser.sh script and any other script that you
may have developed.
Modify your mysadduser.sh script to skip any blank lines in the userlist file.
Modify your mysadduser.sh script to do some simple validation on the data in the userlist file. For
example, if a line has only three fields, then do not add the user.
Modify your mysadduser.sh script to ensure that only root user can execute it. Hint: The root user
has a user ID of 0. This is given by the environment variable $UID. Try echo $UID.

37

Using more /etc/passwd study the last few lines in the /etc/passwd file. This is where the
usernames, groups, etc. of the users you added to the system are held. What are the fields in this
file? Use the Internet, or other resources to find out what these fields represent.
Modify your mysadduser.sh script to check if each of the usernames you are trying to add is
already in the system and if so, report this, by writing this information to a file.

38

CHAPTER 13 VARIABLES
User-Defined Variables
By entering, x=10 you declare a variable named x and assign the value 10 to it. To confirm that this
has been done, try echo $x which should output the value 10. Try x = 10 instead of x=10 and see
what happens.
Go ahead and try, x=abcd and then echo $x. What has happened to the value 10 that was assigned
to the variable x earlier?
Then try x=abcd pqr and then echo $x. What happened to the pqr?
Now, try x=abcd pqr and then echo $x. Think of wanting to save a first name and a second
name. Try using the single quotes instead of the double quotes and then the back quotes instead of
the single quotes. Are there any differences in the output?
The names of variables can be one, or more letters such as x, y, t, firstname and fname but not
variable names used by the system.
System Variables
These are pre-defined for use by the system. For example, echo $USER will display the username
of the logged in user, echo $UID will display the user ID of the logged in user, and echo $PWD
will print your working directory. Try assigning values to these variables using UID=200 etc.
Remember, Linux is case-sensitive uid is not the same as UID.
Exercises
Enter fullname = Ian Williams and see what happens? What do you need to make this work as
what was possibly intended?
Use wikis to find some more system variables and what they represent.
What does the variables $$ and $PPID represent? Is there a $PID?

39

40

CHAPTER 14 Package Management - RPMS


RPMS
Fedora uses Red Hat Package Management. Many packages are provided on your DVD and many
more available on the Internet. You can add packages to your system.
Installing RPMs
There are many ways of installing RPMs. I want you to use the rpm command, for now, as this
helps understand some of the concepts.
To install an RPM, use rpm -i packagename.
For example, rpm -i /mnt/dvd/Fedora/RPMS/dhcp-3.0.4-21.fc6.rpm. In this example, it is
assumed that you have a DVD with the package dhcp-3.0.4-21.fc6.rpm in the directory
Fedora/RPMS on the DVD and that you have mounted the DVD, using mount /dev/dvd
/mnt/dvd after making the directory, /mnt/dvd, using mkdir /mnt/dvd.
Exercises
Use man rpm to find out more about the rpm command, especially the -q and -e options.
Practice installing, querying and erasing some RPMs.
Try rpm -q bind.
Install bind if it is not already installed.

41

42

CHAPTER 15 NETWORKING 1: SSH


From this point onwards, ideally, you need two or more computers. You may be able to
simulate two machines by using two NICs but I am no expert on this and there are no
practical uses for this.
Indeed, we are assuming that all the computers are already connected physically in a network.
SSH
SSH, or Secure Shell is a good way of accessing a remote machine. I use SSH to run my robots
from anywhere in the world.
On one of your computers add a user with username tom and password tompass. Set the IP address
of this machine to 10.0.0.8 using ifconfig eth0 10.0.0.8 and start SSH on the machine using service
sshd start. Set the IP address of another machine on the same network to 10.0.0.9 and type in ssh
tom@10.0.0.8 to use SSH to access the first machine. Answer yes to the question about.... and then
login as tom with password tompass. Now you are using your machine almost like a dumb terminal
to access your account on the remote machine.
If you want to access a Linux machine from a Windows machine (from anywhere in the world,
provided both machines are connected to the Internet), you can use a SSH client named putty.exe, a
tiny programme that can be downloaded free from the internet.
Exercises
Ensure that you can use SSH to access a remote machine.
Ensure that you can use putty.exe from a Windows machine to access a remote Linux SSH server.

43

44

CHAPTER 16 NETWORKING 2: DHCP


Dynamic Host Control Protocol Server and Clients
As a System Administrator, you now want to set up a DHCP server that will lease IP addresses to
some, or all, the computers on a small Local Area Network. You also want to set up the DHCP
clients, some of which will get their IP addresses from the server while others will have static IP
addresses.
Server
Check if the dhcp package is installed by entering, rpm -q dhcp. If it is, you will get the version
of dhcp rpm installed on the system. If not, you will get a message stating that dhcp is not installed
and you will have to install the dhcp rpm.
To install the rpm, you will need to download the rpm from the Internet, or use your installation
DVD. To install it form the DVD, mount the DVD by inserting the disk into the drive and entering,
mount /dev/dvd /mnt/dvd. I am assuming here that you have already created the /mnt/dvd
directory.
Use ls -l /mnt/dvd to list the contents of the DVD. Use ls -l /mnt/dvd/Fedora/RPMS to list the
comments of that directory. You will see a lot of rpms listed. Use ls -l /mnt/dvd/Fedora/RPMS |
grep dhcp to list the dhcp related packages. One of these will be dhcp-x.x.x-xx.fc6.rpm, or
something similar.
Use rpm i /mnt/dvd/Fedora/RPMS/dhcp-x.x.x-xx.fc6.rpm to install the package. The dhcp
version I last installed was dhcp-3.0.4-21.fc6.rpm.
After ensuring that the dhcp package is installed, you need to copy the sample dhcp configuration
file provided to /etc/dhcpd.conf. It is good practice to rename any existing /etc/dhcpd.conf file to
something like /etc/dhcpd.conf.old, using mv /etc/dhcpd.conf /etc/dhcpd.conf.old before
copying the sample file.
The
sample
file
may
be
found
in
some
directory
/usr/share/doc/dhcp-3.0.4/dhcpd.conf.sample and you would use something like,
cp /usr/share/doc/dhcp-3.0.4/dhcpd.conf.sample /etc/dhcpd.conf.

such

as

View the contents of the /etc/dhcpd.conf file and check the IP address given after the word
subnet. Set this to 192.168.0.0.
Use ifconfig eth0 192.168.0.3 to assign the IP address to your network interface. If there are two
or network interfaces on your computer, you may need to use eth1 instead of eth0.

45

You may also need to ensure that your /etc/sysconfig/dhcpd contains the line DHCPDARGS=eth0
if you are using eth0. What should this line be if you are using eth1?
Start the dhcpd daemon using, service dhcpd start. If things have gone to plan, you would get an
OK and the other computers on the network can lease IP addresses from your server.
On the dhcp client computers, you use the ifdown eth0, ifup eth0, and ifconfig eth0 commands to
respectively, disable, enable and then find the leased IP address of, the network interface.
The range of addresses that your server is able to lease is the range listed in the /etc/dhcpd.conf file
on the line starting with the word, range. You can edit this line to vary the range.
Troubleshooting Server Problems
If, when you entered service dhcpd start, you got a FAILED instead of an OK, use,
tail -35 /var/log/messages | more to view the last 35 lines of the /var/log/messages file, page by
page, and use the information therein to try and troubleshoot.
Troubleshooting Client Problems
The default installation of Fedora Core 6 should have set up the necessary files for the dhcp client
machines and usually you will need to do nothing to get these computers to get their addresses from
the dhcp server.
However, if after entering ifdown eth0 and ifup eth0, the ifconfig eth0 does not show the IP
address of the client as one of the IP addresses in the range given in the /etc/dhcpd.conf file on the
dhcp server,
1. ensure that the /etc/sysconfig/network file exists and contains the line,
NETWORKING=yes
2. ensure that the /etc/sysconfig/network-scripts/ifcfg-eth0 file exists and contains the three
lines,
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
3. disable and then enable the network interface, using ifdown eth0 and ifup eth0, in that
order.
Note that if you have more than one network interface, you may need to use eth1 instead of eth0 in
the commands above as well as the filename under bullet-point number 2.
Exercises
Try and figure out what each of the lines in the /etc/dhcpd.conf, /etc/sysconfig/dhcpd,
/etc/sysconfig/network, and /etc/sysconfig/network-scripts/ifcfg-eth0 files do.

46

Try modifying these files. Explore how you can change,


1. the range of IP addresses the dhcp server can lease,
2. the subnet,
3. the lease times, and,
4. the network interfaces used on the server and the client.

47

48

CHAPTER 17 NETWORKING 3: DNS 1


DNS
In forward lookup, DNS resolves human-friendly URLs to computer-friendly IP addresses. If you
connect to the Internet from your home via an Internet Service Provider, your computer sends the
URLs you wish to visit, to a DNS server at your ISP. If the DNS server at your ISP is unable to
resolve the URL to the corresponding IP address, it forwards the request to another DNS server that
may hold the necessary information.
DNS in a LAN
As the Systems Administrator, you have already set up a DHCP server and some clients. You now
want to name the computers in your LAN with names such as tui, kiwi, pukeko, weka and moa.
You also want to name your domain euni.ac.nz and set up a DNS server that will enable anyone on
the LAN to access other computers using their names.
DNS Server
You can set up one of your computers as a DNS server. This can be the same machine which serves
as the dhcp server, or another machine. First check if the bind package is installed using rpm -q
bind. If it is not, install it from the DVD. Remember how you installed the dhcp package?
To start DNS running on the server, with minimum functionality, you need two files. These are
/etc/named.conf and /var/named/euni.ac.nz. If you are using the chrooted (We will worry about
what chroot means at a later stage) version of BIND, you will have to use
/var/named/chroot/etc/named.conf and /var/named/chroot/var/named/euni.ac.nz instead. If you
are using Fedora, you are using the chrooted version. You can check if you are using the chrooted
version, by checking if the directory /var/named/chroot exists.
Backup any existing /etc/named.conf file as /etc/named.conf.old and use the following as a
minimal version of the /etc/named.conf (/var/named/chroot/etc/named.conf if using the
chrooted bind) file.
options {
directory "/var/named/";
};
zone "localhost" {

49

type master;
file "localhost.zone";
};
zone "euni.ac.nz" {
type master;
file "euni.ac.nz.zone";
};
Create the file /var/named/euni.ac.nz.zone (/var/named/chroot/var/named/euni.ac.nz.zone if
using the chrooted bind) with the following as the minimal contents.
$TTL 86400
@

IN

SOA @ root (
2003041801 ; serial
28800 ; refresh
7200 ; retry
604800 ; expire
86400 ; ttl
)

IN

NS

kiwi.euni.ac.nz.

kiwi

IN

192.168.0.3

tui

IN

192.168.0.5

weka

IN

192.168.0.6

moa

IN

192.168.0.8

When typing this file ensure that the full stop at the ends of kiwi.euni.ac.nz. is not left out.
Note that your name server is called kiwi and that your domain name is euni.ac.nz. According to
this zone file, the computer named kiwi has IP address, 192.168.0.3 and two other machines named
tui, and weka have static IP addresses 192.168.0.5 and 192.168.0.6 respectively.

50

Enter
hostname to display the name of your computer. You will probably see
localhost.localdomain. Now, enter hostname kiwi to change the host name of your computer and
then use, ifconfig eth0 192.168.0.3 to set your computer's IP address.
Enter service named start and if things are working fine and there are no typing or other errors in
the files and their names and locations, you should see an OK and not a FAILED.
Enter ping 192.168.0.3 and this should work. However, if you enter ping kiwi or even ping
kiwi.euni.ac.nz these would not work. We have one more file to modify, or create, before these
would work. If a file named /etc/resolv.conf does not exist, create it. If it exists and there are some
lines in it, leave those lines alone for now or delete them. In the /etc/resolv.conf file add the
following lines as the first two lines.
search euni.ac.nz
nameserver 192.168.0.3
Use service named restart and then try ping kiwi and ping kiwi.euni.ac.nz.
On another computer on the same LAN, log in as root user, and enter hostname tui to set its host
name and enter ifconfig eth0 192.168.0.5 to set its IP address. Also, on this computer create or
modify a file named /etc/resolv.conf with the following lines as its first two lines.
search euni.ac.nz
nameserver 192.168.0.3
On a third computer on the same LAN, log in as root user, and enter hostname weka to set its host
name and enter ifconfig eth0 192.168.0.6 to set its IP address. Also, on this computer create or
modify a file named /etc/resolv.conf with the following lines as its first two lines.
search euni.ac.nz
nameserver 192.168.0.3
Now you should be able to ping any of the three computers from the others by using their humanfriendly names as well as their IP addresses.
In case things do not work as expected, try the following.
Edit the /etc/hosts file to look like the following.
# Do not remove the following line, or various programs
# that require network functionality will fail.
::1
localhost.localdomainlocalhost
127.0.0.1
kiwi.euni.ac.nz
kiwi
192.168.0.2 kiwi.euni.ac.nz
kiwi
Ensure that the /etc/nsswitch.conf file contains the following line.
hosts: files dns
If the DNS still does not work properly, use the messages in /var/log/messages to find out why.
As I mentioned earlier, the files I have given are minimal and need to be modified, adding lot more
lines. Also, other files will be needed and have to be created before your DNS server can be
considered a complete DNS server. The reason I have cut down the number of files and the contents
51

of the files to bare-bones is so it may help you understand the basics easily and build up confidence
by first setting up a minimal, but working system. Hope you found this method useful.
Exercises
What do SOA, A, IN, @, NS and TTL represent?
Try to figure out how the files work together.
How do we handle IP addresses that change? The DHCP server does allocate different addresses at
different times to any given machine that gets its IP address using DHCP.
How do we extend the DNS to include computers outside the LAN?

52

CHAPTER 18 NETWORKING 4: DNS 2


Extending our DNS
As mentioned earlier, I cut down the files needed to run, to the bare minimum, so you can have a
basic system, up and running, without facing too many problems. I give below the full details of all
the files needed and the complete versions of the files. Use these, taking care with your typing,
especially, the dots at the ends of things like euni.ac.nz. even when these appear in the middle of
lines.
etc/named.conf (or /etc/var/named/chroot/etc/named.conf)
## named.conf - configuration for bind
#
# Generated automatically by redhat-config-bind, alchemist et al.
# Any changes not supported by redhat-config-bind should be put
# in /etc/named.custom
#
controls {
inet 127.0.0.1 allow { localhost; } keys { rndckey; };
};
#include "/etc/named.custom";
include "/etc/rndc.key";
options {
directory "/var/named/";
};
zone "0.0.127.in-addr.arpa" {
type master;
file "0.0.127.in-addr.arpa.zone";
};
zone "0.168.192.in-addr.arpa" {
type master;
file "0.168.192.in-addr.arpa.zone";
};
zone "localhost" {
type master;
file "localhost.zone";
};
zone "euni.ac.nz" {
type master;
file "euni.ac.nz.zone";
};

53

/var/named/euni.ac.nz.zone (or /var/named/chroot/var/named/euni.ac.nz.zone)


$TTL 86400
@
IN

SOA

kiwi.euni.ac.nz. root.localhost (
200309181 ; serial
28800 ; refresh
7200 ; retry
604800 ; expire
86400 ; ttl
)

IN

NS

192.168.0.3

kiwi
tui
weka

IN
IN
IN

A
A
A

192.168.0.3
192.168.0.5
192.168.0.6

/var/named/localhost.zone

zone file for localhost zone

$TTL 86400
@
IN

SOA

kiwi.euni.ac.nz. root.localhost (
200309181 ; serial
28800 ; refresh
7200 ; retry
604800 ; expire
86400 ; ttl
)

IN

NS

localhost.

IN

127.0.0.1

/var/named/0.168.192.in-addr.arpa.zone zone file for reverse name resolving


$TTL 86400
@
IN

SOA

kiwi.euni.ac.nz. root.localhost (
1 ; serial
28800 ; refresh
7200 ; retry
604800 ; expire
86400 ; ttl
)

IN

NS

kiwi.euni.ac.nz.

3
5
6

IN
IN
IN

PTR
PTR
PTR

kiwi.euni.ac.nz.
tui.euni.ac.nz.
weka.euni.ac.nz.

/var/named/0.0.127.in-addr.arpa.zone
$TTL 86400
@
IN

SOA

zone file for reverse name resolving

kiwi.euni.ac.nz. root.localhost (
1 ; serial
28800 ; refresh

54

7200 ; retry
604800 ; expire
86400 ; ttl
)
@

IN

NS

localhost.

IN

PTR

localhost.

Exercises
Ensure that you are able to run DNS and resolve addresses using the files listed in this chapter.
Try using another computer as the name server.
If you are using the default /etc/dhcpd.conf file on the DHCPD server, the name server address may
be set as 192.168.0.0. Notice that I have used kiwi as your name server and 192.168.0.3 as its
address. You will need to edit the /etc/dhcpd.conf file at some point and change the name server IP
address.

55

56

CHAPTER 19 NETWORKING 5: NFS


NFS
Using NFS, you can share files with other users. Create the following directories.
/nfsshare
/nfsshare/share0
/nfsshare/share1
Use chmod 777 on these directories to grant all permissions to all users.
Create or modify /etc/exports file to include the following lines.
/nfsshare/share0
/nosmnfs/share1

192.168.0.5(ro,all_squash)
192.168.0.6(rw,all_squash)

Use ifconfig eth0 192.168.0.3 to set your computer's IP address. Similarly, assign 192.168.0.5
and 192.168.0.6 to two other machines on your LAN.
Execute exportfs -r on your computer. This will make /nfsshare/share0 available, read-only, to
the computer with IP address 192.168.0.5 and /nfsshare/share1 available, read and write, to the
computer with IP address 192.168.0.6.
On the computer whose IP address is 192.168.0.5 run showmount -e 192.168.0.3 and see if the
files you exported from 192.168.0.3 are shown. If things do not work as expected, disable your
Firewall and SELinux. Use man to find more details of the showmount command.
Create a directory called /nfs1 on the computer with IP address 192.168.0.5 and then use mount
192.168.0.3:/nfsshare/share0 /nfs1. Now the directory /nfsshare/share0 should be available to
you under your /nfs directory as long as the network is connected and the system administrator on
the other machine leaves the settings alone.
If DNS is running, you can also use the hostnames of the computers instead of the IP addresses .
Exercises
Ensure that you are able to mount a shared directory on a remote host on your machine and do some
reading, editing and copying of files on the remote machine.
Run DNS on the remote host and mount files using hostnames instead of IP addresses. This is
important as we move on to Dynamic DNS in the next chapter.

57

58

CHAPTER 20 NETWORKING 6: DYNAMIC DNS


Dynamic DNS
In many real world applications, most client computers on a network are not assigned static IP
addresses. IP addresses are leased by the DHCP server. This means that the DNS needs to be
dynamic so it can cope with changing IP addresses. Let us set up a DDNS for euni.co.nz. We need
the following files on our host that will provide both DHCP and DNS.
/etc/dhcpd.conf
include "/etc/rndc.key";
ddns-update-style interim;
ddns-updates on;
ignore client-updates;
zone euni.ac.nz. {
primary 127.0.0.1;
key rndckey;
}
subnet 192.168.0.0 netmask 255.255.255.0 {
option subnet-mask

255.255.255.0;

option domain-name

"euni.ac.nz";

option domain-name-servers 192.168.0.2;


option time-offset

-18000;

# Eastern Standard Time

# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#
option netbios-node-type 2;
range dynamic-bootp 192.168.0.26 192.168.0.96;
default-lease-time 2000;
max-lease-time 2000;
# we want the nameserver to appear at a fixed address
host ns {
59

next-server kiwi.euni.ac.nz;
hardware ethernet 00:0D:60:76:D6:C6;
fixed-address 192.168.0.2;
}
zone localdomain. {
primary 192.168.0.2;
key rndckey;
}
}
/var/named/chroot/etc/named.conf
controls {
inet 127.0.0.1 allow { localhost; } keys { "rndckey"; };
};
include "/etc/rndc.key";
options {
directory "/var/named/";
};
zone "localhost" {
type master;
file "localhost.zone";
allow-update { key "rndckey"; } ;
notify yes;
};
zone "euni.ac.nz {
type master;
file "euni.ac.nz.zone";
allow-update {
key "rndckey"; } ;
notify yes;
};
60

/var/named/chroot/var/named/euni.ac.nz.zone
$TTL 86400 ; 1 day
euni.ac.nz

SOA

euni.ac.nz.

root.euni.ac.nz. {

2003042254 ; serial
28800
; refresh (8 hours)
7200
; retry (2 hours)
604800 ; expire (1 week)
86400
; minimum (1 day)
)

kiwi

NS

kiwi.euni.ac.nz.

192.168.0.2

Restart dhcp (service dhcpd restart) and dns (service named restart) and you should now be able to
ping any machine on your network using hostnames. Note that the machines on your network do
not have static IP addresses. In case you have set static IP addresses for your clients using ifconfig
eth0 192.168.0.5 or something similar you should run ifdown eth0 and then ifup eth0 on each of
them so they lease their IP addresses from the DHCP server.
If things do not work as expected, you may want to check /var/log/messages to see what is going
on and try the following.
Change ownership of all files in /var/named/chroot/var/named and /var/named/chroot/etc/ to
named using chown named:named /var/named/chroot/var/named/* and chown named:named /
var/named/chroot/etc/* respectively.
Add the line ENABLE_ZONE_WRITE=yes as the last line of the /etc/sysconfig/named file.
Exercises
Ensure that you are able to run DHCP and DNS on the same server and ping all machines on your
network from any other machine using the hostname only.
Ensure that you are able to use NFS using hostnames only.
Ensure that you are able to use SSH using hostnames only.
Try running DHCP and DNS on different hosts. Hint: Edit and relocate the above files. You will
also have to copy a file named rndc.key (usually in /etc/ or /etc/bind/ or /var/named/chroot/etc/ )
from one of the machines to the other.

61

62

CHAPTER 21 NETWORKING 7: FTP


FTP
Let us set up an FTP server. Set the IP address of your computer to 192.168.0.3. Ensure that FTP is
installed on it. Start the FTP service. In Fedora Core 6, this is done using service vsftpd start.
Set the IP address of another machine on the same network to 192.168.0.5 and enter ftp
192.168.0.3. Enter anonymous when asked for a username and hit enter when asked for a
password. You are now connected to the other machine and you would see an ftp> prompt. If you
now type pwd you will see what directory you are in on the FTP server. Very likely this is the /var/
ftp/pub directory.
If there are any files in the /var/ftp/pub directory you can download these to your machine. Create
a file named file4 on the FTP server in the /var/ftp/pub directory. On the client, type in get
pub/file4 /home/file4copy. This will make a download a copy of file4 and save it as file4copy in
the /home directory on the client machine.
If things do not work as expected, check the /etc/vsftpd/vsftpd.conf on the FTP server and ensure
that the following line anonymous_enable=YES is in that file.
You may also use FTP to access your own account on a server, similar to how you used SSH as
described in an earlier chapter. SSH is more secure than ftp. Create a user with username tom on
the server. Set the password for this user to password. Enter ftp 192.168.0.3 on the client and log in
using username tom and password password. You will now be in the /home/tom directory. You
can download files from, and upload files to the server using commands such as get somefile
/mydir/copyofsomefile and send /mydir/myfile uploadedfile.
If you run into problems, or want to explore FTP further have a look at and if necessary edit the
/etc/vsftpd/vsftpd.conf file. Remember the /var/log/messages is a good place to look when trying
to troubleshoot.

63

64

CHAPTER 22 NETWORKING 8: APACHE WEB SERVER


Apache
The most used web server in the world today, and for the past x years is Apache. It runs on both
major platforms, Linux and Windows. What is more it is free Open Source software. Let us now
see how easy it is to set up a simple website using Apache on a Linux machine.
Fedora Core 6 has Apache bundled with it. All you need to do to start with Apache is to set your
machine's IP address to something like 10.1.1.1 and create a file /var/www/html/index.html wih a
few lines in it and then run service httpd start. When you start the Firefox browser which also
comes bundled with Fedora Core 6, and enter http://10.1.1.1 you will see index page you created.
If you know some HTML code or can find a few minutes to learn basic HTML you can start
building your website. Indeed, you can go on to build one or more secure websites with all the bells
and whistles. A popular combination used in industry is called, LAMP, an acronym for Linux,
Apache, MySQL and PHP.
For those who have not had any exposure to HTML, I give below some sample files you can copy
into your /var/www/html directory and have some fun. In addition to these files, you could copy
any *.jpg image as /var/www/html/robot.jpg. Have fun.
/var/www/html/index.html
<html>
<head>
<title>"Sample Page by Ganeshan"</title>
</head>
<body>
<p>"Sample Page by Ganeshan 2007"</p>
<p><table><td valign=middle><img width="120" height="120" border="0" src="robot.jpg"
align="left" alt="Picture missing" /></td></table></p>
<p><a href="helppage.html">"Help Page"</a></p>
<p><img width="100" height="100" border="0" src="robot.jpg" align="right" alt="" /></p>
<p><a href="nextpage.html">"Next Page"</a><p>
</body>
</html>

65

/var/www/html/helppage.html
<html>
<head>
<title>"Sample Page by Ganeshan"</title>
</head>
<body>
<p>"Build your own"</p>
<p><img width="100" height="100" border="5" src="robot.jpg" align="left" alt="" /></p>
</body>
</html>
/var/www/html/nextpage.html
<html>
<head>
<title>"Sample Page by Ganeshan"</title>
</head>
<body>
<p><img width="300" height="300" border="20" src="robot.jpg" align="left" alt="" /></p>
</body>
</html>
Exercises
Ensure that you can start the http daemon and get your simple web page displayed.
Try and include some pictures and music on your website.
Try to access your website from another machine, using IP address and then hostname.

66

Linux
Kathiravelu Ganeshan
Senior Academic Staff Member
School of Computing and Information Technology
Unitec New Zealand

ISBN 978-0-473-12925-5

You might also like