Initially, the owner of a file is the user who creates it. The chown
command is used to change the ownership of files and directories. Changing the user owner requires administrative access. A regular user cannot use this command to change the user owner of a file, even to give the ownership of one of their own files to another user. However, the chown
command also permits changing group ownership, which can be accomplished by either root or the owner of the file.
To change the user owner of a file, the following syntax can be used. The first argument, [OWNER], specifies which user is to be the new owner. The second argument, FILE, specifies which file’s ownership is changing.
chown [OPTIONS] [OWNER] FILE
Currently all the files in the Documents directory are owned by the sysadmin user. This can be verified by using the ls -l
command. Recall that the third column indicates the user owner.
To switch the owner of the hello.sh script to the root user, use root as the first argument and hello.sh as the second argument. Don’t forget to use the sudo
command in order to gain the necessary administrative privileges. Confirm the user owner has changed by executing the ls -l
command. Use the filename as an argument to limit the output:
The user owner field is now root indicating the change was successful.