Professional Documents
Culture Documents
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
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.
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
19
6. An Editor Called vi
21
23
8. Scripts
25
29
31
35
37
13. Variables
39
41
43
45
49
53
57
59
63
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
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
18
20
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
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
27
28
30
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
35
36
$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
41
42
43
44
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
47
48
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
53
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
$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
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
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
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
255.255.255.0;
option domain-name
"euni.ac.nz";
-18000;
# --- 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
63
64
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