Changing File Ownership
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 require 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 of which file the ownership is changing.chown [OPTIONS] [OWNER] FILE
Follow Along
Use the following command to switch to the
Documents
directory:sysadmin@localhost:~$ cd ~/Documents
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 the third column indicates the user owner.sysadmin@localhost:~/Documents$ ls -l
total 148
drwxrwxr-x 2 sysadmin sysadmin 4096 Aug 1 03:40 School
drwxrwxr-x 2 sysadmin sysadmin 4096 Aug 1 03:40 Work
-rw-r--r-- 1 sysadmin sysadmin 39 Mar 14 17:48 adjectives.txt
-rw-r--r-- 1 sysadmin sysadmin 90 Mar 14 17:48 alpha-first.txt
-rw-r--r-- 1 sysadmin sysadmin 89 Mar 14 17:48 alpha-first.txt.original
-rw-r--r-- 1 sysadmin sysadmin 106 Mar 14 17:48 alpha-second.txt
-rw-r--r-- 1 sysadmin sysadmin 195 Mar 14 17:48 alpha-third.txt
-rw-r--r-- 1 sysadmin sysadmin 390 Mar 14 17:48 alpha.txt
-rw-r--r-- 1 sysadmin sysadmin 42 Mar 14 17:48 animals.txt
-rw-r--r-- 1 sysadmin sysadmin 14 Mar 14 17:48 food.txt
-rwxrw-r-- 1 sysadmin sysadmin 112 Aug 1 03:48 hello.sh
-rw-r--r-- 1 sysadmin sysadmin 67 Mar 14 17:48 hidden.txt
-rw-r--r-- 1 sysadmin sysadmin 10 Mar 14 17:48 letters.txt
-rw-r--r-- 1 sysadmin sysadmin 83 Mar 14 17:48 linux.txt
-rw-r--r-- 1 sysadmin sysadmin 66540 Mar 14 17:48 longfile.txt
-rw-r--r-- 1 sysadmin sysadmin 235 Mar 14 17:48 newhome.txt
-rw-r--r-- 1 sysadmin sysadmin 10 Mar 14 17:48 numbers.txt
-rw-r--r-- 1 sysadmin sysadmin 77 Mar 14 17:48 os.csv
-rw-r--r-- 1 sysadmin sysadmin 59 Mar 14 17:48 people.csv
-rw-r--r-- 1 sysadmin sysadmin 110 Mar 14 17:48 profile.txt
-rw-r--r-- 1 sysadmin sysadmin 51 Mar 14 17:48 red.txt
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. Use password netlab123
when prompted:sysadmin@localhost:~/Documents$ sudo chown root hello.sh
[sudo] password for sysadmin:
Confirm the user owner has changed by executing the
ls -l
command. Use the filename as an argument to limit the output:sysadmin@localhost:~/Documents$ ls -l hello.sh
-rwxrw-r-- 1 root sysadmin 112 Aug 1 03:48 hello.sh
The user owner field is now
root
indicating the change was successful.
Consider This
If we try to execute the
hello.sh
script again. It will fail! Why?sysadmin@localhost:~/Documents$ ./hello.sh
-bash: ./hello.sh: Permission denied
Only the user owner has the execute permission, and now the
root
user is the user owner. This file now requires administrative access to execute. Use the sudo
command to execute the script as the root
user.sysadmin@localhost:~/Documents$ sudo ./hello.sh
[sudo] password for sysadmin:
______________
( Hello World! )
--------------
\
\
<(^)
( )
Comments
Post a Comment