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.txt
Explanation:
-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 mydir
Explanation:
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 tty1
Explanation:
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 sda
Explanation:
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.txt
Explanation:
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:
touch mytestfile.txt
Check its ownership details:
ls -l mytestfile.txt
The output will show something like:
-rw-r--r-- 1 username groupname 0 Mar 25 18:00 mytestfile.txt
Here:
username
is the owner.groupname
is 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:
touch mytestfile.txt
Remove write permission for yourself:
chmod -w mytestfile.txt
Try to modify the file:
echo "Hello, World!" >> mytestfile.txt
You'll receive an error because you no longer have write permission.
Add write permission back:
chmod +w mytestfile.txt
Modify the file again:
echo "Hello again!" >> mytestfile.txt
View the file content:
cat mytestfile.txt
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:
ls -l mytestfile.txt
Change ownership:
sudo chown newuser:newgroup mytestfile.txt
View ownership after:
ls -l mytestfile.txt
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:
umask 0027
Create a file and directory:
touch myfile.txt mkdir mydir
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 - 002
Default permissions for files: 666
Umask: 002
Calculated permissions: 664 (rw-rw-r--)
Example 2: 777 - 002
777 - 002
Default 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):
stat -c %a filename
Example Output:
644
2. Show Access Time
View only the access timestamp:
stat -c %x filename
Example Output:
2023-03-25 18:30:00.000000000 +0000
3. Show Modification Time
See only the modification timestamp:
stat -c %y filename
Example Output:
2023-03-25 18:35:00.000000000 +0000
4. Show Change Time
Check only the change timestamp:
stat -c %z filename
Example Output:
2023-03-25 18:40:00.000000000 +0000
5. Show File Type
Determine the file type (e.g., "regular file," "directory," etc.):
stat -c %F filename
Example Output:
regular file
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