Linux File Ownership and Permissions
File Type in Unix
The first character in a Unix file permission string indicates the file type. It provides essential information about the nature of the file. Here are the common file types represented by the first character:
Ordinary File (Regular File):
Ordinary files contain data, text, or program instructions.
Examples: Text files, images, compiled programs.
Output:
-rw-r--r-- 1 user group 12345 Mar 25 10:00 myfile.txtExplanation:
-rw-r--r--: The first-indicates an ordinary file.myfile.txt: The file name.Permissions (
rw-r--r--): Read and write permissions for the owner, read-only permissions for the group and others.
Directory:
Directories are used to organize groups of files.
They can contain both files and other directories.
Output:
drwxr-xr-x 2 user group 4096 Mar 25 10:05 mydirExplanation:
d: Indicates a directory.mydir: The directory name.Permissions (
drwxr-xr-x): Read, write, and execute permissions for the owner, read and execute permissions for the group and others.
Character Device File:
Character devices communicate with hardware devices character by character (e.g., terminals, serial ports).
Output:
crw-rw---- 1 root tty 4, 1 Mar 25 10:10 tty1Explanation:
c: Indicates a character device.tty1: The device name.Permissions (
crw-rw----): Read and write permissions for the owner and group.
Block Device File:
Block devices communicate with hardware devices in fixed-size blocks (e.g., hard drives, USB drives).
Output:
brw-rw---- 1 root disk 8, 0 Mar 25 10:15 sdaExplanation:
b: Indicates a block device.sda: The device name.Permissions (
brw-rw----): Read and write permissions for the owner and group.
Symbolic Links (Symlinks):
Symlinks are pointers to other files or directories.
They allow flexible organization without physically moving files.
Example:
mylink -> myfile.Output:
lrwxrwxrwx 1 user group 4 Mar 25 10:00 my_link.txt -> my_file.txtExplanation:
l: Indicates a symbolic link.my_link.txt: The symlink name.my_file.txt: The file it points to.
2. File Permissions
Explanation
File ownership in Linux is crucial for access control. Each file or directory has an owner (user) and a group associated with it. Let's explore this further.
Practical Exercise
Create a test file:
Check its ownership details:
The output will show something like:
Here:
usernameis the owner.groupnameis the group.The first
-indicates it's a regular file.
3. File Permissions
Explanation
File permissions control who can read, write, or execute a file. The three basic permissions are read (r), write (w), and execute (x).
Practical Exercise
Exercise 1: Remove Write Permission
Create a test file:
Remove write permission for yourself:
Try to modify the file:
You'll receive an error because you no longer have write permission.
Add write permission back:
Modify the file again:
View the file content:
4. Putting It All Together
After changing permissions, let's check the updated permissions using ls -l.
5. Changing Ownership
Remember that changing ownership requires root privileges. Use sudo for this operation.
View ownership before:
Change ownership:
View ownership after:
7. Understanding Umask
Explanation
Umask controls default permissions for newly created files and directories.
Default umask value: 0022 (subtract from 666 for files, 777 for directories).
Practical Example
Change the umask:
Create a file and directory:
Observe the permissions using
ls -l.
9. Calculating Umask
Umask value calculation:
Subtract umask from default permissions (666 for files, 777 for directories).
Example 1: 666 - 002
666 - 002Default permissions for files: 666
Umask: 002
Calculated permissions: 664 (rw-rw-r--)
Example 2: 777 - 002
777 - 002Default permissions for directories: 777
Umask: 002
Calculated permissions: 775 (rwxrwxr-x)
12. Let's explore the stat Command
stat CommandThe stat command provides detailed information about files. It allows you to retrieve various attributes of a file, including permissions, timestamps, and file type.
1. Show Numeric Permissions
Display the numeric permissions (e.g., 644 for files, 755 for directories):
Example Output:
2. Show Access Time
View only the access timestamp:
Example Output:
3. Show Modification Time
See only the modification timestamp:
Example Output:
4. Show Change Time
Check only the change timestamp:
Example Output:
5. Show File Type
Determine the file type (e.g., "regular file," "directory," etc.):
Example Output:
Advantages of Using stat:
stat:Provides granular information about files.
Useful for scripting and automation.
Helps track file changes and access patterns.
Explore these concepts, practice exercises, and master Linux permissions! 🐧🔒 Don't miss the next session whcih talk about the special permission.
Last updated