Overview

As with chmod, this is another program that has stood the test of time…Unix time ba dum tsh. This was also another command that was released with Unix 1, and the GNU version was released as part of Fileutils in October 1992. The chown command stands for change owner. As you may have guessed, it allows one to change the owner of a file or directory, and allows you to change the group as well. A quick summary from the man page

change file owner and group – man 1 chown

The basic usage for chown is

chown new-user filename
chown new-user:new-group filename

The chown utility also has a few extra flags that can be used. But most of the time, you’ll be using -R for recursive to change the owner/group of all files and directories down the tree. If you’re interested in a few of the other flags, use man chown.

Users and Groups on Systems

On most modern Linux systems, you might have noticed that the user id and the group id of the first user is 1000. Each added user, has those numbers increased by one. By default, the group name is the same as the username. So on my Ubuntu server, my user:group would be seen as drt:drt or 1000:1000.

On the latest versions of macOS (I’m on Mojave still), you’ll see that the first user has an id of 501 and is part of the staff group, which has a value of 20. So on my Mac, I would have the user:group as drt:staff or 501:20. You can ignore groups for now.

If you’re unsure of your UID or GID, just use the id command to get an idea.

id
uid=1000(drt) gid=1000(drt) groups=1000(drt),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lxd)

Examples

Let’s create an temp file. We’ll change it so the owner is root and then we’ll change it back to the original owner.

cat > whodis.txt
itsa me, Mario!

ls -l whodis.txt
-rw-r--r--  1 drt  staff  16 Mar 29 19:15 whodis.txt

Now let’s change the user to root. We’ll need to use sudo because we’re not the root user and according to the chown man page:

For obvious security reasons, the ownership of a file may only be altered by a super-user. Similarly, only a member of a group can change a file’s group ID to that group.

sudo chown whodis.txt root
ls -l whodis.txt
-rw-r--r--  1 root  staff  16 Mar 29 19:15 whodis.txt

Now let’s change the group to wheel. On macOS this has a GID of 0, on a Linux system, you’ll want to change it to the root group. Notice that there is a leading : before the group name. This tells chown to keep the user as is, and to change the group only. Again, according to the man page.

If the group operand is specified, it must be preceded by a colon (:) character.

sudo chown :wheel whodis.txt
ls -l whodis.txt
-rw-r--r--  1 root  wheel  16 Mar 29 19:15 whodis.txt

Finally, we can change the user and group back to us in one command. Remember, on Linux, you’ll want do username:username instead of username:staff.

sudo chown drt:staff whodis.txt
ls -l whodis.txt
-rw-r--r--  1 drt  staff  16 Mar 29 19:15 whodis.txt

Using Numbers

Now that we got the file back to normal, let’s go through the process again. This time, we’ll use numerical values instead of names.

Change user to root. Note the 0 for the user and 20 for the group. On Linux machines, you should see 1000 for the group.

sudo chown whodis.txt 0
ls -n whodis.txt
-rw-r--r--  1 0  20  16 Mar 29 19:15 whodis.txt

Change the group to wheel (macOS) or root (Linux). Don’t forget the leading :.

sudo chown whodis.txt :0
ls -n whodis.txt
-rw-r--r--  1 0  0  16 Mar 29 19:15 whodis.txt

Finally change it back to our user. On macOS, this will be 501:20 and on Linux it will be 1000:1000.

sudo chown whodis.txt 501:20
ls -n whodis.txt
-rw-r--r--  1 501  20  16 Mar 29 19:15 whodis.txt
ls -l whodis.txt
-rw-r--r--  1 drt  staff  16 Mar 29 19:15 whodis.txt

Conclusion

Unless your administrating some Linux boxes with multiple users, I don’t know if you’ll be using chown that much. That doesn’t mean you should ignore this. It will come in handy one day. If you’re copying files from a server to a workstation, and the UID or GID was not 1000 you might have some issues. Sometimes, when using nfs you can get botched UIDs and GIDs that are not on your system. If you’re a docker 🐳 user you may have noticed that on macOS systems, the docker copy command set’s the wrong UID and GID. These are some occasions I’ve come across. But most of the time, I’m using chown to make sure that the plex user on my system has access to my media files. Keep this in your administration Swiss army knife, one day you’ll come across an instance where you’re changing owners, and you’ll be glad you read this article.

References