Red Hat Linux Reference Guide

Linux Introduction

VI-EDITOR

Process Management

Linux Installation

Disk Partitions & File System Management

Logical Volume Management - LVM

User & Group Administration

Package Management

Network File System - NFS

Domain Naming System - DNS

Apache HTTP Web Server

KickStart Installations

FTP server

BOOT PROCESS

Dynamic Host Configuration Protocol - DHCP

Job Scheduling

File Links

Swap Management

Log Management

Performance Management

Linux Troubleshooting

Linux Summary

Start Preparation Smartly

We have the collection to start prepartion smartly.

Start Assessment

Red Hat Linux Reference Guide

  DevOps Tech Hub

116 Followers

Filesystem & Directory Structure


Files and Directories:

In Linux, data will  stored into files and files are organized into directories. Files and Directories are organized into Hierarchical format with Parent and Child relationship to the directories, called the file system.

Directory Structure:

Linux uses a hierarchical directory  structure, with root (/) at the base of all other directories, which is called as root directory.

A LINUX directory  is a collection of files and directories with below properties:

  • / is the  root  directory which contains other files and directories
  • Each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier with  i-node.

Following are the directories that exist on the major versions of LINUX:


File Management:

         In LINUX there are three basic types of files:

  1. Ordinary Files: Its Regular File used to save the data like text, command instructions etc..VI-EDITOR is used to edit/add the data  of the file.
  2. Directories: Directories are like Folders to keep  Files and sub-directories. 
  3. Special Files: Special files are files used to store the hardware definitions and  device drive information to  identify and handle the hardware operation. 
Example:
[root@ ~]$ls -l /dev
total 0
crw-r--r-- 1 root root     10, 235 Jun 26 13:00 autofs
drwxr-xr-x 2 root root          80 Jun 26 13:00 block
crw------- 1 root root     10, 234 Jun 26 13:00 btrfs-control
drwxr-xr-x 2 root root        2320 Jun 26 13:00 char
crw------- 1 root root      5,   1 Jun 26 13:00 console
lrwxrwxrwx 1 root root          11 Jun 26 13:00 core -> /proc/kcore
drwxr-xr-x 3 root root          60 Jun 26 13:00 cpu

Directory Management:

Home Directory:

Its the default directory where User is  placed in once User logged in to the Linux server. User get  complete access to the their Home Directory as the user is owner of Home Directory.

Absolute/Relative Path:

Directories are arranged in a hierarchy with root (/) at the top. Any file placed in a directory is located by its path name.

Pathname are separated by a /. Pathname started with / is called as Absolute Path and the path starting with . or .. is called as Relative Path.

Example of absolute filenames:

  • /etc/passwd
  • /etc/shadow

Example of relative filenames:

  • ./passwd
  • ./shadow

Relative Paths are always start with . or .. referencing the path from current or parent directory. 

Permissions:

File or Directory has below Properties:

Owner Permissions: Permissions allowed to owner of file or directory what the owner can do on the file/directory.

Group Permissions: Group Permissions allowed to Group members of file or directory. If File or Directory is in Oracle group then the Group permissions are applied to the members part of Oracle group.

Other (world) Permissions: Others Permissions are applied for the users who are not owner of File or Directory and  not part of the same Group. Usually these permissions are called as World Permissions.

How to see Base Permissions of File or Directory:

$ls -l /home/kloudways
-rwxr-xr--  1	kloudways users	1024	Nov	2	00:10	myfile
drwxr-xr--- 1	kloudways users	1024	Nov	2	00:10	mydir

Here First column shows Base Permission associated with a file or directory.

The base permissions are broken into three groups, and each position in the group determines a specific permission, in the order: read (r), write (w), execute (x):

  • First Character refers its  File or Directory
    • - refers its regular file
    • d refers its directory
  • The first three characters (2-4) from the base permissions section represent the permissions for the file or directory owner. In the above example, 'myfile' has read , write and execute permission for owner called 'kloudways'
  • Next three characters (5-7)  from base permissions are for the group to which the file belongs. In the above example, 'myfile' is in oracle group and read, execute permissions are applied for members part of oracle group.
  • The next three characters (8-10) from the permissions are for everyone else who are not owners and not part of same file/directory group. In the above example, other get only read permission to access 'myfile'

In Linux, File and Directory has Read, Write, Execute permissions. However these permissions are differ in meaning and behavior for File and Directory.

File Permissions:

In Linux, Permissions are the first level of security to  restrict the access to unauthorized users. Linux Files having read, write, and execute permissions, and  the details  explanation of these permissions below:

  • Read:

Grants the access to read file ie. view the contents of the file. ex: $cat

  • Write:

Grants the access to modify, or remove the content of the file. ex: $vi to  add/modify the data

  • Execute:

Grant the access to run the file as program. ex: $./script

Directory Permissions:

  • Read:

Read permission grant the access to see the list of files and sub-directories in the specific directory. ex: $ls

  • Write:

Write permission grant the access to add or delete files of the directory. ex: $rm, $rmdir, $touch $mkdir

  • Execute:

Execute permission grant the access to  enter into the directory using $cd command. Execute permission is mandate to perform read or write operation as user must have access to  navigate to the directory to  perform any operations.

How to change permissions:

Chmod: $ chmod command is  used to change the file or directory permissions. There are two  way  to change the permissions.

  1. Symbolic Mode
  2. Numeric Mode

Symbolic Mode

Operators are used to change the permissions using Symbolic Mode. Below table shows operator supported in Symbolic mode.

Chmod Operator

Description

+

Adds the designated permission(s) to a file or directory

-

Removes the designated permission(s) from a file or directory.

=

Sets the designated permission(s).

#using Symbolic Mode
[kloudways@ ~]$ls -l f1
-rwxrwxr-- 1 root root 0 Jun 27 02:04 f1
[kloudways@ ~]$chmod u-x,g-rw,o+w f1
[kloudway@ ~]$ls -l f1
-rw---xrw- 1 root root 0 Jun 27 02:04 f1
[kloudway@ ~]$

Numeric Mode:

Numbers are used to change the permissions using Numeric Mode. Each permission is  assigned with standard identified number as Read - 4, Write - 2, Execute - 1. To change the permissions for owner, group, and others need to follow the below table to  sum the number required to fill in for each field. 

Number

Permission Representation

Symbolic Reference

0

No Permission

---

1

Execute

--x

2

Write

-w-

3

Write, Execute

-wx

4

Read

r--

5

Read, Execute

r-x

6

Read, Write

rw-

7

Read, Write, Execute

rwx

#using Numeric Mode
[kloudway@ ~]$ls -l f1
-rw---xrw- 1 root root 0 Jun 27 02:04 f1
[kloudway@ ~]$chmod 744 f1
[kloudway@ ~]$ls -l f1
-rwxr--r-- 1 root root 0 Jun 27 02:04 f1
[kloudway@ ~]$

Special Permissions:

Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.Below are the special permissions that can apply for files/directories.

  1. SUID (4)
  2. SGID (2)
  3. Sticky Bit (1)

SUID / SGID

As an example, when you change your password with the passwd command, your new password is stored in the file /etc/shadow.

As a regular user, you do not have read or write access to '/etc/shadow'  file for security reasons, but when you change your password, you need to have write permission to this file to update the latest new password. This means that the passwd program has to give you additional permissions so that you can write to the file /etc/shadow as root privilege. .

Special permissions Set User ID ( SUID) and Set Group ID ( SGID) bits  make this possible to  grant the program owner permissions to the user who  executing the program rather the actual user permissions.

When you execute a program that has the SUID bit enabled, you inherit the permissions of that program's owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.

This is true for SGID as well. Normally programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

The SUID and SGID bits will appear as the letter "s" if the permission is available. The SUID "s" bit will be located in the permission bits where the owners execute permission would normally reside. 

$ ls -l /usr/bin/passwd	
-r-sr-xr-x  1   root	bin  19031 Feb 7 13:47  /usr/bin/passwd*
$		

Which shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.

How to apply SUID/SGID bits:

# Symbolic Method
$ chmod ug+s file
$ ls -l
  drwsr-sr-x 2 root root  4096 Jun 19 06:45 file

#Numeric Method

$ chmod 6742 file
$ ls -l
  drwsr-S-wx 2 root root  4096 Jun 19 06:45 file

Sticky Bit

If Sticky Bit is applied for the directory which has world writable permission then users can not  remove the  files in that directory unless you are meeting the below requirements.

  • The owner of the sticky directory
  • The owner of the file being removed
  • The super user, root

Sticky Bit is  used to  restrict the users not to delete the files owned by other used in the directory where everyone has write permissions. 

In Linux /tmp is the  default directory has sticky bit applied to  restrict the users not to deleted temporary files created by other users. 

[kloudway@ ~]$ls -ld /tmp
drwxrwxrwt 8 root root 172 Jun 27 00:03 /tmp
[kloudway@ ~]$

How to apply Sticky Bit :

[kloudway@ ~]$chmod +t /tmp
[kloudway@ ~]$ls -ld /tmp
drwxrwxrwt 8 root root 172 Jun 27 00:03 /tmp
[kloudway@ ~]$



Previous Next