File Permissions and Access Control Lists
1.) Understanding File Permissions :
Create a simple file and run ls -ltr to see the details of the files.
Each of the three permissions are assigned to three defined categories of users.
Solution:
To understand file permissions in Linux, let's break down the steps for creating a file, viewing its permissions using ls -ltr, and modifying the permissions using chown, chgrp, and chmod.
Create a Simple File: You can create a file using the touch command:
touch example.txt
Check File Permissions: To view the file's details and permissions, run:
ls -ltr
Example of the output:
To Change Ownership : use chown
sudo chown manit example.txt
Replace msd (group) with the any group name. Run ls -ltr to check the new group assignment.
Change Permissions : use chmod
chmod o-r example.txt
Change Group : use chgrp
sudo chgrp msd example.txt
Use the chmod command to change permissions for others.
chmod o-r example.txt
Task : Change Permissions and Note Changes
touch msd.txt
ls -ltr
Output :
Change owner:
sudo chown manit(Group) msd.txt
Run ls -ltr to verify :
Change group:
sudo chgrp msd msd.txt
Run ls -ltr to verify
-rw-r--r-- 1 manit msd 109 Oct 15 22:49 msd.txt
Change permissions for others:
chmod o-wr msd.txt
What we learned :
By using chown, chgrp, and chmod, you can modify the file ownership and permissions. The ls -ltr command helps you verify these changes and understand the current permissions.
2 . Writing an Article:
Answer
File permissions in Linux are critical for maintaining security and proper access control. They define who can read, write, and execute a file or directory. Here, we explore the concepts and commands related to file permissions.
Permissions in Linux are represented by a three-digit number, where each digit represents a different set of users: owner, group, and others.
Highest Permission: 7 (4+2+1)
Maximum Permission: 777, but effectively 666 for files due to security reasons, meaning no user gets execute permission.
Effective Permission for Directories: 755
Lowest Permission: 000 (not recommended)
Minimum Effective Permission for Files: 644 (default mask value of 022)
领英推荐
Default Directory Permission: Includes execute permission for navigation
Each of the three permissions are assigned to three defined categories of users:
Owner: The owner of the file or application. (chown)
Group: The group that owns the file or application. (chgrp)
Others: All users with access to the system. (chmod)
SUID (Set User ID): If SUID is set on an executable file and a normal user executes it, the process will have the same rights as the owner of the file being executed instead of the normal user (e.g., passwd command).
SGID (Set Group ID): If SGID is set on any directory, all subdirectories and files created inside will inherit the group ownership of the main directory, regardless of who creates them.
Sticky Bit: Used on folders to avoid deletion of a folder and its contents by other users though they have write permissions. Only the owner and root user can delete other users' data in the folder where the sticky bit is set.
3. Access Control Lists (ACL):
4. Additional Tasks:
This script will prompt the user to input the directory name and the permissions they want to set. It will then apply the specified permissions to all files in that directory.
This script will allow the user to input a file, a username, and desired permissions. It will then set the specified ACL permissions for that user on the given file.
Understanding Sticky Bit, SUID, and SGID:
What is the Sticky Bit? ( chmod +t )
The sticky bit is a permission that is primarily applied to directories. It restricts the deletion of files within that directory to only the file's owner or the root user, even if others have write permissions.
Use Case:
It's often used in shared directories, like /tmp, to prevent users from deleting each other's files while still allowing them to create and modify their own files.
Setting the Sticky Bit:
To set the sticky bit on a directory, use the chmod command with the +t option
Output:
SUID (Set User ID) (chmod u+s)
What is SUID?
The SUID (Set User ID) permission allows users to execute a file with the permissions of the file's owner. It is mostly applied to executable files. When SUID is set, the process spawned by executing the file runs with the privileges of the file owner rather than the privileges of the user who launched it.
Use Case:
The passwd command is a classic example where SUID is used. The passwd program allows users to change their passwords, which requires modifying the /etc/shadow file, a file that regular users normally do not have permission to modify. SUID allows the program to run with the elevated privileges of its owner (typically root) to modify the file safely.
Setting SUID:
Use chmod with u+s to set the SUID bit on an executable file.
echo "echo 'SUID test script executed!'" > SUID.sh
chmod 777 SUID.sh
Output :
3. SGID (Set Group ID) (chmod g+s)
What is SGID?
The SGID (Set Group ID) permission has two main purposes, depending on whether it is applied to files or directories.
Output :
5.) Backup and Restore Permissions:
Script to Backup Permissions of Files in a Directory
This script will save the current permissions of all files in a specified directory to a backup file.
#!/bin/bash
# Script to backup file permissions
backup_permissions() {
echo "Enter the directory you want to backup permissions for:"
read dir
# Check if directory exists
if [ ! -d "$dir" ]; then
echo "Directory does not exist!"
exit 1
fi
echo "Enter the name of the backup file:"
read backup_file
# Backup permissions using stat command
> "$backup_file" # Clear the backup file if it exists
for file in "$dir"/*; do
if [ -f "$file" ]; then
permissions=$(stat -c "%a %n" "$file") # Get permissions and file name
echo "$permissions" >> "$backup_file" # Write to backup file
fi
done
echo "Permissions backed up to $backup_file"
}
# Run the backup function
backup_permissions
Output :
Script to Restore Permissions from a Backup File :
#!/bin/bash
# Script to restore file permissions from a backup file
restore_permissions() {
echo "Enter the name of the backup file:"
read backup_file
# Check if the backup file exists
if [ ! -f "$backup_file" ]; then
echo "Backup file does not exist!"
exit 1
fi
# Restore permissions from the backup file
while read line; do
permission=$(echo "$line" | cut -d ' ' -f 1) # Extract permission
file=$(echo "$line" | cut -d ' ' -f 2) # Extract file name
# Check if the file exists before restoring permissions
if [ -f "$file" ]; then
chmod "$permission" "$file" # Restore permission
echo "Restored $file to $permission"
else
echo "File $file not found, skipping."
fi
done < "$backup_file"
echo "Permissions restored from $backup_file"
}
# Run the restore function
restore_permissions
Output :
Cloud Engineer at Cloudside | Passionate About Cloud, Automation & CI/CD Pipelines | Linux | Shell scripting | Git & Github | Jenkins | Ansible | Terraform | Docker | Kubernetes | Aws | Azure | Python
5 个月keep it up Manit Singh