How to change file ownership on a Linux server

There are some rare, yet extremely annoying, instances when you might have a script or service that refuses to run because it needs to be run by a particular user. Usually the problem is that the user in question does not have the necessary permissions to run the script. Rather than using chmod to assign global permissions (which can lead to security problems down the line), you can use the Linux command “chown” to assign files to specific users.

To see what user currently owns a file or set of files, just list the files in the directory:

ls -al

The “-al” flags will tell it to list all of the file information, including ownership. The first name will be the user, and the second will be that user’s group. In most cases, the group will be the same name as the user.

Let’s suppose you have a file named “testfile” that you want to change ownership from a user name tony to a user name sylvia. Run the following command as root:

chown sylvia:sylvia testfile

That file will now be owned by sylvia. If you want to assign the entire directory to sylvia, navigate to the parent directory:

cd ..

Then, type the following command:

chown -R sylvia:sylvia testdirectory

This will assign the directory and its entire contents to sylvia.

Chown is useful when you want to move contents from one user with basic permissions to another, as one user will not have permissions to use the files of the other. Another reason to use chown is to assign regular user ownership to files created by root, which is especially useful if you are fixing a problem for users or installing scripts for them.