Day 6 Task: File Permissions and Access Control Listsπ
Table of contents
- Task 1: Understanding File Permissions ππ
- Task 2. Writing an Article:
- Viewing Permissions π
- Changing Permissions ππ
- Special Permissions: Sticky Bit, SUID, and SGID
- Task 3. Access Control Lists (ACL): π
- Task 4. Additional Tasks:
- Task 5. Sticky Bit, SUID, and SGID:
- Task 6. Backup and Restore Permissions: ππΎ
Task 1: Understanding File Permissions ππ
Create a simple file and run ls -ltr
to see the details of the files.
Each of the three permissions is assigned to three specific categories of users. The categories are:
Owner: The person who owns the file or application. π€
- Use
chown
to change the ownership of a file or directory.
- Use
Group: The group that owns the file or application. π₯
- Use
chgrp
to change the group ownership of a file or directory.
- Use
Others: All users who have access to the system, excluding those in the group. π
- Use
chmod
to change the permissions for other users on a file or directory.
- Use
Task: Change the user permissions of the file and note the changes after running
ls -ltr
. βοΈπ
a. Create a file and view details: ππ
touch myfile.txt # Create a simple file
ls -ltr myfile.txt # View detailed permissions and metadata
You'll see an output like this:
You'll see an output like this:
-rw-r--r-- 1 user group 0 Oct 14 10:00 myfile.txt
First character (-): Indicates the file type (
-
for regular file,d
for directory).Next nine characters (rw-r--r--): Indicate permissions for owner, group, and others (in that order).
r
(read),w
(write),x
(execute).
b. Change ownership (chown
): ππ€
sudo chown newuser myfile.txt # Change the owner of the file to 'newuser'
ls -ltr myfile.txt # Verify the changes
c. Change group ownership (chgrp
): ππ₯
sudo chgrp newgroup myfile.txt # Change the group owner to 'newgroup'
ls -ltr myfile.txt # Verify the changes
d. Change permissions (chmod
): π§β¨
codechmod 764 myfile.txt # Owner: rwx, Group: rw, Others: r
ls -ltr myfile.txt # Verify the changes
Task 2. Writing an Article:
Understanding Linux File Permissions π§
In Linux, file permissions control who can access or modify files and directories. Permissions are divided into three categories: owner, group, and others. Each category can have read (r), write (w), and execute (x) permissions. π
Viewing Permissions π
You can view file permissions using the ls -l
commandπ:
codels -l myfile.txt
Output:
code-rw-r--r-- 1 user group 0 Oct 14 10:00 myfile.txt
-rw-r--r--
: The first set of characters shows the permissions for owner, group, and others.rw-
: Owner can read and write.r--
: Group can read.r--
: Others can read.
Changing Permissions ππ
Permissions can be modified using the chmod
command:
Symbolic mode: β¨
codechmod u+x myfile.txt # Add execute permission for the owner
Numeric mode:π’
codechmod 764 myfile.txt # Owner: rwx, Group: rw, Others: r
You can also change the ownership and group of a file:
chown: Change the owner. π€π
codesudo chown newuser myfile.txt
chgrp: Change the group. π₯π
sudo chgrp newgroup myfile.txt
Special Permissions: Sticky Bit, SUID, and SGID
Sticky Bit: π§· Applied to directories to prevent users from deleting files they donβt own.
codechmod +t /tmp/shared_directory
SUID: Allows a file to run with the owner's privileges. π
codechmod u+s /usr/bin/passwd
SGID: Ensures new files in a directory inherit the directory's group. ππ₯
codechmod g+s /mydir
Task 3. Access Control Lists (ACL): π
a. Reading about ACL: Access Control Lists (ACL) provide more detailed control than standard permissions by allowing specific users or groups to have customized permissions for a file or directory. π
b. Commands for ACL: π οΈ
- View ACL (
getfacl
): π
- View ACL (
codegetfacl myfile.txt # View ACL entries for the file
- Set ACL (
setfacl
): βοΈ
codesudo setfacl -m u:username:rwx myfile.txt # Grant 'username' full permissions
sudo setfacl -m g:groupname:rx myfile.txt # Grant 'groupname' read and execute permissions
getfacl myfile.txt # Verify ACL entries
c. Task:
- Create a directory and apply ACL: ππ
codemkdir mydir # Create directory
sudo setfacl -m u:anotheruser:rwx mydir # Give 'anotheruser' full access
getfacl mydir # Verify ACL permissions
Task 4. Additional Tasks:
a. Script to change permissions of multiple files: ππ§
code#!/bin/bash echo "Enter the directory path:" read dir echo "Enter permission (e.g., 755):" read permission for file in $dir/*; do chmod $permission $file echo "Changed permissions of $file to $permission" done
- Save this script as
change_
permissions.sh
, make it executable:
- Save this script as
codechmod +x change_permissions.sh
b. Script to set ACL permissions based on user input: ποΈπ
code#!/bin/bash echo "Enter filename:" read filename echo "Enter username to set ACL:" read username echo "Enter permission for ACL (e.g., rwx):" read permission sudo setfacl -m u:$username:$permission $filename echo "Set ACL for $username with $permission on $filename"
- Save this as
set_
acl.sh
, and run it similarly to the other script.
- Save this as
Task 5. Sticky Bit, SUID, and SGID:
Sticky bit: π§· Ensures only the owner of a file (or root) can delete or rename it within a shared directory.
Example:
bash codemkdir /tmp/shared_directory chmod 1777 /tmp/shared_directory # Apply sticky bit
SUID (Set User ID): π When an executable with SUID is run, it executes with the permissions of the file's owner.
Example:
codesudo chmod u+s /usr/bin/passwd # Example of an executable with SUID
SGID (Set Group ID): π·οΈ When SGID is set on a directory, new files inherit the directory's group.
Example:
codemkdir /tmp/sgid_test chmod g+s /tmp/sgid_test # Apply SGID
Task: Demonstrate each with the above commands and explain the use cases.
Task 6. Backup and Restore Permissions: ππΎ
a. Backup permissions: ποΈβ¨
code#!/bin/bash
echo "Enter directory path:"
read dir
getfacl -R $dir > permissions_backup.txt
echo "Permissions backed up to permissions_backup.txt"
b. Restore permissions: ππ οΈ
#!/bin/bash
echo "Enter directory path:"
read dir
setfacl --restore=permissions_backup.txt
echo "Permissions restored from permissions_backup.txt"
Save them as backup_
permissions.sh
and restore_
permissions.sh
, and make them executable as before.
Subscribe to my newsletter
Read articles from Manav Raut directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by