Wednesday, 31 May 2017

What is PS2 - Continuation Interactive Prompt in Linux

A very long command can be broken down to multiple lines by giving \ at the
end of the line. The default interactive prompt for a multi-line command is
“> “. Let us change this default behavior to display “continue->” by using PS2
environment variable as shown below.

ramesh@dev-db ~> myisamchk --silent --force --fast --
update-state \
> --key_buffer_size=512M --sort_buffer_size=512M \
> --read_buffer_size=4M --write_buffer_size=4M \
> /var/lib/mysql/bugs/*.MYI

[Note: This uses the default ">" for continuation
prompt]

ramesh@dev-db ~> export PS2="continue-> "
ramesh@dev-db ~> myisamchk --silent --force --fast --
update-state \
continue-> --key_buffer_size=512M --
sort_buffer_size=512M \
continue-> --read_buffer_size=4M --write_buffer_size=4M
\
continue-> /var/lib/mysql/bugs/*.MYI
[Note: This uses the modified "continue-> " for

continuation prompt]
I found it very helpful and easy to read, when I break my long commands into
multiple lines using \. I have also seen others who don’t like to break-up long
commands.

What is PS1 - Default Interaction Prompt

The default interactive prompt on your Linux can be modified as shown below
to something useful and informative. In the following example, the default
PS1 was “\s-\v\$”, which displays the shell name and the version number. Let
us change this default behavior to display the username, hostname and
current working directory name as shown below.

-bash-3.2$
export PS1="\u@\h \w> "
ramesh@dev-db ~> cd /etc/mail
ramesh@dev-db /etc/mail>
[Note: Prompt changed to "username@hostname current-
dir>" format]
Following PS1 codes are used in this example:
o \u – Username
o \h – Hostname
o \w - Full pathname of current directory. Please note that when
 you are in the home directory, this will display only ~ as shown
above
Note that there is a space at the end in the value of PS1. Personally, I prefer
a space at the end of the prompt for better readability.
Make this setting permanent by adding export PS1=”\u@\h \w> ” to either
.bash_profile (or) .bashrc as shown below.

47
Linux 101 Hacks
www.thegeekstuff.com
ramesh@dev-db ~> vi ~/.bash_profile
ramesh@dev-db ~> vi ~/.bashrc
[Note: Add export PS1="\u@\h \w> " to one of the above
files]
Refer to the next chapter for several practical examples of PS1 usage in
detail.

How to Display total connect time of users in Linux

Ac command will display the statistics about the user’s connect time.
Connect time for the current logged in user
With the option –d, it will break down the output for the individual days. In
this example, I’ve been logged in to the system for more than 6 hours today.

On Dec 1st, I was logged in for about 1 hour.
$ ac –d
Dec 1
Dec 2
Dec 3
Dec 4
Today
total
total
total
total
total
1.08
0.99
3.39
4.50
6.10
Connect time for all the users
To display connect time for all the users use –p as shown below. Please note
that this indicates the cumulative connect time for the individual users.
$ ac -p
john
madison
sanjay
nisha
3.64
0.06
88.17
105.92

ramesh
total 309.21
111.42
Connect time for a specific user
To get a connect time report for a specific user, execute the following:
$ ac -d sanjay
Jul 2
Aug 25
Sep 3
Sep 4
Dec 24
Dec 29
Today
total
total
total
total
total
total
total
12.85
5.05
1.03
5.37
8.15
1.42
2.95

How to use Diff Command in Linux

diff command compares two different files and reports the difference. The
output is very cryptic and not straight forward to read.

Syntax: diff [options] file1 file2

What was modified in my new file when compare to my old file?
The option -w in the diff command will ignore the white space while
performing the comparison.
In the following diff output:

o The lines above ---, indicates the changes happened in first file in the
 diff command (i.e name_list.txt).

o The lines below ---, indicates the changes happened to the second
 file in the diff command (i.e name_list_new.txt). The lines that
belong to the first file starts with < and the lines of second file starts
with >.

# diff -w name_list.txt name_list_new.txt

2c2,3
< John Doe
---
> John M Doe
> Jason Bourne

How to use Stat Command in Linux

Stat command can be used either to check the status/properties of a single
file or the filesystem.
Display statistics of a file or directory.

$ stat /etc/my.cnf
File:
Size:
Device:
Access:
(
0/
Access:
Modify:
Change:
`/etc/my.cnf'
346 Blocks: 16 IO Block: 4096
regular file
801h/2049d
Inode: 279856
Links: 1
(0644/-rw-r--r--) Uid: (
0/
root)
Gid:
root)
2009-01-01 02:58:30.000000000 -0800
2006-06-01 20:42:27.000000000 -0700
2007-02-02 14:17:27.000000000 -0800
$ stat /home/ramesh
File: `/home/ramesh'
Size: 4096
Blocks: 8
IO Block:
4096
directory
Device: 803h/2051d
Inode: 5521409
Links: 7
Access: (0755/drwxr-xr-x) Uid: ( 401/ramesh)
Gid: (
401/ramesh)
Access: 2009-01-01 12:17:42.000000000 -0800
Modify: 2009-01-01 12:07:33.000000000 -0800
Change: 2009-01-09 12:07:33.000000000 -0800

Display the status of the filesystem using option –f
$ stat -f /
File:
ID:
Blocks:
1876998
Inodes:
"/"
0
Namelen: 255
Type: ext2/ext3
Total: 2579457
Free: 2008027
Available:
Size: 4096
Total: 1310720
Free: 1215892

How to use Cut Command in Linux

Cut command can be used to display only specific columns from a text file or
other command outputs.
Following are some of the examples.

Display the 1st field (employee name) from a colon delimited file

$ cut -d: -f 1 names.txt
Emma Thomas
Alex Jason
Madison Randy
Sanjay Gupta
Nisha Singh

Display 1st and 3rd field from a colon delimited file

$ cut -d: -f 1,3 names.txt
Emma Thomas:Marketing
Alex Jason:Sales
Madison Randy:Product Development
Sanjay Gupta:Support
Nisha Singh:Sales

Display only the first 8 characters of every line in a file

$ cut -c 1-8 names.txt
Emma Tho
Alex Jas
Madison
Sanjay G
Nisha Si

Misc Cut command examples
o cut -d: -f1 /etc/passwd Displays the unix login names for all the users
 in the system.
o free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 Displays the total
 memory available on the system.

How use Uniq Command in Linux

Uniq command is mostly used in combination with sort command, as uniq
removes duplicates only from a sorted file. i.e In order for uniq to work, all
the duplicate entries should be in the adjacent lines. Following are some
common examples.

1. When you have an employee file with duplicate entries, you can do the
following to remove duplicates.
$ sort namesd.txt | uniq
$ sort –u namesd.txt

2. If you want to know how many lines are duplicates, do the following. The
first field in the following examples indicates how many duplicates where
found for that particular line. So, in this example the lines beginning with
Alex and Emma were found twice in the namesd.txt file.
$ sort namesd.txt | uniq –c
2
2
1
1
1
Alex Jason:200:Sales
Emma Thomas:100:Marketing
Madison Randy:300:Product Development
Nisha Singh:500:Sales
Sanjay Gupta:400:Support
3. The following displays only the entries that are duplicates.
$ sort namesd.txt | uniq –cd
2 Alex Jason:200:Sales
2 Emma Thomas:100:Marketing

How to use Sort Command in Linux

Sort command sorts the lines of a text file. Following are several practical
examples on how to use the sort command based on the following sample text
file that has employee information in the format:

employee_name:employee_id:department_name.

$ cat names.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales

Sort a text file in ascending order
$ sort names.txt
Alex Jason:200:Sales
Emma Thomas:100:Marketing
Madison Randy:300:Product Development
Nisha Singh:500:Sales
Sanjay Gupta:400:Support
Sort a text file in descending order

$ sort -r names.txt
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
Madison Randy:300:Product Development
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Sort a colon delimited text file on 2nd field (employee_id)

$ sort -t: -k 2 names.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
Sort a tab delimited text file on 3rd field (department_name) and
suppress duplicates

$ sort -t: -u -k 3 names.txt
Emma Thomas:100:Marketing
Madison Randy:300:Product Development
Alex Jason:200:Sales
Sanjay Gupta:400:Support
Sort the passwd file by the 3rd field (numeric userid)

$ sort -t: -k 3n /etc/passwd | more
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sort /etc/hosts file by ip-addres

$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.100.101 dev-db.thegeekstuff.com dev-db
192.168.100.102 prod-db.thegeekstuff.com prod-db
192.168.101.20 dev-web.thegeekstuff.com dev-web
192.168.101.21 prod-web.thegeekstuff.com prod-web
Combine sort with other commands

o ps –ef | sort : Sort the output of process list
o ls -al | sort +4n : List the files in the ascending order of the file-
 size. i.e sorted by 5th filed and displaying smallest files first.
o ls -al | sort +4nr : List the files in the descending order of the
 file-size. i.e sorted by 5th filed and displaying largest files first.

How to use Xargs Command in Linux

xargs is a very powerful command that takes output of a command and pass it
as argument of another command. Following are some practical examples on
how to use xargs effectively.

1. When you are trying to delete too many files using rm, you may get error
message: /bin/rm Argument list too long – Linux. Use xargs to avoid this
problem.
find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

2. Get a list of all the *.conf file under /etc/. There are different ways to get
the same result. Following example is only to demonstrate the use of xargs.
The output of the find command in this example is passed to the ls –l one by
one using xargs.

# find /etc -name "*.conf" | xargs ls –l

3. If you have a file with list of URLs that you would like to download, you can
use xargs as shown below.

# cat url-list.txt | xargs wget –c

4. Find out all the jpg images and archive it.

# find / -name *.jpg -type f -print | xargs tar -cvzf
images.tar.gz

5. Copy all the images to an external hard-drive.
# ls *.jpg | xargs -n1 -i cp {} /external-hard-
drive/directory

How to use Change the Case in Linux

Convert a file to all upper-case

$ cat employee.txt
100
200
300
400
Jason Smith
John Doe
Sanjay Gupta
Ashok Sharma
$ tr a-z A-Z < employee.txt
100 JASON SMITH
200 JOHN DOE

400 ASHOK SHARMA
Convert a file to all lower-case

$ cat department.txt
100
200
300
400
FINANCE
MARKETING
PRODUCT DEVELOPMENT
SALES

$ tr A-Z a-z < department.txt
100
200
300
400
finance
marketing
product development
sales

How to use Join Command in Linux

Join command combines lines from two files based on a common field.
In the example below, we have two files – employee.txt and salary.txt. Both
have employee-id as common field.
 So, we can use join command to combine

the data from these two files using employee-id as shown below.

$ cat employee.txt
100
200
300
400
Jason Smith
John Doe
Sanjay Gupta
Ashok Sharma

$ cat bonus.txt
100
200
300
400
$5,000
$500
$3,000
$1,250

$ join employee.txt bonus.txt
100
200
300
400
Jason Smith $5,000
John Doe $500
Sanjay Gupta $3,000
Ashok Sharma $1,250

How to Suppress Standard Output and Error Message in Linux

Sometime while debugging a shell script, you may not want to see either the
standard output or standard error message. Use /dev/null as shown below for
suppressing the output.

Suppress standard output using > /dev/null
This will be very helpful when you are debugging shell scripts, where you
don’t want to display the echo statement and interested in only looking at
the error messages.

# cat file.txt > /dev/null
# ./shell-script.sh > /dev/null

Suppress standard error using 2> /dev/null
This is also helpful when you are interested in viewing only the standard
output and don’t want to view the error messages.

# cat invalid-file-name.txt 2> /dev/null
# ./shell-script.sh 2> /dev/null

How to use Find Command in linux

find is frequently used command to find files in the UNIX filesystem based on
numerous conditions. Let us review some practice examples of find command.

Syntax: find [pathnames] [conditions]
How to find files containing a specific word in its name?
The following command looks for all the files under /etc directory with mail
in the filename.

# find /etc -name "*mail*"
How to find all the files greater than certain size?
The following command will list all the files in the system greater than
100MB.

# find / -type f -size +100M
How to find files that are not modified in the last x number of days?
The following command will list all the files that were modified more than 60
days ago under the current directory.

# find . -mtime +60
How to find files that are modified in the last x number of days?
The following command will list all the files that were modified in the last
two days under the current directory.

# find . –mtime -2
How to delete all the archive files with extension *.tar.gz and
greater than 100MB?
Please be careful while executing the following command as you don’t want
to delete the files by mistake. The best practice is to execute the same
command with ls –l to make sure you know which files will get deleted when
you execute the command with rm.

# find / -type f -name *.tar.gz -size +100M -exec ls -l {} \;
# find / -type f -name *.tar.gz -size +100M -exec rm -f {} \;

How to archive all the files that are not modified in the last x
number of days?

The following command finds all the files not modified in the last 60 days
under /home/jsmith directory and creates an archive files under /tmp in the
format of ddmmyyyy_archive.tar.

# find /home/jsmith -type f -mtime +60 | xargs tar -cvf
/tmp/`date '+%d%m%Y'_archive.tar`
On a side note, you can perform lot of file related activities (including finding
files) using midnight commander GUI, a powerful text based file manager for
Unix.

How to use Grep Command in linux

grep command is used to search files for a specific text. This is incredibly
powerful command with lot of options.

Syntax: grep [options] pattern [files]
How can I find all lines matching a specific keyword on a file?
In this example, grep looks for the text John inside /etc/passwd file and
displays all the matching lines.

# grep John /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash
Option -v, will display all the lines except the match. In the example below,
it displays all the records from /etc/password that doesn't match John.
Note: There are several lines in the /etc/password that doesn’t contain the
word John. Only the first line of the output is shown below.

# grep -v John /etc/passwd
jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash
How many lines matched the text pattern in a particular file?
In the example below, it displays the total number of lines that contains the
text John in /etc/passwd file.

# grep -c John /etc/passwd
2
You can also get the total number of lines that did not match the specific
pattern by passing option -cv.

# grep -cv John /etc/passwd
39
How to search a text by ignoring the case?
Pass the option -i (ignore case), which will ignore the case while searching.

# grep -i john /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash

How do I search all subdirectories for a text matching a specific
pattern?

Use option -r (recursive) for this purpose. In the example below, it will search
for the text "John" by ignoring the case inside all the subdirectories under
/home/users.

This will display the output in the format of "filename: line that matching the
pattern". You can also pass the option -l, which will display only the name of
the file that matches the pattern.

# grep -ri john /home/users
/home/users/subdir1/letter.txt:John, Thanks for your
contribution.
/home/users/name_list.txt:John Smith

/home/users/name_list.txt:John Doe
# grep -ril john /root
/home/users/subdir1/letter.txt
/home/users/name_list.txt

How to SSH Session Statistics using SSH Escape Character in Linux

To get some useful statistics about the current ssh session, do the following.

This works only on SSH2 client.
1. Login to remotehost from localhost.
localhost$ ssh -l jsmith remotehost

2. On the remotehost, type ssh escape character ~ followed by s as shown
below. This will display lot of useful statistics about the current SSH
connection.
remotehost$ [Note: The ~s is not visible on the
command line when you type.]
remote host: remotehost
local host: localhost
remote version: SSH-1.99-OpenSSH_3.9p1
local version: SSH-2.0-3.2.9.1 SSH Secure
Shell (non-commercial)
compressed bytes in: 1506
uncompressed bytes in: 1622
compressed bytes out: 4997
uncompressed bytes out: 5118
packets in: 15
packets out: 24
rekeys: 0
Algorithms:
Chosen key exchange algorithm: diffie-hellman-
group1-sha1
Chosen host key algorithm: ssh-dss
Common host key algorithms: ssh-dss,ssh-rsa
Algorithms client to server:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib

Algorithms server to client:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
localhost$
Additional SSH Info
On a side note, to setup SSH key based authentication, refer openSSH and
SSH2 tutorials.

How to Toggle SSH Session using SSH Escape Character in linux

When you’ve logged on to the remotehost using ssh from the localhost, you
may want to come back to the localhost to perform some activity and go back

to remote host again. In this case, you don’t need to disconnect the ssh
session to the remote host. Instead, follow the steps below.
1. Login to remotehost from localhost:
localhost$ ssh -l jsmith remotehost

2. Now you are connected to the remotehost:
remotehost$

3. To come back to the localhost temporarily, type the escape character ~
and Control-Z.
When you type ~ you will not see that immediately on the screen until you
press <Control-Z> and press enter. So, on the remotehost in a new line enter
the following key strokes for the below to work: ~<Control-Z>
remotehost$ ~^Z
[1]+ Stopped ssh -l jsmith remotehost
localhost$

4. Now you are back to the localhost and the ssh remotehost client session
runs as a typical UNIX background job, which you can check as shown below:
localhost$ jobs
[1]+ Stopped ssh -l jsmith remotehost

5. You can go back to the remote host ssh without entering the password
again by bringing the background ssh remotehost session job to foreground on
the localhost.
localhost$ fg %1
ssh -l jsmith remotehost
remotehost$

How to Debug SSH Client Session in linux

Sometimes it is necessary to view debug messages to troubleshoot any SSH
connection issues.

 pass -v (lowercase v) option to the ssh as shown below to
view the ssh debug messages.


Example without SSH client debug message:

localhost$ ssh -l jsmith remotehost.example.com
warning: Connecting to remotehost.example.com failed:
No address associated to the name

Example with SSH client debug message:
locaclhost$ ssh -v -l jsmith remotehost.example.com
debug:
SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
Metaconfig parsing stopped at line 3.

debug:
SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
Setting variable ‘VerboseMode’ to ‘FALSE’.

debug:
SshConfig/sshconfig.c:3130/ssh_config_read_file_ext:
Read 17 params from config file.
debug: Ssh2/ssh2.c:1707/main: User config file not
found, using defaults. (Looked for
‘/home/jsmith/.ssh2/ssh2_config’)
debug: Connecting to remotehost.example.com, port 22...
(SOCKS not used)
warning: Connecting to remotehost.example.com failed:
No address associated to the name

how to Login to Remote Host using SSH in linux

The First time when you login to a remotehost from a localhost, it will display

the host key not found message and you can give “yes” to continue. The host
key of the remote host will be added under .ssh2/hostkeys directory of your
home directory, as shown below.
localhost$ ssh -l jsmith remotehost.example.com
Host key not found from database.
Key fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-
tuxum
You can get a public key’s fingerprint by running
% ssh-keygen -F publickey.pub

on the keyfile.
Are you sure you want to continue connecting (yes/no)? Yes
Host key saved to
/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub
host key for remotehost.example.com, accepted by jsmith Mon
May 26 2008 16:06:50 -0700
jsmith@remotehost.example.com password:
remotehost.example.com$
The Second time when you login to the remote host from the localhost, it will
prompt only for the password as the remote host key is already added to the
known hosts list of the ssh client.
localhost$ ssh -l jsmith remotehost.example.com
jsmith@remotehost.example.com password:
remotehost.example.com$
For some reason, if the host key of the remote host is changed after you
logged in for the first time, you may get a warning message as shown below.
This could be because of various reasons such as:
o Sysadmin upgraded/reinstalled the SSH server on the remote host
o Someone is doing malicious activity etc.,
The best possible action to take before saying “yes” to the message below, is
to call your sysadmin and identify why you got the host key changed message
and verify whether it is the correct host key or not.
localhost$ ssh -l jsmith remotehost.example.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
WARNING: HOST IDENTIFICATION HAS CHANGED!
@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-

middle attack)!
It is also possible that the host key has just been changed.
Please contact your system administrator.
Add correct host key to
“/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pu
b”
to get rid of this message.
Received server key’s fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-
tuxum

You can get a public key’s fingerprint by running
% ssh-keygen -F publickey.pub
on the keyfile.

Agent forwarding is disabled to avoid attacks by corrupted
servers.

Are you sure you want to continue connecting (yes/no)? yes
Do you want to change the host key on disk (yes/no)? yes
Agent forwarding re-enabled.
Host key saved to

/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub
host key for remotehost.example.com, accepted by jsmith Mon
May 26 2008 16:17:31 -0700
jsmith @remotehost.example.com’s password:
remotehost$

How to Identify SSH Client Version in linux

Sometimes it may be necessary to identify the SSH client that you are
currently running and it’s corresponding version number.

Use ssh –V to
identify the version number. Please note that Linux comes with OpenSSH.

The following example indicates that this particular system is using OpenSSH:

$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
The following example indicates that this particular system is using SSH2:

$ ssh -V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version)
on i686-pc-linux-gnu

How to Display Future Date and Time

Following examples shows how to display a future date and time.

$ date
Thu Jan
1 08:30:07 PST 2009
$ date --date='3 seconds'
Thu Jan 1 08:30:12 PST 2009
$ date --date='4 hours'
Thu Jan 1 12:30:17 PST 2009
$ date --date='tomorrow'
Fri Jan 2 08:30:25 PST 2009
$ date --date="1 day"
Fri Jan 2 08:30:31 PST 2009
$ date --date="1 days"
Fri Jan 2 08:30:38 PST 2009
$ date --date="2 days"
Sat Jan 3 08:30:43 PST 2009
$ date --date='1 month'
Sun Feb 1 08:30:48 PST 2009
$ date --date='1 week'
Thu Jan 8 08:30:53 PST 2009
$ date --date="2 months"
Sun Mar 1 08:30:58 PST 2009
$ date --date="2 years"
Sat Jan 1 08:31:03 PST 2011
$ date --date="next day"
Fri Jan 2 08:31:10 PST 2009

$ date --date="-1 days ago"
Fri Jan 2 08:31:15 PST 2009
$ date --date="this Wednesday"
Wed Jan 7 00:00:00 PST 2009
23

How to Display Current Date and Time in a Specific Format in linux

Following are different ways of displaying the current date and time in
various formats:


$ date
Thu Jan

1 08:19:23 PST 2009
$ date --date="now"
Thu Jan 1 08:20:05 PST 2009
$ date --date="today"
Thu Jan 1 08:20:12 PST 2009
$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s
18001
$ date '+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S'
Current Date: 01/01/09
Current Time:08:21:41
$ date +"%d-%m-%Y"
01-01-2009
$ date +"%d/%m/%Y"
01/01/2009
$ date +"%A,%B %d %Y"
Thursday,January 01 2009

Following are the different format options you can pass to the date
command:

o %D date (mm/dd/yy)
o %d day of month (01..31)
o %m month (01..12)
o %y last two digits of year (00..99)
o %a locale’s abbreviated weekday name (Sun..Sat)
o %A locale’s full weekday name, variable length
   (Sunday..Saturday)
o %b locale’s abbreviated month name (Jan..Dec)

o %B locale’s full month name, variable length
   (January..December)
o %H hour (00..23)
o %I hour (01..12)
o %Y year (1970...)

How to Set Hardware Date and Time in linux

Before setting the hardware date and time, make sure the OS date and time
is set appropriately as shown in the hack#7.

Set the hardware date and time based on the system date as shown below:

# hwclock –systohc
# hwclock --systohc –utc
Use hwclock without any parameter, to view the current hardware date and
time:

# hwclock
Check the clock file to verify whether the system is set for UTC:

# cat /etc/sysconfig/clock
ZONE="America/Los_Angeles"
UTC=false
ARC=false

How to Set System Date and Time in linux

To change the system date use:

# date {mmddhhmiyyyy.ss}
o
o
o
o
o
o
mm – Month
dd – Date
hh – 24 hour format
mi – Minutes
yyyy – Year
ss – seconds

For example, to set system date to Jan 31st 2008, 10:19 p.m, 53 seconds
# date 013122192009.53

You can also change system date using set argument as shown below.

# date 013122192009.53
# date +%Y%m%d -s "20090131"
# date -s "01/31/2009 22:19:53"
# date -s "31 JAN 2009 22:19:53"
# date set="31 JAN 2009 22:19:53"
To set the time only:
# date +%T -s "22:19:53"

# date +%T%p -s "10:19:53PM"

How to Use “shopt -s cdspell” to automatically correct mistyped directory names on cd in linux

Use shopt -s cdspell to correct the typos in the cd command automatically as
shown below. If you are not good at typing and make lot of mistakes, this will
be very helpful.
# cd /etc/mall
-bash: cd: /etc/mall: No such file or directory
# shopt -s cdspell
# cd /etc/mall
# pwd
/etc/mail
[Note: By mistake, when I typed mall instead of mail,
cd corrected it automatically]

How to use Set System Date and Time in linux

To change the system date use:

# date {mmddhhmiyyyy.ss}
o
o
o
o
o
o
mm – Month
dd – Date
hh – 24 hour format
mi – Minutes
yyyy – Year
ss – seconds
For example, to set system date to Jan 31st 2008, 10:19 p.m, 53 seconds

# date 013122192009.53

You can also change system date using set argument as shown below.

# date 013122192009.53
# date +%Y%m%d -s "20090131"
# date -s "01/31/2009 22:19:53"
# date -s "31 JAN 2009 22:19:53"
# date set="31 JAN 2009 22:19:53"
To set the time only:
# date +%T -s "22:19:53"

# date +%T%p -s "10:19:53PM"

How to Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

Use shopt -s cdspell to correct the typos in the cd command automatically as
shown below. If you are not good at typing and make lot of mistakes, this will
be very helpful.

# cd /etc/mall
-bash: cd: /etc/mall: No such file or directory
# shopt -s cdspell
# cd /etc/mall
# pwd
/etc/mail

How to Use dirs, pushd and popd to manipulate directory stack


You can use directory stack to push directories into it and later pop directory
from the stack. Following three commands are used in this example.

o dirs: Display the directory stack
o pushd: Push directory into the stack
o popd: Pop directory from the stack and cd to it
Dirs will always print the current directory followed by the content of the
stack. Even when the directory stack is empty, dirs command will still print
only the current directory as shown below.
# popd
-bash: popd: directory stack empty
# dirs
~
# pwd
/home/ramesh
How to use pushd and popd? Let us first create some temporary directories
and push them to the directory stack as shown below.
#
#
#
#
mkdir
mkdir
mkdir
mkdir
/tmp/dir1
/tmp/dir2
/tmp/dir3
/tmp/dir4
# cd /tmp/dir1
# pushd .
# cd /tmp/dir2
# pushd .
# cd /tmp/dir3
# pushd .
# cd /tmp/dir4
# pushd .

# dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
[Note: The first directory (/tmp/dir4) of the dir
command output is always the current directory and not
the content from the stack.]
At this stage, the directory stack contains the following directories:
/tmp/dir4
/tmp/dir3
/tmp/dir2
/tmp/dir1
The last directory that was pushed to the stack will be at the top. When you
perform popd, it will cd to the top directory entry in the stack and remove it
from the stack. As shown above, the last directory that was pushed into the
stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and
remove it from the directory stack as shown below.
# popd
# pwd
/tmp/dir4
[Note: After the above popd, directory Stack Contains:
/tmp/dir3
/tmp/dir2
/tmp/dir1]
# popd
# pwd
/tmp/dir3
[Note: After the above popd, directory Stack Contains:
/tmp/dir2
/tmp/dir1]
# popd
16

# pwd
/tmp/dir2
[Note: After the above popd, directory Stack Contains:
/tmp/dir1]
# popd
# pwd
/tmp/dir1
[Note: After the above popd, directory Stack is empty!]
# popd
-bash: popd: directory stack empty

How to Use “cd -” to toggle between the last two directories


You can toggle between the last two current directories using cd - as shown
below.
# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/subdir1/subdir2/subdir3
# cd -
# pwd
/tmp/very/long/directory/structure/that/is/too/deep
# cd -
# pwd
/tmp/subdir1/subdir2/subdir3
# cd -
# pwd
/tmp/very/long/directory/structure/that/is/too/deep

How to Perform mkdir and cd using a single command

Sometimes when you create a new directory, you may cd to the new directory
immediately to perform some work as shown below.


# mkdir -p /tmp/subdir1/subdir2/subdir3
# cd /tmp/subdir1/subdir2/subdir3
# pwd
/tmp/subdir1/subdir2/subdir3
Wouldn’t it be nice to combine both mkdir and cd in a single command? Add
the following to the .bash_profile and re-login.
$ vi .bash_profile
function mkdircd () { mkdir -p "$@" && eval cd
"\"\$$#\""; }
Now, perform both mkdir and cd at the same time using a single command as
shown below:
13

# mkdircd /tmp/subdir1/subdir2/subdir3
[Note: This creates the directory and cd to it
automatically]
# pwd
/tmp/subdir1/subdir2/subdir3

How to Use cd alias to navigate up the directory effectively

When you are navigating up a very long directory structure, you may be using
cd ..\..\ with multiple ..\’s depending on how many directories you want to go
up as shown below.
# mkdir -p
/tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/very/long/directory/structure/that/is/too/deep
# pwd
/tmp/very/long/directory/structure/that/is/too/deep
# cd ../../../../
10
Linux 101 Hacks
www.thegeekstuff.com
# pwd
/tmp/very/long/directory/structure
Instead of executing cd ../../../.. to navigate four levels up, use one of the
following three alias methods:
Method 1: Navigate up the directory using “..n”
In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3
directory level, ..2 to go up 2 directory level. Add the following alias to your
~/.bash_profile and re-login.
alias
alias
alias
alias
alias
..="cd .."
..2="cd ../.."
..3="cd ../../.."
..4="cd ../../../.."
..5="cd ../../../../.."
# cd
/tmp/very/long/directory/structure/that/is/too/deep
# ..4
[Note: use ..4 to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure/
Method 2: Navigate up the directory using only dots
In the example below, ..... (five dots) is used to go up 4 directory level.
Typing 5 dots to go up 4 directory structure is really easy to remember, as
when you type the first two dots, you are thinking “going up one directory”,
after that every additional dot, is to go one level up. So, use .... (four dots) to
go up 3 directory level and .. (two dots) to go up 1 directory level. Add the
following alias to your ~/.bash_profile and re-login for the ..... (five dots) to
work properly.
alias ..="cd .."

alias
alias
alias
alias

...="cd ../.."
....="cd ../../.."
.....="cd ../../../.."
......="cd ../../../../.."
# cd /tmp/very/long/directory/structure/that/is/too/deep
# .....
[Note: use ..... (five dots) to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure/
Method 3: Navigate up the directory using cd followed by
consecutive dots
In the example below, cd..... (cd followed by five dots) is used to go up 4
directory level. Making it 5 dots to go up 4 directory structure is really easy to
remember, as when you type the first two dots, you are thinking “going up
one directory”, after that every additional dot, is to go one level up. So, use
cd.... (cd followed by four dots) to go up 3 directory level and cd... (cd
followed by three dots) to go up 2 directory level. Add the following alias to
your ~/.bash_profile and re-login for the above cd..... (five dots) to work
properly.
alias
alias
alias
alias
alias
cd..="cd .."
cd...="cd ../.."
cd....="cd ../../.."
cd.....="cd ../../../.."
cd......="cd ../../../../.."
# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd.....
[Note: use cd..... to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure


Method 5: Navigate up the directory using cd followed by number
In the example below, cd4 (cd followed by number 4) is used to go up 4
directory level.
alias
alias
alias
alias
alias
cd1="cd
cd2="cd
cd3="cd
cd4="cd
cd5="cd
.."
../.."
../../.."
../../../.."
../../../../.."

How to Use CDPATH to define the base directory for cd command

Use CDPATH to define the base directory for
cd command

If you are frequently performing cd to subdirectories of a specific parent
directory, you can set the CDPATH to the parent directory and perform cd to
the subdirectories without giving the parent directory path as explained
below.

[ramesh@dev-db ~]# pwd
/home/ramesh
[ramesh@dev-db ~]# cd mail
-bash: cd: mail: No such file or directory
[Note: This is looking for mail directory under current
directory]
[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
/etc/mail
[Note: This is looking for mail under /etc and not
under current directory]
[ramesh@dev-db /etc/mail]# pwd
/etc/mail
To make this change permanent, add export CDPATH=/etc to your
~/.bash_profile

Similar to the PATH variable, you can add more than one directory entry in
the CDPATH variable, separating them with : , as shown below.
export CDPATH=.:~:/etc:/var
This hack can be very helpful under the following situations:
o Oracle DBAs frequently working under $ORACLE_HOME, can set
 the CDPATH variable to the oracle home
o Unix sysadmins frequently working under /etc, can set the
 CDPATH variable to /etc
o Developers frequently working under project directory
 /home/projects, can set the CDPATH variable to /home/projects
o End-users frequently accessing the subdirectories under their
 home directory, can set the CDPATH variable to ~ (home
directory)
 

Tuesday, 30 May 2017

How to Set System Date and Time in Linux / Unix


Set System Date and Time 
 
To change the system date use:
# date {mmddhhmiyyyy.ss}
o
mm – Month
o
dd – Date
o
hh – 24 hour format
o
mi – Minutes
o
yyyy – Year
o
ss – seconds
For example, to set system date to Jan 31
st
2008, 10:19 p.m, 53 seconds
# date 013122192009.53
You can also change system date using set argument as shown below.
# date 013122192009.53
# date +%Y%m%d -s "20090131"
# date -s "01/31/2009 22:19:53"
# date -s "31 JAN 2009 22:19:53"
# date set="31 JAN 2009 22:19:53"
To set the time only:
# date +%T -s "22:19:53"
18
# date +%T%p -s "10:19:53PM"

How to use FIND command in Unix / Linux / AIX

1. Find Files Using Name in Current Directory

Find all the files whose name is tecmint.txt in a current working directory.
# find . -name tecmint.txt
./tecmint.txt

2. Find Files Under Home Directory

Find all the files under /home directory with name tecmint.txt.
# find /home -name tecmint.txt
/home/tecmint.txt

3. Find Files Using Name and Ignoring Case

Find all the files whose name is tecmint.txt and contains both capital and small letters in /home directory.
# find /home -iname tecmint.txt
./tecmint.txt
./Tecmint.txt

4. Find Directories Using Name

Find all directories whose name is Tecmint in / directory.
# find / -type d -name Tecmint
/Tecmint

5. Find PHP Files Using Name

Find all php files whose name is tecmint.php in a current working directory.
# find . -type f -name tecmint.php
./tecmint.php

6. Find all PHP Files in Directory

Find all php files in a directory.
# find . -type f -name "*.php"
./tecmint.php
./login.php
./index.php
Part II – Find Files Based on their Permissions

7. Find Files With 777 Permissions

Find all the files whose permissions are 777.
# find . -type f -perm 0777 -print

8. Find Files Without 777 Permissions

Find all the files without permission 777.
# find / -type f ! -perm 777

9. Find SGID Files with 644 Permissions

Find all the SGID bit files whose permissions set to 644.
# find / -perm 2644

10. Find Sticky Bit Files with 551 Permissions

Find all the Sticky Bit set files whose permission are 551.
# find / -perm 1551

11. Find SUID Files

Find all SUID set files.
# find / -perm /u=s

12. Find SGID Files

Find all SGID set files.
# find / -perm /g=s

13. Find Read Only Files

Find all Read Only files.
# find / -perm /u=r

14. Find Executable Files

Find all Executable files.
# find / -perm /a=x

15. Find Files with 777 Permissions and Chmod to 644

Find all 777 permission files and use chmod command to set permissions to 644.
# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16. Find Directories with 777 Permissions and Chmod to 755

Find all 777 permission directories and use chmod command to set permissions to 755.
# find / -type d -perm 777 -print -exec chmod 755 {} \;

17. Find and remove single File

To find a single file called tecmint.txt and remove it.
# find . -type f -name "tecmint.txt" -exec rm -f {} \;

18. Find and remove Multiple File

To find and remove multiple files such as .mp3 or .txt, then use.
# find . -type f -name "*.txt" -exec rm -f {} \;
OR
# find . -type f -name "*.mp3" -exec rm -f {} \;

19. Find all Empty Files

To find all empty files under certain path.
# find /tmp -type f -empty

20. Find all Empty Directories

To file all empty directories under certain path.
# find /tmp -type d -empty

21. File all Hidden Files

To find all hidden files, use below command.
# find /tmp -type f -name ".*"
Part III – Search Files Based On Owners and Groups

22. Find Single File Based on User

To find all or single file called tecmint.txt under / root directory of owner root.
# find / -user root -name tecmint.txt

23. Find all Files Based on User

To find all files that belongs to user Tecmint under /home directory.
# find /home -user tecmint

24. Find all Files Based on Group

To find all files that belongs to group Developer under /home directory.
# find /home -group developer

25. Find Particular Files of User

To find all .txt files of user Tecmint under /home directory.
# find /home -user tecmint -iname "*.txt"
Part IV – Find Files and Directories Based on Date and Time

26. Find Last 50 Days Modified Files

To find all the files which are modified 50 days back.
# find / -mtime 50

27. Find Last 50 Days Accessed Files

To find all the files which are accessed 50 days back.
# find / -atime 50

28. Find Last 50-100 Days Modified Files

To find all the files which are modified more than 50 days back and less than 100 days.
# find / -mtime +50 –mtime -100

29. Find Changed Files in Last 1 Hour

To find all the files which are changed in last 1 hour.
# find / -cmin -60

30. Find Modified Files in Last 1 Hour

To find all the files which are modified in last 1 hour.
# find / -mmin -60

31. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.
# find / -amin -60
Part V – Find Files and Directories Based on Size

32. Find 50MB Files

To find all 50MB files, use.
# find / -size 50M

33. Find Size between 50MB – 100MB

To find all the files which are greater than 50MB and less than 100MB.
# find / -size +50M -size -100M

34. Find and Delete 100MB Files

To find all 100MB files and delete them using one single command.
# find / -size +100M -exec rm -rf {} \;

35. Find Specific Files and Delete

Find all .mp3 files with more than 10MB and delete them using one single command.
# find / -type f -name *.mp3 -size +10M -exec rm {} \;
That’s it, We are ending this post here, In our next article we will discuss more about other Linux commands in depth with practical examples. Let us know your opinions on this article using our comment section.

50 Frequently asked question in Unix / Linux / AIX

1. tar command examples

Create a new tar archive.
$ tar cvf archive_name.tar dirname/
Extract from an existing tar archive.
$ tar xvf archive_name.tar
View an existing tar archive.
$ tar tvf archive_name.tar

2. grep command examples

Search for a given string in a file (case in-sensitive search).
$ grep -i "the" demo_file
Print the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Search for a given string in all files recursively
$ grep -r "ramesh" *

3. find command examples

Find files using file-name ( case in-sensitve find)
# find -iname "MyCProgram.c"
Execute commands on files found by the find command
$ find -iname "MyCProgram.c" -exec md5sum {} \;
Find all empty files in home directory
# find ~ -empty

4. ssh command examples

Login to remote host
ssh -l jsmith remotehost.example.com
Debug ssh client
ssh -v -l jsmith remotehost.example.com
Display ssh client version
$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003

5. sed command examples

When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command.
$sed 's/.$//' filename
Print file content in reverse order
$ sed -n '1!G;h;$p' thegeekstuff.txt
Add line number for all non-empty-lines in a file
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'

6. awk command examples

Remove duplicate lines using awk
$ awk '!($0 in array) { array[$0]; print }' temp
Print all lines from /etc/passwd that has the same uid and gid
$awk -F ':' '$3==$4' passwd.txt
Print only specific field from a file.
$ awk '{print $2,$5;}' employee.txt

7. vim command examples

Go to the 143rd line of file
$ vim +143 filename.txt
Go to the first match of the specified
$ vim +/search-term filename.txt
Open the file in read only mode.
$ vim -R /etc/passwd

8. diff command examples

Ignore white space while comparing.
# diff -w name_list.txt name_list_new.txt

2c2,3
< John Doe --- > John M Doe
> Jason Bourne

9. sort command examples
Sort a file in ascending order
$ sort names.txt
Sort a file in descending order
$ sort -r names.txt
Sort passwd file by 3rd field.
$ sort -t: -k 3n /etc/passwd | more

10. export command examples

To view oracle related environment variables.
$ export | grep ORACLE
declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"
To export an environment variable:
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0

11. xargs command examples

Copy all images to external hard-drive
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Search all jpg images in the system and archive it.
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
Download all the URLs mentioned in the url-list.txt file
# cat url-list.txt | xargs wget –c

12. ls command examples

Display filesize in human readable format (e.g. KB, MB etc.,)
$ ls -lh
-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz
Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
$ ls -ltr
Visual Classification of Files With Special Characters Using ls -F
$ ls -F

13. pwd command
pwd is Print working directory. What else can be said about the good old pwd who has been printing the current directory name for ages.

14. cd command examples

Use “cd -” to toggle between the last two directories
Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

15. gzip command examples
To create a *.gz compressed file:
$ gzip test.txt
To uncompress a *.gz file:
$ gzip -d test.txt.gz
Display compression ratio of the compressed file using gzip -l
$ gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
              23709               97975  75.8% asp-patch-rpms.txt

16. bzip2 command examples

To create a *.bz2 compressed file:
$ bzip2 test.txt
To uncompress a *.bz2 file:
bzip2 -d test.txt.bz2

17. unzip command examples
To extract a *.zip compressed file:
$ unzip test.zip
View the contents of *.zip file (Without unzipping it):
$ unzip -l jasper.zip
Archive:  jasper.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
    40995  11-30-98 23:50   META-INF/MANIFEST.MF
    32169  08-25-98 21:07   classes_
    15964  08-25-98 21:07   classes_names
    10542  08-25-98 21:07   classes_ncomp

18. shutdown command examples

Shutdown the system and turn the power off immediately.
# shutdown -h now
Shutdown the system after 10 minutes.
# shutdown -h +10
Reboot the system using shutdown command.
# shutdown -r now
Force the filesystem check during reboot.
# shutdown -Fr now

19. ftp command examples

Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following.
$ ftp IP/hostname
ftp> mget *.html
To view the file names located on the remote server before downloading, mls ftp command as shown below.
ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html

20. crontab command examples
View crontab entry for a specific user
# crontab -u john -l
Schedule a cron job every 10 minutes.
*/10 * * * * /home/ramesh/check-disk-space

ore crontab examples: Linux Crontab: 15 Awesome Cron Job Examples

21. service command examples

Service command is used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, you can use the service command.
Check the status of a service:
# service ssh status
Check the status of all the services.
service --status-all
Restart a service.
# service ssh restart

22. ps command examples

ps command is used to display information about the processes that are running in the system.
While there are lot of arguments that could be passed to a ps command, following are some of the common ones.
To view current running processes.
$ ps -ef | more
To view current running processes in a tree structure. H option stands for process hierarchy.
$ ps -efH | more

23. free command examples

This command is used to display the free, used, swap memory available in the system.
Typical free command output. The output is displayed in bytes.
$ free
             total       used       free     shared    buffers     cached
Mem:       3566408    1580220    1986188          0     203988     902960
-/+ buffers/cache:     473272    3093136
Swap:      4000176          0    4000176
If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
$ free -g
             total       used       free     shared    buffers     cached
Mem:             3          1          1          0          0          0
-/+ buffers/cache:          0          2
Swap:            3          0          3
If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.
ramesh@ramesh-laptop:~$ free -t
             total       used       free     shared    buffers     cached
Mem:       3566408    1592148    1974260          0     204260     912556
-/+ buffers/cache:     475332    3091076
Swap:      4000176          0    4000176
Total:     7566584    1592148    5974436

24. top command examples

top command displays the top processes in the system ( by default sorted by cpu usage ). To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below.
Current Sort Field:  P  for window 1:Def
Select sort field via field letter, type any other key to return

  a: PID        = Process Id              v: nDRT       = Dirty Pages count
  d: UID        = User Id                 y: WCHAN      = Sleeping in Function
  e: USER       = User Name               z: Flags      = Task Flags
  ........
To displays only the processes that belong to a particular user use -u option. The following will show only the top processes that belongs to oracle user.
$ top -u oracle

25. df command examples
Displays the file system disk space usage. By default df -k displays output in bytes.
$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             29530400   3233104  24797232  12% /
/dev/sda2            120367992  50171596  64082060  44% /home
df -h displays output in human readable form. i.e size will be displayed in GB’s.
ramesh@ramesh-laptop:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              29G  3.1G   24G  12% /
/dev/sda2             115G   48G   62G  44% /home
Use -T option to display what type of file system.
ramesh@ramesh-laptop:~$ df -T
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/sda1     ext4    29530400   3233120  24797216  12% /
/dev/sda2     ext4   120367992  50171596  64082060  44% /home

26. kill command examples

Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.
$ ps -ef | grep vim
ramesh    7243  7222  9 22:43 pts/2    00:00:00 vim

$ kill -9 7243

27. rm command examples
Get confirmation before removing the file.
$ rm -i filename.txt
It is very useful while giving shell metacharacters in the file name argument.
Print the filename and get confirmation before removing the file.
$ rm -i file*
Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
$ rm -r example

28. cp command examples

Copy file1 to file2 preserving the mode, ownership and timestamp.
$ cp -p file1 file2
Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ cp -i file1 file2

29. mv command examples

Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ mv -i file1 file2
Note: mv -f is just the opposite, which will overwrite file2 without prompting.
mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.
$ mv -v file1 file2

30. cat command examples

You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.
$ cat file1 file2
While displaying the file, following cat -n command will prepend the line number to each line of the output.
$ cat -n /etc/logrotate.conf
    1 /var/log/btmp {
    2     missingok
    3     monthly
    4     create 0660 root utmp
    5     rotate 1
    6 }

31. mount command examples

To mount a file system, you should first create a directory and mount it as shown below.
# mkdir /u01

# mount /dev/sdb1 /u01
You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
/dev/sdb1 /u01 ext2 defaults 0 2

32. chmod command examples

chmod command is used to change the permissions for a file or directory.
Give full access to user and group (i.e read, write and execute ) on a specific file.
$ chmod ug+rwx file.txt
Revoke all access for the group (i.e read, write and execute ) on a specific file.
$ chmod g-rwx file.txt
Apply the file permissions recursively to all the files in the sub-directories.
$ chmod -R ug+rwx file.txt

33. chown command examples
chown command is used to change the owner and group of a file. \
To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.
$ chown oracle:dba dbora.sh
Use -R to change the ownership recursively.
$ chown -R oracle:dba /home/oracle

34. passwd command examples

Change your password from command line using passwd. This will prompt for the old password followed by the new password.
$ passwd
Super user can use passwd command to reset others password. This will not prompt for current password of the user.
# passwd USERNAME
Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
# passwd -d USERNAME

35. mkdir command examples

Following example creates a directory called temp under your home directory.
$ mkdir ~/temp
Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
$ mkdir -p dir1/dir2/dir3/dir4/

36. ifconfig command examples

Use ifconfig command to view or configure a network interface on the Linux system.
View all the interfaces along with status.
$ ifconfig -a
Start or stop a specific interface using up and down command as shown below.
$ ifconfig eth0 up

$ ifconfig eth0 down

37. uname command examples
Uname command displays important information about the system such as — Kernel name, Host name, Kernel release number,
Processor type, etc.,
Sample uname output from a Ubuntu laptop is shown below.
$ uname -a
Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux

38. whereis command examples

When you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command.
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
$ whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk

39. whatis command examples

Whatis command displays a single line description about a command.
$ whatis ls
ls  (1)  - list directory contents

$ whatis ifconfig
ifconfig (8)         - configure a network interface

40. locate command examples

Using locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.
The example below shows all files in the system that contains the word crontab in it.
$ locate crontab
/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/cron/examples/crontab2english.pl.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim72/syntax/crontab.vim

41. man command examples

Display the man page of a specific command.
$ man crontab
When a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below.
$ man SECTION-NUMBER commandname
Following 8 sections are available in the man page.
  1. General commands
  2. System calls
  3. C library functions
  4. Special files (usually devices, those found in /dev) and drivers
  5. File formats and conventions
  6. Games and screensavers
  7. Miscellaneous
  8. System administration commands and daemons
For example, when you do whatis crontab, you’ll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.
$ whatis crontab
crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron

$ man 5 crontab

42. tail command examples

Print the last 10 lines of a file by default.
$ tail filename.txt
Print N number of lines from the file named filename.txt
$ tail -n N filename.txt
View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.
$ tail -f log-file


43. less command examples
less is very efficient while viewing huge log files, as it doesn’t need to load the full file while opening.
$ less huge-log-file.log
One you open a file using less command, following two keys are very helpful.
CTRL+F – forward one window
CTRL+B – backward one window


44. su command examples
Switch to a different user account using su command. Super user can switch to any other user without entering their password.
$ su - USERNAME
Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.
[john@dev-server]$ su - raj -c 'ls'

[john@dev-server]$
Login to a specified user account, and execute the specified shell instead of the default shell.
$ su -s 'SHELLNAME' USERNAME

45. mysql command examples

mysql is probably the most widely used open source database on Linux. Even if you don’t run a mysql database on your server, you might end-up using the mysql command ( client ) to connect to a mysql database running on the remote server.
To connect to a remote mysql database. This will prompt for a password.
$ mysql -u root -p -h 192.168.1.2
To connect to a local mysql database.
$ mysql -u root -p
If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).

46. yum command examples

To install apache using yum.
$ yum install httpd
To upgrade apache using yum.
$ yum update httpd
To uninstall/remove apache using yum.
$ yum remove httpd

47. rpm command examples

To install apache using rpm.
# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm
To upgrade apache using rpm.
# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm
To uninstall/remove apache using rpm.
# rpm -ev httpd

48. ping command examples

Ping a remote host by sending only 5 packets.
$ ping -c 5 gmail.com

49. date command examples

Set the system date:
# date -s "01/31/2010 23:59:53"
Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
# hwclock –systohc

# hwclock --systohc –utc

50. wget command examples

The quick and effective method to download software, music, video from internet is using wget command.
$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
Download and store it with a different name.
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701

Known NFS Issues in Linux

Known Issues and Workarounds:
RHEL 5.x
  1. Issue 1:
    RHEL5.8: Mount with sec=krb5 displays warning as 'rpc.idmapd/rpc.gssd appears not to be running'.
    Description: Mount displays an error as 'rpc.idmapd/rpc.gssd appears not to be running' though they are running, hence nfs-kerberos fails when it is mounted with sec=krb5.
    Workaround: Update the nfs-utils package to version 1.0.9-66.el5 or later. For more information, see [RHEL5.8] NFSv4 'mount' command produces warnings about RPC services not running.
  2. Issue 2:
    RHEL5.9: chmod, chgrp, and chown commands fail on NFSv3 mounts.
    Workaround: Traverse to the mounted path and then use  chmod, chgrp, and chown commands to work.
  3. Issue 3:
    RHEL 5.10 and RHEL 5.11 : NLM clients can fail to recover locks with pending UNLOCK operations.
    Description: When clients perform NLM lock recovery, RHEL 5.10 and RHEL 5.11 NLM clients can get into a state where they stop issuing pending UNLOCK operations and only reclaim existing locks, leaving the locks with pending UNLOCK operations not unlocked and not reclaimed.
    Workaround: No workaround available in RHEL5. Upgrade to RHEL 6.6 GA clients which properly handle pending UNLOCK operations when performing lock recovery and all locks are cleaned up after recovery.
  4. Issue 4:
    RHEL 5.11:"EIO (errno=5)" hit on RHEL5U11 NFSv3 mounts during Storage Faiover operations.
    Description: RHEL 5.11 does not have the latest SUNRPC layer fixes which results client EIO errors.
    Workaround: No workaround available in RHEL5. Upgrade the client to latest RHEL kernels like RHEL 6.6 GA which addresses this problem and has latest fixes. 
  5. Issue 5:
    RHEL 5.11: "ERROR: No locks available (errno=37)" hit on NFSv3 mounts during Storage Failover operations
    Description: RHEL 5.11 does not have the latest SUNRPC layer fixes which results "error # 37" failure.
    Workaround: No workaround available in RHEL5. Upgrade the client to latest RHEL kernels like RHEL 6.6 GA which addresses this problem and has latest fixes. 
  6. Issue 6:
    The following error messages are reported:
    • /var/log/messages:Aug 16 19:08:17 uscf1plat0 kernel: NFS: v4 server 10.113.49.8 returned a bad sequence-id error!

      grep sequence-id messages.1
      Aug 16 19:08:17 uscf1plat0 kernel: NFS: v4 server 10.113.49.8 returned a bad sequence-id error!
      ...
      Aug 22 01:09:27 uscf1plat0 kernel: NFS: v4 server 10.113.49.8 returned a bad sequence-id error!
    • No logs matching on the storage system
    • Checked pktt: After the client goes into a loop for write call, the storage system returns NFS4ERR_BAD_STATEID
    Workaround: For more information, see Red Hat BUG 620502 - [NetApp 5.6 bug] RHEL NFS clients disconnect from NetApp NFSv4 shares with: v4 server returned a bad sequence-id error!
RHEL 6.x
  1. Issue 1:
    RHEL6.x: nfs-kerberos fails with a permission denied error if allow_weak_crypto is disabled.
    Description: With kinit enabled on these clients, nfs-kerberos authentication will fail and displays kinit: No supported encryption types (config file error?).
    Workaround: Add allow_weak_crypto=yes in /etc/krb5/krb5.conf to get that nfs-kerberos working.
     
  2. Issue 2:
    RHEL6.3/6.4: chmodchgrp, and chown commands fail on NFSv3 mounts.
    Workaround: Traverse to the mounted path and then use  chmodchgrp, and chown commands to work.
     
  3. Issue 3:
    RHEL 6.2/6.3 : Applications running on Redhat 6.2/6.3 clients might not be able to stop writing to files to a NFS mount point. Pressing 'Ctrl C' will not help. RHEL6 (> kernl 2.6.25) and later mount options intr/nointr options have been deprecated. Applications that are hung should be killed using SIGKILL to interrupt.
    Workaround: Use the command kill -9 pid to kill the process.
     
  4. Issue 4:
    RHEL6.3/6.4: The NFSv3 process occasionally exits with 'No locks available' when NFS server crashes.
    Description: NLM is NOT retrying waiting byte lock request after the server crashes and just fails the application with -ENOLCK error.
    Workaround: Upgrade to RHEL 6.3 Errata Kernel (2.6.32-279.46.1.el6). 6.4 Errata Kernel (2.6.32-358.49.1.el6).
     
  5. Issue 5:
    RHEL 6.1/6.2/6.3:
    Description: Application running on RHEL 6.1/6.2/6.3 clients might hang with the BAD_STATID error while NFS v4.0 delegations are enabled
    Workaround: Upgrade to RHEL 6.3 Errata Kernel (2.6.32-279.46.1.el6).
     
  6. Issue 6:
    RHEL 6.4: NFS v4.0 referral directories missing from directory listing on RHEL 6.4 clients.
    Description: RHEL 6.4 client getting lookups for all the volumes but its listing only the volumes of one of the HA storage nodes. Tracked with RedHat found at https://bugzilla.redhat.com/show_bug.cgi?id=963337
     
  7. Issue 7:
    RHEL 6.4: Mount on RHEL 6.4 client succeeds with sec=krb5 though the volume has export ro rule set as sys.
    Description: RHEL 6.4 client switches its security flavor from 'RPCSEC_GSS' to 'AUTH_UNIX' during mount and hence, the mount gets succeeded while mounting with sec=krb5, though the volume has exported with the ro rule set as sys. Similarly, export with ro rule as sys and mount with krb5 also succeeds, since the client switches the security flavor based on the SECINFO reply; but this seems to be a security gap. Tracked with RedHat found at https://bugzilla.redhat.com/show_bug.cgi?id=948145
     
  8. Issue 8:
    RHEL 6.4: NFS v4.0 state recovery can deadlock with permission checking on delegated OPEN.
    Description: The state manager can deadlock on an open owner's nfsv4 sequence ID if you happen to be recovering OPEN state while the client is also checking open permission for a delegated OPEN.
    Workaround: Upgrade to RHEL 6.4 Errata Kernel (2.6.32-358.49.1.el6.x86_64).
     
  9. Issue 9:
    RHEL 6.4: NFSv4.0: Fix handling of revoked delegations by setattr.
    Description_nfs4_do_setattr() will use the delegation stateid if no writeable open file stateid is available. If the server revokes that delegation stateid, then the call to nfs4_handle_exception() will fail to handle the error due to the lack of a struct nfs4_state, and will just convert the error into an EIO.
    Workaround: Upgrade the client to Rhel 6.4 Errata Kernel (2.6.32-358.49.1.el6.x86_64) and above.
   
  • Issue 11:
    RHEL6.x: CDOT: Circular directory structure warning when running chown -R on junctioned volumes via NFSv4. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1223978.
    Description: when you have a multilevel junction path (/vol1/vol2/vol3), mounted, you might see a recursive directory structure warning while doing setattr or chmod.
    Workaround: upgrade to latest kernel of 6.7.
 
RHEL 7.x
Best Practices:
See TR-4067 for 'Clustered Data ONTAP NFS Best Practice and Implementation Guide'.


  • Issue 12:
    RHEL6.x:  "MIT krb5 gss_accept_sec_context() implementation used by SECD rejects an incoming token if the ticket end-time expires before the server current time, with no Kerberos clock skew consideration.This is being tracked at http://krbdev.mit.edu/rt/Ticket/Display.html?id=8268
    Description: "If the check triggers, gss_accept_sec_context() returns GSS_S_CREDENTIALS_EXPIRED on GSS context refresh which is incorrect as itsuggests that the NFS client's verifier_cred_handle has expired.
    The check fails and ""Permission Denied"" is returned to the application when:
    1) The server clock is ahead of the client clock, but within the server's configured Kerberos clock skew.
    2) The maximum service ticket lifetime is less than the maximum TGT lifetime.
    3) The NFS client is sending requests on a Kerberos share during the ""clock skew window"" when the client service ticket is good according to the client clock, and expired according to the server clock.
    This results in a ""Permissions Denied"" error being returned to the application during the clock skew window. In this case, the server will reject the NFS request with an AUTH_ERROR which triggers a GSS context refresh attempt on the client. The client will use the existing server service ticket for the refresh which is rejected by the server with the GSS_S_CREDENTIALS_EXPIRED error. Note that if the service ticket is refreshable, this situation heals itself once the service ticket expires on the client, forcing the client to refresh the service ticket." Workaround: The workaround is to configure the KDC maximum service ticket lifetime to be equal to the TGT maximum lifetime. In this case, the client when asked to refresh the service ticket, it first must refresh the TGT which on the Linux NFS client takes the Kerberos clock skew into consideration.
  • Issue 13:
    RHEL6.3,6.4,6.5:  RHEL NFS client hangs and I/O outages with older RHEL 6 kernels and NFSv4.1 (BURT:814789).
    Description: When using NFSv4.1 with Red Hat Enterprise Linux (RHEL) NFS clients running olderRHEL 6 kernels such as RHEL 6.4 and RHEL 6.3, you might encounter client issues such as hangs & long I/O outages when performing NetApp-specific controller tasks such as LIF migrations, that require clients to recover state under heavy workloads.
    Workaround: The above issues have been addressed in the latest RHEL 6.5.z errata packages described below:
    1) kernel-2.6.32-431.29.2.el6
    2) nfs-utils-1.2.3-39.el6_5.3
    3) libtirpc-0.2.1-6.el6_5.2
     To avoid these issues, it is recommended to upgrade clients to these respective RHEL 6.5.z errata packages.
  • Issue 14:
    RHEL 6.4,6.5,6.5z:  RHEL NFS client I/O outages and errors (BURT:866544).
    Description: I/O outages and errors are observed on Red Hat Enterprise Linux (RHEL) NFS clients running older kernels such as RHEL 6.5. This is seen for all NFS versions including NFSv3, v4.0 & v4.1.
    Workaround: The above issues have now been addressed in the RHEL 6.6 GA kernel-2.6.32-504.el6. It is recommended to upgrade clients to RHEL 6.6 GA to obtain these fixes and avoid encountering the above issues.
    1. Issue 1:
      RHEL7.0x: RHEL 7.0 NFS client issues & fixes (BURT:915798)
      Description:NFS client I/O errors, kernel crashes, permission-denied errors on Kerberized mount points and an intermittent data corruption issue have been observed on older Red Hat Enterprise Linux (RHEL) kernels such as RHEL 7.0. This is seen across all NFS versions including NFS v3, v4.0 & v4.1.
      Workaround:The above issues are now addressed in the RHEL 7.1 GA kernel-3.10.0-229.el7. Upgrade clients to this RHEL 7.1 GA release to access these fixes and thereby avoid encountering the above issues.
       
    2. Issue 2:
      RHEL7.x: on pNFS mounts umount does not destroy session to DS after getting EIO error. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1234986.
      Description:On RHEL 7.x clients, if you are mounted using pNFS, and you do an umount command, the client might fail to delete session with the server.
      Workaround: upgrade to latest kernel version of 7.1z or 7.2 ( > kernel 3.10.0-320 ) .
    3. Issue 3: RHEL7.x:  Received EIO because of ADMIN_REVOKED on the SETATTR on Rhel7.1 with pNFS . This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1214410
      Description: On RHEL 7.x clients, if you are mounted using pNFS and the server has to go through a failover (takeover/giveback), you might see EIO error ( error=5)
      Workaround: upgrade to latest kernel version of 7.1z or 7.2 ( > kernel 3.10.0-289)
    4. Issue 4:
      RHEL7.1: NFS IO for vols on disaster side cluster intermittently fails post MCC SO. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1240790
      Description: On RHEL 7.x client, if you are mounted using NFSv4 and the storage system has to go through a Metrocluster switchover, you might see and EIO (error=5).
      Workaround:upgrade to latest kernel version of 7.1z or 7.2 ( > kernel 3.10.0-295)
    5. Issue 5:
      RHEL7.x: CDOT: Circular directory structure warning when running chown -R on junctioned volumes via NFSv4. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1225090.
      Description: when you have a multilevel junction path (/vol1/vol2/vol3), mounted, you might see a recursive directory structure warning while doing setattr or chmod.
      Workaround: upgrade to latest kernel of 7.1
    6. Issue 6:
      RHEL7.1: NFSv4 sharelocks not cleared after closing files following LIF migrate with new RHEL7.1 clients.This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1263376
      Description: If there are files open and Locks on an NFSv4.x mount and a server goes through a LIF migrate or an SFO, you might see locks not being cleared. this might lead data corruption.
      Workaround: use kernel verion 3.10.0-320 or above
    7. Issue 7:
      RHEL6.x/7.x:  "MIT krb5 gss_accept_sec_context() implementation used by SECD rejects an incoming token if the ticket end-time expires before the server current time, with no Kerberos clock skew consideration.This is being tracked at http://krbdev.mit.edu/rt/Ticket/Display.html?id=8268
      Description: "If the check triggers, gss_accept_sec_context() returns GSS_S_CREDENTIALS_EXPIRED on GSS context refresh which is incorrect as itsuggests that the NFS client's verifier_cred_handle has expired.
      The check fails and ""Permission Denied"" is returned to the application when:
      1) The server clock is ahead of the client clock, but within the server's configured Kerberos clock skew.
      2) The maximum service ticket lifetime is less than the maximum TGT lifetime.
      3) The NFS client is sending requests on a Kerberos share during the ""clock skew window"" when the client service ticket is good according to the client clock, and expired according to the server clock.
      This results in a ""Permissions Denied"" error being returned to the application during the clock skew window. In this case, the server will reject the NFS request with an AUTH_ERROR which triggers a GSS context refresh attempt on the client. The client will use the existing server service ticket for the refresh which is rejected by the server with the GSS_S_CREDENTIALS_EXPIRED error. Note that if the service ticket is refreshable, this situation heals itself once the service ticket expires on the client, forcing the client to refresh the service ticket."
      Workaround: The workaround is to configure the KDC maximum service ticket lifetime to be equal to the TGT maximum lifetime. In this case, the client when asked to refresh the service ticket, it first must refresh the TGT which on the Linux NFS client takes the Kerberos clock skew into consideration.

Linux NFS troubleshoot

This is intended as a step-by-step guide to what to do when things go wrong using NFS. Usually trouble first rears its head on the client end, so this diagnostic will begin there.

7.1. Unable to See Files on a Mounted File System

First, check to see if the file system is actually mounted. There are several ways of doing this. The most reliable way is to look at the file /proc/mounts, which will list all mounted filesystems and give details about them. If this doesn't work (for example if you don't have the /proc filesystem compiled into your kernel), you can type mount -f although you get less information.
If the file system appears to be mounted, then you may have mounted another file system on top of it (in which case you should unmount and remount both volumes), or you may have exported the file system on the server before you mounted it there, in which case NFS is exporting the underlying mount point (if so then you need to restart NFS on the server).
If the file system is not mounted, then attempt to mount it. If this does not work, see Symptom 3.

7.2. File requests hang or timeout waiting for access to the file

This usually means that the client is unable to communicate with the server. See Symptom 3 letter b.

7.3. Unable to mount a file system

There are two common errors that mount produces when it is unable to mount a volume. These are:
  1. failed, reason given by server: Permission denied This means that the server does not recognize that you have access to the volume.
    • Check your /etc/exports file and make sure that the volume is exported and that your client has the right kind of access to it. For example, if a client only has read access then you have to mount the volume with the ro option rather than the rw option.
    • Make sure that you have told NFS to register any changes you made to /etc/exports since starting nfsd by running the exportfs command. Be sure to type exportfs -ra to be extra certain that the exports are being re-read.
    • Check the file /proc/fs/nfs/exports and make sure the volume and client are listed correctly. (You can also look at the file /var/lib/nfs/xtab for an unabridged list of how all the active export options are set.) If they are not, then you have not re-exported properly. If they are listed, make sure the server recognizes your client as being the machine you think it is. For example, you may have an old listing for the client in /etc/hosts that is throwing off the server, or you may not have listed the client's complete address and it may be resolving to a machine in a different domain. One trick is login to the server from the client via ssh or telnet; if you then type who, one of the listings should be your login session and the name of your client machine as the server sees it. Try using this machine name in your /etc/exports entry. Finally, try to ping the client from the server, and try to ping the server from the client. If this doesn't work, or if there is packet loss, you may have lower-level network problems.
    • It is not possible to export both a directory and its child (for example both /usr and /usr/local). You should export the parent directory with the necessary permissions, and all of its subdirectories can then be mounted with those same permissions.
  2. RPC: Program Not Registered (or another "RPC" error) This means that the client does not detect NFS running on the server. This could be for several reasons.
    • First, check that NFS actually is running on the server by typing rpcinfo -p on the server. You should see something like this:
      program vers proto   port
         100000    2   tcp    111  portmapper
         100000    2   udp    111  portmapper
         100011    1   udp    749  rquotad
         100011    2   udp    749  rquotad
         100005    1   udp    759  mountd
         100005    1   tcp    761  mountd
         100005    2   udp    764  mountd
         100005    2   tcp    766  mountd
         100005    3   udp    769  mountd
         100005    3   tcp    771  mountd
         100003    2   udp   2049  nfs
         100003    3   udp   2049  nfs
         300019    1   tcp    830  amd
         300019    1   udp    831  amd
         100024    1   udp    944  status
         100024    1   tcp    946  status
         100021    1   udp   1042  nlockmgr
         100021    3   udp   1042  nlockmgr
         100021    4   udp   1042  nlockmgr
         100021    1   tcp   1629  nlockmgr
         100021    3   tcp   1629  nlockmgr
         100021    4   tcp   1629  nlockmgr
      
      This says that we have NFS versions 2 and 3, rpc.statd version 1, network lock manager (the service name for rpc.lockd) versions 1, 3, and 4. There are also different service listings depending on whether NFS is travelling over TCP or UDP. UDP is usually (but not always) the default unless TCP is explicitly requested. If you do not see at least portmapper, nfs, and mountd, then you need to restart NFS. If you are not able to restart successfully, proceed to Symptom 9.
    • Now check to make sure you can see it from the client. On the client, type rpcinfo -pserver where server is the DNS name or IP address of your server. If you get a listing, then make sure that the type of mount you are trying to perform is supported. For example, if you are trying to mount using Version 3 NFS, make sure Version 3 is listed; if you are trying to mount using NFS over TCP, make sure that is registered. (Some non-Linux clients default to TCP). Type man rpcinfo for more details on how to read the output. If the type of mount you are trying to perform is not listed, try a different type of mount.
      If you get the error No Remote Programs Registered, then you need to check your /etc/hosts.allow and /etc/hosts.deny files on the server and make sure your client actually is allowed access. Again, if the entries appear correct, check /etc/hosts (or your DNS server) and make sure that the machine is listed correctly, and make sure you can ping the server from the client. Also check the error logs on the system for helpful messages: Authentication errors from bad /etc/hosts.allow entries will usually appear in /var/log/messages, but may appear somewhere else depending on how your system logs are set up. The man pages for syslog can help you figure out how your logs are set up. Finally, some older operating systems may behave badly when routes between the two machines are asymmetric. Try typing tracepath [server] from the client and see if the word "asymmetric" shows up anywhere in the output. If it does then this may be causing packet loss. However asymmetric routes are not usually a problem on recent linux distributions.
      If you get the error Remote system error - No route to host, but you can ping the server correctly, then you are the victim of an overzealous firewall. Check any firewalls that may be set up, either on the server or on any routers in between the client and the server. Look at the man pages for ipchains, netfilter, and ipfwadm, as well as the IPChains-HOWTO and the Firewall-HOWTO for help.

7.4. I do not have permission to access files on the mounted volume

This could be one of two problems. If it is a write permission problem, check the export options on the server by looking at /proc/fs/nfs/exports and make sure the filesystem is not exported read-only. If it is you will need to re-export it read/write (don't forget to run exportfs -ra after editing /etc/exports). Also, check /proc/mounts and make sure the volume is mounted read/write (although if it is mounted read-only you ought to get a more specific error message). If not then you need to re-mount with the rw option.
The second problem has to do with username mappings, and is different depending on whether you are trying to do this as root or as a non-root user.
If you are not root, then usernames may not be in sync on the client and the server. Type id [user] on both the client and the server and make sure they give the same UID number. If they don't then you are having problems with NIS, NIS+, rsync, or whatever system you use to sync usernames. Check group names to make sure that they match as well. Also, make sure you are not exporting with the all_squash option. If the user names match then the user has a more general permissions problem unrelated to NFS.
If you are root, then you are probably not exporting with the no_root_squash option; check /proc/fs/nfs/exports or /var/lib/nfs/xtab on the server and make sure the option is listed. In general, being able to write to the NFS server as root is a bad idea unless you have an urgent need -- which is why Linux NFS prevents it by default. See Section 6, “Security and NFS” for details.
If you have root squashing, you want to keep it, and you're only trying to get root to have the same permissions on the file that the user nobody should have, then remember that it is the server that determines which uid root gets mapped to. By default, the server uses the UID and GID of nobody in the /etc/passwd file, but this can also be overridden with the anonuid and anongid options in the /etc/exports file. Make sure that the client and the server agree about which UID nobody gets mapped to.

7.5. When I transfer really big files, NFS takes over all the CPU cycles on the server and it screeches to a halt

This is a problem with the fsync() function in 2.2 kernels that causes all sync-to-disk requests to be cumulative, resulting in a write time that is quadratic in the file size. If you can, upgrading to a 2.4 kernel should solve the problem. Also, exporting with the no_wdelay option forces the program to use o_sync() instead, which may prove faster.

7.6. Strange error or log messages

  1. Messages of the following format:
    Jan 7 09:15:29 server kernel: fh_verify: mail/guest permission failure, acc=4, error=13
    Jan 7 09:23:51 server kernel: fh_verify: ekonomi/test permission failure, acc=4, error=13
    
    These happen when a NFS setattr operation is attempted on a file you don't have write access to. The messages are harmless.
  2. The following messages frequently appear in the logs:
    kernel: nfs: server server.domain.name not responding, still trying
    kernel: nfs: task 10754 can't get a request slot
    kernel: nfs: server server.domain.name OK
    
    The "can't get a request slot" message means that the client-side RPC code has detected a lot of timeouts (perhaps due to network congestion, perhaps due to an overloaded server), and is throttling back the number of concurrent outstanding requests in an attempt to lighten the load. The cause of these messages is basically sluggish performance. See Section 5, “Optimizing NFS Performance” for details.
  3. After mounting, the following message appears on the client:
    nfs warning: mount version older than kernel
    
    It means what it says: You should upgrade your mount package and/or am-utils. (If for some reason upgrading is a problem, you may be able to get away with just recompiling them so that the newer kernel features are recognized at compile time).
  4. Errors in startup/shutdown log for lockd
    nfslock: rpc.lockd startup failed
    
    They are harmless. Older versions of rpc.lockd needed to be started up manually, but newer versions are started automatically by nfsd. Many of the default startup scripts still try to start up lockd by hand, in case it is necessary. You can alter your startup scripts if you want the messages to go away.
  5. The following message appears in the logs:
    kmem_create: forcing size word alignment - nfs_fh
    
    This results from the file handle being 16 bits instead of a multiple of 32 bits, which makes the kernel grimace. It is harmless.

7.7. Real permissions don't match what's in /etc/exports

/etc/exports is very sensitive to whitespace - so the following statements are not the same:
/export/dir hostname(rw,no_root_squash) 
/export/dir hostname (rw,no_root_squash)
The first will grant hostname rw access to /export/dir without squashing root privileges. The second will grant hostname rw privileges with root squash and it will grant everyone else read/write access, without squashing root privileges. Nice huh?

7.8. Flaky and unreliable behavior

Simple commands such as ls work, but anything that transfers a large amount of information causes the mount point to lock.
This could be one of two problems:
  1. It will happen if you have ipchains on at the server and/or the client and you are not allowing fragmented packets through the chains. Allow fragments from the remote host and you'll be able to function again. See Section 5, “Optimizing NFS Performance” for details on how to do this.
  2. You may be using a larger rsize and wsize in your mount options than the server supports. Try reducing rsize and wsize to 1024 and seeing if the problem goes away. If it does, then increase them slowly to a more reasonable value.

7.9. nfsd won't start

Check the file /etc/exports and make sure root has read permission. Check the binaries and make sure they are executable. Make sure your kernel was compiled with NFS server support. You may need to reinstall your binaries if none of these ideas helps.

7.10. File Corruption When Using Multiple Clients

If a file has been modified within one second of its previous modification and left the same size, it will continue to generate the same inode number. Because of this, constant reads and writes to a file by multiple clients may cause file corruption. Fixing this bug requires changes deep within the filesystem layer, and therefore it is a 2.5 item.

What is PS2 - Continuation Interactive Prompt in Linux

A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-lin...