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.