User and Group Ownership


Let's pretend you have a file called mail_test and you want to change its ownership from the root user to natika. Because root is the only user that can make that change, you need to prepend your command with sudo. This is very simple.

sudo chown natika mail_test 


Tip

Remember that you can also type sudo -i to enter a root shell, should you need to do several administrative commands. Then, just type exit to return to the regular non-admin shell.


You can also use the -R option to change ownership recursively. Let's use a directory called test_directory as an example. Once again, it belongs to root and you want to make every file in that directory (and below) owned by natika.

sudo chown R natika test_directory 


The format for changing group ownership is just as easy. Let's change the group ownership of test_directory (previously owned by root) so that it and all its files and subdirectories belong to group accounts.

sudo chgrp R accounts test_directory 


You can even combine the two formats. In the following example, the ownership of the entire finance_data directory changes to natika as the owner and accounts as the group. To do so, you use this form of the chown command:

sudo chown R natika:accounts finance_data 


Quick Tip

You can use the -R flag to recursively change everything in a subdirectory with chgrp and chmod as well.


Now, you are aware that files (and directories) are owned by some user and some group. This brings us to the next question.

Who Can Do What?

From time to time, you need to modify file permissions. One reason has to do with security. However, the most common reason is to make a shell script file executable. This is done with the chmod command.

 chmod mode filename 


For instance, if you have a script file called list_users, you make it executable with the following command:

chmod +x list_users 


That command allows execute permissions for all users. If you want to make the file executable for the owner and group only, you specify it on the command line like this:

chmod u+x,g+x list_users 


The u means user (the owner of the file, really), and g stands for group. The reason you use u for the owner instead of o is that the o is being used for "other," meaning everyone else. The chmod +x list_users command can then be expressed as chmod u+x,g+x,o+x list_users. Unfortunately, this starts to get cumbersome. Now let's look at a much more complicated set of permissions. Imagine that you want your list_users script to have read, write, and execute permissions for the owner, read and execute for the group, and read-only for anybody else. The long way is to do this is as follows:

chmod u=rwx,g=rx,o=r list_users 


Notice the equal sign (=) construct rather than the plus sign (+). That's because the plus sign adds permissions, and in this case you want them to be absolute. If the original permissions of the file allowed write access for other, the plus sign construct would not have removed the execute permission. Using the minus sign () removes permissions. If you want to take away execute permission entirely from a file, you can do something like this:

chmod x list_users 


One way to simplify the chmod command is to remember that r is 4, w is 2, and x is 1, and add up the numbers in each of the three positions. rwx is then 4 + 2 + 1, or 7. r-x TRanslates to 4 + 1, and x is simply 1. That monster from the second to last example can then be rewritten like this:

chmod 751 list_users 





Moving to Ubuntu Linux
Moving to Ubuntu Linux
ISBN: 032142722X
EAN: 2147483647
Year: 2004
Pages: 201

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net