In Linux, every file and directory has an owner and a group. The owner is typically the user who created the file or directory. The group is a collection of users, and it allows multiple users to have a certain set of permissions on a file or directory. You can view the owner and group of a file or directory using the ls -l
command.
ls -l example.txt
# Output example: -rw-r--r-- 1 user group 1024 Sep 1 12:00 example.txt
In the output, user
is the owner, and group
is the group associated with the file.
There are three types of permissions in Linux: read (r
), write (w
), and execute (x
). These permissions can be assigned to three different entities: the owner, the group, and others (everyone else).
r
): Allows a user to view the contents of a file or list the files in a directory.w
): Allows a user to modify the contents of a file or create/delete files in a directory.x
): Allows a user to run a file as a program or access a directory.The permissions are represented in a 9 - character string. The first three characters represent the owner’s permissions, the next three represent the group’s permissions, and the last three represent the permissions for others.
To change the owner of a file or directory, you can use the chown
command. The basic syntax is chown [new_owner] [file_or_directory]
.
# Change the owner of example.txt to newuser
chown newuser example.txt
To change both the owner and the group, you can use the syntax chown [new_owner]:[new_group] [file_or_directory]
.
# Change the owner to newuser and the group to newgroup of example.txt
chown newuser:newgroup example.txt
There are two ways to change permissions in Linux: using symbolic notation and numeric notation.
The symbolic notation uses letters to represent the entities (u
for user, g
for group, o
for others, and a
for all) and the operators (+
to add permissions, -
to remove permissions, and =
to set permissions).
# Add execute permission for the owner of example.txt
chmod u+x example.txt
# Remove write permission for others of example.txt
chmod o-w example.txt
The numeric notation assigns a value to each permission: 4
for read, 2
for write, and 1
for execute. You add these values together to get the permission number for each entity.
# Set permissions 755 (rwxr-xr-x) for example.txt
chmod 755 example.txt
For sensitive files such as configuration files or private keys, you should restrict access to only the owner. You can set the permissions to 600
(rw——-).
chmod 600 private_key.pem
When multiple users in a group need to work on a set of files, you can set the group’s permissions to allow read and write access. For example, set the permissions to 660
(rw-rw—-).
chmod 660 group_project_file.txt
If you have a shell script, you need to make it executable. You can use the chmod +x
command.
chmod +x myscript.sh
Periodically review the permissions of your files and directories to ensure that they are set correctly. You can use scripts to automate this process.
#!/bin/bash
find /path/to/directory -type f -exec ls -l {} \;
Create and use groups to manage permissions more efficiently. For example, create a group for developers and assign the appropriate permissions to files and directories related to development projects.
Avoid using the root account for normal operations. Use sudo
when necessary to perform administrative tasks. This reduces the risk of accidentally modifying system - critical files.
Navigating permissions and ownership in Linux using the command line is a fundamental skill that every Linux user should master. By understanding the basic concepts of ownership and permissions, and learning how to use commands like chown
and chmod
, you can ensure the security and integrity of your files and directories. Following common and best practices will help you manage your Linux system more effectively.