7 janv. 2008

Using ll and chmod

What is great with Linux is the console thing. You seem like you are the master of everything when you are typing commands in your shell. That's the way I feel anyway :)

You can easily change the permissions and access to your files via the console, without having to look for the file in your exploration window and then do the click right and all the stuff. Open a console window, get to the folder you need with cd. The syntax is cd way/to/the/folder/you/want/to/be/in.

[root@localhost poipoi]# cd /var/lib/mlocate/

Check your file status with ll (or ls-l) the-name-of-your-file.

[root@localhost mlocate]# ll mlocate.db
-rw-r----- 1 root slocate 3326223 2007-10-21 19:06 mlocate.db

The first dash shows us that the file is NOT a directory (or you would have a d instead of a dash), then you can see 3 sets of 3 dashes or letters. I'll tell you about it in 1 minute.

We can now use chmod.

Two ways to use chmod:
1/ with the letters
you have four different types of users hidden behind the letters U, G, O or A

  • the file's owner, called with a U as User
  • the users from the group (if a group is defined), called with G
  • the others, called with O
  • everybody at the same time, called with A (and so stand for U, G and O at once)
And you can give the users three different permissions:
  • Read, or R = the user can only read it, no editing permitted
  • Write, or W = the user can read and write
  • Execute, or X = the user can use the file if it's an executable file (script,...)
If we get back to our ll result:
-rw-r----- 1 root slocate 3326223 2007-10-21 19:06 mlocate.db
We can see that the user/owner is root and can read and write the file, but not execute it. We can see that the group of users is slocate and can only read the file. The other users have no access to the file.

The syntax is: if you want to add permissions:
chmod [users]+[permissions] file
or chmod [users]-[permissions] file if you want to withdraw them.

So now, if you want to make your file mlocate.db writable and executable by the group and the others, you type: chmod go+wx mlocate.db. ll will then give you:

[root@localhost mlocate]# ll mlocate.db
-rw-rwx-wx 1 root slocate 3326223 2007-10-21 19:06 mlocate.db

But if you want to make it writable and executable for the group but just executable by the other users, you'll have to run chmod twice. That's the case when using numbers is faster.

2/ with numbers
Numbers as in binary numbers. Hey don't go !! I'll make it easy ;) You have the 3 sets of permissions:
rw- r - - - - - To convert them in numbers, just put a 1 when there is a permission, and a 0 if not.
110 100 000

In binary we start to count from the right, so every set corresponds to 22 21 20, 4 2 1. So the rw combinaison is worth 4+2 =6, or only r is worth 4. The next table shows you them all.

PermissionsBinaryBecomes
---0000
--x0011
-w-0102
-wx0113
r--1004
r-x1015
rw-1106
rwx1117


So now, you know that if you want your file to be readable and executable (r-x) you must choose the number 5. Let's see how to use it if we want to get this mlocate.db writable and executable for the group, and just executable for the other people.

chmod 671 mlocate.db will do what you want: get the user to read and write (rw- 4+2+0), the group to do everything (rwx 4+2+1), and the others just to execute the file (--x 0+0+1). And now you can see that my example is a bad choice, because the group will have more permissions than the actual owner, which is a very bad idea !! :)

Be careful with the numbers, you must take care of all the type of users, and not just give one number or you might lose your owner's rights! Trouble you don't have using the letters, because they only apply on what you actually say, and don't change anything about what you don't say !

In real life, what I just wanted to do is give the read to group and user to mlocate.db, because locate (my computer search engine) is not working with my user right now. So i'll just chmod 644 mlocate.db. And now it's working :)

My source: chmod by the numbers

Aucun commentaire: