Advertisement
Tech

Basic Linux Commands: The chown Command

Linux administration requires modifying permissions and ownership. Without such a tool as chown to alter ownership, applications such as Apache would have to be run as the root user - which would leave your server wide open. This article will introduce you to the tool to make this possible.

By jlwallen
Desk Tech
Reading time 3 min read
Word count 477
Linux Computing Linux commands
Basic Linux Commands: The chown Command
Advertisement
Quick Take

Linux administration requires modifying permissions and ownership. Without such a tool as chown to alter ownership, applications such as Apache would have to be run as the root user - which would leave your server wide open. This article will introduce you to the tool to make this possible.

On this page

History

The chown command first appeared in AT&T UNIX version 7. The chown command is similar to the chmod command but instead of changing permissions, chown changes the owner (hence the name ch - change and own - owner) of a file or directory.

Basics of ownership

Every file and directory in Linux has an owner. If you issue the command ls -l in your ~/ folder (that is your users’ home folder) you might see returned something similar to this:

Advertisement

-rwxr–r– 1 root root 685 2008-10-02 12:38 suspend.sh*

drwxr-xr-x 5 jlwallen jlwallen 4096 2008-10-15 12:45 Test/

Advertisement

-rw-r–r– 1 jlwallen jlwallen 0 2008-11-14 12:56 test1

drwxr-xr-x 9 jlwallen jlwallen 4096 2008-09-09 12:57 THEMES/

Advertisement

drwxr-xr-x 13 jlwallen jlwallen 4096 2008-09-11 14:35 thunderbird/

drwx—— 15 jlwallen jlwallen 4096 2008-11-19 16:37 tmp/

Advertisement

As you can see, most every file but one is owned by jlwallen. That means that the user, in this case me (jlwallen) owns these files and can modify their permissions. There is one file, suspend.sh, that is owned by root. That means the user jlwallen can not change the permissions of that file. In fact, according to the permissions/ownership, the user jlwallen can only read the suspend.sh file.

Using chown

The chown command is a very simple command to use. Of course, in order to change the ownership of a file you have to have permission to make the change. But let’s say you can change the ownership of a file (or you must change ownership and have to either su to the root user or use sudo), to do this the command would be:

Advertisement

chown USERNAME FILENAME

(Where USERNAME is the actual user you want to own the file, and FILENAME is the actual file name.)

Advertisement

There is an added bonus with the chown command. You can also change the group ownership of a file as well. Say I want the file test1 to belong to user jlwallen but the group accounting. To do this I would issue the command:

chown jlwallen:accounting test1

Advertisement

Now the test1 file belongs to jlwallen and the accounting group (so anyone in the accounting group can access the file).

Like chmod you can issue chown recursively to change ownership of a directory and all the files within the directory. To do this you add the -R argument. So if test1 were a directory and you wanted it to belong to user jlwallen and group accounting you would issue the command:

Advertisement

chown -R jlwallen:accounting test1

Final Thoughts

The chown command will come in handy for you at some point in your Linux administration lifetime. Whether it’s installing a new web-based application that has to be run by the apache group, or giving a group permission to access a certain file, chown is one of those commands that will collect dust until the day comes when you cannot move forward without its help.

Advertisement
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement