Linux Basic Administration
USER ACCOUNTS
In a multi-user Operating Systems, such as Unix and Windows NT,
more than one users can use the system at the same time. In a
multi-user environment the most important thing is the privacy of a
user’s data from the other users. To implement this privacy, the
concept of user accounts is used, that is, each user has his own
account, which is protected by a password. A home directory is
associated to every account, where a user can keep his data and
protect it from the other users. Also system administrators can
grant usage of system resources to different users by granting or
un-granting access to the resources to their accounts.
In Linux (generally, in all the Unix-based operating systems), a
users account has the following main components:
• Entry for the user in /etc/passwd file and /etc/shadow file.
/etc/passwd file is like a database of the users, it has an entry
for each user account. Each entry has seven fields separated
by a colon (:) as shown below:
field1:field2:field3:field4:field5:field6
:field7
Field 1 is the user name of the user. Field 2 is the encrypted
passwd, but it is usually replaced by the alphabet x and the
actual password is saved in /etc/shadow file. Field 3 is the
user’s UID (user ID). UID is an integer and each user has a
unique UID. Field 4 is the GID (Group ID). In Unix users
can be divided into groups each group is assigned a unique
integer ID called GID. The information of all the groups is
kept in /etc/group file. Field 5 is the information of the user,
such as, the name, telephone number, etc. Field 6 is the path
of the home directory of the user. Field 7 is default shell of
the user. Shell is like the command prompt in Unix. You can
run various commands by typing them on the shell. There are
many different types of shells are available, such as C shell,
Bourne shell, Korn shell, etc. The binaries for all the shells
are inside the /bin directory. The password for each user is
kept inside the /etc/shadow file. /etc/shadow file has an entry
for each user. Each entry has two important fields, i.e., user
name and encrypted password.
• Home directory of the user. When a user account is created
his home directory is created and path of the home directory
is put in the field 6 of the entry of that user in the /etc/passwd
file. The user is given the ownership of the home directory.
• Environment for the user is set up in the special files which
are use to keep the user environment. For C shell, these
special files are .cshrc and .login. These files are kept inside
the user’s home directory. When the user logs in, the
operating system runs the commands specified in the users
.cshrc and .login files to set up the environment for the user.
We will discuss about the user environment (and .cshrc and
.login files) in detail in subsequent labs.
MANUALLY CREATING A USER ACCOUNT
Enter the entry for the new user in the /etc/passwd file. For
example, if we want to create a user named testusr, then we can
enter the following entry in /etc/passwd:
Testusr:x:1001:30:Test
User:/home/testusr:/bin/csh
Enter the entry for the new user in /etc/shadow. Keep the passwd
field empty. You can set up the password, when you log in as the
new user after creating his account. Following will be the entry in
/etc/shadow:
Root::11587:0:99999:7:::
Create a directory for the user in the /home directory as follows:
% cd /home
% mkdir testusr
Give new user the ownership of his home directory that was
created in the last step:
% chown testusr /home/testusr
Copy the .cshrc and .login files from an existing user’s home
directory to the new user’s home directory. For example, if we
have an existing user guest, then we can do it as follows:
% cp ~guest/.cshrc ~testusr/.cshrc
% cp ~guest/.login ~testusr/.login
CREATING A USER ACCOUNT USING ADDUSER OR
USERADD COMMAND
See the details of adduser or useradd command from the manual
pages as follows:
% man adduser
or
% man useradd
% useradd –u 1000 –g 1000 –d /home/wasiq –s
/bin/bash –p test123 wasiq
Understanding Unix File System
Unix File System is organized in the form of a tree. The root of the
file system tree is always “/” (slash) as shown in Figure 1. Under
“/” are the top level directories such as dev, etc, bin, usr, tmp,
home, etc. Under these top level directories there are other files
and directories. In this way the tree moves downwards.
etc usr bin home dev tmp
user1 user2
dir1 dir2
file1 file2 file3 file4
Figure 1: Unix file system,
Referring to a file or directory
A file or a directory can be referenced using its full path (starting
from “/”) or relative path. For example, the full path for the file
named file1 in Figure 1 is /home/user1/dir1/file1.
The relative path is traversed (or calculated) from the current
directory (i.e., the directory in which you are currently working).
For example, if you are currently under the directory user1, the
relative path to file1 will be “dir1/file1”.
There is a special symbol “..” , which refers to the parent directory.
For example, if you are inside the directory dir1, then you can go
to the directory user1 (which is the parent directory of dir1) by
typing “cd ..”.
As a more complicated example, suppose that you are in dir2 and
you want to refer to the file1, which is inside the dir1, then you can
reference file1, using the relative path, as “../dir1/file1”.
Filesystems and Mount Points
Every hard disk is divided into several partitions, and each of these
contains a filesystem. While Windows assigns a letter to each of
these filesystems (actually, only to those it recognizes), Linux has
a unique tree structure of files, and each filesystem is mounted at
one location in the tree structure.
Just as Windows needs a "C: drive", Linux must be able to
mount the root of its file tree (/) somewhere, in fact on a partition
which contains the root filesystem. Once the root is mounted, you
can mount other filesystems in the tree structure, at different
mount points in the tree structure. Any directory below the root
structure can act as a mount point. Note that you can also mount
the same filesystem several times.
For example, in Figure 1 (a) the directory /var/mountpoint is empty
and nothing is mounted on it. Suppose, we mount a filesystem
shown in Figure 1 (b) on /var/mountpoint, then the file system of
Figure 1 (a) will change to the filesystem of Figure 1(c).
There are two things you need to know about mount points:
1. the directory which acts as a mount point must exist;
2. and this directory should preferably be empty: if a directory
chosen as a mount point already contains files and
subdirectories, these will simply be "hidden" by the newly
mounted filesystem, but they will not be accessible anymore
until you free the mount point.
To mount a filesystem on a particular mount point, the mount
command is used. For example, to mount the partition (or
filesystem) /dev/hda1 on the directory (or mount point) /mnt,
following command can be used:
% mount -t ext2 /dev/hdb1 /mnt
The -t option is used to specify what type of file system the
partition is supposed to host. Among the filesystems you will
encounter most frequently are ext2, ext3 (Linux file system),
VFAT (for all DOS/Windows partitions: FAT 12, 16 or 32) and
iso9660 (CD-ROM filesystem). If you do not specify any type,
mount will try and guess which filesystem is hosted by the
partition by reading the superblock. It rarely fails at doing so.
To unmount a filesystem, the umount command is used. For
example, to unmount the filesystem /dev/hda1 from the mount
point /mnt, following command can be used:
% umount /dev/hda1
or
% umount /mnt
/
etc usr bin home dev var
Figure 1 (a) mountpoint
dir1 dir2
file1
Figure 1 (b)
/
etc usr bin home dev var
mountpoint
dir1 dir2
file1
Figure 1 (c)
Mounting CDROMS and Floppies
In Linux, like in UNIX, diskettes and CD-ROMs need to be
mounted before they can be used. What mount does is to associate
the devices (floppy or CD-ROM drives) to a mount point (a
directory) in the root file system. The usual mount point for
diskettes is /mnt/floppy , while the usual mount point for CD-
ROMs is /mnt/cdrom. This is not to say that we need to use these
specific mount points. You can get help about mount by typing
man mount in a shell prompt
• CD-ROM
In a shell prompt (or shell window) you can mount a CD-
ROM in /mnt/cdrom as follows:
mount -t auto /dev/cdrom /mnt/cdrom
If /etc/fstab has been modified as explained below under
"Using the mount tool", then you can just type:
mount /dev/cdrom
• Floppy
In a shell prompt (or shell window) you can mount a floppy
diskette in /mnt/floppy to read DOS formatted diskettes or
Linux formatted diskettes, respectively, as follows:
mount -t msdos /dev/fd0 /mnt/floppy
mount -t ext2 /dev/fd0 /mnt/floppy
• Using the mount tool
you can mount CD-ROMs and diskettes by simply typing
usermount in a shell window. A graphical window will open
and let you mount and unmount both type of devices.
To allow users other than root to mount and unmount devices
you need to edit as root the lines in /etc/fstab corresponding
to floppy and CD-ROM, respectively, to look like the
following:
/dev/fd0 /mnt/floppy auto user,defaults,noauto 0 0
/dev/cdrom /mnt/cdrom auto user,ro,defaults,noauto 0
0
The /etc/fstab File
The /etc/fstab file makes it possible to automate the
mounting of certain filesystems, especially at system start-up. It
contains a series of lines describing the filesystems, their mount
points and other options. Here is an example of an /etc/fstab
file:
/dev/hda1 / ext2 defaults 1 1
/dev/hda5 /home ext2 defaults 1 2
/dev/hda6 swap swap defaults 0 0
/dev/fd0 /mnt/floppy auto sync,user,noauto,nosuid,nodev,unhide 0 0
/dev/cdrom /mnt/cdrom auto user,noauto,nosuid,exec,nodev,ro 0 0
none /proc proc defaults 0 0
none /dev/pts devpts mode=0622 0 0
Mounting USB Drive
A USB drive is mapped to /dev/sda1 or /dev/sdb1. You can mount
it as follows using the mount command:
% mount /dev/sda1 /mnt/usb
However, you should have USB kernel modules loaded for the
mounting of USB to be successful. You can check for USB
modules using lsmod command:
% /sbin/lsmod
usb-ohci 22216 0 (unused)
usbcore 82592 1 [hid usb-ohci]
If you don’t find usb-ohci and usbcore modules in the
output of the lsmod command, you will have to load them as
follows:
/sbin/modprobe usb-uhci
/sbin/modprobe usbcore
Setting Up the Network Interface
Before connecting a computer to a network, its network interface
has to be configured. The basic command to configure the network
interface in Linux (and other Unix systems) is ifconfig. The
ifconfig command needs the following input parameters to
configure a network interface:
Interface Name: Name of the interface that you want to configure.
E.g., eth0.
IP Address: IP Address to be assigned to the host. It should
belong to the network to which the host is being connected.
Netmask: Netmask of the network to which the host is being
connected.
For example, to configure and turn on the network interface eth0,
we can use the following command:
% ifconfig eth0 192.1.64.3 netmask
255.255.255.0 up
To disable or turn off the interface, we can use ifconfig command
as follows:
% ifconfig eth0 192.1.64.3 netmask
255.255.255.0 down
A simpler way of configuring the interface is by using the Network
Configurator utility inside the Control Panel.
To configure the hostname, DNS Domain Name, and gateway and
other networking options, /etc/sysconfig/network file is used.
Following is an example /etc/sysconfig/network file:
NETWORKING=yes
HOSTNAME=linux.ccse.kfupm.edu.sa
GATEWAY=196.1.64.253
NISDOMAIN=ccse
DOMAINNAME=ccse.kfupm.edu.sa
To translate hostnames to IP Addresses, /etc/hosts file is used. If
you want to access some remote machine using its hostname, then
you should have an entry for that machine in /etc/hosts file.
Following is an example of /etc/hosts file:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
196.1.64.140 linux linux.ccse.kfupm.edu.sa
196.1.64.30 remotehost remotehost.kfupm.edu.sa
Testing Network Connection Between Two Hosts
The basic tool to test the network connection between two hosts is
the ping command. For example, if we have two hosts 192.1.64.1
and 192.1.64.2 and want to test the connection between them. We
can simply log on 192.1.64.1, and ping 192.1.64.2 from there as
follows:
% ping 192.1.64.2
Setting Up or Activating Network Services
Common Network Services, like telnet, ftp, etc., are controlled by
a daemon (a program that runs quietly in the background listening
for the incoming requests) named xinetd in Linux. To register a
service with xinetd, the configuration file for that service has to be
put inside /etc/xinetd.d directory. Following is an example
configuration file for telnet service (its location is
/etc/xinetd.d/telnet):
# default: on
# description: The telnet server serves telnet sessions; it
uses
# unencrypted username/password pairs for authentication.
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
disable = no
}
Setting Up the DNS
The DNS is setup in /etc/resolve.conf files. The list of DNS servers
should be put in /etc/resolv.conf file.
search ccse.kfupm.edu.sa
nameserver 196.1.64.10
nameserver 196.1.64.3
Setting Up Routers
The default router (gateway) can be setup in
/etc/sysconfig/network file. However route command can be used
to add and delete routes in real time:
% /sbin/route add default gw 196.1.64.253
% /sbin/route add -net 196.1.65.0 netmask
255.255.255.0 gw 196.1.64.253
Routes can also be deleted using the route command:
% /sbin/route del default gw 196.1.64.253
% /sbin/route del -net 196.1.65.0 netmask
255.255.255.0 gw 196.1.64.253
To check out the routing table, netstat command is used:
% netstat –nr
Destination Gateway Genmask Flags MSS Window irtt Iface
196.1.64.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 196.1.64.253 0.0.0.0 UG 0 0 0 eth0
Adding Kernel Module
Most of the device drivers of Linux are shipped in the form of
loadable kernel modules. Use insmod command to load a kernel
module. You should know the path of the binary (object) file of the
module:
% insmod /path/to/snarf.o
To list the already loaded kernel modules, lsmod command is
used.
Installing Device Drivers
Scheduling Tasks
The cron daemon is used to schedule commands. You can
configure cron daemon to run a particular command at a particular
time for you by editing the crontab file (in /var/spool/cron
directory). For example, the crontab file for the root is
/var/spool/cron/root.
Format of the Crontab entry
Minute hour day month weekday command
Examples
45 10 * * 1-5 (meaning: 10:45 a.m., Monday through Friday)
0,30 * 13 * 5 (meaning: every half-hour on Friday and every
half-hour on 13th)
1 * * * * /scripts/clean-filesystem
#!/bin/sh
find / -name core –exec rm –f {} \;