File Linking and Permissions in linux!!!

Hellooo !! I am Sejal Marawade lets start with new blog Day-3 linux Workshop led by Pranav Jambare

In todays session these topics were covered ..

•A soft link is also known as a symbolic link or symlink.

•It is a special type of file that acts as a pointer or as a reference to another file or directory.

•If the original file is deleted, the soft link will be pointing to a file that does not exist anymore and will be considered a corrupted link.

•Syntax:

ln -s <mainFile> <SLFile>

•Example:

ln -s Testuser Test1

•Verifiying the softlink file:

ls -l [Soft_Link_File]

•If the original file is deleted, the soft link will be pointing to a file that does not exist anymore and will be considered a corrupted link.

•A hard link acts as a copy (mirrored) of the selected file. It accesses the data available in the original file.

•The changes made in the original or main file are reflected in the hard link as well.

•Syntax :

ln <Main_File> <Hard_Link_File>

•Verifying the Hard Link File :

ls -l [Hard_Link_File]

•If the earlier selected file is deleted, the hard link to the file will still contain the data of that file.

•Linux file permission explained :

File permissions are core to the security model used by Linux systems. They determine who can access files and directories on a system and how.

How to view linux file permissions?

The ls command along with its -l (for long listing)

ls -l

There are three types of permissions: Read, Write, and Execute. These permissions are assigned to three different categories of users: the owner of the file, the group associated with the file, and other users who are not the owner or part of the group.

•Read (r)

Read permission is used to access the file's contents. You can use a tool like cat or less on the file to display the file contents. You could also use a text editor like Vi or view on the file to display the contents of the file. Read permission is required to make copies of a file, because you need to access the file's contents to make a duplicate of it.

•Write (w)

Write permission allows you to modify or change the contents of a file. Write permission also allows you to use the redirect or append operators in the shell (> or >>) to change the contents of a file. Without write permission, changes to the file's contents are not permitted.

•Execute (x)

Execute permission allows you to execute the contents of a file. Typically, executables would be things like commands or compiled binary applications. However, execute permission also allows someone to run Bash shell scripts, Python programs, and a variety of interpreted languages.

#Syntax:

ls -l <filename>
-rw-rw-r--1 myUser myGroup 27 Jul 15 9:30 mainFile

According to the above statement.

(-rw-rw-r--) - The owner has read and write permissions, the group has read permission, and others have read permission as well.

1 - Indicates the link count number.

myUser - Owner of the file

myGroup - Group owner of the file

27 - Size of the file

mainFile --> Name of the file

•Maximum file permissions are

For directory 777 by default

For file 666 by default

•666 means only r/w permissions

• 777 means all permissions/rights are given to all.

•Default permissions of a file and directory are:

For file 644 f and command is

ls -l [file_name]

For directory 755 and command is

ls -ld [directory_name]

•How do you modify Linux file permissions?

You can modify file and directory permissions with the chmod command, which stands for "change mode.

chmod <Permission> <nameofile/directory>

•Changing the owner and group owner of the file/directory:

To change the owner and group owner of the file or directory, we can use chown and chgrp commands respectively. But you can do it using a single chown command.

#Syntax
            chown <owner>:<groupowner> <filename/directoryname>
            #Example
            chown TestUser:TestGroupOwner TestFile #This is for file
            chown TestUser:TestGroupOwner TestDirectory #This is for directory

•What are Octal values?

When Linux file permissions are represented by numbers, it's called numeric mode. In numeric mode, a three-digit value represents specific file permissions (for example, 744.) These are called octal values. The first digit is for owner permissions, the second digit is for group permissions, and the third is for other users. Each permission has a numeric value assigned to it:

•r (read): 4

•w (write): 2

•x (execute): 1

For example, a file might have read, write, and execute permissions for its owner, and only read permission for all other users. That looks like this:

Owner: rwx = 4+2+1 = 7

Group: r-- = 4+0+0 = 4

Others: r-- = 4+0+0 = 4

The results produce the three-digit value 744.

•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

root@localhost ~]$ umask

#to change

root@localhost ~]$

umask 077

What are special file permissions?

Special permissions are available for files and directories and provide additional privileges over the standard permission sets that have been covered.

Linux special permissions set additional access controls on resources. There are three special permissions:

Set User ID (SUID)

Set Group ID(SGID)

The Sticky Bit.

Special Permissions

Description

SUID

A file is executed by a user with the identity of the owner, even if that user is not the owner

SGID

The contents of a directory automatically inherit the group association of the parent folder (great for directories shared by project teams)

Sticky bit

The file loads into memory automatically and cannot be deleted or altered by anyone other than the owner

Special permissions are configured using a fourth bit (leftmost):

•SUID = 4

•SGID = 2

•Sticky Bit = 1

•SUID :

#To enable SUID
chmod 4777 [binary name]
#To disable SUID
chmod 0000777 [binary name]

SGID :

#To enable SGID
  chmod 2777 [directory name]
  #To disable SGID
  chmod 00777 [directory name]

Sticky Bits :

#To enable Sticky bits
chmod 1777 [directory name]
#To disable Sticky bits
chmod 0777 [directory name]
10
Subscribe to my newsletter

Read articles from CO 50 MARAWADE SEJAL directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CO 50 MARAWADE SEJAL
CO 50 MARAWADE SEJAL