Suppose that you've been working on a file called rowyourboat and you want to have your coworkers down the stream review it. To do so, you'll need to give other people permission to access the document. You can either give people in specific groups access, or you can give everybody on the UNIX system access. In particular, you can specify permissions for u(ser—that's you), g(roup), o(thers), and a(ll).
In addition to specifying permissions, you can also specify how much access a person or group can have to your file. For example, you can specify r(ead), w(rite), and (e)x(ecute) access, depending on how much you trust them not to ruin your rowyourboat masterpiece.
As shown in Code Listing 5.7, your first step is to check out what the current permissions are. Then, you can set permissions, add to them, or remove them as necessary.
To check current permissions:
ls -l r*
For the user (rwx, in this example)
For the group (r-x, here)
For the world (---, here)
[ejr@hobbes permissions]$ ls -l r* -rwxr-x--- 1 ejr users 152779 Jul 24 15:10 rowyourboat [ejr@hobbes permissions]$ |
To set permissions:
chmod u=rwx,g=rx,o=r row*
To add permissions:
chmod g+w rowyourboat
The category. In this case, we've used g, for group, but you could also use o for others,...or, of course u for user, but you already have that access. You could also use a for all users (which includes u, g and o).
A plus sign indicates that you're adding the permission to the existing permissions, rather than setting absolute permissions.
The permissions to grant. Here, we've used w, for write permission, but you could also use r for read or x for execute permissions, as your needs dictate.
The file name (rowyourboat)
[ejr@hobbes permissions]$ ls -l total 332 -rw-rw-r-- 1 ejr users 24850 Jul 24 14:59 black -rwxr-x--- 1 ejr users 152779 Jul 24 15:10 rowyourboat -rw-rw-r-- 1 ejr users 128889 Jul 24 14:33 sage.sayings -rw-rw-r-- 1 ejr users 23890 Jul 24 14:33 sayings [ejr@hobbes permissions]$ chmod u=rwx,g=rx,o=r row* [ejr@hobbes permissions]$ ls -l total 329 -rwxr-xr-- 1 ejr users 24850 Jul 24 14:59 black -rwxr-xr-- 1 ejr users 152779 Jul 24 15:10 rowyourboat -rwxr-xr-- 1 ejr users 128889 Jul 24 14:33 sage.sayings -rwxr-xr-- 1 ejr users 23890 Jul 24 14:33 sayings [ejr@hobbes permissions]$ |
Removing permissions:
chmod go-w rowyourboat
Tip
You can also use the -R flag with chmod to recursively apply the changes you make to permissions to all subdirectoriesin a directory. For example, chmod -R go-rwx* revokes all permissions from everyone except the user for all files in the current directory, all subdirectories in the current directory, and all files in all subdirectories.
Tip
There are approximately a million and one ways to express permissions. For example, you could use chmod ugo= * (note the space before the *) or chmod u-rwx,grwx, -> o-rwx * to revoke all permissions from all files in the directory. (Note that you'll have to add your own permissions back to the files before you can do anything with them, if you try this out.)
Tip
If you want to change permissions for multiple files, either use a wildcard expression or separate the file names with commas (but no spaces).
18.221.141.44