Friday, April 26, 2013

Before understanding the difference between soft link and hard link, we need to understand the concept of soft link and hard
link as well as concept of Inode in unix
file system. Actually, link concept originated to replicate the file at
different location without copying it and it is based on file system. File
System is closely related withinode which is unique in system.

How to
create Soft Link in Linux?

We will create soft
link as well as hard link in two examples and then we will check for
difference.

As per above example, we have created soft link of shellscript.shto
this file /root/ctier/test/shells.sh. i.e. new file is /root/ctier/test/shells.sh.
If you check inode of new file, it is different. And in long listing, it is
showing that it points to some other file. Size of file is different at both
the locations. Because in soft link, there is no copy of file, it is just
pointing to the original location. If we are trying to open a /root/ctier/test/shells.sh
this file, then it will open original file i.e. /root/ctier/shellscript/shellscript.sh.
If we made any changes in original file then it will reflect in soft linked
file as well.

If you see above example of hardlink then you can understand thatin hard link, file’s new copy
is created at new location with its original size. Original file’s changes will
reflect in new file. But there is no linking showing in long listing because
new file is pointing to the original file’s inode. if we delete any one of file
or original file then it will not impact on this hard link. Another file will
be accessible.

Difference
between Soft Link and Hard Link

Difference

Soft Link

Hard Link

Inode

Inode will be different for both the files

Inode will be same for both the files

Deletion of original file

Deletion of original file, impact on link. Another file will not be
accessible.

Deletion of original file, no impact on link. Another file will be
accessible.

Time to execute

Access time is slow as compared to hard link

Access time is fast as compared to soft link

Cross File System

In Cross file system, Soft link works

In cross file system, hard link is not working. Needed same file
system

Inode in file system is
a fundamental concept of unix file system and each object in the file system
represented by an inode. Inodes store
information about files and directories like ownership, access mode, file type
etc. Each file is having unique inode. When we copy file from one location to
other that time new inode number assigned to the new file.

If you want to see the inode number of file then use
following command:

root@hello:~/hello/shellscript#
ls -i shellscript.sh

5644164 shellscript.sh

root@hello:~/hello/shellscript#

5644164 is the inode of shellscript.sh file.

We will copy this file in other location and check the inode
for same file:

root@hello:~/hello/shellscript#
mkdir test

root@hello:~/hello/shellscript#
cp shellscript.sh test/

root@hello:~/hello/shellscript#
cd test

root@hello:~/hello/shellscript/test#ls -i shellscript.sh

5770669 shellscript.sh

root@hello:~/hello/shellscript/test#

You can see same file is having different Inode5770669. i.e. Each file is having unique inode.

Wednesday, April 24, 2013

We will go through the examples of Sed substitution using regular expression as follows. Sed command in unix examples, we have given as follows. For all examples, we will consider one file as an input and get desired output from that file. i.e. employee.txt

root@rhel:~/test# cat employee.txt

1001,Ajay,Manager,25000

101,Satish,Founder,30000

302,Atul,CEO,26000

434,Raj,Senior Manager, 26000

1231,Kalyan,Human Resource,20000

root@rhel:~/test#

Replace the last five characters in every line of file

In this example, we are replacing last five characters of each line by “Salary not decided” phrase.

root@rhel:~/test# cat employee.txt

1001,Ajay,Manager,25000

101,Satish,Founder,30000

302,Atul,CEO,26000

434,Raj,Senior Manager, 26000

1231,Kalyan,Human Resource,20000

root@rhel:~/test# sed 's/.....$/Salary not decided/g' employee.txt

1001,Ajay,Manager,Salary not decided

101,Satish,Founder,Salary not decided

302,Atul,CEO,Salary not decided

434,Raj,Senior Manager, Salary not decided

1231,Kalyan,Human Resource,Salary not decided

root@rhel:~/test#

Delete all commented lines in a file

In this example, we are deleting all commented lines of a file by sed command.

Wednesday, April 17, 2013

Groupadd command is used to create a new group account using the
specified values with options or default values from the system. All
information related to the group added into the system files as needed. There
are many options present in group add command to change account settings of
group.

We will understand all major options of groupadd command with examples:

How to create new group ?

Suppose, we want to create a new group and but we are not sure given
group ID is used somewhere or not. So, in this case we can use –f option with
–g option to create group forcefully. i.e. if gid is already used then it will
take new gid and create the group.

As below example, 1010 gid is used already but due to –f option,
command executes and it has taken next group ID i.e. 1011.

root@rhel:~#
cat /etc/group | grep test

test1:x:1010:

root@rhel:~#
groupadd -fg 1010 login

root@rhel:~#
cat /etc/group

login:x:1011:

How to create system group?

We can create system group with –r option with groupadd command. The
numeric id of system groups always chosen in the SYS_GID_MIN-SYS_GID_MAX range,
which is defined in login.defs
instead of GID_MIN-GID_MAX

Usermod command is used to
modify user account. This command is useful to change the parameters of only
existing users. It can change home directory of existing user, login name,
login shell, comment of passwd file etc.

We will understand usermod command with examples:

How to change home directory of existing user?

Sometimes we need to change
user’s home directory with new directory but all content of old directory
should have to move new directory. In this case, we need to use –m option with
–d, which moves all old directory content to new directory. If we don’t want to
move old directory content to new then no need to use –m option.

Test1 is already created user. We need to move old directory content to
new location and set it as new home directory.

Sometimes, we forget to mention shell while creating user. But we can change the default shell to desired as
below:

root@rhel:~/test2#
useradd test1

root@rhel:~/test2#
su test1

$

$

$ echo $USER

test1

$

$ echo $SHELL

/bin/sh

$

$ exit

root@rhel:~/test2#
usermod -s /bin/bash test1

root@rhel:~/test2#
su test1

test1@rhel:/root/test2$
echo $SHELL

/bin/bash

test1@rhel:/root/test2$

Rename the user

We can change the login name of
user with usermod command. But if
you change the username then it will not impact on other details like home
directory, expiry date etc. It will remain same.

root@rhel:~/test2#
cat /etc/passwd | grep test

test1:x:1008:1010::/home/test1:/bin/bash

root@rhel:~/test2#
usermod -l login1 test1

root@rhel:~/test2#
cat /etc/passwd | grep test

login1:x:1008:1010::/home/test1:/bin/bash

root@rhel:~/test2#
su login1

login1@rhel:/root/test2$

Lock user’s password

Root user can lock user’s
password as well as unlock it
using the usermod command. As below example, we locked user’s password by –L
option through root user and then tried to switch user from normal user but
couldn’t login. Again login by root user, unlock the user’s password by –U
option and the tried to login then it is allowing successfully.

root@rhel:~#
usermod -L login1

root@rhel:~#
exit

logout

machindra@rhel:~$

machindra@rhel:~$
su login1

Password:

su:
Authentication failure

machindra@rhel:~$
sudo -i

root@rhel:~#
usermod -U login1

root@rhel:~#
exit

logout

machindra@rhel:~$
su login1

Password:

login1@rhel:/home/test$

login1@rhel:/home/test$

Change UID of existing user

We can change the UID of
existing user by usermod command. For this purpose, we need to use –u option with
desired UID and it should not be used by any other user.

Old UID: 1008

New UID: 3045

root@rhel:~#

root@rhel:~#
cat /etc/passwd | grep test

login1:x:1008:1010::/home/test1:/bin/bash

root@rhel:~#
usermod -u 3045 login1

root@rhel:~#

root@rhel:~#
cat /etc/passwd | grep test

login1:x:3045:1010::/home/test1:/bin/bash

root@rhel:~#

Change the group of user

We can change the group of user
with –g option. Old group id of user login1 is 1010 and we will change it to
130 i.e. group will be rdeck.

Tuesday, April 16, 2013

Useradd command is very useful in unix to add
user or update the user’s information or user’s home directory, comment etc.
Almost all options are same for useradd command in solaris/Linux/Ubuntu. There
are many uses of useradd command. We will understand the useradd command with
examples:

Add user with home directory:

If you are not specifying the home directory
while creating user, then system will create default base directory for that
user. The base directory is concatenated with account name to define home
directory. If you want to specify different home directory then we need to
specify home dir(-d). When we use –d option then no use of –b option.

[root@rhel home]# useradd -d /home/test test

[root@rhel home]#

[root@rhel home]# ls -lrta /home/test

total 28

-rw-r--r--. 1 test test 124 Jun 22
2010 .bashrc

-rw-r--r--. 1 test test 176 Jun 22
2010 .bash_profile

-rw-r--r--. 1 test test 18 Jun 22
2010 .bash_logout

drwxr-xr-x. 2 test test 4096 Jul 14 2010 .gnome2

drwxr-xr-x. 4 test test 4096 Mar 20 18:06
.mozilla

drwxr-xr-x. 4 root root 4096 Apr 12 22:59 ..

drwx------. 4 test test 4096 Apr 12 22:59 .

[root@rhel home]#

Add user with comment, home directory:

If we want to give comment to identify the
user then we can use –c option and for home directory –d option.

We can disable added user automatically after
some period. For this purpose, we need to specify date on which the user
account will disable. The date should have to specify in YYYY-MM-DD format. If
this parameter is not specified while creating user then system will consider default expiry date (/etc/default/useradd)
specified in this file. If you want to extend the expiry date of user then you
can do it by usermod command.

[root@rhel home]# useradd -e 2013-04-17 -d
/home/test3 test3

[root@rhel home]#

[root@rhel home]# cat /etc/passwd | grep test3

test3:x:503:503::/home/test3:/bin/bash

[root@rhel home]#

Add user in existing group:

We can add user in existing group by
specifying group name while creating user first time. We can change the group by
usermod command after creation of user as well. Or we can insert user in
multiple groups as well. If group is not already exist then create it by
groupadd command.