Linux Administrative Tasks Basics
Linux is a very powerful operating system and most of its power lies in it's capability in various utilities and programs available which runs through terminal or console. So, it is very essential to learn basics of Linux administration along with main commands and how to use them to administer you Linux system.
Lets get started with Startup Process. The Linux startup process starts with Kernel loading itself into the memory. This loads everything needed for basic operation of the system such as device drivers, memory handling files etc - but doesn't load anything for user-interfaces. There is a process called init which does the job of loading user related interfaces.
The init Process at Start
The kernel loads itself first into the memory and calls the init process in the end of it. After init process is called, it starts initializing the system by starting various process, programs and daemons such as - starting the getty process, network services etc. the getty process is responsible to enable you to login to the system using console.
The init process first looks into its configuration file located at /etc/inittab and runs the scripts which are normally located in the /etc/rc.d directory.
The init process at Shutdown or Reboot
The init process is also called at the time of rebooting or shutdown of the system. The basic job of init process at this time is to make sure the system shuts down cleanly so that you don't loose your data. It does this by terminating all running processes and unmount all the file systems. Please be aware that your unsaved file will not be saved by init process if you have not saved it yourself.
The init Script
As we said above, init process looks its configuration files first and then execute scripts found there. There are several number of shell scripts under /etc/rc.d directory. These shell scripts normally start with making sure all the necessary files are present in the system and then starts the process. When init process is called at the time of shutdown or reboot, it terminates the running processes. It also created status report file for process and can also check if a particular process is running or not.
Runlevels in Linux defines the levels of operations and the its information is stored in /etc/inittab file. Commonly used runlevels defined in most of the Linux distributions are as follows -
- 0 : This runlevel is used to shut down the system
- 1 : Single User Mode - This is basically for Superuser and used mostly for maintenace and repair pf the system
- 2 : Multi-user Mode - More than one user allowed to login but network services are not initialized. Usually folloiwng services are started in this runlevel - Web Servers, System Loggers, inetd daemon and cron daemon
- 3 : Full Multi-user Mode - All the improtant services including network services are started in this runlevel.
- 4 : Not used (Reserved)
- 5 : X Windows System - This boots the system into X Window System.
- 6 : This runlevel is used to Reboot the system.
Runlevel is set by modifying following line in the /etc/inittab file -
id:5:initdefault: - Just replace the 5 with your choice of runlevel
Changing runlevel: Runlevel can be changed ny issuing init command with runlevel as follows-
#init 3 and hit Enter
The /etc/rc.d/ directory structure (Red Hat)
Here the directory structure is shown based in Red Hat Linux. It is different in Debian based Linux. Although, the concept is same for the runlevel in all kind of Linux distribution.
#ls -Fa /etc/rc.d/ and hit Enter
./ rc* rc.sysinit* rc2.d/ rc5.d/
../ rc.firewall* rc0.d/ rc3.d/ rc6.d/
init.d/ rc.local* rc1.d/ rc4.d/
As you can see above, every runlevel has got their corresponding script directory identifed by runlevel number. Lets look inside one of them.
#ls -Fa /etc/rc.d/rc3.d/ and hit Enter
You will notice in the above example that script name starts with k and s. as the system starts to boot, it will start with s followed the sequence number. It reads the filenames in alphabetic orer and lower script number will have higher priority. When the system shutdown, it follows the reverse and run the scripts starting with k letter.
Important thing to notice here is that these scripts are actually symbolic links and actaul files are loacted in /etc/rc.d/init.d:
Manually Controlling a Process:
You can manually start, stop or restart a any process by running appropriate init script -
#/etc/rc.d/init.d/httpd start and hit Enter - This will start the httpd service
#/etc/rc.d/init.d/httpd stop and hit Enter - This will stop the httpd service
#/etc/rc.d/init.d/httpd restart and hit Enter - This will restart the httpd service
Modifying the init process:
The init process can be modified by manually creating or deleting symbolic links within /etc/rc.d/ directory. But this is very completed and not so user-friendly method. In Red Hat, there is a utility called chkconfig which is can be used to display, activate and de-activate init process easily and safely.
#chkconfig -list : This will display list of all init processes known to the system.
#chkconfig -list httpd : This will list the information about httpd only.
#chkconfig -httpd off : to disable the httpd
#chkconfig -httpd on : to enable httpd back
Shutting Down the System
The simple command to shutdown the system is to type init 0 to shutdown and init 6 to reboot the system from console. The halt command can also be issued to reboot command to halt or reboot the system.
Although, these command works great if you are the only user in the system but not suitable for multi-user server system. It will be polite to send warning messages to users before shutting down the system. In this case, you can use shutdown command. The shutdown command send warning message and waits for specified time with command before it actually shuts down the system. It then calls the init process to end the running processes and shut down the system properly.
The syntax of shutdown command:
#shutdown [-t delay] [-r |-h | -k | -c] time message
-t option is the delay in seconds between the warning and the actual shutdown. The time option can be given in two formats - exact time (i.e. 11:24) or delay in minutes (for example, +10m to shutdown in ten minutes). You can also use now with shutdown command to immediately shutdown the system.
-r option is used to reboot the system
-h option is used to halt the system
-k option cause shutdown to send out warning messages, but not actually causing it to shutdown
-c option is used to cancel a running shutdown process
Disk Partitioning and Formatting
As a system administrator of Linux, you will come across disk partitioning and formatting. It sounds very scary but it is not that difficult.
Lets get started - first thing to know before you start partitioning and format the disk is the Device names and understand how Linux assigns them. All peripherals and devices in Linux are referred as files and they are kept in the /dev directory. The IDE hard drives are referred as hd with letters referencing their location on the IDE chain and SCSI hard drives are referred as sd with numbers referencing to their location on SCSI chain.
IDE Drive Device Names
IDE Name Device Name
Primary Master /dev/hda
Primary Slave /dev/hdb
Secondary Master /dev/hdc
Secondary Slave /dev/hdd
SCSI Drive Device Names
SCSI Position Device Name
First SCSI hard drive /dev/sda
Second SCSI hard drive /dev/sdb
Third SCSI hard drive /dev/sdc
Tenth SCSI hard drive /dev/sdj
In the above example, the device names refer to entire hard drive. When the hard drive is partitioned, a partition number is appended to the device name and it will be referenced such as /dev/hda1 for partition number 1 on /dev/hda hard drive as follows in the table below -
IDE Drive Partition Device Names
Partition Number Device Number
Using fdisk to Format
The fdisk command is used to format the disk. You must identify the device name first before you issue this command. The fdisk takes /dev/hda as default drive.
It is also to note that if you are using a large hard drive, use it in LBA mode to make it compatible with other operating system.
Lets assume that you want to format Primary Slave drive /dev/hdb, you will issue the following command -
Now, fdisk will present you with Interactive Mode prompt where you can type single letter commands to list, create and delete partitions. The first command probably you will want to use is the p command which will print out the current partition table details-
Command (m for help): p
This will display the partition details. After this you will use d command to delete the existing partition table and make it ready for Linux partitions.
Command (m for help): d
Now, you will have to type the partition number you want to delete and after the existing partitions are deleted, use p command to view the partition tables which should be empty if all the partitions are deleted.
Primary and Extended Partitions
Linux supports upto four (4) primary partitions. If you need more than four partitions, you can create one of your partitions as an Extended partition and this Extended Partition can hold many logical volumes. But the only limitations with this kind of this kind of partitions is that you cannot boot system from them.
The Primary Partitions, sometimes also referred real partitions, are numbered from 1 to 4 and logical volume partitions starts from 5 onward.
Again to add partitions, you will use fdisk command. The n command creates New Partitions. The t command is used to modify partition types. The l command will list all the known partition types.
First, you will create partitions as per your plan using n command and then use use t command to assign partition types to each of your partitions - for example 82 for Linux Swap and 85 for Linux Extended partition types. You can see the list of available partition type by using l command.
After you have completed creating partitions and assigned types, you will need to use w command to write these changes and make it permanent.
Formatting Your Partitions
After partitions were created and types assigned to them, you will need to format them now before they can be used to install Linux Operating System.
You will run mkswap program to format your swap space -
#mkswap -c /dev/hdb1 : -c switch will check for bad blocks while formatting
You will use mke2fs to format Linux's native file system which is called ext2 filesystem
#mke2fs -c /dev/hdb2
The mke2fs command prints lot of information which you can note down for future references. For example - list of blocks on which it stores backup of superblock. The superblock contains the filesystem information. The system keeps many copies of this block information to recover itself in the event when the filesystem gets damaged.
The -c option makes the mke2fs command to provide a progress report as it works.
Checking Your Filesystem
The command to check Linux Filesystem is e2fsck. The -f option along with e2fsck forces it to check a filesystem.
#e2fsck -f /dev/hdb2
By default, the e2fsck command doesn't check a filesystem marked as clean and prints the usage statistics instead. If the Filesystem is not marked as Clean, it goes through five(5) separate phases. You can use -y option to allow e2fsck to correct the error without your intervention.
The superblock in Linux is equvalent to FAT in MSDOS Filesystems which contains various file system information. This information is very vital so ext2 filesystem saves backup of the superblock.
If the e2fsck commands doesn't find the superblock in case there is an error, you can supply backup superblock address via the -b command as below -
#e2fsck -b 7324 /dev/hdb2
Linux supports most of the hardware such as IDE / SCSI tapes, CD/DVD writers and many more which can be used to backup Linux files. But we shall be discussing three in-built Linux utilities for backup purpose -
1. cpio: The cpio stands for copy In/Out - it copies files into or out of archives and has three modes of operations.
- Create and Archive
- Extract from Archive
- Passthrough Mode
#ls *.txt | cpio -ov > text.cpio
This is an example of using in archive mode. It creates an archive of all the txt files in the current directory.
The ls command generates list of all files you want to archive. The o option tells cpio to create an archive and v for verbose.
This creates the archived file in current directory. You want to create this file in another media, just replace the name with backup media device filename.
EXTRACT FROM AN ARCHIVE
#cpio -iv < text.cpio
This will extracts the archive file into current directory. The i option tell cpio to extract the files from archive.
#cpio -i --list < text.cpio
This gives listing of files inside the archive similar to ls command.
This doesn't do backup in traditional way but mostly used to copy all files from one directory tree to another keeping file mode and permissions. This is traditionally used when you are transferring a filesystem to another hard drive and wants to copy all the files to that drives maintaining file modes and permissions.
#find . -print | cpio -pd /mnt where /mnt is the device file name for new drive and it will copy all the files from current drive to /mnt drive. The v option can also be used to print the names of the files as it copies and capital V to print dot (.) instead of a filename.
The dump command is most powerful backup utility among all the three discussed above and most suitable for backing larger data. This supports incremental backups - means it only back up newer files or recently modified files as it increments instead of copies all the files every time.
- I also want to mention that it has ten backup levels and at zero level, it does full backup.
- The u option is used for incremental backup which tells the dump to update its database (/etc/dumpdates)
- The f option is also passed to dump followed by the device name to which files are to be backed up.
- Last option is the filesystem you want to backup such as /home.
#dump 0uf /dev/hdd1 /home
Note: If dump runs out of the space on the backup media, it prompts and continue after the media has been replaced with another one.
The restore is used to store the back up file created by dump backup utility.
#restore ivf /dev/hdd1
option i is for interactive mode
option v is for verbose operation
option f is to store from device name
In the interactive mode, it will allow you to use commands such as ls, cd and pwd and you can terminate the interactive mode by typing quit.
This is widely used to create distribution files for Linux software pacjages known as tar-balls.
tar t | x | c [z] [v] [f] tarfile list-of-files
- One of three options from c, x and t has be selected - c stands or create, x for extract and t for test.
-The z option tells tar to create or extract from compressed archive
- The option v is for verbose operation
- The option f is followed by filename you are creating or device name to which you are backing up
#tar czvf /tmp/testbackupfile.tar.gz /home/rakesh
This will copy all the files inside /home/rakesh directory and create a back up file called "testbackupfile.tar.gz in the current directory
#tar xzvf testbackupfile.tar.gz
This will extract the files from tar file into current directory
Installing Software Packages Manually
As Linux has developed, installing software packages are not so difficult any more. In Red Hat Linux, you can use rpm software package installer and apt-get package installer can be used in Debian. But, here we are going to talk about how to install software packages manually. The GNU Autoconf tool has made the software installer for new users too. In this section, we will cover the process of installing software from source code file (a .tar.gz or .tgz file) refereed as tarball. Usually these files contains two very improtant files README and INSTALL. You must read them before moving to the installation steps.
The tarball also contains an executable shell script called configure. This file search for libraries and header files needed for the programs and attempts to configure the package for compilation on your Linux system.
Run the shell script file configure first and then once the configuration process is over, you can start the compilation process thereafter.
- In a common convention, manually installed software files goes to /usr/local directory to keep it separate from system-installed software. As most of the tarball automatically installs software in /usr/local directory, you should make sure that /usr/local/bin directory is in your search path.
STEP 1: Untar the tarball file
#tar xzvf examplesoft-1.0.tar.gz
This will create a new directory containg the same name such as examplesoft-1.0 which will have atleast following files - configure, README and INSTALL.
STEP 2: Running configure
# ./ configure
You need to type ./ before configure as on many Linux system, the current directory is not in the search path for security reasons.
If the configure scripts runs successfully, move on to next step else rectify the problem reported by configure file and then run it again until you get it to run successful.
STEP 3: Compiling the Source Code
This will start the compilation process. Warning mesages can be ignored but errors needs correction
STEP 4: Installing
# make install
After the compilation was successful, this command will start the installation of the software package usually to the /usr/local directory.
# make -n install
This runs the installation scripts in test mode.
STEP 5: Updating the system
This step is required as your system may not be aware of the new installation - such as installation directory not in the search-path.
Run this -
# export PATH=$PATH:/usr/local/bin if using bash shell
#rehash if using C-shell
You also need to update system's main library path but be very careful while doing so as it might cause the entire system to become unstable.
- The library paths are contained in this file - /etc/ld.conf.so
- Some system may not have /usr/local/lib in this file which will definitively cause the libraries failure.
# cat /etc/ld.conf.so
This will show the file's content. if the path is not found in the file, you should add that to this file and then update the library using following command -
# ldconfig -v
This will re-build the library cache.
This is it. These are the most common administrative tasks for a Linux System Administrator. Your comment is most welcome.