Managing ownership and file permissions is probably the most essential task of a system administrator. In any multi-user operating system like Linux, properly assigning ownership of files and directories is crucial.

The chown command is the most helpful tool for this. Not to be mistaken with chmod, chown can modify user ownership of files and assign them to different groups. It is an essential command to master for any serious Linux user.

Table of Contents

    Here is a guide to get you started.

    How Do You Check the Ownership of A File?

    Before you start shifting files around different owners and groups, you should first learn how to check a file’s current owner. The process is simple: adding a -l flag to a regular ls command includes ownership information of the file or directory queried.

    Say you have a file called example.txt. This is how the command to view its ownership information will look like:

    ls -l example.txt

    Using chown On A Single File

    The simplest way to use chown is to change the user owning a particular file. The syntax is sudo chown username filename, where username is the name of the user you want to give the file to, and filename is the name of the file in question. This is what it looks like in practice:

    sudo chown someone_else example.txt

    Note that this does not change the group ownership of the files, only the user. To change the group owner, you have to use a different syntax – sudo chown :groupname filename

    In this specific case, this becomes:

    sudo chown :group2 example.txt

    You can also combine both commands into a single line to change the user as well as group ownership of a file:

    sudo chown me:group1 example.txt

    Changing Ownership of Multiple Files With Chown

    Changing the ownership of every file individually is rather tedious when dealing with a larger number of files. Thankfully, most Linux commands let you chain together multiple space-separated file names within a single command. Like this:

    sudo chown someone_else:group2 example1.txt example2.txt

    Use the same trick to check the ownership of multiple files as well:

    ls -l example1.txt example2.txt

    Even for combining multiple file names into a single command, the process is too inconvenient for more than a couple of files. A better approach is to change the ownership of the entire contents of a directory at once.

    This is achieved by adding a -R flag to the chown command. This makes chown go through the directory’s contents and recursively change the ownership of every file inside. Here is a demonstration:

    sudo chown -R someone_else:group2 examples

    We can use the recursive flag again to check the ownership of the files in the examples folder.

    ls -l -R examples

    Modify File Ownership With UID

    System administrators managing many users will quickly get tired of entering user names repeatedly. A single typo in any of the names throws an error in using chown, slowing things down considerably.

    A better alternative is to use the user ID instead. The UID is a four-digit number assigned to each user created, starting from 1000 and going up. This is far easier to enter than a string and much less error-prone.

    To use this method, just replace the username with the UID:

    sudo chown 1001 example.txt

    If you don’t know the UID of a user, you can quickly check it with the id command. Just enter id -u username to see the unique ID of that user.

    This method can be extended to group names as well. To get the UID of a user’s login group and other groups they belong to, use the id command without the -u flag.

    id someone_else

    As you can see, we have various group ids to which the specified user belongs. Putting it all together, we can use chown like this to assign a new owner and change the group of a file:

    sudo chown 1001:1003 example.txt

    What Else Can You Do With the chown Command?

    We have already demonstrated most of the common uses of chown. You can now change users and groups that own a file through various methods. But that’s not the complete limit of the command’s abilities.

    You can refer to the official man pages for a technical description and a complete list of arguments you can use with the command. Just enter man chown in the terminal to view it.

    Is Chown Useful?

    If you are the sole user of your computer, then you will never need to use chown. But if you use a Linux system in a professional setting, be it a commercial server or a university computer, then mastering the chown command (in addition to chmod) is of utmost importance.

    The ability to assign and remove files to users and groups is essential in maintaining strict boundaries in multi-user systems. The best part about chown is its flexibility – you can work with individual files or whole directories with the same command.

    You can also assign ownership to users and groups separately and in a combined statement. Used with the more convenient UIDs, it makes handling even the most complex user hierarchies a breeze.

    Leave a Reply

    Your email address will not be published. Required fields are marked *