With an understanding on how permissions work in Linux, and how to change them, it is probably time for a blog post on chown.
Before jumping into chown, let’s look at the permission of an object in more detail using stat, and a little bit of an overview on Linux IDs as this will be helpful later on.
In the previous blog posts I mentioned (a few times) owner and group. For example in the below screen grab the owner of the objects is Pi and the group is also called Pi.
The owner and group columns are located just before the size column. To see the details in more information we can use the stat command, which works as:
So for the secret.txt file:
The stat command returns a lot of information, including:
- File – the filename
- Size – the file size (0 here as it’s an empty file for my example)
- Access – the permissions of the file, including in octal
- Access / Modify / Change – the date and time the file was last accessed, modified or changed
And more importantly for this blog post:
- Uid – the User Identification (Uid)
- Gid – the Group Identification (Gid)
Every user account in Linux has an ID value attached to it. If you want to view the ID of the account currently logged in, use the command:
I’m currently running Raspbian (part of the Debian family) on a Raspberry Pi, and it starts the uid numbering for user accounts at 1000. So the first user (Pi for Raspbian) has the uid of 1000. If you want to see the accounts on your Linux system, their uid and their home folder path take a look in the /etc/passwd file. If you want to take a look at the groups available on your Linux system then take a look in the /etc/group file.
Warning Note: I would use the less command to view either of these files and I do not recommend editing the files directly.
less /etc/passwd less /etc/group
Both /etc/passwd and /etc/group will contain system accounts and system groups that you may not have been aware of, which may be vital to your Linux system.
The above understanding of Linux uid should now come into play. Previously when discussing object permissions I have written about the owner (o), group (g) and world (w). As chmod can change permissions of an object, chown can…you guessed it…change the owner of an object. If the owner of an object is changed then it also changes who has the owners permissions!
Using the same example directory as my previous blog posts, I have a directory called “permissions” containing several files and a directory.
The owner of all of these is the user Pi (uid 1000). The command to change ownership with chown is:
sudo chown NEW_OWNER_USERNAME FILE
For example, to change the owner of top_secret.info from the user Pi (uid 1000) to geek (uid 1001):
sudo chown geek top_secret.info
And this can then be confirmed using the stat command:
The ownership has changed from pi to geek, but the group is still the pi group which is tied to the user pi. This is not too much of a problem if the group permissions are different from the owner permissions but you may also want to change the group permissions.
This can be done via chown. Yep, as well as changing owner chown can also change group. The command:
sudo chown :GROUP_NAME FILENAME
will change the group to whatever group name has been entered after the colon (:). For giving group permissions to the geek group for top_secret.info the command is:
sudo chown :geek top_secret.info
The chown command is not limited to changing owner or group, it can do both at the same time using the command:
sudo chown USERNAME:GROUP_NAME FILENAME
To change the bigger_secret.txt file so that it is owned by the user geek and part of the geektechstuff group this command would become:
sudo chown geek:geektechstuff bigger_secret.txt
If you are setting the owner and group to the same (i.e. the user account geek and the logon group for geek) then:
sudo chown NAME:
is the command for you, i.e. to make geek the owner and group for confidential.txt:
sudo chown geek:
chown can be used recursively, so if you have a directory of objects to change ownership on use:
sudo chown -R ACTION /path/to/objects
sudo chown -R pi ~/permissions
to set the user pi as the owner of all the objects in ~/permissions, or
sudo chown -R pi: ~/permissions
to set the owner and group to pi for all the objects in ~/permissions. It works with any of the actions listed above (setting owner, setting group, setting both).
You must be logged in to post a comment.