File Ownership & Permission in Linux
File Ownership & Permission in Linux
md 2024-01-16
1. Links:
Hard Links:
A hard link is an additional reference to an existing inode (data structure on a filesystem that
stores information about a file or directory).
Changes to the data in one hard link are reflected in all other hard links.
Removing any hard link does not affect the actual data until the last reference is removed.
A symbolic link is a separate file that contains a path pointing to the target file or directory.
If the original file is deleted, the symbolic link becomes dangling.
Symbolic links can span filesystems.
Examples:
Certainly! Let's step into the practical realm of file linking with a real-world scenario involving a text
document.
Imagine you have a document called report.txt that contains important information. Now, you want to
create a backup copy without using extra disk space.
ln report.txt backup-report.txt
You've just created a hard link named backup-report.txt. The beauty here is that both report.txt and
backup-report.txt share the same data on the disk. If you edit either file, the changes will be reflected in
both.
1/7
File Ownership & Permission in Linux.md 2024-01-16
rm backup-report.txt
Even if you remove the backup-report.txt link, the original report.txt still holds all the information. Hard
links provide a space-efficient way to have multiple references to the same data.
Now, imagine you have a frequently updated document called latest-report.txt, and you want to create a
shortcut for quick access.
ln -s latest-report.txt shortcut-report.txt
The shortcut-report.txt is a symbolic link, acting like a shortcut. If you update latest-report.txt, the
changes are instantly visible when accessing shortcut-report.txt.
rm latest-report.txt
Uh-oh! The shortcut-report.txt still exists, but it's pointing to nothing. Soft links are excellent for creating
aliases or shortcuts, but they depend on the original target's existence.
In practical terms, hard links are like creating efficient document backups, while soft links act as handy
shortcuts to essential files. Use them wisely to streamline your file management tasks!
2. Paths:
Absolute Path:
Relative Path:
Examples:
# Absolute path
/home/user/documents/file.txt
2/7
File Ownership & Permission in Linux.md 2024-01-16
# Relative path
./documents/file.txt
3. File Ownership:
Each file and directory in Linux has an owner and a group associated with it.
Examples:
4. File Permissions:
Basic Permissions:
Permission Categories:
Examples:
ls -l gitspace.txt
- rwxrw-r--1 linuxlab virtualenv 457 Jan 10 11:55 gitspace.txt
File Information:
Breakdown:
3/7
File Ownership & Permission in Linux.md 2024-01-16
Permissions: The permissions are set to allow the owner (linuxlab) to read, write, and execute, the
group (virtualenv) to read and write, and others to read only.
Numeric Representation:
Examples:
ls -l gitspace.txt
- rwxrw-r--1 linuxlab virtualenv 457 Jan 10 11:55 gitspace.txt
Numerical Breakdown:
4/7
File Ownership & Permission in Linux.md 2024-01-16
Imagine your Linux computer as a bustling city, and each file represents a building. Users are the occupants,
and groups are communities within the city.
Checking Ownership:
ls -l important_document.txt
In the output, the first column shows the file's owner (e.g., john), and the second column displays the
associated group (e.g., users).
Changing Ownership:
Using sudo (superuser privileges), you, as the system administrator, can transfer ownership to Jane and assign
the file to the 'teamA' group.
Now, each file in the city has access shields, determining who can view, modify, or run it. The shields have
layers for the owner, the group, and others.
Checking Permissions:
ls -l confidential_report.pdf
The output displays permission settings like -rw-r-----, where the first three characters are for the owner,
the next three for the group, and the last three for others.
Changing Permissions:
This command grants read and write permissions to the owner, read-only to the group, and no permissions to
others.
Practical Example
5/7
File Ownership & Permission in Linux.md 2024-01-16
Imagine you have a sensitive file called financial_data.xlsx in your city's finance department. You, as the
administrator, initially set strict permissions:
Initial Setup:
Here, John (the owner) has read and write permissions, while the 'users' group has no access.
Changing Ownership:
Now, Jane is the new owner, and the file belongs to the 'finance' group.
Modifying Permissions:
The file's access shields now allow Jane to read and write, the 'finance' group to read, and others have no
access.
C. Checking and Displaying File Permissions in Linux: Using getfacl and stat Commands
To list the permissions of a specific user and group for a file in Linux, you can use the getfacl command. This
command is part of the Access Control List (ACL) utilities and provides a detailed list of permissions for a file,
including user and group permissions.
Here is an example:
getfacl filename
Replace "filename" with the actual name of the file you want to check. The output will include information
about the owner, group, and any additional users or groups with specific permissions.
For a more concise view focusing on just the user and group permissions, you can use the stat command:
This command prints the username of the file owner, the group name, and the octal representation of the file
permissions.
6/7
File Ownership & Permission in Linux.md 2024-01-16
Adjust the "filename" accordingly to the file you are interested in.
These concepts are fundamental for managing files and directories effectively in a Linux environment.
Understanding and using them correctly ensures a secure and organized system.
7/7