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

 

 

 

Foreground and Background Process(fg & bg)

Foreground (fg) &Backround( bg) Jobs


1.The Foreground and Background jobs (fg & bg) it allows the user’s to run multiple programs on a terminal.

2.By default the shell allows to execute only one program at a time on the terminal, Now if you want to run another program or command on the terminal we have to wait until the running programs get completed.

Before we get into the practical stuff, let me tell you in short about the foreground and background process.

3.Sending a program in the background is like minimize a running program in Windows operating system and foreground is like restore it back to the screen i.e maximize the program in windows operating system.

Foreground:  1.Is nothing but the program or process that is connected to the terminal

2. A Process is said to be in the foreground since it could connect with the users via the screen, keyboard etc….

Background:  Background jobs are not connected to the terminal, So you can place a program in the background that doesn’t require any user interaction.

When do we need to send a program to the background?

For example, The administrator is performing a backup task and that task would take more than an hour to get complete, after executing the backup program, the terminal will not allow the administrator to run another program ,To run the another program he has to wait until it gets finished, So here when a program it takes more time to finish its task (ex: backup task, capturing network packet tasks and many more..) those programs or commands we should send it to in the background .

Now  let us see,how to send a program to background and then how to bring it back to the foreground,

Ex:1 Run a program in the Background:

It’s very easy to perform, all you have to do is add the ampersand symbol at the end of the command,thats all , once you hit the enter button that program will run in the background.

Syntax: <command>   &

Let me create a file,

#touch  /vasanth.txt

#tail   -f      /vasanth.txt

If I enter this program  this would occupy the entire terminal and it won’t allow us to run another program

Now am going to send this program to the background (by adding ampersand at the end of the program)so that we can run multiple programs on the terminal

#tail   -f   /vasanth.txt   &

This will send the program to the background

Note:  (Here I use the tail command, this will make the file in buffering mode on the terminal, In your production environment  you  can use any program that takes more time to complete)

To check the background jobs details:

After you run a program in the background, now it’s time to check the bg jobs details.

we have a command called “jobs” this will give you a detail report about the bg jobs

#jobs

will give you the details of the bg  jobs  like,

1.bg id,

2. +/-  (job priority)

3.status of the bg jobs and the program name

4. program name

 

Sample Output: [1]  +   Running   tail -f /vasanth.txt

Note: 1.Current job always flagged with  +

          2. The previous job always flagged with  

 

 

Ex2: How to Bring the background(bg) jobs to the foreground(fg):

It’s quite easy to do it, all we have to do is use the fg command followed by %(percent) symbol and the background job id

Syntax: fg   %<bg id>

Always remember before you bring the bg jobs to the foreground, you should check the background job details by using the jobs command to get the bg id, status etc…

#jobs

Now once I confirmed the bg job is in running status and also make a note of the bg id, run the command

#fg  %1

That’s it……The bg process now it will come in fg.

 

Ex:3 How to send a running program to the background?

Use the cntrl+z  key, this will suspend the running program and move it to the background with “stop ”
status.

Note: If you Press cntrl+c the program gets killed

Now check  whether the program is sent back to the background by using the jobs command

#jobs

Sample Output: [1]+  stopped  tail -f /vasanth.txt

from the output the fg job successfully moved to the bg, You would see the status as stopped now

the above screen shot says we have a job at the bg with stopped status, What

Now, What we will have to do if I need to bring this job to the foreground?

The first thing you have to bring the job (which is in stopped staus at bg in our case) to the running status and then by using the fg command you could move it back to the foreground.

#jobs

Sampleutput: [1[+  Stopped tail -f /vasanth.txt

#bg  %1

SampleOutput: [1]+   tail  -f  /vasanth.txt &

#jobs

SampleOutput: 1]+     Running tail  -f  /vasanth.txt &

Now, move the program to the foreground,

#fg  %1

 

Ex:4 Let me show you the difference between  + and – Flagg from the jobs command output

Here am going to run two programs in the background

#tail  -f  /vasanth.txt  &

Sample output: [1]  5525

#firefox  &

Sample output: [2] 5526

Now run the jobs command to view the status of all the bg jobs

#jobs

Sample Output: [1] - Running   tail -f /vasanth.txt

               [2]  +  Running  /firefox

 

So from the above output, you can notice that the second program flagged with the “+” which says this one is the latest job, The first program flagged with the ” -” which says this one is the previous  job

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

 

How to Modify the File’s and Directories Permission in Linux

File/Directory Permissions


Filer Permission is a very important concept in Linux, not only in Linux but also in all flavors of UNIX), In this topic, we are going to cover, how to how to modify the files and directory permission, How the default permissions are getting assigned for file and directory and much more…..

When you run the ls command with -l option, you would see the following details of the file.

1.Type of the file(i.e, Whether it’s a regular file or directory or link file or special file …)

2.Owner of the file

3.Group owner(i.e, The file/dir belongs to which group)

4.Size of the file

5.Date and time stamp Pieces of information (When and at what time the file created)

 

#touch   /test
#ls  -l  /test
Sample Output:  -rw-r--r--   1  root  root   0   Sep 7   19:54    /myfile.txt

 

From the above screen shot you can see the details of the file, You would see a symbol  “-“ before the permission, it indicates  what type of file it is(i.e, a regular file or directory or link file etc……) in our case, since we have created a regular file that’s why we have got the symbol is “-“ before the permission..

In UNIX everything is a file, if it is not a file then it’s  process.

Types of File Components :

So now you can easily identify the file types from the ls  -l  output,  – means regular file,  d- means directory and so on…

Note 1: Block device and special file concepts I will be posting in our upcoming topics.

Note 2: Hard-Linked files looks like a regular file the symbol is “-“, so to identify the hard linked files use the command  “ls  –li to get the inode number along with the output(i.e, the files which are having the same inode number are hard linked files).

Permission Types:

The following explains the permissions for the regular file type:

1.read “r” permission:  Grants the permission to open and read the contents

2.write “w” permission: Grants the permission to edit and delete the files

3.execute “x”:  Grants the permission to run a file as a program(ex: a script file).the users those who all have the execute access can run a file as a program

Methods to modify the files and directories permissions:

We have two methods to modify the default files and directories permissions, let us see  one by one

1)Numeric method
2)Symbolic method

Numeric:  In numeric method  the following numeric values  we have to use to assign the permissions for the files/directories

for read- r–>4
for write-w–>2
for execute-x–>1

Symbolic: In this method, we have to use some operators to set and remove the permission from the file/directory

operators: + –> Add the permission to a file/directory
                   –  —>Remove the permission from the file/directory
                   = —>Sets the designated permission

 

What is the default permission for the newly created files and directories?

Once you have created a file/directory,  check with the ls -l  command to view the details of the file, you would seerw-r–r– in the first line from the output, it says the default permission for the newly created file for the owner, group and other users.

-rw-r–r–

owner=read&write access,

group=read only access,

others=read only access

Note:     The default permission for newly created file is 644(rw-r–r–)
                 The default permission for newly created directory is 755(rwxr-xr-x)

Let us see what is that 644  here in detail…

Owner(6) =6means (4+2) read and write access
Group(4)= means only read access
Other users(4)= means only read access

The same for directory also:

Owner(7) =7means (4+2+1) read and write access
Group(5)= means only read access
Other users(5)= means only read access

 

Most of them would get little confused in assigning or understanding the permission values, let me give you a reference table for both the methods (numeric and symbolic), this would really help you in understanding the permission values.

 

How to modify the File/Directory Permissions?

As I said already in two ways we can modify the permissions either by using Numeric or by symbolic method.

By using the “chmod “(Change Modification) command we can modify the file/directory permissions,Let us see one by one in detail,

 

Method:1(Numeric)

Ex:1 Task: Assign execute permission for other users(other) for the file whatsapp.txt

Syntax to modify the file/directory permission

#chmod   <permission>  <file/directory>

Let me create a file,

#touch  /whatsapp.txt

Check the default permission for the file

#ls  -l  /whatsapp.txt

#chmod  654   /whatsapp.txt

By default, the group will have the read permission, in our task we need to add execute permission along with that, so Here I have added execute permission for the group (4+1=5), Now the group will have read and execute access.

Check whether the given permissions are updated on the file

#ls  -l  /whatsapp.txt
Sample Output: -rw-r-xr--

You will see r-x  in group filed

 

Ex:2 How to give no permission to other users?

Method: Numeric

Task: Set no permission to other users(other) to the file /whatsapp.txt

Before modify, Check the current permission details

#ls  -l  /whatsapp.txt
Sample Output: -rw-r-xr--

 

From the output, we have confirmed other users are having only read permission.

Now it’s time to  give “no permission” for other users by using the following command,

#chmod   650  /whatsapp.txt

“0” means it will set to no permission(check the reference table above )

Now check whether the given permission is updated on the file,

#ls  -l  /whatsapp.txt
Sample Output: -rw-r-x---

From the above  screen shot, we have confirmed the given permissions are updated for other users(check the other user field)

 

Tips and tricks: You can also modify the perm like this chmod 5 /whatsapp.txt,now here this will update the other users field(i.e, with read and execute) and if you give chmod 56 /whatsapp.txt, this will update the other and group fields, so when you are using numeric method to modify the permissions give all the values(owner, group, others) .

Note: chmod  5  /whatsapp.txt (This method will update it from the reverse field (i.e, other, group, owner) and in our case, this will set read and execute for other user’s and also this will set no permission for the remaining fields (ie, for group and owner in our case) by default.

Let me show you this with some couple of examples,

#chmod  5  /whatsapp.txt

 

#chmod  64   /whatsapp.txt

This will update the other and group filed

I hope now you have clearly understood how to modify the permissions by using different syntax.

Don’t use this syntax in your production environment unless you have full access to revert it back to default if anything went wrong.

Ex:3 How to set permissions to all the files in a single syntax?

For a single file, we can easily modify the permissions, for example, to change the permissions for all the files inside  /backup directory and assume that directory holds some 50,100 files.would it be possible for you to change the permissions for all the files immediately? yes, you can do that……

As we all know about the special wild card character “*” which select all the files alone from the directory, by using this wild card we can change the permissions for all the files,…..Here you go

Task: OWNER=full access,GROUP=add execute,OTHER’S=add write

Let me first create a directory and inside that directory going to create some 10 files.

#mkdir   /backup
#cd  /backup
#touch  f1  f2  f3  f4  f5  f6  f7  f8 f9  f10
#ls  -l
Sample Output:f1 f2 f3 f4 f5 f6 f7 f8 f9 f10

 

Now change the permissions for all the files from the directory /backup

Task: OWNER=give full access,GROUP=add execute,OTHER’S=add write

#chmod  -v 756  /backup/*

-v –>verbose option (not mandatory always, use  only if you want to view the detailed output on the screen)

Now verify all the files permissions have changed or not,

#cd  /backup
#ls  -l

Symbolic method:

Ex:4 Changing the file/directory permission by using symbolic method

Task: Set read and write access to other users

Method: Symbolic

When you are using the symbolic method to modify the permissions, keep this in your mind

owner=u
group=g
other=o

Check the current permission set for the file /whatsapp.txt

#ls  -l /whatsapp.txt
Sample Output: rw-r-----

 

Now add the permission,

#chmod  o+rw   /whatsapp.txt
#ls  -l  /whatsapp.txt
Sample Output: rw-r-xrw-

 

Ex 5: How to Set permissions for owner, group, others in a single syntax?

Task: Remove write permission for other’s, add the write permission for the group, add execute permission for the owner.

You have to use comma ,between the fileds to assign permission for all .

#chmod  u+x,g+w,o-w   /whatsapp.txt
#ls  -l  /whatsapp.txt
Sample Output: -rwxrw-r--

From the output, we have successfully removed the write permission for other’s and inserted write permission for the group &also gave the execute permission for owner

Ex:6 How to set the designated permissions?

The same as above here we have to use the operator =, procedure everything is same as above examples

Task: For Owner give  r&w and for group give read &for other’s give read permission

#chmod   u=rw,g=r,o=r   /whatsapp.txt
SampleOutput:-rw-r--r--

 

 How are the default permissions assigned to the files and directories?

When a user created a file/directory you can see the permissions as 644 for file and 755 for the directory, Wonder!! how these permissions are getting assigned, the umask value is responsible to assign the permissions.

Umask, is used to set the default permissions for the newly created files and directories, your umask value is different  for root and normal user

1.If you Logged in as root user:umask value is 022

2.If you Logged in as normal user:umask value is 002

Note: The maximum permission for a file is 666(This is because only scripts and binaries should have executed permission, normal and regular files should have just read and write permission.)

The maximum permission for the directory is 777(Directory require 777 for viewing the contents in it, so they can have 777 as max permission.

Let me show you how the system calculates  to give the default permissions

Maximum permission   –   your umask value  =  default permission

For example, if you logged in as a root user to the server(ur umask is 022)

#touch  /data.txt
#ls  -l   /data.txt
sample output:-rw-r--r--

For file, the default permission is 644, Now let me  show you  how this permission automatically assigned

666-022=644

The same for directory also

777-022=755

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

I hope you have enjoyed this article and We will see more about the permissions in our Linux topic.

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

Under standing Hard link and soft link in Linux

 

Explaining Soft-link and Hard-link in Linux?

A soft-link or symbolic link is an actual link to the original file,Whereas a hard-link is a mirror copy of the original file.If you delete the original file,the soft-link has no value,because it points to a non-existent file.But in the case of hard-link,it is entirely opposite.

Even If you delete the original file,the hard-link can still has the data of the original file. Because hard-link acts as a mirror copy of the original file.

Let us see some of the difference between softlink and hardlink

Hard-link:

It refers to the specific location of physical data.

  1. Any changes made to the original or hard-linked file will reflect the other
  2. Even if you delete anyone of the files,nothing will happen to the other hard-links
  3. You cant link a directory even within the same file system
  4. In hard-link method both original file and hard-linked file will have the same inode number.

Soft-link

  1. Soft-link is a symbolic link to the original file(like windows shortcut)
  2. Soft-links will have different inode numbers
  3. Soft-link points to the original file.If you delete the original file,the soft-link fails.
  4. You can link a directory using soft-link on same file system and also on other file system.

How to create a Soft-Link file?

Let us create an empty directory with the name  “backup”

#mkdir  /backup

Change your path to the /backup directory

#cd   /backup

cd (change directory)-Command used to change your path

#pwd

sample output:/backup

pwd(Present working directory)- Used to check your current directory path

Now,Create a file with the name  “sourcefile” with some data as shown below

#cat  > sourcefile

Welcome to Vasanth blog

Now,let us view the data of the sourcefile

#cat  sourcefile

sample output:Welcome to Vasanth blog

Now,Create a symbolic link to the sourcefile

Syntax:

#ln   -s   <sourcefile>   <linkfile>

s—>Soft-link

#ln  -s  sourcefile   mysoftlink

 

Now,Let us view the data of the  mysoftlink file

#cat   mysoftlink

Sample output: Welcome to Vasanth blog

As you can see in the above output,”mysoftlink” file displays the same data as “sourcefile”

Now,Check the inodes and permissions of mysoftlink and sourcefile

As we can see in the above screen shot,Even though the mysoftlink file has the same contents as sourcefile,the inode numbers (1177444 vs 1177349) are different and the file permissions are different .

Hence it is proved that the soft-link doesn’t share the same inode number and permission of original file

Now let us remove the original file(i.e sourcefile) and see what happens

#rm  -vf  sourcefile

Sample output:removed "sourcefile"

Check  whether you can  access the mysoftlinkfile

#cat   mysoftlink

Sample output:mysoftlink:No such file or directory

As you see above,there is no such file or directory called mysoftlink after we removed the original file(i.e sourcefile).so,now  we understand that soft-link is just a link that points to the original file

Note:However,if you remove the link-file,the original file will still present.

 

Creating Hard Link:

Create a file “myfile.txt”  with some contents as shown below:

#echo “Welcome to Vasanth Blog” >/myfile.txt

or

#cat  >/myfile.txt

Let us verify the contents of the file

#cat   /myfile.txt

Sample output:Welcome to Vasanth Blog

The source file with the name myfile.txt has created now

Let us create the hard-link to the source file “myfile.txt” by using the following command

Syntax:

#ln  <source file>  <link file>

So “ln” command without option will create the hard-link

#ln  /myfile.txt  /myhardlink

Check the contents of the linkfile

#cat  /myhardlink

Now,Let us check the inode number for both the files,it should display different inode number for the source file and the link file in hard link.

#ls   -i   /myfile.txt

#ls  -i   /myhardlink

 

Now,Check the permission for the sourcefile and the  link file

#ls   -lit   /myfile.txt

Here the option “-i” to display the inode number and “-t”  from the output to display the latest modified file from the directory in the first.

 

Now,we see both the hard-link and the source file are having the same inode number(15920) and file permission(-rw-r–r–).

Hence it is proved that the hard-link file shares the same inode number and permission of the original file

Now,remove the  source file and see what happens

#rm    -vf   /myfile.txt

Sample output:removed `myfile.txt`

After removing the source file check the content of the hard-link file

#cat    /myhardlink

Sample output:Welcome to Vasanth Blog

As you  can see from the above screen shot output,even though if i delete the original file i can still view the contents of the hard-link file

Hence it has been proved that the hard-link  shares the same inode number,permissions and data of the original file.

 

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

You may have a question pop up in your mind in soft-link,  once you delete the original file how the link file gets removed .

here you go…..

Let me first explain the hard-link in detail:The file references or points to a spot on a hard-drive ,it is a representation of hard-drive, exactly the inode layer on the hard-drive how the hard-disk stores the data.

So myfile.txt points to a specific spot on a hard-drive where its store data,when you create hard-link file myhardlink it pointing to the same exact spot on the hard-disk. So it effectively gives you is two separate files with the exception if you edit the myfile.txt myhardlink file will also get edited because its pointing to same spot on the hard disk .

Soft-link: It works in a different way ,now the source file(i.e original file) in our case does the same thing,points to the spot on the hard-drive to stores its data.

When you create soft-link, instead of another file (i.e link file)pointing to the same spot on the hard-drive ,this file actually points to the sourcefile(i.e original file),It points to the name of the file instead of pointing to spot on the hard drive.

So If you delete the original file then the link file(in our case mysoftlink)will be completely useless because it is going to pointing at something that doesn’t exists.

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