SoftLink, HardLink, Permissions and Special Permissions in Linux

Hey, there! I'm Rudrakshi Sawant, back with a new blog.

Let us start with the content covered in the 3rd session of our Linux Workshop conducted byPranav Jambare sir.

This blog presents information regarding Hard Links, Soft Links, and Permissions in Linux.

  • A type of file that points to another file or directory is said to be SoftLink in Linux.

  • When we open a SoftLink it redirects us to the original or main file or directory.

  • It is also called a Symbolic Link.

  • It is similar to the shortcuts in Windows.

  • SoftLink makes it easier to access files that are stored in different directories.

  • They do not take more additional space on your hard disk.

  • Softlink is a powerful tool that can be used to simplify file management in Linux.

  • SoftLink can't work or be accessed If the original file is removed or Deleted.

  • A command for creating a SoftLink is:

~]$ ln -n [MainfileName] [SoftLinkName]

Let us see practically how to create a soft link and how it works:

1] Let's create a new file named "TestFile" and add some data to it:

[rsawant@localhost ~]$ touch TestFile
[rsawant@localhost ~]$ echo "Hey! Welcome to my blog." >>TestFile

2] Now, let's create a SoftLink:

# syntax - ln -s [mainfile] [softlinkfile]
[rsawant@localhost ~]$ ln -s TestFile New.SoftLink

3] Let's view the data of the Main file (TestFile) & SoftLink:

[rsawant@localhost ~]$ cat TestFile
Hey! Welcome to my blog. 

[rsawant@localhost ~]$ cat New.SoftLink
Hey! Welcome to my blog.

As we can see the main file and the SoftLink store the same data. If any changes or incremented data is added in the SoftLink then it is automatically updated in the Main file. so this is how SoftLink works.

4] Let's see what happens if the main file is deleted:

[rsawant@localhost ~]$ rm -rf TestFile
[rsawant@localhost ~]$ cat New.SoftLink
cat: New.SoftLink: No such file or directory

Thus, SoftLink cannot be accessed or viewed if the main file is deleted.


  • Like a SoftLink, a HardLink is also a type of file that points to the same data as another file.

  • HardLinks create a separate copy of a file's data.

  • This means if the main file is renamed, relocated, or removed from the system it does not bother the copy created by HardLink it still works smoothly.

  • HardLinks cannot be created for directories, if does then it may cause circular reference or loop, which causes issues with file system operations and can lead to data corruption.

  • The command for creating a HardLink:

~]$ ln [MainfileName] [HardLinkName]
  • Let us see practically how to create a HardLink and how it works:

1] Let us again create a new file named "TestFile" and add some data to it.

[rsawant@localhost ~]$ touch TestFile
[rsawant@localhost ~]$ echo "Hey, there!" >TestFile

2] Now, let's create a HardLink:

#~]$ ln [MainfileName] [HardLinkName]
[rsawant@localhost ~]$ ln  TestFile New.HardLink

3] Now, let's view the data of the Main file (TestFile) & HardLink (New.HardLink):

[rsawant@localhost ~]$ cat TestFile
Hey, there!
[rsawant@localhost ~]$ cat New.HardLink
Hey, there!

here, the data stored in the copy of the main file (TestFile) is the same as the HardLink (New.HardLink).

4] As we've used the " ls -lia" command to check the permission and inode value.

#ls -lia
  2482521 -rw-rw-r--. 2 rsawant rsawant 6 jul 14 04:29 New.HardLink
  2482521 -rw-rw-r--. 2 rsawant rsawant 6 jul 14 04:28 TestFile
#<-inode-> <-permission->

Following are the fields of the "ls -l" command where,

  1. "2482521" - this is the inode value.

  2. "-rw" - this is the permission for the owner of the file.

  3. "-rw" - this is the permission for the group owner of the file.

  4. "-r" - this is the permission for the other users of the file.

  5. "2" - this is the Link count.

  6. "rsawant" - this is the owner of the file.

  7. "rsawant" - this is the group owner of the file.

  8. "6" - this is the size of the file.

  9. "Jul 14 04:28" - this is the last modified date & time.

  10. "TestFile" - Name of the file.

Thus, we have observed that both the HardLinked file and the main file have the same inode value and permissions.

5] Let's see what happens if we delete the main file "TestFile" :

[rsawant@localhost ~]$ rm -rf TestFile
[rsawant@localhost ~]$ cat New.HardLink
Hey, there!

As we can see HardLinked file still works smoothly and can be accessed even after deleting the main file.


Permissions in Linux

  • In Linux, permissions are used to control access to files and directories.

  • these permissions are classified into three types:

    1. read - allow a user to access the content of the file or directory.

    2. write - allows a user to modify the content of the file or directory.

    3. execute - allows to run the file or view the content.

these permissions are assigned to the owner of the file, the group owner of the file, and other users.

Impact of Permissions on File & Directory

PermissionImpact on FileCommands (File)Impact on DirectoryCommands (Directory)
readallow to read file contentcatallow reading directory contentls / ls -l
writeallow changing file contentvi/vimallow to change directory contenttouch
executeallow to execute the file-allow to execute directory contentcd

Maximum & Default Permission

let's see what is the maximum permission & default permission for File & Directory.

FilesDirectory
Maximum Permission666 ( Ideally it's 777)777 ( ideally it's 755)
Default PermissionAs Root User: 644As Root User: 755
As Normal User: 664As Normal User: 775

Modifying the Permission

  • We use the "chmod" (Change mode) command for assigning or changing the permissions.

  • Syntax:

chmod [permission] [fileName]
  • Let's practically use the command:
#chmod [permission] [fileName]
[rsawant@localhost ~]$ touch xyz
[rsawant@localhost ~]$ chmod 665 xyz
#lets verify if changed
[rsawant@localhost ~]$ ls -l
-rw-rw-r-x 1 rudrakshi rudrakshi 15980813 Jul 10 14:25  xyz

Changing only the user of the file:

Syntax: chown [UserName] [FileName]
 #before
[rsawant@localhost ~]$ ls -l pqr
-rw-rw-r--. 2 rsawant rsawant 6 jul 14 04:28 xyz

#command
[rsawant@localhost ~]$ chown Rocky xyz

#verify
[rsawant@localhost ~]$ ls -l xyz
-rw-rw-r--. 2 Rocky rsawant 6 jul 14 04:28 xyz

Changing only group of the File:

Syntax: chgrp [GroupOwnerName] [fileName]
#before changing
[rsawant@localhost ~]$ ls -l xyz
-rw-rw-r--. 2 rsawant rsawant 6 jul 14 04:28 xyz

#command
[rsawant@localhost ~]$ chgrp programmer xyz

#verify
[rsawant@localhost ~]$ ls -l xyz
-rw-rw-r--. 2 rsawant programmer 6 jul 14 04:28 xyz

Changing the group and user of the file manually using the "chown" command:

Syntax: chown [UserName]:[GroupOwnerName] [FileName]
#before
[rsawant@localhost ~]$ ls -l xyz
-rw-rw-r--. 2 rsawant rsawant 6 jul 14 04:28 xyz

#command
[rsawant@localhost ~]$ chown testuser:tester1 xyz

#verify
[rsawant@localhost ~]$ ls -l xyz
-rw-rw-r--. 2 testuser tester1 6 jul 14 04:28 xyz

Special Permissions

  • In the previous blog, we've seen an overview of what are permissions and their types. ( it refers to the permissions assigned to a file or directory that specifies who can read, write & execute the particular file or directory. )

  • Now let's see what are different types of special permissions:

USID:

  • SUID (Set User ID) is a special type of permission that allows a program to run with the permissions of the user who owns the file.

  • SUID is mostly used for system administration programs, such as passwd, which requires root privileges to change a user password.

  • Syntax:

#Set SUID
$ chmod u+s FileName
#Unset SUID
$ chmod u-s FileName
  • Let us see a simple example of how we can set and remove SUID:
#Setting USID
[root@localhost ~]$ chmod u+s /usr/bin/passwd
[root@localhost ~]$ ls -l /usr/bin/passwd
--rwsr-xr-x. 1 root root 0 jul 14 13:26 /usr/bin/passwd
#Removing USID
[root@localhost ~]$ chmod u-s /usr/bin/passwd
[root@localhost ~]$ ls -l /usr/bin/passwd
-rwxr-xr-x. 1 root root 0 jul 14 13:26 /usr/bin/passwd

SGID:

  • SGID (Set Group ID) is a type o special permission that allows a program to change the group owner of files created.

  • This is useful when multiple users need to access the same file or directory.

  • When set on any directory, SGID ensures that any file further created in that particular directory inherits group ownership of that parent directory.

  • Syntax:

#set SGID
$ chmod g+s [directory]
#remove SGID
$ chmod g-s [directory]
  • Let us see a simple example of how we can set SGID:
#Lets created new directory named Main
[root@localhost ~]$ mkdir main
[root@localhost ~]$ ls -l
drwxr--xr-x. 2 root root 6 jul 14 16:02 main
[root@localhost ~]$ cd main
[root@localhost main~]$ touch file1 file2 
[root@localhost main~]$ ls -l
-rw-r--r--. 1 root root 0 jul 14 16:02 file1
-rw-r--r--. 1 root root 0 jul 14 16:02 file1

#Setting SGID
[root@localhost ~]$ chmod g+s main
[root@localhost ~]$ ls -ld main
drwxr-sr-x. 2 root root 6 jul 14 16:02 main
[root@localhost ~]$ cd main 
[root@localhost ~]$ ls -l
-rw-r-sr--. 1 root root 0 jul 14 16:02 file1
-rw-r-sr--. 1 root root 0 jul 14 16:02 file1

#remove SGID
[root@localhost ~]$ chmod g-s main
[root@localhost ~]$ ls -ld main
drwxr--xr-x. 2 root root 6 jul 14 16:02 main

Sticky Bit:

  • The sticky bit is a special permission that is used on the directory.

  • When sticky bit is applied it prevents users from deleting or renaming files in a directory that they do not own.

  • This is commonly used on directories that are shared publicly shared such as /temp directory.

  • where you can create files, and check others' files but cannot delete or rename files.

  • Syntax:

$ chmod +t [directory]
  • Let's see how to set and remove the Sticky bit:
[root@localhost ~]$ ls -ld main
drwxr-sr-x. 2 root root 66 jul 14 15:12 main

#for applin sticky bit it is mendatory for directory to have 777 permission
[root@localhost ~]$ chmod 777 main
[root@localhost ~]$ ls -ls main
drwxrwsrwx. 2 root root 66 jul 14 15:12 main

#setting sticky bit 
[root@localhost ~]$ chmod +t main
[root@localhost ~]$ ls -ld main
drwxrwsrwt. 2 root root 66 jul 14 15:12 main

#removing sticky bit
[root@localhost ~]$ chmod -t main
[root@localhost ~]$ ls -ld main
drwxrwsrwx. 2 root root 66 jul 14 15:12 main

Umask

  • In Linux "Umask" stands for "user file creation mask".

  • It is used to set default permission to files and directories that are recently created.

  • To find the default permission of a file or directory, umask is subtracted from the maximum limit of permission.

  • Umask value can be set using the "umask" command followed by a three-digit number (octal number).

  • In this three-digit number, the first digit represents the owner's permission, the Second digit represents the group's permission, and the third digit represents all other user's permission.

Let's see how to view and change umask:

#to view
rsawant@localhost ~]$ umask

#to change
rsawant@localhost ~]$ umask 077
30
Subscribe to my newsletter

Read articles from RUDRAKSHI SAWANT directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RUDRAKSHI SAWANT
RUDRAKSHI SAWANT

Hey there! I'm a passionate Computer Engineering Student, with a strong interest in cloud technology and development. I Love working with Linux and I'm looking for ways to improve my programming skills. Do read my blogs !!!!