13.4 chown user:group Syntax
Right, let’s talk about the chown user:group syntax. You’ve probably seen it, maybe even used it, and thought, “Yeah, that makes sense.” And it does, mostly. Until it doesn’t. This little colon is the source of more than a few head-scratching moments, so let’s get it sorted.
The basic incantation is simple: you’re telling the system to change the owner and the group of a file or directory in one fell swoop. The magic spell goes like this:
chown newowner:newgroup filename
For example, to make www-data the owner and the www-admin group the group owner of a directory for your website:
chown www-data:www-admin /var/www/my_website/
This is almost always better than running two separate commands (chown then chgrp) because it’s a single operation. On a slow filesystem or for a script that’s changing thousands of files, that atomicity matters.
The Subtle Madness of the Colon
Here’s the first “gotcha.” The colon (:) is the standard separator, but guess what? A period (.) also works. As in chown user.group file. Why? Because legacy systems and old standards couldn’t make up their minds. It’s a classic UNIX “choice is good, until it’s confusing” situation. My advice? Pick one and stick with it. The colon feels more modern and is less likely to be misinterpreted by a human reader (is that a dot in a filename or the separator?). The system, of course, doesn’t care.
The Dangers of Ambiguity
This is where the real fun begins. What do you think this command does?
chown bob: /path/to/file
If you said “changes the owner to bob and the group to… nothing?” you’d be wrong. It changes the owner to bob and the group to bob’s login group. The system sees the colon and interprets whatever comes after it. If nothing comes after it, it assumes you meant the user’s default group. This is a fantastic way to accidentally change a file’s group to something you didn’t intend.
Conversely, what about this?
chown :www-data /path/to/file
This one is safer. It leaves the owner completely alone and only changes the group to www-data. The colon at the beginning signals that the owner field is empty. This is functionally identical to using the chgrp command, but now you know how to read it.
When the User or Group Doesn’t Exist
Let’s say you’re tired and you type chown unicorn:leprechaun important_file.db. What happens? The command fails, spectacularly and correctly. It will tell you invalid user: 'unicorn:leprechaun' or something similar. It does not create a new user or group. It does not proceed with a partial change. It’s an all-or-nothing operation. This is a good thing. It means you can’t accidentally invent new users on your system by mistyping a chown command.
The Numeric Option (Because Sometimes Names Are Hard)
Sometimes, you know the UID (User ID) and GID (Group ID) numbers, but not their names. Maybe you’re in a Docker container that’s missing the passwd file, or you’re recovering a system. You can use numbers directly with the same syntax.
chown 1001:1002 file.txt
This sets the owner to the user with UID 1001 and the group to the group with GID 1002. The system handles this just fine. It doesn’t check if the names exist; it just applies the numbers. This is incredibly powerful and equally dangerous. If you get the numbers wrong, you’ll assign ownership to a non-existent user, which will show up as the raw number in an ls -l listing until you fix it.
The Recursive -R Flag
You won’t often change a single file. You’ll want to change a whole directory tree. That’s what the -R flag is for (Recursive, obviously).
chown -R www-data:www-data /var/www/
Now, a word of caution that has burned me more than once: be very careful with the path you specify after -R. If you accidentally add a space or a wildcard, you can end up changing the ownership of your entire filesystem. No, really. Always double-check that line before hitting enter. The system will happily let you chown -R newguy:newgroup / var/www (note the space) and then you will have a very, very bad day. Consider using a full path to be safe.
So there you have it. The user:group syntax is a compact, powerful tool. Respect the colon, understand its quirks, and always, always know exactly what you’re telling it to do before you unleash it recursively on your system.