1) ifconfig = allows the operating system to setup network interfaces and allow the user to view information about the configured network interfaces

Examples

ifconfig

View the network settings on the Ethernet adapter currently used.

ifconfig eth0

View the network settings on the first Ethernet adapter installed in the computer.

ifconfig -a

Display into on all network interfaces on server, active or inactive.

ifconfig eth0 down

If eth0 exists would take it down causing it cannot send or receive any information.

ifconfig eth0 up

If eth0 exists and in the down state would return it back to the up state allowing to to send and receive information.

ifconfig eth0 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255

Assign eth0 with the above values for IP, netmask and broadcast address.

.

2) ifup = bring a network interface up

ifup eth0 – bring up interface eth0

.

3) ifdown = bring a network interfacedown

ifdown eth2 – bring down interface eth2

.

4) ping = ping allows a user to ping another network IP address. This can help determine if the network is able to communicate with the network.

Example:

ping gmail.com – Would ping the gmailhost .com to see if it is alive.

ping gmail.com -c 1 – Would ping the hgmailost .com once and return to the command line

.

5) traceroute = print the route packets take to network host

Examples:

$ traceroute google.co.in — Trace the route for Google Server.

$ traceroute -v 192.168.12.1 – Show more details.

$ traceroute -n server — Print the HOP Address in numeric form.

$ traceroute -I server
— Use ICMP ECHO instead of UDP datagrams.

$ tracerout
e -i eth1 server — Use eth1 for send the probe packets.

$ traceroute server 100
— Sent 100 Bytes packet instead of default 40 Bytes packet.

6) host = simple utility for performing DNS (Domain Name Service) lookups.  It is normally used to convert names to IP addresses and vice versa

Examples:

$ host google.com — Show the IP Address of Google.com.

$ host -v google.com — Show detailed output.

$ host -a google.com — Show more details.

$ host 127.0.0.1 — Do reverse lookup and Show the host name.

$ host -l mydomain.com — List all hosts available in MyDomain.

.

7) dig = DNS lookup utility

Ecamples:

dig yahoo.com

dig 66.94.234.13

dig -x 192.168.52.

dig -t ANY yahoo.com

1) jobs = lists the jobs that you are running in the background and in the foreground

Syntax

jobs [-p | -l] [-n] [-p] [-x] [job id]

If the prompt is returned with no information no jobs are present

jobs -p : list only the PID of process group leader

jobs -l : list only jobs that have change status since last notified by their status

jobs -r : resrict output to running jobs

jobs – s : restrict output to stopped jobs

.

2) bg = place a job in background

Normally user can run a job in background, by adding & at end of the
command (ex: sleep 10 &).

bg is a shell command. It is used to move a job from foreground to the
background, as if it had been started with `&’. If JOB is not present,
the shell’s notion of the current job is used.

Examples (follow all the steps below)

a) $ sleep 100 — Start a dummy job in foreground.   (sleep = waits a x amount of second)
Press Ctrl+z to stop the current job.

b) $ bg — Move the last stopped job to background.

c) $ sleep 150 — Dummy job 1
Press Ctrl+z to stop the current job.

d) $ sleep 140 — Dummy job 2
Press Ctrl+z to stop the current job.

e) $ sleep 130 — Dummy job 3
Press Ctrl+z to stop the current job.

f) $ jobs — List all active jobs.

g) $ bg 2 — Move the 2nd active job to background.

.

.3) fg = continues a stopped job by running it in the foreground

Examples

fg – Typing fg alone will resume the first job were it was left off.

fg 1 – specifying the job (in this case 1) will resume that particular job. The job id can be determined by running bg

.

4) nice = run a command with modified priority

nice command is used to run the given command with its scheduling
priority adjusted. Priority range goes from -20 (highest priority)
to 19 (lowest priority).

Examples:

$ nice – Prints the current priority value.

$ nice ls — Increment the priority value of the ls command
by 10 (Default value) and run.

$ nice -n 5 ls — Increment the priority value of the ls command
by 5 and run.

# nice -n -2 ls — Decrement the priority value of the ls command by
-2 and run.

NOTE: Incrementing the priority value will reduce the priority level
and vice versa. Priority range is -20 (high) to 19 (low).

.

5) renice = renice is used to alters the scheduling priority of one or more
running processes, priority range goes from -20 (highest priority)
to 19 (lowest priority).

Examples:

$ renice +1 123 — Increment the priority value of a process, which
process ID is 123.

# renice +1 123 – Decrement the priority value of a process, which
process ID is 123. (Root only can decrement ).

$ renice +1 -p 123 -p 200 — Same as above. Here no. of process is 2.

$ renice +1 -u sbharathi – Increment the priority value of all
processes, which is owned by a user (sbharathi).

$ renice +1 -g backup — Increment the priority value of all
processes, which is owned by a group (backup).

NOTE: Incrementing the priority value will reduce the priority level
and vice versa. Priority range is -20 (high) to 19 (low).

.

6) at = schedules a command to be ran at a particular time, such as a print job late at night

Syntax
at –  executes commands at a specified time.
atq - lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed. The format of the output lines (one for each job) is: Job number, date, hour, job class.
atrm -  deletes jobs, identified by their job number.
batch - executes commands when system load levels permit; in other words, when the load average drops below 1.5, or the value specified in the invocation of atrun.

Examples

at -m 01:35 < atjob = Run the commands listed in the ‘atjob’ file at 1:35AM, in addition all output that is generated from job mail to the user running the task. When this command has been successfully enter you should receive a prompt similar to the below example.

commands will be executed using /bin/csh
job 1072250520.a at Wed Dec 24 00:22:00 2003

at -l = This command will list each of the scheduled jobs as seen below.

1072250520.a Wed Dec 24 00:22:00 2003

at -r 1072250520.a = Deletes the job just created.

or

atrm 23 = Deletes job 23.

If you wish to create a job that is repeated you could modify the file that executes the commands with another command that recreates the job or better yet use the crontab command.

Note: Performing just the at command at the prompt will give you an error “Garbled Time”, this is a standard error message if no switch or time setting is given.

.

7) top = provide information (frequently refreshed) about the most CPU-intensive processes currently running

Description of the fields

a: PID — Process Id
The task’s unique process ID, which periodically wraps, though never restarting at zero.
b: PPID — Parent Process Pid
The process ID of a task’s parent.
c: RUSER — Real User Name
The real user name of the task’s owner.
d: UID — User Id
The effective user ID of the task’s owner.
e: USER — User Name
The effective user name of the task’s owner.
f: GROUP — Group Name
The effective group name of the task’s owner.
g: TTY — Controlling Tty
The name of the controlling terminal. This is usually the device (serial port, pty, etc.) from which the process was started, and which it uses for input or output. However, a task need not be associated with a terminal, in which case you’ll see ‘?’ displayed.
h: PR — Priority
The priority of the task.
i: NI — Nice value
The nice value of the task. A negative nice value means higher priority, whereas a positive nice value means lower priority. Zero in this field simply means priority will not be adjusted in determining a task’s dispatchability./dd>
j: P — Last used CPU (SMP)
A number representing the last used processor. In a true SMP environment this will likely change frequently since the kernel intentionally uses weak affinity. Also, the very act of running top may break this weak affinity and cause more processes to change CPUs more often (because of the extra demand for cpu time).
k: %CPU — CPU usage
The task’s share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment, if ‘Irix mode’ is Off, top will operate in ‘Solaris mode’ where a task’s cpu usage will be divided by the total number of CPUs. You toggle ‘Irix/Solaris’ modes with the ‘I’ interactive command.
l: TIME — CPU Time
Total CPU time the task has used since it started. When ‘Cumulative mode’ is On, each process is listed with the cpu time that it and its dead children has used. You toggle ‘Cumulative mode’ with ‘S’, which is a command-line option and an interactive command. See the ‘S’ interactive command for additional information regarding this mode.
m: TIME+ — CPU Time, hundredths
The same as ‘TIME’, but reflecting more granularity through hundredths of a second.
n: %MEM — Memory usage (RES)
A task’s currently used share of available physical memory.
o: VIRT — Virtual Image (kb)
The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out.

VIRT = SWAP + RES.

p: SWAP — Swapped size (kb)
The swapped out portion of a task’s total virtual memory image.
q: RES — Resident size (kb)
The non-swapped physical memory a task has used.

RES = CODE + DATA.

r: CODE — Code size (kb)
The amount of physical memory devoted to executable code, also known as the ‘text resident set’ size or TRS.
s: DATA — Data+Stack size (kb)
The amount of physical memory devoted to other than executable code, also known as the ‘data resident set’ size or DRS.
t: SHR — Shared Mem size (kb)
The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.
u: nFLT — Page Fault count
The number of major page faults that have occurred for a task. A page fault occurs when a process attempts to read from or write to a virtual page that is not currently present in its address space. A major page fault is when disk access is involved in making that page available.
v: nDRT — Dirty Pages count
The number of pages that have been modified since they were last written to disk. Dirty pages must be written to disk before the corresponding physical memory location can be used for some other virtual page.
w: S — Process Status
The status of the task which can be one of:
‘D’ = uninterruptible sleep
‘R’ = running
‘S’ = sleeping
‘T’ = traced or stopped
‘Z’ = zombieTasks shown as running should be more properly thought of as ‘ready to run’ — their task_struct is simply represented on the Linux run-queue. Even without a true SMP machine, you may see numerous tasks in this state depending on top’s delay interval and nice value.
x: Command — Command line or Program name
Display the command line used to start a task or the name of the associated program. You toggle between command line and name with ‘c’, which is both a command-line option and an interactive command.When you’ve chosen to display command lines, processes without a command line (like kernel threads) will be shown with only the program name in parentheses, as in this example:( mdrecoveryd )

Either form of display is subject to potential truncation if it’s too long to fit in this field’s current width. That width depends upon other fields selected, their order and the current screen width.

Note: The ‘Command’ field/column is unique, in that it is not fixed-width. When displayed, this column will be allocated all remaining screen width (up to the maximum 512 characters) to provide for the potential growth of program names into command lines.

y: WCHAN — Sleeping in Function
Depending on the availability of the kernel link map (‘System.map’), this field will show the name or the address of the kernel function in which the task is currently sleeping. Running tasks will display a dash (‘-‘) in this column.Note: By displaying this field, top’s own working set will be
increased by over 700Kb. Your only means of reducing that overhead
will be to stop and restart top.
z: Flags — Task Flags
This column represents the task’s current scheduling flags which are expressed in hexadecimal notation and with zeros suppressed. These flags are officially documented in . Less formal documentation can also be found on the ‘Fields select’ and ‘Order fields’ screens.

.

Interactive commands

While top is running you may issue some options that will interact immediately with top these options are:

h
Help, displays a summary of command that will modify the behavior of top
k
Kills a process, you will be able to kill only your own processes, unless you are running top as root
n
Once this command is entered top will ask you how many lines you want on your screen, if you enter 0 top will display as much as it can
q
Exits top
r
Change the priority of a process, as well as with k you will only be able to act on your own processes unless you are root
W
Writes the current configuration to your personal configuration file, which is $HOME/.toprc

1) ps = report process status

ps : typing ps alone would list the current running processes

ps -u username : to search processes run by a certain user

ps ag : to get information about all running process

ps aux : to display the owner of the processes along with the processes

ps ax | grep  processname : to see if a particular process is running or not

.

2) ptree : to display a tree of processeses

pstree -p | less : also display the pid of the process

.

3) pgrep : look up or signal processes based on name and other attributes

pgrep sshd : to list the processes called sshd

pgrep -u root sshd : will only list the processes called sshd AND owned by root

pgrep -u root,daemon : will list the processes owned by root OR daemon

.

4) pidof – find the process ID of a running program

pidof emacs – list the process id for emacs

pgrep and pidof is much the same except that grep is more powerful it has more options (check man pages for both)

.

5) pkill = kill all processes matching the search text

pkill sshd : kill all sshd processes

pkill -9 –u userlogin : kill a process owned by a particular user

.

6) kill = Send a signal to terminate one or more process IDs

kill 2345 : terminate process 2345 (to list process id use ps)

kill -9 2345 : if kill 2345 doesnt work, use kill -9 2345 to force it

.

7) killall = kill all processes by name

killall nautilus

Unlike the kill command, it is not necessary to first try to find the PID(s) for nautilus. And if there are multiple instances of nautilus running, all will be terminated immediately.

killall is similar to pkill


1) useradd = create a new user or updates default new user information

Usage:

useradd login options

Examples:

# useradd usr1 — Add new user usr1 with default settings

# useradd usr1 -d /home/usr1 — Create the new user’s home dir in /home

# useradd usr1 -e 2009-04-30 — From 30/4/2009 the user acc will be disabled.

# useradd usr1 -f 6 — After passwd expires, system will allow the user to login for 6 days with a warning to change his passwd.

# useradd usr1 -g staff — Set user1 group as staff

# useradd usr1 -p $1$d8 — Create the usr1 with the given encrypted password. For No passwd, acc disabled.

# useradd usr1 -s /bin/csh — Set C Shell as the default login shell for the usr1

.

2) userdel = delete a user account and user’s related files 

userdel loginname – delete the user account

userdel -r loginname - delete the user account together with user’s home directory and all files inside it

userdel -f loginname – for the deletion even the user is still log in, it will force to delete the user’s home directory and files as well, this option is dangerous use with caution

.

3) usermod = to modify a users account

Examples:

# usermod -d /home2/usr1 usr1 — Create the new home Dir for usr1 in /home2 & Move old Dir contents to this Dir.

# usermod -e 2005-04-30 usr1 — From 30/4/2005 the usr1 acc will be disabled.

# usermod -f 6 usr1 — After passwd expires, system will allow the user to login for 6 days with a warning to change his passwd.

# usermod -g prof usr1
— Set usr1’s initial group as prof.

# usermod -p $1$d8 usr1 — Set the new passwd for the usr1

# usermod -s /bin/bash usr1 — Set Bash as the default login shell for the usr1.

# usermod -L usr1 — Lock a user’s password.

# usermod -U usr1 — Unlock a user’s password.

.

4) groupadd = create a new group

# groupadd sales

.

5) last = show listing of last logged in users 

Examples:

$ last — List all entries.

$ last tty6 — List, who are all logged thru tty6.

$ last bharathi — List all entries of bharathi (user).

$ last -6 — List only last 6 entries.

$ last -R
— Don’t show the Host Name field in output.

$ last -a – Show Host Name field in last column.

$ last -i – Show IP Number for Remote host.

$ last -t 20080818120000 — List all entries upto Aug 18 12:00:00 2008

.

6) lastb = display a list of recent bad login attempts (from the /var/log/btmp file). Accepts the same option flags and arguments as last


1) Tab key = to complete a file name. very useful for long file name

ls <enter>

finance-report-june-2008  sales-report-june-2008  engineering-report-june-2008

if i want to open finance report i can just type

more fi<tab>, it will auto complete the filename become more finance-report-june-2008

.

2) Ctrl+R = to search for a keyword in history

.

3) !string and  !num = to perform previous action in history

Example for !string

!ssh <enter>

it will perform previous command in history ssh 122.456.789.123 -l mylogin

.

Example for !num

type history to see all the previous commands, you can see for each command is assigned an id

!54 <enter> ( ! followed by the id number)

it will perform the command in history with id number 54

.

4) Tilde (~) = shortcut to home directory

cat ~/.bash_profile

to refer to your own directory

cat ~julie/public_html

to refer to another user’s home directory

.

5) Command editing Tricks

There are many but i find these are the most useful

Ctrl + a = moves to beginning of line

Ctrl + e = moves to end of line

Ctrl + w = deletes the word before the cursor

Ctrl + u = deletes to beginning of line

Ctrl + k = deletes to end of line

Ctrl + y = undo a deletion

Alt + b = moves to beginning of line word by word

Alt + f = moves to end of line word by word

1) su = become super user or another user

Examples:

I already login as a normal user. To switch to root I can type

su <enter> : it will ask for root password then will log in to root account but maintaining the user environment

su root <enter> : same as above

su – <enter> : after entering the root password it will log in to root account with root environment (processing all login scripts)

su – david <enter> : login to david’s account with david’s environment

The difference between su and su – is one will maintain the user environment while the other will use the new user environment

.

2) sudo = execute a command as another user, as specified in the sudoers file.

a) by default, sudo requires that users authenticate themselves with a password (not root password)

b) sudo determines who is an authorized user by consulting the file /etc/sudoers. the login access have to be created by root user in this file

c) basically sudo is used to give access to superusers to do administrative job without them login using root account, so monitoring of each task done by many administrators can be recorded properly

Examples:

sudo more /etc/sudoers : in order to view a content where the user has no access, he can use sudo

sudo shutdown -h now : to shutdown a machine

using sudo is by just adding the word sudo in front of any command that requires root/superuser privileges

.

3) wc = wc displays a count of lines, words, and characters in a file

Examples:

wc resignationletter.txt

output is : 10   52   121  resignationletter.txt

10 = lines,  52 = words, 121 = characters

wc -w resignationletter.txt

the command above is to get only the word count

wc -c filename : to get the character count

wc -l filename : to get the number of lines in the document

.

4) cut = takes a vertical slice of a file, printing only the specified columns or fields

Eg 1 : For Findind List of users available on your linux machine
( see the previous post)

cut -d : -f 1 /etc/passwd

/etc/passwd is the file about user information , the general format of this file is

username:password:userid:groupid:userdirectory:usershell
or
antony:x:103:105:/home/antony:/bin/sh

x means password is in /etc/shadow field

now i want ot retrieve the all username and their userid from that file
the command is

cut -d : -f 1 3 /etc/passwd

cut – command name
-d – delimiter (:)
-f – field numbers seperated by space (1 -username , 3 – userid)

Eg 2: the same like we can retrieve the groups details from /etc/groups

cut -d : -f 1 /etc/groups

taken from http://linuxforadmin.blogspot.com/2007/08/cut-command.html

.

5) diff = displays two files and prints the lines that are different

Examples:

diff filenew.txt fileold.txt : use to compare the different between the two files.

more examples at http://lowfatlinux.com/linux-compare-files-diff.html

1) chown :  change file owner and group

To check the ownership of a file or directory use ls -l

Usage: chown [-Rcfv] newowner filenames/directory. Take note only root can change the ownership.

Example:

chown linda file.txt

This will cause file.txt to now be owned by linda.

chown -R abu:sales /home/account/

This is going to make all files inside /home/account/ and its subdirectories to belong to abu and to be associated with the group sales. -R means include all subdirectories

.

2) chgrp : change group ownership

Usage : chgrp [-Rcfv] groupname foo.txt

Example:

chgrp marketing file.txt - to change the group specified to a certain document

chgrp oracle /usr/database – to change the group specified to a certain directory

chgrp -R marketing /sales/2008 – to change the group specified to a certain directory recursively

.

3) chmod : to change the permissions of a file or directory. Use ls -l to see the permission settings.

Below is how the permission is assigned.

rwx rwx rwx = 111 111 111
rw- rw- rw- = 110 110 110
rwx --- --- = 111 000 000

and so on...

rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4

For example, if we wanted to set some_file to have read and write permission for the owner, but wanted to keep the file private from others, we would:

chmod 600 some_file

Here is a table of numbers that covers all the common settings. The ones beginning with “7” are used with programs (since they enable execution) and the rest are for other kinds of files.

Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
755 (rwxr-xr-x) The file’s owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
700 (rwx——) The file’s owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
666 (rw-rw-rw-) All users may read and write the file.
644 (rw-r–r–) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
600 (rw——-) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.

Here are some useful settings for directories:

Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.
755 (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.
700 (rwx——) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.

taken from http://www.linuxcommand.org/lts0070.php

chmod Shortcuts

Who

The “who” is a list of letters that specifies whom you’re going to be giving permissions to. These may be specified in any order.

u The user who owns the file (this means “you.”)
g The group the file belongs to.
o The other users
a all of the above (an abbreviation for ugo)

.

Permissions

Of course, the permissions are the same letters that you see in the directory listing:

r Permission to read the file.
w Permission to write (or delete) the file.
x Permission to execute the file, or, in the case of a directory, search it.

Let’s say we have these files:

-rwxrwxrwx  joe  acctg  wordmatic
-r--r--r--  joe  acctg  calcmatic

We’d like to remove write permission for the group and others on wordmatic, and add write and execute permission for all users on calcmatic.

Rather than try to figure out what the new permissions are and do these commands:

   chmod go=rx wordmatic
   chmod a=rwx calcmatic

The chmod command literally lets us add and subtract permissions from an existing set by using + or - instead of =.

Thus, we can take away the first file’s write permission for the group and others with this command:

   chmod go-w wordmatic

…and we can add write and execute permission to the second file for all users with:

   chmod a+wx calcmatic

Copying Permissions

As one other shortcut, it’s possible to tell chmod “give users of one class the same permissions that some other class has.”

Let’s say we have these files:

d------rwx  joe  acctg  sales
-rw-r--r--  joe  acctg  info.dat

The other users have full permissions on the sales directory. We’d like to say “ the user and group should be assigned (=) the permissions belonging to others.” That translates to:

   chmod ug=o

Similarly, to make info.dat readable and writable to the group, we can say:

   chmod g=u info.dat

(you can read this as “the group is assigned (=) the permissions currently held by the user.”)

You may also use + and – to add and subtract the permissions that currently belong to a different class of user.

You can’t mix the standard permissions (r, w, and x) with the coyping shortcuts. chmod will protest if you give it something like this:

   chmod g=wu info.dat

Taken from http://catcode.com/teachmod/chmod_cmd.html

.

4) umask = set file creation mask

The ‘UMASK‘ is the default permission setting that is applied to your files and directories when they are created. After files and directories are created, the chmod command can be used to change the permissions to allow or disallow access as before. The UMASK is set when you login to a UNIX machine.

It is, however, possible to change your UMASK and put the UMASK in your login files so that your default permissions are always set for files when you create them.

Just like chmod, a umask works on a number. However, instead of the numbers being ADDED like chmod, with a umask the numbers are SUBTRACTED from 7.

So from chmod -

Read – 4
Write – 2
Execute – 1

If a user wants all directories to be created with rwxr-xr-x, that is

Owner == Read, Write, Execute == 7 – 4 – 2 – 1 == 0
Group == Read, Execute == 7 – 4 – 1 == 2
Others == Read, Execute == 7 – 4 – 1 == 2

Then the umask would be 022

There is one important difference with the UMASK and files and directories – the execute part will be set on directories, but they have to be manually changed on files after the file has been created. The read and write parts remain the same.

You can change your default UMASK using the command ‘umask’. For example, the command:

umask 022

Will change your umask to the permissions stated above.

To make your new umask be the default for your next login, you will have to edit your .login file and place the umask command down the bottom of that file. The next time you login to your UNIX system, your default umask will be set to that which you have specified in your .login file.

For redhat, to change permanently modify .bashrc and include umask nnn in one of the lines.

Follow

Get every new post delivered to your Inbox.