Changing the ownership of filename
To change the ownership of a single file, you can run following command with specifing username and user group.sudo chown <username>:<groupname> <filename>
example :
sudo chown harsha:harsha textfile.txt
To change the ownership of a single file with only specifying username only you can run following command.
sudo chown <username> <filename>
example :
sudo chown harsha textfile.txt
Changing the ownership of directory
To change the ownership of a directory you can following command which recursively apply the ownership changes to sub directories and files also.
sudo chown -R <username>:<groupname> < directory >
sudo chown -R <username>:<groupname> < directory >
example :
sudo chown harsha:harsha textDirectory/
By only username
sudo chown -R <username> < directory >
example :
sudo chown harsha textDirectory/
Access permissions
How To change permission of file folder so only root can access open it
Step 1
example:
sudo chown root:root textDirecotry/
Step 2
example:
If file is a executable file you may give "sudo chmod 700" instead of "sudo chmod 600".
Details about permission numbers
All other users (except root) have no access.
sudo chown harsha:harsha textDirectory/
By only username
sudo chown -R <username> < directory >
example :
sudo chown harsha textDirectory/
Access permissions
In the above posts I have describe how to change the ownership of a directory or file.But for a movement if you change the ownership to root and let only root can access the directory and files you may need to change the access permissions to particular directory.
If you want to get a quick understanding about the permission matrices which show the read write and execute permissions in Linux I recommand this link will provide better understanding.Numbers coming along with "chmod" command are the way of expressiong permissions.
How To change permission of file folder so only root can access open it
Step 1
- Change the ownership of the directory / file to root
example:
sudo chown root:root textDirecotry/
Step 2
- Change the permissions so that only the owner of the file can read/write/execute it.(There is no point of applying permission for all the directories,cause if particular user cannot access root directory,then user won't be able to access the files and directories inside the root directory)
example:
sudo chmod 700 textDirecotry/
If file is a executable file you may give "sudo chmod 700" instead of "sudo chmod 600".
Details about permission numbers
- chmod 700
All other users (except root) have no access.
- chmod 771
in the directory. This can be useful for server hardening,where you prevent someone from listing directory contents,but allow access to a file in the directory if someone already knows it’s there.
- chmod 777
- chmod 0000
However, backup programs that run as non-root may fail to back up the directory’s contents.
- chmod 666
- chmod 644
The first 0 in the mode line can usually be dropped (so you can use 777 instead of 0777).
The -R option is a handy feature of the chmod command. With -R, you can
recursively change permissions of all files and directories starting from a
point in the file system. Here are some
examples:
$ sudo chmod -R 700 /tmp/test Open permission only to owner below /tmp/test
$ sudo chmod -R 000 /tmp/test Close all permissions below /tmp/test
$ sudo chmod -R a+rwx /tmp/test Open all permissions to all below /tmp/test
This comment has been removed by the author.
ReplyDeleteI am new to linux and thanks for sharing about chown options. Sharing below site which says about --preserve-root option that will prevent changing the ownership. Hope it helps
ReplyDeleteHow to Change File/Directory Ownership with Linux chown Command