Rescue Mode In RedHat Linux

What is Rescue Mode?

When something goes wrong in your Linux box, there are so many ways to resolve that, however, these methods require that you to understand the system very well. As the name implies, rescue mode is there to rescue you from something. In normal operations, the Red Hat Linux systems use the files located on the system harddrive’s to do everything. But there may be a time when you are unable to get the Linux running completely enough to access its files on your system harddrive’s, By rescue mode, it is possible to access your files stored on your system hard drive, even if you can’t actually run the Linux from your system Harddrive.

When Rescue Mode is required?

You might need to boot in to rescue mode for any of these following reasons mentioned below;

1.You forgot the root password and bootloader password

2. You might need to re-install the Bootloader(i.e, GRUB)

3. Having Software/Hardware issues and you need to retrieve some important data from the hard drive

4.Unable to Boot the Linux in to the run level 3 or 5

Forgot the root and GRUB password:

What if you forgot your root password? You can boot your system to single user mode and from where you can reset the root password by using the “passwd” command, To boot the system to single user mode while booting, via bootloader is the only way to get in to single user mode, now what if I have assigned the bootloader password? You have no options now to get in to the single user mode when there is no way to recover your Linux box, “Rescue Mode” is the only way to resolve all these types of problems.

Having Software/Hardware issues:

There can be as many different situations under this category, Things like failing hard drive and forgetting to run the GRUB after building a new kernel, are the two things that can keep you from booting Rehat Linux. If you can get in to the rescue mode you may be able to resolve the problems and you can also get off some important files from your failing hard drives.

How to Boot the system in to Rescue Mode?

Use the following steps to boot in to the Rescue mode.

Step1: Boot the system from the Installation Media(cd/DVD)

Once the system has successfully booted  from the iso image, you will get the Red Hat Linux boot screen, choose the Rescue installed the system from the MENU SCREEN

Suppose if the Rescue option is not available means then you will need to choose the resue mode by using the following options at the boot prompt

[F1-main]  [F2:option]  [F3:Genera] [F4:Kernel] [F5:Rescue]

boot: Rescue

Step:2 Choose the Language

Select Language by using the arrow  keys and press enter

 

Step:3 Choose the Keyboard Type

Select the Keyboard type by using the arrow  keys and press enter

 

Step:4 Select the media contains the Rescue image

 

Step:5 Network type

If you want to access the system over the network in Rescue mode you can configure it from here. Mostly in the rescue mode network configuration are unnecessary.

 

Step:6 Next, a screen will appear telling you that the program now will attempt to find the RedHat Linux installation to rescue, choose “select” and enter

Step:7 Now you are in Rescue mode, if you grant it, then your root filesystem is mounted under the /mnt/sysimage directory, for example, all you files will be mounted under this location,your /etc/inittab file will get mounted under /mnt/sysimage/etc/inittab.

Once it is mounted under the /mnt/sysimage you will get the below confirmation on your screen

Step:8 Start the resuce mode shell and enter

Run the below command after entering in to the command line mode

#chroot   /mnt/sysimage

After this, you can open the system files to make the neccessary changes to make it function properly.

To exit from Rescue mode:

Type “exit” command twice to come out from the rescue envirnonment

#exit      (Leaving chroot environment)

#exit      (Leaving Rescue mode and restart)

 

Then choose “reboot” and press enter, this time your system will restart and reload all the selinux polices .

 

I hope you have enjoyed this tutorial if so Kindly subscribe and share it with your friends.
                                          🙏🙏 Thank you 🙏🙏
#############################################################
For More Videos Subscribe My Youtube Channel  Linux Vasanth
If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

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

 

Basic concepts of RAID

RAID(Redundancy Array of Independent Disk):

A RAID  is an only way in which you can link up  several hard disks so that if any one of them fails, the other one can take over the load

Types of   RAID:

1)Hardware RAID

2) Software RAID

Hardware RAID: It has its own independent disk subsystem and resources.It doesn’t use any resource from the system such as RAM, CPU, and power.Since it has its own dedicated resources it will not put any additional Load on the system, It also provides very high performance.

Software RAID: Performance wise when compared to hardware RAID software RAID delivers slow performance since it uses all the resources from the system.

concept of RAID:

1.Hot spare: This is the additional disk in the RAID array, if any disks fail, data from the faulty disk will be migrated to the spare disk automatically.

2.Mirroring: The copy of the same data will be available on the mirror disk, like making an additional  copy of data

3.Striping: If this feature is enabled then data is written to all the available disks randomly, it is like sharing the data between all disks, so all of them fills quickly.

4.Parity: The parity regenerates the lost data from the saved parity information.

There are different levels of RAID available based on how mirroring and striping needed, Among these levels, LEVEL 0, LEVEL 1, LEVEL 5   are mostly used in Redhat Linux.

Let us have a look at these different levels of RAID

RAID 0-Striping:

It provides striping without parity, Since it doesn’t store any parity data performs the read and write operation equally, Speed would be much faster than other levels, Minimum two hard disks required for this level.All the harddisks in this levels are filled equally.You should use this level only if the read and write speed are concerned.when you decide to use this level always have a backup plan on your data because a single disk failure from the array will result in total data loss.

RAID 1-Mirroring:

In this level it provides parity without striping, data will be written on both the harddisks, if any of these failed or remove still we can get the data.This level requires two hard disks.It means if you want to use two hard disks then you will have to deploy 4 hard disks or if you want to use one hard disk then you will have to use two hard disks, First hard disk stores the original data while the other disks provides the exact same copy of the first disk, performance is reduced since data is written twice,You should use this level only if data is concerned at any cost

RAID 5 Parity with Striping:

This level provides Parity and striping, This level requires minimum 3hard disks.It writes parity data equally in all disks.If anyone of disk fails, then data can be reconstructed from the parity data available on remaining disks.

NOTE: When you are using Hardware RAID  device, use hot swappable hardware RAID device with spare disks, if any disk fails data will be reconstructed on the first available spare disk without any time.

In our next article, I will show you how to configure RAID in Red hat Linux.

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

Never miss an article Do like my official  FB page 👉 Learn Linux in an easier way

 

LVM Logical Volume Manager

Introduction:

Logical Volume Manager(LVM) is the powerful tool in Linux to manage the Disk management system.Adding more space to the existing filesystem or partitions can be easily done by using the LVM.If a filesystem needs more space it could be added to its logical volumes from the free spaces in its volume group and then filesystem can be resized as per the requirement.

In today’s IT environment all the server’s needs more space day by day and we need to expand that based on our requirements.LVM can be used in RAID, SAN.A physical disk will be grouped together to create a  volume group, Then from the volume group, we need to create the individual partitions and create a new filesystem then mount it on some mount point directory to make it visible to the users.

Let me say this in simple words.To bring a disk in to the LVM structure,

1.Group all the physical disks together to create a physical volume

2.Create a volume group and add all the disks in the volume group

3.Now from the total volume group size we need to create the individual partitions i.e logical volumes

Logical Volume Manager Features:

1.It is very flexible to increase the size of the partition at any time.

2.Any filesystem can be installed

3.Migration can be used to recover the defective disk.

Now let us configure the LVM disk storage,

Ex:1 How to configure LVM

In this example, i  will add three hard disks(more than 3 u can use there are no limitations) to the server.After adding the additional hard disks to the server check whether the kernel recognized the disks by using the “fdisk” command.

#ls -l     /dev/sd*

Note: You can also use the “fdisk -l” command to check.

As you can see from the above screenshot, the newly added disks are recognized by the kernel.

Now its time to configure the LVM,Before we bring the disks in to the LVM structure we should format the disks with the LVM id.This can be done in two ways, I will show you here both the methods to label the disk with LVM partition type.

Remember you have to use either method:1 or method:2 to do this if you try both the methods when you run pvcreate command. you will get the disk missing error message 

Note: LVM Partition type ID is 8e

Method:1
#fdisk  /dev/sdb

Follow the steps as shown in the screenshot to associate the harddisks with LVM id

Repeat the same steps for the remaining harddisks i.e for /dev/sdc and /dev/sdd

Now check with “fdisk -l” whether all the disks are labeled with LVM id partition type

AS YOU CAN SEE THE ABOVE SCREENSHOT ALL THE DISKS HAVE BEEN LABELED WITH THE LVM PARTITION ID.

Method:2

Syntax:

Step:1

#pvcreate   <disk1>     <disk2>   <disk3>

In our case we have /dev/sdb,/dev/sdc,/dev/sdd

#pvcreate   /dev/sdb   /dev/sdc  /dev/sdd

This will write an LVM  header to the devices to indicate that they are ready to be added to a volume group

Step:2 Check the Physical Volume Details(PV)

Now verify that LVM has  registered the physical volumes by using the following command,

#pvdisplay

or

#pvs

From the above screenshot, all the three devices are present in the PV column

To view the same in detailed output run the following command

#pvdisplay

Note: For better view output has been truncated

Step:3  Create Volume Group(VG)

Now add the physical volumes to a volume group

Syntax:

#vgcreate    <VG name>    <disk1>   <disk2>  <disk3>
#vgcreate    VG1   /dev/sdb   /dev/sdc   /dev/sdd

Now if we check the pvdisplay or pvs command again, we can see that our physical volumes are now associated with the new volume group

#pvs or pvdisplay

As you can see from the above screenshot, now the physical volume disks have been added to volume group VG1.

To check the VG details
#vgs

or

#vgdisplay

As you can see from the above output the volume group VG1 has three physical volumes and zero logical volumes.

Here is the description of each parameter as shown in above screen shot.

1.VG NAME=Volume group name

2.Format = LVM architecture used lvm2

3.VG access = Volume group is in read and write and ready to use

4.VG status = Volume group can be resized, we can expand more if we need  to add more space

5.Cur LV= Currently there were two logical volumes in this volume groups

6.PE size= Physical extents, size for a disk can be defined using PE or GB size, 4MB is the default PE size of LVM, Say for example if we need to create 5 GB size of logical volume we can use the sum of 1280 PE, Got confused????🙄🙄

let me explain this in a easier way,as we all know 1024 MB=1 GB,if so 1024 MB * 5=5120 PE =5 GB,now divide the  5120 / 4= 1280,4 is the default PE size.

7.Total PE=  This volume group have

8.Alloc PE=  Total PE used.full PE already used,6141 * 4 PE = 24564

9.Free PE= Spaces are available as the lv are not yet been started using

Step:5 Create a Logical volume from the Volumegroup pool

We have a volume group available, we can use it for creating logical volumes.Unlike conventional partitioning, when working with logical volumes, you don’t need to know the layout of the volume since LVM maps and handle this for you.You only need to provide the size of the volume and name.

Now we will create three seprate logical volumes from the volume group

2G for  “lv1” volume

4G for “lv2” volume

4G for “lv3” volume

Syntx:

#lvcreate   -L <size>  -n  <Lv Name>   <Volume group name>
#lvcreate  -L  +2G  -n  lv1   vg1
#lvcreate  -L +4G  -n  lv2  vg1
#lvcreate   -L +4G  -n    lv3  vg1

Now check the logical volumes and their relationship with the volume group

#vgs

Step:6 Create a filesystem on logical volumes

Everything is done,inorder to make use of this logical volumes to store data we need to create a filesystem

#mkfs  /dev/vg1/lv1

#mkfs /dev/vg1/lv2

#mkfs  /dev/vg1/lv3

Repeat the same for the remaining logical volumes also i.e for lv2 and lv3

Step 7: Mount the Logical volumes to some mount point directory
#mount  /devv/vg1/lv1   /BACKUP

#mount  /dev/vg1/lv2   /DATABASE

#mount  /dev/vg1/lv3  /WEBDATA

All the logical volumes have been successfully mounted and its now ready to use

check the mounted filesystem information by using the following command

#df  -h

In our next article, we will see how to grow the size of the logical volume partitions, increase the size of the volume group and many more.

Never miss any articles from Vasanth blog follow my facebook page for updates Learn Linux in an easier way

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

 

 

 

Understanding SSH and SCP Protocols in Linux Operating System

What is SSH?

SSH is a Secure Shell protocol that lets you to open the remote terminal or shell session on any Unix based server where according to the permission available to the account you logged in to and execute commands.The primary advantage of ssh over other protocols including telnet is that everything you do during the session  will be encrypted so that anyone who might be watching at any point between you and  remote host will see only the unreadable text

Note: SSH stands for Secure Shell.All SSH session is encrypted and it requires authentication.It provides a very safe and secure way of exchanging the commands, configuring the services over remotely.Another important point is when you connect to the remote server using ssh you log in using an account that exists on the remote server.

Note: The port number for SSH protocol is 22

An Overview of how SSH works?

1.When an administrator connects to the remote server using SSH he will be dropped in to a shell session (usually bash), where you can execute commands, it will allow you to use only text-based  interface, whatever command you execute in to your local terminal are sent through an SSH tunnel (with encryption)and then it executed on your server

2.The SSH connection is purely based on the client-server model this means for an SSH connection to be established, the remote server must be active with the ssh daemon(sshd).This daemon will listen for the connections on the specific port(ssh), it authenticates the connection request and allows the connection if the user provides the correct credential details.

3.The client system must have an SSH client software and this software knows how to communicate with the SSH protocol, provides information about the remote host, username to use, credentials that need to passed to authenticate and many more.

How does SSH Authenticate users?

1.Most clients use to authenticate by using the password which is very less secured and not recommended to use, use the SSH keys which is a very secure way to connect.

2.SSH keys are sets of cryptographic keys which can be used for the authentication.Each set contains Public key and Private key.

Public Key: It is made available to everyone, it can be shared with anyone without concern.

Private Key: It must remain confidential to its respective owner

Note: Whatever is encrypted with a public key only be decrypted by its corresponding private keys.

Let me tell you how ssh key-based authentication works at the backend,

If you want to authenticate using SSH keys, the user should have an SSH key pair on their local system, now on the remote server, the public key must be copied to the file within the user’s home directory at ~/.ssh/authorzied_keys.This file contains a list of public keys, one per line, that are authorized to log in to this account.

Now when a user connect to a host, wishing to use the SSH key-based authentication, it will inform the server of this request and tell the server which public key to use,then the server checks its authorized_keys file for the public key,generate a random string and encrypts it using the public key,this encrypted message can be only decrypted with the associated private key.The server then will send this encrypted message to the user to test whether they actually have the associated private key.

Upon receipt of this message, the client will decrypt it using the private key, it then compares both the values if both the values are same then it allows the connection.This is how  SSH key based authentication works.

Now let us see how to connect to the remote server with SSH protocol

Ex:1 Connect to remote server  from local server:

The basic syntax to for this as follows,

#ssh <remote server ip address or host name>

In this example, I use my two  Linux servers for the demonstrations.

Server details:

Server1 IP address:10.250.1.50/linuxvasanth.com-Located at USA

Server2 IP address:10.250.1.100/productionclient-Located at LONDON

Let us see how to connect to server2 from server1.

linuxvasanth.com #ssh   10.250.1.100

If this is the first time you use the SSH you will see the below messages on your terminal

 

After giving Yes this will add your server to your list of known hosts(~/.ssh/known_hosts)

Each and every server will have a host key and the above confirmation question is related to verify and save the host key, Now next time when you connect to the server, it can easily verify that it is a trusted known server.After the server authentication is successfully finished it asks for a password.

Note: By default, SSH allows the direct root login, so here you have to give the root user password of the remote server(i.e 10.250.1.100 ip root user)

Now you can execute any commands, can configure services and many more.Here, for example, my task is to create a user account and password on remote server

The above output says the account has been created successfully on the remote server.

Once you have done with your task with the remote server you can leave the session or disconnect by using the exit command

To exit the connection

A

As you can see from the above output after executing “exit” command the remote server login session gets disconnected and your terminal now changes it to your local server session.

Ex 2: How can I log in as a normal user to a remote server?

In our first example I have explained how to login to remote server as a root user, As you know by default SSH allows the direct root login to remote server, in case if you want to connect to the remote server with non-root user run the following syntax

Note: Check the non-root user account exists or not on the remote server before you start.

Syntax:

linuxvasanth.com #ssh non-rootuser@remoteserverip
linuxvasanth.com #ssh john@10.250.1.100

After giving john user password you will connect it to the remote server terminal session as follows

Now if you want to gain root access you can use the switch user command “su” to switch to multiple user accounts as follows

Now to disconnect the session first you need to log out from the accounts you have connected as follows

How to change the default SSH Port number?

To protect your server from anonymous attack changing the default port number to another any unused port number would help .all users with Linux servers can change the SSH port number from SSH configuration file(the default port number for SSH is 22).

The configuration file for SSH is /etc/ssh/sshd_config

All you need to do is edit this sshd _config file, open the file with your preferred editor, before that it is always good to take a backup of the original file before you made any changes in it.

#cp  /etc/ssh/sshd_config    /etc/sshd_config.original

Open the file with the vi editor

#vi   /etc/ssh/sshd_config

# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.

#Port 22 -->default port number used for SSH now change this to your prefered port number
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
"/etc/ssh/sshd_config" 137L, 3848C

From the above file the line #port 22 here the # means it tells the server to ignore anything after it on the same line, now we will need to remove that character and put your preferred new port number.

Note: Make sure you are not using the port number which is already in use.If you are unsure check this TCP/IP and UDP Port numbers and its uses

Try to use the port number which is not listed in the above link, here I use 2222 port number.

Note: In firewall also you will need to change SSH port number to the new one.

Now restart the SSH service as follows

After making any changes in the default configuration file you will need to restart the respective service to make the changes come in to effect.here we have to restart the ssh service

#service sshd restart

From now onwards SSH will listen to the port number you have specified.

Understanding SCP Protocol in Unix/Linux operating system:

Scp stands for Secure Copy used to send files from Local server to remote server(Uploading) and copying files from remote server to local server(uploading) with securely, secured means all the data while transferring through the network  is encrypted.

SCP is installed by default on all Linux distributions as a part of OpenSSH package

Note: SSH is used to connect to the remote server with text-based interface

SCP- used to transfer files between the different servers

Scp it uses the  SSH port number 22 to establish the connectivity between the server

Ex:1 How to transfer  a file from Local server to remote server

For this example, the syntax would look like this

#scp  <FILE TO BE TRANSFER FROM LOCAL SERVER>  <USERNAME@REMOTESERVER IP ADDRESS>  :<DESTINATION PATH REMOTE SERVER>

Server1:10.250.1.50(Local server)

Server2:10.250.1.100(Remote server)

Now  am going to transfer a file from local server to remote server

#scp  /documents root@10.250.1.100:/tmp

Note:/documents is the local server file to be transfer

/tmp is the remote server destination directory path

Once the authentication is successful the file will transfer to the remote server destination path, you will see the percentage as 100 which indicates the entire data has been successfully transferred to the remote server.

Now to verify go to the remote server (10.250.1.100) /tmp directory and list whether the file /docments is successfully saved.

#cd  /tmp

#ls -t

Note: -t option is to list the latest modified or create file to display  in the first

The above screenshot has confirmed the file has been successfully saved under /tmp directory of the remote server.

Ex:2 How to transfer a directory and all its contents from local server to the remote server?

To copy the entire directory we need to use the option -r  with the scp command i.e recursive which will select the entire directory contents.

Syntax:

#scp  -v  -r  <Local server dir>   <user@remote server ip>:<remote server destination dir path>

you can also use the -v verbose option to view the detail output on your screen.

From Server1 (10.250.1.50) am going to transfer /mydatabase directory to the remote server /myfolder path

As you can see from the above output mydatabase directory is have some files and subdirectories.

#scp   /mydatabase root@10.250.1.100:/myfolder

Note: If you forget to mention -r option while transferring the entire directory you will get the error message it is not a regular file, check the above screenshot.

Always use  -r while transferring the entire directory to the remote server.

After giving the correct password scp will transfer the /mydatabase directory to the remote server directory /myfolder

Now go to the remote server /myfolder path and confirm whether the directory /mydatabase is successfully transferred.

#cd  /myfolder

#ls

AS YOU CAN SEE FROM THE ABOVE OUTPUT THE ENTIRE DIRECTORY ALL ITS CONTENTS FROM THE LOCAL SERVER /MYDATABASE  WAS SUCCESSFULLY TRANSFERRED TO /MYFOLDER PATH.

 

Note: To copy the files from remote server to the local server path you would use the same syntax in reverse as follows
#scp  username@remoteserverip:<remote serverfile>   <local server path>

I hope now you have understood the SSH and SCP protocols their uses in production environment

Never miss an article from this blog, Kindly do like my FB page Learn Linux in a easier way
If you found this article useful, Kindly Subscribe here 👉👉🏿Click this link to Subscribe

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″]

 

 

 

 

How to delete the Partition in RHEL

 Delete the partition:


In our previous tutorials, we have seen the concepts of  Linux disk management and creating the partitions, MBR, GPT partitions schemes, Now we are going to learn how to delete the partition permanently.

Whatever the task we do in Linux there are some procedures available for each and every task that we must have to follow in order to avoid the critical errors.

Procedures to delete the partition:


1.Check the hard disk  partition details

2.Check the filesystem is in active state  or in inactive state (i.e mounted state or unmounted state)

3.Unmount the mounted filesystem.

4.Verify whether it is unmounted properly

45Using the “fdisk “command to delete the partition

6.Save the changes and confirm it by using the fdisk command

Step:1 Check the  harddisk partition details


#fdisk  -l

From the above screenshot, the disk /dev/sdb is having two paritions, we will select the second partition(/dev/sdb2/) here to delete.

Step:2 Check the filesystem is in mounted state  or in unmounted state


#df  -h

The above screenshot says the partition /dev/sdb2 is in mounted state, so now we have to make this in to unmounted state(i.e inactive)

Step:3 Unmount the mounted filesystem


Syntax: To unmount a filesystem

#umount   <filesystem>

or

#umount  <mount point directory>

#umount   /dev/sdb2

or you can use the mount point directory to unmount (I have displayed both the ways to unmount, you can either use the mount point directory or use the filesystem name)

Step:4 Verify whether it is unmounted properly


#df  -h

From the above output, we have confirmed the /dev/sdb2 partition is in inactive state

Step:5  Using the fdisk command delete the partition


#fdisk   /dev/sdb

From the above screenshot, we have successfully deleted the partition /dev/sdb2

Step:6 Confirm it by using the “fdisk” command


#fdisk  -l

Now the partition /dev/sdb2 successfully remove from the hard disk

Note: If you forget to save the partition changes by pressing “w” then it won’t save the changes to the kernel memory when you run “fdisk -l you will see only the previous partition layout information.So always make sure you have saved the partition table changes to the kernel memory by pressing “w” before you exit from the fdisk program.

I hope now you have understood the Linux disk management concepts…if you have any queries feel free to leave a comment….. 

 

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

 

 

How to Create an Extended Partition in Linux

How to create an Extended Partition in Linux


In our previous tutorial we have learned about the Linux Disk Management and also have learned the procedures that have to be followed before creating a partition, So here in this tutorial, we are going to learn about the extended partition and how to create the extended partition.

We know that in MBR partition scheme it allows us to create a maximum of 4 partitions only, in order to create more partition we have to choose the extended partition, from this extended partition we can create more logical partitions.(max 15 Logical partitions we can create).

Step:1 First Create three primary partitions, before that check the partition layout information by using the following command.


#fdisk  -l

 

From the above about its confirmed, the disk /dev/sdb is having  only one partition (/dev/sdb1)

Step:2 Create the second partition:


#fdisk  /dev/sdb

 

After giving the partition number, give the first available sector value or you can accept the default sector value by not entering any values(press enter), Give the last cylinder value the size of the partition in MB, GB, KB format and press enter

Now to save this partition table press “w”

create two more partitions in the same way

To check the partition details run the following command

#fdisk  -l  or u can use “p” option from the fdisk menu

The above output shows now the disk /dev/sdb is having three partitions

Step:3 Create  the fourth partition and then after that try create another primary partition


We have created four primary partitions, Now this will not allow us to create any more primary partitions on this disk since MBR partition scheme doesn’t allow more than four primary partitions.

If you need more partition now you must have to delete one Primary partition from the disk, then we can create one extended partition can be used to create more logical partitions.

When you try to create another primary partition the above output you will find an error message saying that you must have to delete a primary partition in order to create an extended partition on this.

Let me delete the fourth primary partition to make it available for creating the extended partition.

Type “d” option to delete the partition, after that give the partition number you want to delete

Press “w” to save the partition changes

Now Let us create one extended partition so that we can create the logical partitions.Here for extended we have to give the maximum disk space , I  am going to assign  give 1G size for this

Now check whether the new;y created extended partition updated

#fdisk   -l

From the above output we have created an extended partition /dev/sdb4 with the size 1GB, Now we could create logical partition up to 1GB.Now let us create three logical partitions on this, first partition with the size 500MB, Second partition with 100MB size.

From the output, the first logical partition /dev/sdb5 created successfully.

Do the same for the remaining two partitions

Now check the partition details by using the fdisk command

#fdisk  -l

From the above output, you can see the extended partition /dev/sdb4 with the size 1GB, from the extended we have created two logical partitions /dev/sdb5 /dev/sdb6.

Step:4  Now we have to create a filesystem on these partitions.


#mkfs    -t   ext3   /dev/sdb5

#mkfs  -t ext3   /dev/sdb6

Note: You cannot create a file system on the extended partition(/dev/sdb4)  because it cannot be used to hold the data, Logical partitions are used to store the data, so we have to format the logical partitions with the supported filesystem type.

Now to make it visible these newly created logical partitions to the users we have to mount it on some mount point directory structure.

Step:5 To mount a filsystem:


Syntax:

#mount  <filesystem>   <mountpoint directory>

Let me mount all the logical parttions to some mount point directory

#mkdir  /facebook

#mkdir  /whatsapp

#mount /dev/sdb5   /facebook

Repeat the same for the remaining logical partitions

#mount  /dev/sdb6  /whatsapp

Step:6 Now to view the mounted filesystem details, run the command


#df  -h

I hope you  have understood the Concepts of  Partitions in LINUX

If you miss my previous tutorial(Linux disk management) here is the link Linux Partition


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

How to create a Partition in Linux

Understanding Linux Disk Management


Before we start creating the partitions it’s very important to know the basis of the disk management, So let us first learn the basic concepts of the Linux disk management.

The disk management Includes  Adding the disk, Removing a disk from the storage, creating partitions and mounting the partitions to the directory structure

1.BIOS(Basic Input and Output System)

2UEFI(Unified Extensible Firmware Interface)

3.File system type

4.MBR (Master Boot Recorder)

5.Primary Partition

6.Extended  Partition

BIOS/UEFI:

a)This is the first program that starts when you power on your system(based upon the operating system either BIOS/UEFI it starts)

b)The main task for the BIOS/UEFI is to perform the hardware test on your system,This will check each and every hardware devices that are associated with your system,if it found no error then it will search  and run the next process(i.e starting the boot loader program)This test is called as Power on Self-Test(POST).

Let me show you some of the important difference between BIOS and UEFI

BIOS(Basic Input and Output System)

1.Very old method to connect and detect the hardware in the system.

2.It doesn’t provide us any Troubleshooting options.

3.It runs in 16-bit processor mode &4.It has only 1MB of space

4.It supports only MBR partition.

5.It doesn’t have drivers for the latest network cards and other storage devices.

6.Settings are saved in Non-Volatile CMOS memory

UEFI(Unified Extensible Firmware Interface)

1.It came as a replacement of BIOS

2.It has very advanced options for troubleshooting such as emergency mode, remote diagnosis, internet connectivity and backup.

3.It can run in 32-bit or 64-bit mode

4.It supports MBR and GPT partition

5.It can detect and support all the latest modern devices.

6.All settings are saved in Flash memory

Now let us see  what is MBR partition and GPT Partition

MBR(Master boot recorder) Partition :

1.It stores in the 1st sector of the hard disk, it contains the information that is necessary to boot the systems, Inside MBR the bootloader program resides.

2.Bootloader contains only the partition layout pieces of information and how many harddisks connected, it doesn’t know anything about the operating system filesystems.

3.The default size of the sector is only 512 bytes and from these only 64 bytes are taken to save the partition table pieces of information.So due to this less space, it allows creating only 4 partitions(With the limitations of 2Tib(Tebibyte) in each partition).

4.Out of four partitions, MBR it allows one partition to use as an extended partition, with this extended partition we can create more logical partitions(It depends upon the operating system it allows to create more logical partitions), In Linux, we could create 15 partitions.

Note: The size of the extended partition and it’s all logical partitions should not exceed 2TiB

The below diagram explains you  the MBR partition scheme:

 

 

GPT Partition:

1.It supports a maximum of 128 partitions.

2.Here all partitions are equal(i.e, Primary, Extended and Logical partition method not used here)

3.It allows the maximum partition size is 8 ZiB(Zebibyte)

4.It uses the 64-bits standard to save the partition information

5.It has one main feature the information is saved in two locations, so if any one location is corrupted, the system will boot from the alternative one.

Note: In MBR all the information are saved in first sector of the hard disk, so in case if this sector corrupted, then booting process will get fail.

Linux file system types:

In order to use the disk to store your data, a filesystem must be created on the partition.We can also create a separate filesystem type in each partition or can create same filesystem type in all partitions.

File system types support in Red Hat Enterprise Linux:

EXT:(Extended file system): Early version of Linux, Now  EXT is not in use.

EXT2:( Extended file system2): It has very less features only, This was the default file system up to RHEL4.

EXT3:( Extended file system3): The default file system for RHEL5, It has so many features.

EXT3: (Extended file system3)

1.It supports the Journaling features

2.A directory can have 32,000 sub-directories

3.Supports filesystem up too 16TiB in size.

4.Up to 2TiB file size, it supports

EXT4: (Extended file system4) The default file system in RHEL6

1.A directory can have 62,000 subdirectories

2.Supports filesystem up too 1EiB in size.

3.Up to 16TiB file size, it supports

4.It also supports the Journaling features with an additional feature to enable and disable the journaling features.

Now, Let us see how to create a partition from the command line Before you start creating you should know the procedures to create the partition.

Procedure to create a partition:

1.Identify the correct hard disk from the server(coz in production servers you will see more than one hard disk connected)

2.Plan the layout(i.e How many partitions needed, size of the partition, where to mount that partition etc..)

3.Create the partition with the required size

4.Save the partition to the memory

5.Reboot the Linux system (so that that newly created partitions information will get saved in the kernel memory)

6.Make a new filesystem on the partition(i.e in Windows we call it as format a drive )

7.Mount the partition to some mount point directory

Ex:1 Create a 10GB Partition  and mount it to the directory   /sysbase

Let me demonstrate  by using oracle virtual box,  let me show you first how to add a new  hard disk to the server in virtual box

First Power off  the running virtual box,  we cannot add a disk when the  machine is in running state(In real production servers you can add and remove the disk while the server is in running state)

Step:1 Power off the virtual machine

Step:2 Now add a new hard disk to the server with the size 30GB(U can give the size as u wish, but I recommend  minimum 15 Gb you have to give)

Step:3  After choosing the new hard disk click the option .vdi disk and in the next screen it will ask you to choose the storage on the physical hard drive, here you have to choose “Dynamically allocated” after that the size of the hard disk screen will display, give the total size of the hard disk.

After assigning the size click create, you will see the newly attached disk under the controller

 

Then give ok and power on the virtual machine,thats it.. we have completed the initial steps up, now it’s time to create a partition on the newly attached hard disk

Step 4: Run the following command to check  how many harddisks attached and its partition layout information:
#fdisk  -l

From the above output we have confirmed two harddisk were attached on the server, The first one /dev/sda is a default bootable harddisk and the second one /dev/sdb is a newly attached disk to the server.

Note:/dev/sda indicates  either SCSI or SATA hard disk, The name of the first hard disk start with the letter “a” ,if the server has more hard disk, the next avaiable letter will be used for that one(ex:sdb,sdc,sde so on)

 

Step:5  To format the Hard Disk, run the following command
syntax:
#fdisk   <Logical name of the harddisk>
#fdisk   /dev/sdb

This will take you to the format menu ..

choose “m” to see all the available options  from the format menu

From this output u  will come to know which option to use to create a new partition, “n” to ccreate a new partition..

Now, choose “n” and enter, this will prompt you two options which partion you are going to create either primary or extended, choose primpary by giving “p”, As we know in MBR partition it allowed to create only four partions.If you want to create more partition then we have to use one primary partition as extended  one, In extended it allowed to create 15 logical partitions.

We are going to create the first partition on this disk, Since we haven’t created any partitions on this, so give the partition number as  “1”. Then after that it will ask you to give the starting cylinder value,don’t give any value here since system will take the default available cylinder, After this, it will prompt you to enter the total size of the parrtition(give the desired size at the end cylinder)

Step 6: Now to save this allocated partition information we have to press “w”

From the above output,  u can see a  Warning message, it says the fdisk program cannot update the inmemory kernel partiton table since the disk is mounted(busy), so we have to restart the system then after that the kernel reads the partion table.

Note: You can also use the command “partprobe” to force the kernel to read the partition table with out reboot.

#partprobe

From the above output if you see it didn’t show any warning or error messages because here we used the disk which doesn’t contain any mounted partition.

Step 7: Check the partiton details
#fdisk  -l

Now the partition created successfully, inorder to use this partition(i.e to store your data) we need to create a filesystem on this, if we dont create a filesystem on this partiton then it would become useless.

Step 8: Create a new file system

Syntax:

#mkfs   -t  <filesystem>  <Partition>

-t  =To mention the filesystem type

Now format the newly created partition with ext3 filesystem.

#mkfs   -t   ext3   /dev/sdb1

Step 9: Mount the filesystem
Mount: In order to make  use of this partition and visible to  all users you need to mount the filesystem to  some mount point directory
Note: Mounting and unmounting concepts I will explain in our next Tutorial

To mount a file system to a directory

Syntax: 

#mount   <filesystem>   <mount point directory>

Let me create a mount point directory

#mkdir  /ios.txt

Now mount  the newly created partition to this directory

#mount   /dev/sdb1   /Sybase

 

Step 10:  Now to check the currently mounted filesystem details,run the following command
#df  -h

df (Disk File System)- to display the disk space usage details

h- Human readable format(to display the size in (kb,mb.gb format)

From the above output, it’s  now confirmed the filesystem /dev/sb1 is mounted on the directory /Sybase

In our next tutorial, we will see how to create an extended partition.

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

 

 

 

How to copy a file/directory in Unix?

Copy Command in Unix(cp command)


How do i copy files and directories under UNIX like operating systems?

You need to use the “cp” command to copies files and directories under the UNIX like operating systems

Syntax:To copy a file to a directory

#cp    <options>   <source >    <destination>

Options:

-r -recursive (use this option only when you need to remove the entire directory and all its contents)
-v-Verbose   
-f-Forcefully  

Note: I have mentioned some of the important options here ,you can refer the man page to know more about the available options

Ex:1 copy a file called “backup.txt” in to another directory called  /tmp.

First create a file with some contents

#echo  “This is my first line” >/backup.txt

Check the contents

#cat  /backup.txt

Sample output: This is my first line

 

Now copy this file to the /tmp directory

#cp  /backup.txt   /tmp

Successfully  the file copied to the directory /tmp

To confirm go the /tmp directory and verify

#cd  /tmp

#ls

Sample output: backup.txt

 

 

Ex:2 To copy multiple files in to a directory

syntax:

#cp   <source file>   <source file>   <destination directory>

Leave space between the files to copy multiple files to a directory

Let me create some couple of files with some contents :

#echo  “Buy lots of eggs” >/mydoc

#echo “Buy some chicken” >/mydata

#echo “Buy some  cake” >/myfile

we have successfully created three files with some contents .

Now copy all these files to a directory called /vasanth

#mkdir  /vasanth

#cp    -v   /mydoc   /mydata   /myfile     /vasanth

Here i have applied the verbose option (its not mandatory )

Sample output:mydoc -> /vasanth/mydoc `mydata -> `/vasanth/mydata` `myfile -> `/vasanth/myfile`

Go to the  /vasanth directory and confirm

#cd   /vasanth

#ls

Sample output: mydata mydoc myfile

Ex:3 To copy all directory and all its content to another directory:

A directory and all its content can be copied from source to destination with the recursive option -r

It allows directories including all of their contents to be copied:

syntax:

#cp   <option>   <source>   <destination>

Let me create a directory and add some files inside

#mkdir   /testdir

#cd  /testdir

#touch  f1  f2  f3  f4

#mkdir  d1  d2  d3  d4

#cd  /

Now the directory /testdir is having some couple of files and some sub directories inside

#mkdir    /output

This is the destination directory

#cp    -rvf    /testdir     /output

Here i have applied verbose and force option since while copying each and every file it will ask the confirmation from the user (Difficult  for the system administrators if the directory contains some 100 or 500 files), i have granted yes to all by using the -f option.

Go to the destination directory and confirm whether all the files and sub directories copied

#cd  /output

#ls   -l

Sample Output:  drwxr-xr-x  6 root  root  4096   dec 12  13:37  testdir

 

Ex:4  How to select and copy  all the files from a directory to another directory

Let us assume if a directory contains some files and sub directories if i want to select only the files from that folder you have to use a special wildcard character “*”(Asterisk),this will select only the files from the directory and skips all the directories

Create a directory with some files and sub directories inside

#mkdir   /nirmal

#cd  /nirmal

#touch   f1 f2  f3  f4  f5

#mkdir  d1  d2  d3  d4  d4

#cd   /

Now select all the files and copy

#cp  -v    /nirmal/*     /tmp

Ex:5  To avoid overwriting the existing file

Copy only when the destination file doesn’t exist

#cp  -n   <source>  <destination>

Let me create a file :

#mkdir  /test

#cd   /test

#touch result.txt

#ls

Now create a file in / directory with the same name

#touch  /result.txt

#echo “This is my first line”>/test/result.txt

#echo “This is my second line” > /result.txt

Read  both the file  contents :

#cat   /test/result.txt

#cat /result.txt

#cp  -n   /result.txt    /test/result.txt

#cat  /test/result.txt

Sample output: its a new content

Here as we can see that after copying file result.txt in test directory the original file result.txt in test folder is not changed since of “-n” option.

Ex:6  To confirm before overwriting

syntax:

#cp   -i   <source>  <destination>

#cp  -i  /result.txt   /test/result.txt

Sample output: cp:overwrite /test/result.txt?y

#cat /test/result.txt

Sample output:This is my SECOND line

Ex:7  To make a backup up of files,if copying file has the same name:

#cat    /result.txt

Sample output: Hello there

#cat   /test/result.txt

Sample output: Testing line 

use the option –backup before the destination file contents gets overwrite with the new contents

#cp  –backup  /result.txt    /test/result.txt

Now go to the test directory path and check you will see two files

#cd  /test

#ls

Sample output: result.txt   result.txt~

Now check the destination file content

#cd  /test

#cat  result.txt

Sample output: Hello there

#cat    result.txt~

Sample output: Testing line

As you can see here that when result.txt is copied from the source to destination in test directory,a backup of the original file result.txt in the same directory is made as result.txt~ and new file is copied in the result.txt as usual

 

Ex 8:  How to copy only when the source file is newer

In this let me show you how to copy only the newest files from the source location to the destination directory location,to do that we have option -u.

Let me create some files

#touch  /a.txt  /b.txt   /c.txt  /d.txt   /e.txt

Now i have some files in the / directory with 0 bytes size

#ls  -lt

 

let me create a directory

#mkdir  /etc/vasanth

inside /etc/ directory i have created another subdirectory vasanth

#cp   *.txt   /etc/vasanth

This will select and copy  all the files with the extension .txt

#cd  /etc/vasanth

#ls  -l

#touch /newdoc

#ls  -l   *.txt

 

Now  when we use -u option combine with -v option to see what is being done,cp command will only copy a file which is newer from destination directory.

#cp  -uv  *.txt   /etc/vasanth

Sample output:newdoc.txt ->  /etc/vasanth/newdoc.txt`

As the result, we see that only newdoc.txt is copied in to the /etc/vasanth directory

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