Managing User Account in Linux

Users

Managing user account is an important task for the system administrators on their daily task, in this article  I will explain to you how to administrate the user accounts and also we will see the configuration files that are needed for maintaining the user accounts.Basically all the users on the system are identified by username and the user id(UID) number,Humans can recognize the user by its username but the operating system uses the UID number to identify the users in your system, when you create a user account by default a UID will get generate with an account.Each and every user will have the unique UID number.

Special Users

While installing the operating system some default user accounts will get created in your system, these accounts are normally called as the default system accounts.These special users will have different UID numbers.

Groups

Every user on your system is also a member of one or more groups.Instead of setup individual permissions for each and every user, adding a user to a group and then assigning the permission is the easiest way of setting permissions for different users.Like UID groups will have GID (group identification number).

System default configuration files that store the user account information

When you create a user or group all the default information will get an update on some configuration files, there are three important configuration files available to store all the user and group updated information.As you know all the configuration files come under the directory /etc inside this we will have passwd,shadow&group files.
1./etc/passwd
This /etc/passwd file stores the User essential information which is must require during the login.Total seven fields are there in this file, By default, the passwd file will look like below entry format only.
a)Username
a)Password
b)user ID
d)Group ID(GID)
e)Comment
f)Default Home Directory
g)Login Shell
Each field is separated by a colon(:)
Let me explain the fields one by one
Username: User Id when users logs in to the server.Maximum allowed characters for the username is in between 1 and 32.
Password: An character indicates the encrypted password is stored in the /etc/shadow file.
User ID(UID): The UID number for the root user is “0”, The UID 1-499 is reserved for the default system accounts, above 500 will use it for the secondary user accounts which we create it manually by useradd command.
Group ID(GID): It shows the Group ID that is stored in /etc/group file.
Home Directory: The default home directory for non-root user logins, if this directory does not exist then the user directory become / only, login problem might occur if /home is not available while login.
Login Shell: This indicates The default shell to be used when the user login to the system.
Let me show you the screenshot of /etc/passwd file how the  fields are separated,

Check the  file permission for /etc/passwd

#ls  -l /etc/passwd
As this file contains sensitive user information The permission for other user is set to read-only so that users cant modify this file,

2)/etc/shadow

This file holds the user’s encrypted password information, once you have created the password it would be encrypted and stored inside this file along with your login name.Only the root user can read this file, other users cannot read this file.Let us have a look at this file
#cat  /etc/shadow

1.Username: This is your login name
2.Password: Your encrypted password information, The $id is the algorithm used on GNU/Linux as follows
a.$1$ is MD5Algorithm
b.$2a$ is Blowfish Algorithm
c.$5$ is SHA-256 Algorithm
3.The last password changed: Days since the last password was changed.
4.Minimum: The Number of days left before the user is allowed to change his password.
5.Maximum: The no of days the password is valid
6.Warning: The number of days before password is set to expire that users are warned to change his password.
Note: Last two fields separated by colon are mentioned below
7.Inactive: The number of days after  password expires that account is disabled
8.Expire: Days since the account is disabled.
Note: The password filed which starts with an exclamation mark (!) means that the password is locked if it starts without ! means account is unlocked.
Let me show you this with one example…
When the account is in locked state

From the above output, you can see the encrypted password starts with the ! mark which indicates  the account is in the locked state
After the account is unlocked

From the above output, the encrypted password starts without the ! mark coz the account has been unlocked.

3./etc/group file

It holds the user groups information like which user belongs to which group, As like the above files all the entries are separated by colon(:)

1.Group name: It indicates the group name
2.Password: By default password is not used hence it is empty, if the password is there for the group then it can store the encrypted password, If you need a group with privileged access then create a password for a group.
3.GroupID(GID): All users must be assigned a group ID when you check the /etc/passwd file you will find the group associated with each account.
4.Group List: It holds the usernames who all are members of the group, all the names are separated by commas.

To Check the group informations

#cat /etc/group

or

#less /etc/group

or

more /etc/group

To find out the Groups the user is added

#groups  <user name>
#groups  vasanth

Here the user Vasanth belongs to the system groups ntp and adm.

How to create a user account?

Creating a user to Linux box is very easy, however, this operation is allowed to be performed by the root user only.In two ways you can add a user to Linux box.
1)By editing the /etc/passwd file(i.e,Manually adding all the fields like UID,GID,LOGINNAME,COMMENT,SHELL)
2)By using the “useradd” command which creates the account automatically as long as you give the correct details

Syntax: To create a user account by using the “useradd” command

#useradd    -u <uid>    -g <gid>    -d <home_directory>  -s <login_shell>   -c <comment>    <login_name>

Options:

s —–>To define the user Login shell
c —–> To leave a comment for a user account
Now let us add a user account by using this syntax
#useradd -u 1500  -g  10  -d  /home/nirmal  -s  /bin/bash  -c "Site Admin"  nirmal

After adding the account successfully, all the information will get automatically updated in the /etc/passwd file.
#cat /etc/passwd

From the above output, all the fields successfully updated in /etc/passwd file.

Now if you want to confirm to which group the user “hema” was added run the following command,

#id  <user name>

#id  hema

The group name for the id 10 is “wheel”.If you have your own group you can also mention that with the useradd command, in this example i have used the default sys group id 10 (wheel).
Note: Sometimes  the useradd command might fail under the following conditions

1.The UID you specify has already taken

2.The GID you mention does not exit

3.The comment conatins specail charcters syuch as (!) and (/)

4.The shell you specify doesnot exist.

Method:2

Syntax:
#useradd   <user name>
In this method, the system uses the defaults to create the user account and update the same in /etc/passwd file,
#useradd  jeya

Now check the account details in /etc/passwd file

#cat /etc/passwd  |grep jeya

Note: The root UID and GID is always 0, and default group for root is always 0.
Note: Check the second field that appears with “x” character which means its a password filed(“x” appears coz we are using the process called password shadowing) I will explain you about the password shadowing in our upcoming posts.
Note: In /etc/shadow if you see exclamation (!!)  in the password field it indicates no password assigned to the user.

 

Since the user Vasanth has the password you will see the encrypted password line, Now check the other users Hema and jeya you can see the !! symbol which says both the users don’t have the password.
As I said useradd <username> will take the defaults to create the user account, if you would like to know what default values would be assigned to a user when creating a user account with useradd command, here you go..
In Linux, there are two configuration files available which hold the default values to be assigned to a user with user add command.
1)/etc/default/useradd file

#cat /etc/default/useradd

or
you can also use the following command to fetch the same details
#useradd -D

2)/etc/login.defs

This file conatin the values like UID,GID,expiry information,password encryption method and many more informations
#cat /etc/login.defs

You can also change the default values with the useradd command, Let me show you some couple of example on how to change the default values  of  useradd command

Change the default values of useradd command?

In two ways you can  change the default values of the useradd command
1.Editing the /etc/default/useradd file manually
2.With useraddd command by using some options

Now am going to change the default  home directory for all new users

#useradd -D

From the above output all the users will use /home as their default home directory, Now let us change this default home directory,
#userad -D -b /var/users

Now check whether it is updated in the configuration file
#useradd -D

or

#useradd -D |grep HOME

The above output shows,  from now onwards all the new users will use /var/users as their default home directory

Change the default Login Shell

By default all the users will use the /bin/bash as their default login shell, now am going to change from bash to bourne shell i.e, sh
#useradd -D -s /bin/sh

#useradd -D

From the output we can see the default shell from now onwards all the new users will use sh as their login shell
Once you have created a user account the next step is to set a password to the account we have a command passwd by using this we can set the password for the account.

Ex:1 To set a password to a account

Syntax:
#passwd  <user name>

#cat /etc/shadow  |grep hema

From the above screenshot, you will not see the encrypted lines in the password filed as the user is not having the password yet and the (!!) indicates the account is not yet set with the password(i.e, No password)
#passwd Hema

New password:******

After creating the password it should get update as an encrypted format in the /etc/shadow file
#cat /etc/shadow |grep Hema

As you can see from the output, before you create a password for the account in /etc/shadow file nothing is showing in the password field you will see only !! (which indicates no password NP), after assigning the password you can see the encrypted line in the password filed.

Note: Even for the account lock it shows the same !! mark

Ex:2 To check the details or status of an account password

With passwd command you have to use the option -S to fetch the status of the account password,

Syntax:

#passwd  -S  <username>

S --> To fetch the status of the user password

#passwd -S  hema

The result  will give you seven fields, each one with different status
1.The first field is USER LOGIN NAME
2.The second field says whether the account is in locked state(LK) or no password(NP)
3.The third field shows the date of the last password change
4.The Fourth field shows the Minimum age for the password
5.The fifth field shows the maximum age for the password
6.The sixth field shows the warning period for the password
7.The seventh field shows the inactivity period for the password.

Ex:3 To Lock a specified account

Syntax:

#passwd   -l   <username>

l -->indicates to lock the account password

#cat /etc/shadow  |grep hema

Now lock the user account as below
#passwd -l  hema

Now check the shadow file for the changes,
#cat /etc/shadow  |grep hema

Ex:4  To Unlock the account

Syntax:
#passwd  -u  <username>
#passwd  -u hema

#cat /etc/shadow  |grep hema

From the output you can see once the account has brought it back to unlock state the !! mark removed before the $ sign, so as an admin you should know the meaning for !!, NP, PS in the shadow file.
I will show you one small example of how the status is getting updating before and after the account is locked and unlocked

PS –>Account has password and it is in active state
LK –> Account is Unlocked

Ex:5 To set Minimum number of days Before the password change

The user cant change or modify his/her password till the minimum allowed days gets completed,
if I assign 6 days as a minimum password age for the user Vasanth then the user Vasanth must have to use the current password for at least 6 days and he is not allowed to change the password within these 6 days.
Syntax:
#passwd  -n  <days>  <username>
#passwd  -n   6  vasanth

Now check the password status for the user Vasanth,
#passwd  -S vasanth

From the above output now the minimum days required to change the password is changed to 6 days

Ex:6  Set the Maximum number of days before the password change

Is nothing but telling the user how many days the user can use the current password, means within this allowed maximum days the user must have to change his/her password, once the maximum days get over the account will automatically Lock.
Syntax:
#passwd  -x <days> <username>
#passwd  -S hema

From the above screenshot the max number of days allowed before the password change is 7 days for the user Hema, Let me modify this by using the following command
#passwd  -x 10 hema

Now check the status
#passwd  -S hema

Ex:7 How to Set warning days before the password expires

If you set the warning days for a user then he/she will receive an alert message to change the password 12 days before the account expiry date.
Syntax:
#passwd  -w  <warning days>  <username>
#passwd -w 12  hema

Now check the status whether it is updated on the password management file

Ex:8 How to DELETE the password for a user account?

In two ways you can perform this, one is by editing the /etc/shadow file,i.e, removing the encrypted line for the user and the second one is its quite easy way to execute by using the “passwd” command with the “-d “option you can remove the password.
Synatx:
#passwd -d  <username>
Let me remove the password for the user hema, remember after removing the pasword check the password staus in /etc/shadow fiile
#passwd  -S hema

Now delete the password by using the following command
#passwd -d Hema

#passwd -S hema

or

#cat  /etc/shadow  |grep hema

From the above screenshot, you will see the password status has been updated on all the password management files.
In our next tutorial, i will explain you how to control the password management by using the “chage” utility.
If you found this article useful, Please do Subscribe and share it with your friends.Thank you🙂🙂

 

[rainmaker_form id=”235″]

Importance of “lsof” command in Linux

lsof stands for List Of Openfiles is a powerful  command  to analyze which files are open by the process .this command really helps the system administrators to keep track of the process usage, When  you are trying to unmount a filesystem or device and if it shows the device is busy  means the files are being used, with the help of the lsof command now we can easily identify the files which are in use.

What do we get from the lsof output?

With lsof you can use some options to get more detail output about the open files by the process, Below are the details you can get it after executing the command

1.Process in the system

2.User

3.Network service

4.Regular file

5.Directory

6.Network file (NFS, Internet socket, Unix domain socket)

Note: By default in Unix/Linux this command comes with pre-installed. When you are executing lsof and if it is showing error lsof: command not found, it could be the command lsof is not in your PATH, check with /bin and /sbin directory for this command if the command is not listed in these directories then you have to install it manually.

Now let us see some of the examples with the lsof command in detail,

Ex:1 To list all open files by all the process

#lsof

Without any option, this will list you all opened files and process.

From the above output, you can see the details of all open files, FD column stands for File descriptor and it shows some values

CWD Current working directory

rtd Root directory

txt Program text code

mem Memory

FD column numbers like 10u is a file descriptor and it is followed by u,r,w modes

r means read access

w means write access

u means both read and write access.

TYPE –file types and  identity

DIR– Directory

REG– Regular file

CHR-Character special  file

FIFO-First In First Out

Ex:2 How to get the details of all process which has opened file?

#lsof   /hello.txt

In this example I have opened the file /hello.txt for live monitoring so I use tail -f /hello.txt to let the file in open stream, Now check with the lsof to see which process is using the file /hello.txt

As you can see from the above output the file /hello.txt is opened by the process “tail”

Ex:3 How to list all opened files by a user?

by adding  -u option with the lsof  you can get the files which all are opened by the user

#lsof  -u Vasanth

www.linuxvasanth.com

From the above output, you can see the files opened by the user Vasanth (marked with square red box)

You can also add multiple users by providing comma between the username

#lsof -u anis, Nirmal, Marshall

Ex:4 To list all files opened by  a particular command

#lsof  -c  <command>

Let me put a file in buffering mode by using the tail -f  /cts then after that  run the lsof to view files opened by the tail command

#lsof -c  tail

From the output, you can see the files opened by the tail command from the path /home/Vasanth/data file and then from the root directory path /cts file and much more…

To list all files opened by more than one  commands use the below syntax

#lsof -c firefox,top

Ex:5 To list files opened by a particular User and command?

Here you can also combine the options -u and -c together

#lsof  -u Vasanth  -c firefox

From the output, you can see the user opened files as well the files which all are opened by the command firefox.

Ex:6 How to list all open files by a process using the PID number

Its nothing just add the option -p with the lsof command will list the files opened by the process with PID

First get the PID number of the running program by using top or ps command

#top

Once you got the PID use the same with the lsof command.Here I use the PID 18

#lsof -p  <PID>

From the above output, the PID has opened some files from the path / and /proc and also you can see the user who is running that program(here root), the command name and what type of files the PID is using and much more.

Ex:7  To list all network connection

#lsof  -i

here I means internet socket i.e TCP and UDP sockets)

From the above screenshot, you can see the port status whether it is listening or non -listening, the type of protocol connected, the node and many more details you can find it.

If you want to get all the TCP open socket connection details

#lsof  -i tcp

Ex:8 How to get which process is using a port?

you can also use the netstat command  for this

#lsof  -i:22

you can also use the service name instead of the port number

#lsof -i:ssh

I hope you have understood the need of using the lsof command in Unix/Linux Operating system.

If you found this article useful, Kindly Subscribe here 👉🏿👉🏿Click here to Subscribe

 

 

 

How to Unmount a Busy Filesystem in Linux

In our previous tutorial I have explained the concepts of mounting and unmounting the filesystems, Now let us see how to unmount a busy filesystem …In Linux/Unix If a device is reporting busy then it won’t let you bring the device to inactive state, the file system will report busy (umount /dev/***: device is busy)when you  try to unmount that it could be  due to various reasons,
1.When more users are accessing that filesystem.
2.Any media mounted in that mount point(CD/DVD/FLOPPY/USB).
So bringing those filesystems to Unmount state without any data loss is the challenging for most of the system admins.
We have a utility called “fuser”  it helps us to unmount a busy filesystem without any data loss.

What is meant by fuser?

fuser helps us to identify the processes that are currently accessing the filesystem by giving the owner name for the processes, the process id number and much more…With this utility, we can also apply the options to get the brief details from the fuser output.Here are some of the important options we use frequently with the fuser utility.

Options:
k – Kill the process
c – Current Directory
e – Executable file being run
v – Verbose output
u – To get the username.
Let me show you how to unmount the busy filesystem with the help of “fuser” utility.
Syntax:

#fuser  <option>   <mount point directory path>

or

#fuser  <option>  <device name or filesystem>

Ex:1 Unmount the busy filesystem.

On my disk I have a  filesystem /dev/sda2 and it is mounted on the mount point directory /home, As all, we know /home is the default home directory for the normal user logins Let me first log in to the server as a normal user(nirmal), then after that as a root user I will try to unmount the /home filesystem ,obviously it will give you the output as the “Device is busy” as all the initialization files will run from this directory to create the user login desktop.

Check the mounted filesystem details

#df -h

From the above output, the filesystem /dev/sda2 is mounted on the directory /home

Umount the /dev/sda2 filesystem

You can either use device name  or mount point directory to unmount

#umount  /dev/sda2

or

#umount /home

To Learn Mount and Unmount  concepts click this link—> Mounting and Unmounting

The above output says the device is busy since it is accessing by some process, Now check how many processes currently occupying the filesystem.

Identify the processes occupying the current directory

#fuser -c  /home

From the above output  the numerical value indicates the “PROCESS ID”  and character “c” means  the “Current Directory”, so currently, two processes are running on the filesystem, Now let us try to kill the processes that are occupying the current mount point directory by running the following syntax,

#fuser -ck   /dev/sda2

k –>kill

Check whether the running process successfully killed or not,

#fuser -c /dev/sda2

From the above output it is confirmed all the process killed by the fuser, Now try to unmount the filesystem

#umount /dev/sda2

Now this time you will not see the device busy error

Now confirm /dev/sda2 is unmounted or not by running the following command

#df -h

From the above output, the filesystem /dev/sda2 successfully unmounted.

Ex:2  Display all the Processes that are using the current Directory

#fuser   .

Here “.” indicates the current working directory

From the above output, we can see more processes are occupying the current directory.

Ex:3 Check with the -v verbose output

#fuser -v  .

The output now displayed the owner name of the process, PID  and much more in a separate column.
Note: You can also use the -u option with the “fuser” command to get the owner list for all the processes that are occupying your current directory

#fuser -cu  /home

Ex:4 Display which Processes using the executable

In this example let me try open the firefox page on my server by using the command “firefox” and after that let us check whether “fuser” identifies the executable file path from this firefox program.

#firefox

Now, I will get the path for the executable program(firefox) by running the following command,

#ps  -aef  |grep firefox

From the output the first line shows the executable path for the Firefox, we will use this path with the fuser now,

#fuser  /usr/lib64/firefox-3.6/firefox

 The output shows the PID of the process and “e” indicates the file is an executable one.

Ex:4 Umount the filesystem with “-f” option

You can also unmount a busy filesystem with the -f option(forcefully), But remember running the following command will put your filesystem in maintenance state  or data loss also may occur as it will forcefully kill the running process ,So it is highly recommended before you test this in your production box take a full back up of the particular filesystem so that if any data loss occurs you can restore it back later .
Note: Programs which access the files will get an error after unmounted with -f option.

#umount -f /home

###################################################################################

 

If you found this article useful, Kindly Subscribe 👉🏿👉🏿Click this link to Subscribe

 

 

 

Understanding Mount and Unmount Concepts.

Mounting is attaching the filesystem to the directory structure in Unix and UNIX like operating systems.As all we know the filesystem is used to organize the data on a system or in storage media like USB, CD-ROM, DVD.In flavors of UNIX operating system, all the directory falls below the / (i.e root) directory.
If you want to access the data on a storage or partition devices, you must let the Operating system to know in the directory tree where to mount the device,For example  if you want to access the files from CDROM, the user should inform the OS to make the filesystem  on the CDROM display in some directory, by default /media  or /mnt Directory exists for this purpose.So to access the data user must have to attach the partition to some mount point directory.

Mount point is nothing but, its a directory where the filesystem gets attached or mounted, So all the directories are accessible through “/ “only. Manually users can create the mount point directory at any locations.
Mount: To make  the  filesystem visible to the users and the operating system
unmount: To safely detach the filesystem from its mount point directory tree.usually when we need to shrink a filesystem size or running the filesystem check(fsck) to check the integrity test on the partition it should be on inactive status.
So in order to access the data from the partition, the user must have to attach the filesystem to some mount point directory structure.Let us see how to mount the partition to the mount point directory,
Before you start mounting you should create a partition and format it with the preferred filesystem(i.e ext3,ext4).All we have to do is create an empty directory and then attach the filesystem to this mount point directory.
Syntax: To mount a filesystem to mount point directory
#mount  -t    <Device to be mount>    <mount point directory>

-t  –> To mention the filesystem type(without -t option it takes the default filesystem).

Ex:1 To mount a filesystem


I have a  partition on one of my disks with the size 1GB (/dev/sda3), I am going to use this partition for mounting..
Create a new mount point directory
#mkdir   /mydata

#mount  -t  ext4   /dev/sda3     /mydata

After mounting use the “df” command to check whether it is mounted properly on the mount point directory.
To check the currently mounted filesystem details run the  below command,
#df  -h

h –> human readble format(i.e display the size in (KB,MB,GB)

From the above output, we can see the details of  all the currently mounted filesystem,
Let me explain you the above output in detail(column-wise),
1.The mounted device
2.Total size of the partition
3.Used size of the partition
4.Avaiable size
5.used size in %
6.Where it is mounted (mount point directory).

Ex:2  How to unmount a mounted filesystem safely?


Unmounting is nothing but brining  the active file system in to inactive state, for this we have to use the command “umount” followed by the filesystem name or you can use the mount point directory to safely detach it from its mount point directory.
Syntx:
#umount    <Filesystem>

or

#umount  <mount point directory>
Always remeber before you decide to unmount a filesystem  the following things you need to check,
1.The filesystems is in active state or not(if the filesystem is in active state make it inactive)
2.How many users are accessing that parition (inform all the users regarding this activity)
3.Once you have done all these you can use the umount command to detach the filesystem from its mount point directory.
Check the currently mounted filesystem details
#df -h

The above output shows /dev/sda2 is in mounted state, let us unmount this
To unmount
#umount  /dev/sda2

or

#umount  /mydata

Check whether it is unmounted safely, To confirm that Run once again the “df” command
#df -h

From the above output, it is confirmed the partition /dev/sda2 is unmounted.

Ex:3 How to mount a CD-ROM manually 


As we all  know /dev is the directory which holds all the device files, device files for the CD-ROM will reside in /dev directory.
For the removable media, we have two mount point directory available by default in Linux /media or /mnt The, CD-ROM will be mounted on either  /media or /mnt.
Now if you want to access the files from the CD-ROM, first insert the CD in to the drive and mount the drive manually to /media mount point directory to access the files from the CDROM.Make sure /media or /mnt directory is available before you mount the drive if the directory is not available create a new one.
Syntax
#mount  -t  <device>  <mount point directory>
Before you mount the CDROM we need to check and find the CD/DVD drives available.To do that we have a command “wodim” with the    -devices option it will scan and give the output names found inside the /dev directory.
#wodim  --devices
Sample output: wodim:Overview of accessible drives

0 /dev="/dev/sr0"   rwrw-- :  CD/DVDW SH-5H9K
From the above output CD/DVD drives could access from the location /dev/sr0, Now mount the CDROM
#mount  -t  iso9660    /dev/sr0   /media
Note: ISO9660 is the standard filesystem for all CD-ROM, So here we inform the mount command to mount the iso9660 filesystem type on /dev/sr0(Removable device)

The above output showing some error /dev/sr0 is write-protected because you are trying to mount the CDROM with read and write access, Remember always you have to mount the CD/DVD devices with ro permission only.
#mount -t  iso9660  -o  ro   /dev/sr0   /media

After mounting you can access  all the files from the CD-ROM from the mount point directory /media
Run the “df -h” command to  check the currently mounted filesystem information
#df -h

Now to access the files from the CDROM we need to change our path to the  /media directory and list  all the files
#cd  /media
#ls

From the above output, it prints all the files from the CDROM.

To unmount the  Media


#umount  /media
#df -h

Follow the same steps if you want to mount manually the DVD/floppy devices.

Importance of /etc/mtab file


In Linux to  monitor and track of mounted filesystem  the first method is  loaded in kernel and deliver the information to the user via /proc/mounts file, and the second method is with the file  /etc/mtab(mount table),the “mount” command uses this file to retrieve and display all the information.
/etc/mtab is a sensitive file as the kernel always track the mounted filesystem, so editing this file by hand would cause the system to lead to inconsistency and booting problem.
#cat  /proc/mounts

The above screenshot shows, the mounted filesystem details including  the mount point directory, filesystem type, flag information.
Let us see the output of /etc/mtab file
#cat  /etc/mtab

From the above output
1.The first line shows the mounted device or filesystem
2.The second line shows where the device is mounted i.e mount point directory
3.The Filesystem type i.e ext filesystem or removable media type filesystems
4.Options for mounting i.e,rw or ro
5.Dump command option,”0″ means no check
6.Fsck check order (while booting  it will check the filesystems) 0 means no check

Understanding “fstab” configuration file


This file is the most important one in Linux, the path for the fstab is located inside the /etc/fstab directory
fstab will  look for the filesystem and automatically mount them when the system boots next time.
fstab stands for filesystem table ,if you need to mount the filesystem automatically during the booting time you must put the filesystem inside /etc/fstab ,Usually after we mounting the filesystem to some mount point directory it is available for the users to access the data, now what would happen when you restart or shutdown the system?When you power on next time, All The mounted filesystems will go to unmounted state by default, Now To bring the filesystem  to active state  you have to mount one by one all the filesystems  which all are went to unmounted state,
Imagine what would happen If the disk with 100 or more mounted paritions, its really a headche for the system adminstrator to keep all these in memory, so to avoid these difficulties it’s safe to put all the important  filesystems inside the  /etc/fstab file to get automatically mount on next power on the system.
Let us have a look at the enties inside the fstab file.You can use the vi editor to edit this file if you are not comfortable with the vi editor you can use the gedit utility

Description of fstab file


1.LABEL – List the device to be mounted

2.Mount Point- Notes the directory where the filesystem will be mounted

3.Filesystem format- Describe the filesystem type i.e ext2,ext3,ext4

4.Defaults-read-only or read write, exec or noexec (exec means it allows to execute binaries,noexec means cant execute)

5.Dumpvalue- It means the data is automatically saved to disk by the dump command when you exit Linux

6.Filesystem check order(fsck check)- we use numeric values to check the filesystem 0 means ignore, nonzero means check.

#vi  /etc/fstab

the inside view of the fstab file will look like this 👇🏻

From the above 👆🏻 output some partitions and device entries have been  saved already  by the kernel, if you are adding a new filesystem or device you need to manually create a mount point  directory and insert it in to this file./dev/sda2 is not listed inside the
From the above output  /dev/sda2 is not listed inside the fstab file, so every time when you power on the system you will have to manually mount the /dev/sda2 filesystem to its mount point directory to make it visible and accessible to the users and system.
To make it mount automatically whenever you power on the system, add the /dev/sda2 details inside this fstab file.
I am going to show you how it looks before and after adding the filesystem information in to fstab file.

After login run the df -h to check the mounted filesystem details , it will not mount the /dev/sda2 filesystem automatically.

Now put the /dev/sda2 details inside the fstab file and reboot  the system, once you logged in check once again with the df -h  this time /dev/sda2 will get automatically mounted, let me show you how to add the /dev/sda2 filesystem entries inside the fstab file.

I have successfully added the /dev/sda2 filesystem entries inside the fstab file, use the tab button to go to the next field don’t use the spacebar  button to go the next filed doing so would create error and also it may  cause booting problem as  kernel will check each and every line inside the fstab  during the booting process, if no error found inside this file then after that it starts processing further program, even if you add a  dot or, any special characters (which is not required) inside the file, the system will not boot.
Now restart the system to make the changes effect,
#reboot

Now check whether /dev/sda2 is automatically mounted
#df -h

As you can see the above output  /dev/sda2  gets automatically mounted since we have mentioned the filesystem details inside the /etc/fstab file.

To display all the mount details


After mounting the partition to some mount point directory you can either use “mount command or df -h” to display all the details.
Run the mount command without any option
#mount

To mount all the filesystem mentioned in the /etc/fstab file


The filesystems mentioned in the /etc/fstab file will get automatically mounted while booting, after booting sometime  we unmount some of the filesystems listed inside /etc/fstab for some purpose.
Now if you want to mount only the filesystems that are listed inside the /etc/fstab file run the “mount”  command with the option -a, this will mount only the filesystems that are listed inside the fstab file.
#mount  -a

To unmount  only the filesystem listed in fstab


#umount -a
Note: This command is not recommeded to run in production server as it would bring the server to downstate after execution.

From the above screenshot, it has been confirmed all the filesystems mentioned inside the fstab is unmounted except “/”, coz you cannot bring the / to unmounted state, bringing the / to unmount state is like trying to  format the c:/ drive in windows OS.That is why you are getting an error message stating that “/” device is busy.

How to remount a flesystem


You can remount a mounted filesystem by using the “remount” command,usally adminstartor use this command only when they need to bring it back the filesystem status from read only to read write permission.
Let me show you one example, i have a parition /dev/sda3 and it is mounted as readonly in the mount point directory /skype, now i need to mount the filesystem  /dev/sda3  with read &write  permission.

 

Check the mounted  status of the /dev/sda3 filesystem by using the following command
#mount |grep /skype

Now remount the filesystem with read &write
#mount  -o  remount,rw   /skype

#mount   |grep   /skype

The above output shows the filesystem /dev/sda3 is now mounted with readwrite access.

How to attach the mount point to a new directory


It is possible to bind a moutpoint to another directory, doing so  the users can access the data from both the mount point directory at the same time.With mount command add – -bind option followed ny old mount point and new mpunt point.
Syntax:
#mount  -  -bind   <old mount point directory>   <new mount point directory>
#df  -h

Now bind the mount point to another directory
#mount  -  -bind   /skype   /whatsapp

Now check whether the filesystem can be access via two mount point directory
#mount  |grep  /skype

The above output shows both the mount point merged properly, Now if you do any modification in any of the mount point  it will  get update on the other mount point also.
Let me show you this with one example, Am going to create some directorys inside the /skype , after creating check with the another mount point directory(/Whatsapp) you would see the newly created directory there.
#cd  /skype
#ls
#mkdir  dir1 dir2
#ls

#cd  /whatsapp
#ls

I hope now have understood the mounting and unmounting concepts 😊😊…

##############################################################################################

 

If you found this article useful, Please Subscribe and leave your comments below, ThankYou😊😊

[rainmaker_form id=”235″]