Here, the lrwxrwxrwx portion represents the file’s permissions in symbolic notation. The user and group values indicate the file’s owner and group, respectively.
Using stat
For a better examination of file permissions and additional file information, you can utilize the stat command. This command provides a comprehensive overview of a file’s attributes.
To use stat, simply enter the following command:
stat <filename_or_directory>
Again, replace <filename_or_directory> with the actual name of the file or directory you wish to inspect. The output will present lots of information, including access, modification, and change times, in a structured format.
These commands will empower you to examine and understand the permissions of files and directories on your VPS, an essential skill for managing access and security effectively.
How to Use chmod to Modify Permissions
The chmod command is used to change file permissions. It allows you to set or modify the permissions for a file or directory using either symbolic notation or octal notation.
Symbolic Notation: This method uses letters (u, g, o, a) and symbols (+, -, =) to add or remove permissions.
Symbol |
Meaning |
u |
User (owner of the file) |
g |
Group (users who are in the same group as the file) |
o |
Others (everyone else) |
a |
All (a combination of u, g, and o) |
+ |
Adds the specified permission |
– |
Removes the specified permission |
= |
Sets the specified permission and removes all others |
Example: To add write permission for the user and group and remove all permissions for others on a file named example.txt, you can use the following command:
chmod ug+w,o-rx example.txt
Octal Notation: This method uses three digits (0-7) to represent permissions for the owner, group, and others. Each digit corresponds to read (4), write (2), and execute (1) permissions.
Digits (0-7) |
Corresponding Permissions |
0 |
No permissions |
1 |
Execute (1) |
2 |
Write (2) |
3 |
Write + Execute (2+1) |
4 |
Read (4) |
5 |
Read + Execute (4+1) |
6 |
Read + Write (4+2) |
7 |
Read + Write + Execute (4+2+1) |
Example: To give read and write permissions to the owner, read-only permission to the group, and no permissions to others on a file named example.txt, you can use the following command:
chmod 640 example.txt
Understanding User and Group Ownership
Ownership plays a significant role in determining file and directory permissions. In this chapter, we will explore how ownership affects permissions and how to check file ownership using the ls -l command.
How Ownership Affects Permissions
In Unix-based systems, every file and directory is associated with two levels of ownership: user ownership and group ownership. Understanding these ownership levels is crucial because they directly influence who can access, modify, or execute a file or directory.
User Ownership: The user who creates a file or directory becomes its owner by default. This user has special privileges and can change the file’s permissions, read, write, and delete it, regardless of the permissions set for others.
Group Ownership: Each user on a Unix system belongs to one or more user groups. A file or directory can be assigned to a specific group. Users who are part of that group gain group ownership over the file or directory. Group ownership allows group members to access the file according to its group permissions.
How to Check File Ownership Using the ls -l Command
The ls command lists files and directories in a directory. When used with the -l option, it provides a detailed listing that includes ownership information. Here is how you can use it to check file ownership:
ls -l
The output of this command will display information in a format like the following:
-rw-r--r-- 1 user1 group1 1234 Sep 6 10:00 file.txt
Here is a breakdown of what each column represents:
– -rw-r–r–: These characters represent the file’s permissions. The first character indicates the file type (in this case, a regular file), followed by three sets of permissions for the file owner, group owner, and others.
– 1: This number indicates the number of hard links to the file.
– user1: This is the name of the file owner.
– group1: This is the name of the group owner.
– 1234: This is the file size in bytes.
– Sep 6 10:00: This is the date and time of the last modification.
– file.txt: This is the file or directory name.
In the example above, user1 owns the file file.txt, and it is part of the group group1. The file’s permissions are rw-r–r–, which means that the owner has read and write permissions, but others can only read the file.
Changing File Ownership
Changing file ownership is an essential task, allowing you to transfer ownership of files and directories between users and groups. In the following chapter, we will explore how to change the owner and group of a file or directory using the chown and chgrp commands and provide code examples.
How to Change File Ownership using chown
The chown command changes the owner of a file or directory. It allows you to transfer ownership from one user to another. To use chown, you will need superuser (root) privileges or ownership of the file or directory.
If you want to learn more about the different users on a Linux server, check out our Practical Guide to Superuser Accounts, sudo & root.
The basic syntax for chown is as follows:
chown [new_owner:new_group] [file_or_directory]
– new_owner: The new owner’s username.
– new_group: The new group’s name (optional).
– file_or_directory: The file or directory whose ownership you want to change.
How to Change Group Ownership using chgrp
The chgrp command changes the group ownership of a file or directory. Like chown, you need superuser privileges or ownership of the file or directory to use chgrp.
The basic syntax for chgrp is as follows:
chgrp [new_group][file_or_directory]
– new_group: The new group’s name.
– file_or_directory: The file or directory whose group ownership you want to change.
Code Examples for Changing Ownership
Here are some practical code examples for changing ownership of files and directories:
- Changing File Ownership with chown:
To change the owner of a file named file.txt to a user named newuser, use the following command:
sudo chown newuser file.txt
To change both the owner and group ownership of the same file, use:
sudo chown newuser:newgroup file.txt
- Changing Directory Ownership with chown:
To change the owner of a directory named mydir and all its contents to newuser, use the -R option for recursive ownership change:
sudo chown -R newuser mydir
- Changing Group Ownership with chgrp:
To change the group ownership of a file named file.txt to a group named newgroup, use the following command:
sudo chgrp newgroup file.txt
- Changing Group Ownership of a Directory with chgrp:
To change the group ownership of a directory named mydir and all its contents to newgroup, use the -R option for recursive group ownership change:
sudo chgrp -R newgroup mydir
Always remember to replace newuser and newgroup with the actual usernames and group names you want to assign. Additionally, use sudo to execute these commands with superuser privileges, as changing ownership typically requires elevated permissions. Be cautious when changing ownership, as it can have significant implications for access control and security on your system.