How to Change Permissions and Owners in Linux?

admin-img By Manvinder Singh

A Linux VPS allows you to add multiple users to seamlessly collaborative system management. Also, users who have full access can modify vital folders and files that affect your system and functions. Also, the user has the ability to fully modify the vital files and folders that impact functionality of system. In this blog, we will explore how you can change Linux permissions and ownership.

What is User Ownership and Permissions in Linux?

Different users have different permission and ownership in operating system to make sure that the files are secured. Furthermore, to put limitations and restriction on modification of the content in the file, different users utilize different system in Linux.

Understanding User Ownership and Permissions in Linux

Different users in the operating system have ownership and permission to ensure that the files are secure and put restrictions on who can modify the contents of the files. In Linux, different users use the system:

Root User: It is a superuser who has access to all the directories and files in our system and it can perform any operation. An important thing to note is that only the root user can perform changing of permissions or ownerships of the files that are not owned by them.

Regular User: These users have limited access to files and directories and can only modify the files that they own.

Also, every user has some access that is associated with them like most of the users have access to a home directory, and User ID. You can add users to a group to make the operations of handling and managing users more seamless. Therefore, a group can have zero or more users that can be linked with a default group.

Ownership and Permissions

To secure and protect files in Linux, it has three permissions that are read, write and execute

1. Understand file permission levels

Linux systems have three permission levels for files and directories – read, write, and execute. Here are their explanations:

Read(r): It enables users to view the content of directory or file. User can access it in view only mode

write(w): It allows users to make changes in the content of file. Also, users can create new files, making changes in existing one or delete or move files within the folder.

execute(e): It allows user to operate a file as program. Accessing executable permission over folders enables them to easily shift between working directory and access its information.

Why change permissions and Owners in Linux?

Let’s check out the several benefits of changing permissions and ownership in Linux.

1. Security and Access Control

Restrict Unauthorized Access: By changing permissions, you can control who can read, write, or execute a file. This helps protect sensitive data from unauthorized access.

Limit Damage: If a malicious user gains access to your system, restrictive permissions can limit their ability to modify critical files.

Example:

A file with permissions `777` (read, write, execute for everyone) can be accessed and modified by anyone, which is a security risk. Setting it to `644` (read and write for owner, read-only for others) restricts modification access.

2. Proper Functioning of Applications

Many applications require specific permissions or ownership settings to work correctly. For example, a web server like Apache needs read access to certain files but shouldn't have written access to them for security reasons.

Example:

If a web application is installed under the `/var/www` directory, files should be owned by the user running the web server (e.g., `www-data`), and not by a random user.

3. User Isolation

On multi-user systems, changing ownership and permissions helps ensure that users cannot access or modify each other's files.

Example:

User `alice` should not have access to user `bob`'s private files. By setting the correct ownership and permissions (`chown bob:bob file` and `chmod 600 file`), you can restrict access.

4. Collaboration and File Sharing 

When working in shared environments (e.g., a shared project folder), setting proper permissions (`chmod`) and ownership (`chown`) helps manage who can edit files. Group ownership (`chown :groupname`) can allow members of a specific group to have shared access.

Example:

In a team project, setting a folder's group ownership to `developers` allows all users in that group to access the files without giving access to others.

5. System Maintenance and Management

Changing ownership and permissions is often necessary when managing backups, restoring files, or transferring data between users. Without setting the right permissions, system files may become inaccessible or prone to errors.

Example:

After restoring a backup, files may have incorrect ownership (e.g., `root:root`). Changing ownership back to the intended user with `chown` ensures they can access their files.

Commands Used:

chmod`: Changes file or directory permissions.

Example: `chmod 755 script.sh` — Grants the owner full access (read, write, execute) and others read and execute access.

`chown: Changes the owner and/or group of a file or directory.

Example: `chown user:group file.txt` — Sets the specified user and group as the owner of `file.txt`.

chgrp`: Changes the group ownership of a file.

Example: `chgrp developers file.txt` — Changes the group ownership to `developers`.

Steps to Change Permissions and Owners in Linux

In Linux, changing permissions and ownership of files and directories is done using the chmod and chown commands.

Changing Permissions (chmod)

Owner, group, and others represent permissions in Linux. You can set permissions using numeric or symbolic modes.

Numeric Mode

Permissions are represented as a three-digit number:

  • 4 - Read (r)
  • 2 - Write (w)
  • 1 - Execute (x)

Each digit represents a set of permissions:

Owner permissions

Group permissions

Others permissions

For example:

chmod 755 filename

This sets:

  • Owner: Read, Write, Execute (7)
  • Group: Read, Execute (5)
  • Others: Read, Execute (5)

Symbolic Mode

Permissions can also be modified with symbols (+, -, =):

  • +: Add permission
  • -: Remove permission
  • =: Set permission exactly

Examples:

chmod u+x filename # Adds execute permission for the owner

chmod g-w filename # Removes write permission for the group

chmod o=r filename # Sets read-only permission for others

Changing Ownership (chown)

To change the owner or group of a file, use chown:

  • Change owner: chown new_owner filename
  • Change group: chown :new_group filename
  • Change both owner and group: chown new_owner:new_group filename
  • Change recursively: Add -R to change ownership or permissions for directories and their contents:

chmod -R 755 /path/to/directory

chown -R new_owner:new_group /path/to/directory

Viewing Permissions

To check the permissions and ownership of a file or directory:

ls -l filename

This will display permissions, owner, group, and other details.

Summing Up

Setting up permissions in Linux allows you to fine-tune system access control to improvise virtual private server ( VPS) security and avoid illegal modification. It also helps to maintain user data confidentially to cope up with regulations. FIles and Directors in Linux have three permissions: read, write and execute. With the help of this, you can easily change permissions and owners in Linux.

Frequently Asked Questions

Q 1. How to change ownership of all in Linux? Ans. To change ownership in Linux Use chown -R username:groupname /path/to/directory. Q 2. What is chmod 777? Ans. It grants read, write, and execute permissions to the owner, group, and others. Q 3. How to switch user in Linux? Ans. Use su - username or sudo su - username. Q 4. What is sudo in Linux? Ans. It allows executing commands with superuser (root) privileges. Q 5. How to change users in Linux? Ans. To change Use su - username or sudo -u username command.

autor-img

By Manvinder Singh

Manvinder Singh is the Founder and CEO of HostingSeekers, an award-winning go-to-directory for all things hosting. Our team conducts extensive research to filter the top solution providers, enabling visitors to effortlessly pick the one that perfectly suits their needs. We are one of the fastest growing web directories, with 500+ global companies currently listed on our platform.