Wednesday, August 31, 2011

Linux Basic Commands


Linux operating system has a beautiful graphical interface which most of us will be using. It will be good to learn the basic commands in Linux to work interactively with the Linux operating system. Linux has a back end access know as shell. You can control and activate all the process in Linux from the shell. So it is very important to learn few basic commands to work with Linux operating system.

First we will learn how to login for shell access. There are 7 terminals for Linux. 6 terminals are non - GUI and 1 terminal for GUI access. You can login to each terminal using Alt + Ctrl + F1 , F2, .. F7. Each terminals will request your username and password for login. If you want to use the shell in the graphical interface (GUI), press Alt + F2 and type "konsole". As a user you will have permission to access only your /home/user directory and other directories in it.

Now lets check out some commands:

pwd
This command is used to find the current location or current working directory.

Eg:
[user@ws26 ~]$ pwd
/home/user

cd
This command id used to change the directory. You can move from one directory to another using this command. Few examples are given below.

Concider you have a directory structure /home/user/test/test1/ . test and test1 are directories in user home.

Example 1:
Consider you have a directory "test" in /home/user. Your current working directory is /home/user. You want to change your current working directory from /home/user to /home/user/test, use the following command
[user@ws26 ~]$ pwd
/home/user
[user@ws26 ~]$ cd test
[user@ws26 ~]$ pwd
/home/user/test

Example 2:
If you want to move back to /home/user, use the following command
[user@ws26 ~]$ cd ..
[user@ws26 ~]$ pwd
/home/user

Example 3:
To get back to the home directory of the user
[user@ws26 ~]$ cd ~
[user@ws26 ~]$ pwd
/home/user.

Listing contents in a directory - [ ls, ls -l, ll ]

ls
This command is used to list all the files and directory in the current directory.

Eg:
[user@ws26 ~]$ ls
1152696870.jpg book OperaDownloads snapshot52.png
image300.jpg Desktop test spiderman2.jpg

ls -l or ll
This command is also used to list all the files and directories. Here you will get more details about the files and directories present in the current directory. You will see the permission set, creation date, file / directory size etc.

Eg:
[user@ws26 ~]$ ls -l
total 5
-rw-rw-r-- 1 user user 33188 Dec 22 02:56 1152696870.jpg
-rw-rw-r-- 1 user user 17647 Aug 19 2006 534458.gif
-rw-rw-r-- 1 user user 26817 Aug 19 2006 534477.gif
drwxrwxr-x 2 user user 4096 Jan 13 04:20 book
drwx------ 3 user user 4096 Feb 4 02:34 Desktop

Reading files in Linux - [ cat, more, less ]

Cat
This command is used to display the contents of a file. You can read the file contents using the cat command.

Example:
[user@ws26 ~]$ cat testfile
this is a test file.. You can read me..


Less and More
Both the less and more commands serve similar function. They are used to display file one screen at a time. You can press spacebar to continue reading the file. These commands are mainly used while reading large files.

Example 1:
[user@ws26 ~]$ less testfile
this is a test file.. You can read me..

Example 2:
[user@ws26 ~]$ more testfile
this is a test file.. You can read me..



Manipulating files - [ cp, mv, rm, mkdir ]

cp
This command is used to copy files/directory.

Example:
[user@ws26 ~]$ cp file1 directory1
This will copy the file1 into the directory1

cp -R
This command is used to copy directories recursively (copy all the files and folders inside the directory).

Example:
[user@ws26 ~]$ cp directory2 directory1
This will copy the whole directory2 into directory1

mv
This command is used to move the file or directory.

Example:
[user@ws26 ~]$ mv directory2 directory1
This will move the whole directory2 into directory1

rm
This command is used to remove or delete files and directories.

Example:
[user@ws26 ~]$ rm directory2
This will delete the directory - directory2

mkdir
This command is used to create a new directory

Example:
[user@ws26 ~]$ mkdir directory3
This will create a new directory - directory3.

create new file in Linux - vi editor

The Vi editor is mainly used to create new files in Linux.

Example:
vi testfile
The command will open a new window and you can insert text to it. To begin press "i". After adding your contect press ":wq" to save the file.


Linux Run Levels & Init

Once kernel and drivers are loaded, Linux starts loading the rest of the system. This starts with the First Process, known as init and it has the process id of 1 (the kernel itself has the process id of 0, which cannot be displayed by using the "ps" command).

The init process takes control of the boot operation. The init process in turn runs /etc/rc.d/rc.sysinit, which performs a number of tasks, including network configuration, SELinux status, keyboard maps, system clock, partition mounts, and host names.

The runlevels are controlled by a configuration file which init process reads from the location /etc. The name of the init configuration file is "inittab".

The init process then determines the runlevel by looking at the initdefault directive in /etc/inittab configuration file. The following are the defined runlevels. The init process remains active as long as the system is running.

Runlevel value

Description

0

Halt

1

Single-user mode

2

Multiuser, with some network services

3

Multiuser, with networking

4

Unused

5

Full Multiuser mode with X Windows (GUI login screen)

6

Reboot


Inittab configuration file

After the kernel boots, it always executes a program called "init". The init program then reads its configuration file called /etc/inittab and commences the boot process. A sample inittab file is copied below. The lines which are commencing with a "#" are comments.

### Beginning of inittab ###


# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
#
# Author: Miquel van Smoorenburg,
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#

# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:5:initdefault:

# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"

# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon

### End of inittab ###


Format of Inittab

The format of each line in inittab file is as follows:

id:runlevels:action:process

• id: A unique sequence of one to four characters that identifies this entry in the /etc/inittab file.

• runlevels: The runlevels at which the process should be invoked. For example, the runlevels entry 123 specifies something that runs at runlevels 1, 2, or 3.

• action: Describes what action should be taken. Options for this field are explained in the next table.

• Process: Names the process (or program) to execute when the runlevel is entered.

The following table specifies some valid actions for the action field.

Action Field in inittab file

Description

respawn

The process will be restarted whenever it terminates.

wait

The process will be started once when the runlevel is entered, and init will wait for its completion.

once

The process will be started once when the runlevel is entered; however, init won’t wait for termination of the process before possibly executing additional programs to be run at that particular runlevel.

boot

The process will be executed at system boot. The runlevels field is ignored in this case.

bootwait

The process will be executed at system boot, and init will wait for completion of the boot before
advancing to the next process to be run.

ondemand

The process will be executed when a specific runlevel request occurs. (These runlevels are a, b,
and c.) No change in runlevel occurs.

initdefault

Specifies the default runlevel for init on startup. If no default is specified, the user is prompted for a runlevel on console.

sysinit

The process will be executed during system boot, before any of the boot or bootwait entries.

powerwait

If init receives a signal from another process that there are problems with the power, this process will be run. Before continuing, init will wait for this process to finish.

powerfail

Same as powerwait, except that init will not wait for the process to finish.

Powerokwait

This process will be executed as soon as init is informed that the power has been restored.

ctrlaltdel

The process is executed when init receives a signal indicating that the user has pressed the ctrl-alt-del key combination. Keep in mind that most X Window System servers capture this key combination, and thus init may not receive this signal if the X Window System is active.



Linux Boot Loader => GRUB

Boot loader is a software code that runs before the Operating System and helps in loading the Operating System. Boot loaders usually contain several ways to boot the Operating System kernel and also contain commands for trouble-shooting or passing some values to the kernel while booting.

When a computer with Red Hat Enterprise Linux is powered on, the Red Hat Enterprise Linux Operating System is loaded into memory and started by a boot loader. A boot loader program is located on the system's primary hard drive and the boot loader has the responsibility of loading the Linux kernel with its required necessary files into the computer’s memory.

RedHat enterprise editions for different hardware architecture use different boot loaders. The following table shows the different boot loaders for different hardware platforms.

Architecture

Boot Loaders

AMD32, AMD64,

GRUB

Intel x86, EMT64T

GRUB

Intel Itanium

ELILO

IBM eServer System i

OS/400

IBM eServer System p

YABOOT

IBM System z

z/IPL

The GRUB (GNU GRand Unified Boot loader) is the default boot loader for AMD32, AMD64, Intel x86 and Intel EMT64T based hardware platforms. GRUB (GNU GRand Unified Boot loader) enables the selection of the installed operating system at boot time. GRUB also allows the user to pass arguments to the kernel while booting.

Different stages of Linux booting.

1. The Stage 1 or primary boot loader is read into memory by the BIOS from the Master Boot Record (MBR). The primary boot loader exists on less than 512 bytes of disk space within the MBR and is capable of loading either the Stage 1.5 or Stage 2 boot loader.

2. The Stage 1.5 boot loader is read into memory by the Stage 1 boot loader.

3. The Stage 2 or secondary boot loader is read into memory. The secondary boot loader displays the GRUB menu and command environment. This interface allows the user to select which kernel or operating system to boot, pass arguments to the kernel, or look at system parameters.

4. The secondary boot loader reads the operating system or kernel as well as the contents of /boot/sysroot/ into memory. Once GRUB determines which operating system or kernel to start, it loads it into memory and transfers control of the machine to that operating system.

5) init program is initiated and it will read the inittab file (/etc/inittab) and set up the appropriate runlevel.


A sample grub.cof file is shown below.

The lines beginning with a "#" are comments.



### Beginning of grub.conf ###

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu

#####First Operating System#####

title Red Hat Enterprise Linux Server (2.6.18-8.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-8.el5 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.18-8.el5.img

#####Second Operating System#####

title RedHat Operating System 2
root(hd1,0)
kernel /vmlinuz-2.6.18-8.el5 ro root=/dev/sdb2 rhgb quiet
initrd /initrd-2.6.18-8.el5.img

### End of grub.conf ###

The grub.conf configuration file is explained in detail below.

• The default=0 directive points to the first stanza, which is the default Operating System to boot.

• The timeout=5 directive specifies the time, in seconds, for GRUB to automatically boots the default operating system.

• The splashimage directive locates the graphical GRUB screen.

• The hiddenmenu directive means that the GRUB options are hidden.

A stanza begins with a titile, (the text to be displayed in boot menu for selecting the Operating System) and the next three lines specify the location of the /boot directory, the kernel, and the initial RAM disk (The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available), respectively.

root (hd0,0) - Specifies the boot directory is in first hard disk, first Partition.

kernel /vmlinuz-2.6.18-8.el5 ro root=LABEL=/ rhgb quiet - Specifies the kernel location which is inside the /boot folder. This location is related to the root(hd0,0) statement. The "ro" option specifies the kernel should be opened as read only to protect it from any accidental writes from the initial RAM disk and "rhgb" enables the RedHat Graphical boot option.

initrd /initrd-2.6.18-8.el5.img - Initial RAM disk.

Linux Directory Structure


If you are working in Linux for the very first time,you will be confused about the directory structure. But dont worry we will make it clear to you here.

If you work on a Windows pc,you have seen that there are three folders created by default in your C:\ drive;Windows,Program files and Document and settings. Windows folder keeps OS own files,Program files stores the installed programs files whereas Document and Setting folder keeps users personal settings and personal data.


But in Linux scenario is a little bit different. In window, where we call Folders,are called “Directories” here. All the files are stored in directories which can have many other subdirectories. In Linux,everything stored under the root directory and '/' [forward slash ] is the symbol of root. Whereas in Windows everything stored in 'C:\' [back slash].


Now have a look at the Linux Directory Structure.


/

It is also know as root. All other directories in linux are subdirectories of / [root].It is the top level directory of Linux Directory Hierarchy. Only the root user has the right to write into this directory.


/bin

This directory contain shells like bash and csh and executable commands which can run by both normal users and root user. Commands like touch, mkdir, cat, vi, cp, mv etc.. are stored in this directory. These commands are very necessary bcoz even in single user mode you can run these commands to rescue your system .


/sbin

This directory too contain binary executable commands but these commands can only be run by the root user. Command stored in this directory are used for the system administration and maintenance purpose. Commands like fdisk, iptables, reboot, mdadm, fsck, ifconfig are stored in this directory.


/home

This is the home directory of a normal user. Every user who has an account on the system,has its own private data in this directory. This directory can be modified by only superuser or the owner of the directory. Like user test1 & test2 will have their own directory like /home/test1 and /home/test2.


/root

This is the home directory of the superuser or root. Root's own private data is stored in this

directory. All the administrative task are performed by loging in to this directory. It is advisable that you should not login with the root access as any small mistake can make your system unbootable.

/etc

This is the main configuration directory of your linux os. All important network and other services are configured into this directory. Some major server configuration files like samba.conf, httpd.conf, dhcpd.conf are configured in this directory.


/var

This is the variable directory of your system. Variable data is a data which repetitively changes.

Information about system logs,mails,print-queue and database is stored in this directory.

Running system operations uses this directory thus the data changes continuously.


/tmp

This is the temporary directory of your linux os. The applications and programs which run on your system,creates some temporary files into this directory. But as the name says itself that it is temporary,the data is flushed from here once you restart your system.


/opt

This is the optional directory where all the softwares and add-ons are stored. This is the default software installation directory of your system and usually you will find softwares like starOffice ,teamviewer and kde installed here.


/proc

This is the virtual directory or Pseudo filesystem which stores information of system processes and the kernel. Like you can find information about cpu and memory in /proc/meminfo and /proc/cpuinfo.


/dev

This is the device directory of your system. Every device in linux is assumed as files.This directory contain device files that are required to communicate with various hardwares attached to your system like USB and CD/DVD.


/lib

This directories contains the library files that are required by system programs to run and to boot the system. If you wish to develop the kernel of the os, you will be using the libraries. These libraries are same as Windows DLL files.


/boot

These directory contain the booting files that are necessary to boot your linux os. Files such as vmlinuz[kernel], grub, initrd etc.are placed here. If you delete any file from this directory ,your system can be unbootable.


/mnt

This is the temporary directory in which storage devices can be mounted. Mounting is a process by which you can assign a filesystem to a partition so that you can access that partition.


/media

This is the temporary directory in which removable media devices are mounted such as CD-ROM and USB devices. Whenever you insert your cd-dvd's into the cd-tray you can access them with this directory


/srv

This directory contain data files of server related processes. Data files of FTP and HTTP can be found here. Usually this directory is empty all the time.


/lost+found

This directory is used to recover any lost or corrupted data. If your system shutdown unexpectedly , the data can be corrupt or lost. In that case when your system goes through the FSCK[file system check],it is possibly that we can recover that data here.


/selinux

Selinux is Secured Enhanced Linux. This directory contain data related to linux security.



File System In Linux

In GNU/Linux, everything is represented as a file. A file is a collection of data or may be a stream of characters (that is, a byte stream). A typical file can contain either text or code data. Text files are readable by a user and code data (binary file) is readable by the computer (executables, images etc).

Different types of files in GNU/Linux

• Ordinary files. Contain either text or code data.

•Directories. Directories are files that are lists of other files. Each directory entry represent either a file or a subdirectory.

• Special files. Usually represent devices used by the system.

• Links. A link is a special file which used to make a file or directory visible in multiple parts of the File System.

What is a File System?

A filesystem is a database of files and directories that you can attach to a GNU/Linux system at the root (/) or some other directory in a currently attached filesystem.

GNU/Linux supports large number of filesystems. Following are the important file systems supported by GNU/Linux.

• minix: The oldest but most reliable File System, but with limited features.

• ext: An older version of ext2 that wasn't upwards compatible. It is hardly ever used in new installations any more, and most people have converted to ext2.

• ext2: The most featureful of the native Linux filesystems, but with no journalling support.

• ext3: The ext3 filesystem has all the features of the ext2 filesystem with journaling has been added. Journalling allows the filesystem to repair itselfafter an improper shutdown or other type of damages. Journaling keep record transaction which are to be performed, or which have been performed.

• reiserfs: A stable journaled filesystem.

• jfs: JFS is a journaled filesystem designed by IBM to to work in high performance environments

• xfs: XFS was originally designed by Silicon Graphics to work as a 64−bit journaled filesystem. XFS was also designed to maintain high performance with large files and filesystems. In addition, support for several foreign filesystems exists, to make it easier to exchange files with other operating systems. These foreign filesystems work just like native ones, except that they may be lacking in some usual UNIX features, or have curious limitations, or other oddities.

• msdos: Microsoft FAT filesystems.

• vfat: This is an extension of the FAT filesystem known as FAT32. It supports larger disk sizes than FAT. Most MS Windows disks are vfat.

• iso9660: The standard CD−ROM filesystem; the popular Rock Ridge extension to the CD−ROM standard that allows longer file names is supported automatically.

• nfs: (Network File System) A networked filesystem that allows sharing a filesystem between many computers to allow easy access to the files from all of them.

• hpfs: (High Performance File System) The OS/2 filesystem.

• ntfs: (NT File System) Latest Microsoft filesystem much better and stable than old Microsoft filesystems. NTFS has journaling support also.

Most Popular Linux Distributions

Major Linux Distributions


Red Hat Enterprise Linux

• Fedora Linux

Ubuntu: Ubuntu is considered to be the most popular Linux distributions and it was first announced in September 2004. The brain behind Ubuntu is Mark Shuttleworth who is a South African technocrat.

Ubuntu web site: http://www.ubuntu.com/

Pros: Great community of users and developers, great documentation, up to date packages, fast release cycle.

Cons: Some of Ubuntu's own software are proprietary.

SuSE Linux (Now under Novell): SUSE (Software und System Entwicklung) project was started in 1992 by four German Linux enthusiasts Roland Dyroff, Thomas Fehr, Hubert Mantel and Burchard Steinbild.

SUSE web sites: SUSE web site: http://www.novell.com/linux/ and http://www.opensuse.org/

Pros: Up-to-date, easy to use, good looking, stable.

Cons: Speed and performance.

Mandriva (Before Mandrake): Mandriva Linux was launched by Gael Duval in July 1998 under the name of Mandrake Linux. At first, it was just a re-mastered edition of Red Hat Linux with the more user-friendly KDE desktop, but the subsequent releases also added various user-friendly touches. Mandriva headquarters is located at Paris.

Mandriva web site: http://www.mandriva.com

Pros: Highly up-to-date, easy to use, good looking desktop, good community support.

Cons: Unstable, releases are initially reserved to mandrivaClub members and then made public after several weeks.

Debian: Debian is one of the oldest and stable GNU/Linux distributions. It was created in 1993 by Ian Murdock who named it after the combination of his own name and his girlfriend Debra.

Pros: Open-Source philosophy, non-commercial project, strong community, huge selection of packages and supported architectures, one of the best package management, excellent documentation, extremely stable and well-tested releases, modular, fast.

Debian web site: http://www.debian.org/

Cons: Slow release cycle, text-based installer, lack of configuration tools

CentOS: CentOS is a community project (launched in 2003) with the goals of rebuilding the source code for Red Hat Enterprise Linux (RHEL) into an installable Linux distribution and to provide timely security updates for all included software packages. We can say CentOS is a clone of RedHat Enterprise Linux.

CentOS web site: http://www.centos.org/

Pros: Extremely well-tested, stable and reliable; free to download and use; comes with 5-years of free security updates; prompt releases and security updates

Cons: Lacks latest Linux technologies.

Knoppix (Live CD): Knoppix is a live CD version of Linux. Knoppix can be run in a machine with out installing it.

Knoppix web site: http://www.knoppix.net/

Pros: Live Distribution, If the hardware is atleast 4 years old, then knoppix is a viable choice of installing and using Linux.

Cons: Knoppix is optimized for use as a live distro. So when installed on the hard-disk will boot up a bit slower than other distributions.

Introduction to Linux

The Free Software Foundation was founded in 1985. Free software Foundation promots the users right to use, study, copy, modify, and redistribute computer programs. The FSF promotes the development and use of free (as in freedom) software (Mainly GNU operating system or GNU/Linux).

The web sites of Free Software Foundation are http://www.fsf.org and http://www.gnu.org.

The word "Free" in "Free software" is related with liberty, not price. You should think of the word "Free" as in "free speech," not as in "free beer."

Free software promotes users' freedom to run, copy, distribute, study, change and improve the software. Free software refers to four kinds of freedom as listed below:

1) The freedom to run the program, for any purpose (freedom 0).

2) The freedom to study how the program works, and adapt it to your needs (freedom 1). Access to the source code is a precondition for this.

3) The freedom to redistribute copies so you can help your neighbor (freedom 2).

4) The freedom to improve the program, and release your improvements (and modified versions in general) to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this.

The GNU Project was launched in 1984 to develop a complete Unix-like operating system which is free software: the GNU system. GNU's kernel isn't finished, so GNU is used with the kernel Linux. The combination of GNU and Linux is the GNU/Linux operating system.

Linus Benedict Torvalds (born December 28, 1969) began the development of Linux, an operating system kernel, and today acts as the project coordinator.

Inspired by MINIX (a kernel and operating system developed by Andrew Tanenbaum), he felt the need for a capable UNIX operating system that he could run on his home PC. MINIX was a small UNIX-like operating system written by Professor Andrew Tanenbaum. MINIX was written from scratch, with no AT&T code, for teaching purpose. MINIX is a UNIX like operating system and is very useful for anyone who wants to learn the basics of UNIX operation.

Torvalds did the original development of the Linux kernel primarily in his own time and on his equipment.

Torvalds originally used Minix on his computer, a simplified kernel written by Andrew Tanenbaum for teaching operating system design. The Linux system quickly surpassed Minix in functionality.

The first version of the linux kernel was released to the Internet in September 1991. with the second version following shortly thereafter in October