Sunday, October 7, 2007

Basics of Unix Permissions

this post is dedicated to my unix mentor Gloria who taught me this:

I'm on a development project right now that is using Linux and Python. It has been about 5 years since I have worked in Unix (unfortunatly!) and it has been about 2 years since I've programmed in Python (Sadly!) so, I need to get up to speed quick.

I'll have a series of posts about Unix, Python, and Bash Scripting:

This posting is the first of these postings and it covers the basics of unix permissions.

First, login, and then do a $ls -l to see your permissions

Two examples:
drwxrwxrwx
-rw-r--r--

The first letter d means that the permissions are for a directory. - means that the permissions are for a file.

the rest the letters should be broken up into three parts (user, group, and other)
rwx rwx rwx
rw- r-- r--

user = you
group = any group with rights to this file or directory
other = everyone

r=read
w=write
x=execute

to change the permissions of a file named textFile so that other(everyone) has execute rights, here is the command:
$chmod o+x textFile

to take away the permissions of a file named textFile so that other(everyone) has execute rights, here is the command:
$chmod o-x textFile

There is another way to think of permissions in unix. You could think of these permissions as on or off and represent them in your mind like this:

rwx, rwx, rwx = on on on, on on on, on on on
rw-, r--, r-- = on on off, on off off, on off off

now, since binary is good at representing on/off we can make a leap and change this to:
rwx, rwx, rwx = on on on, on on on, on on on = 111,111,111
rw-, r--, r-- = on on off, on off off, on off off = 110, 100, 100

now, since we're doing unix and being geeky is par for the course, we can change this representation to Octal numbers without missing a beat. So, now we have:
rwx, rwx, rwx = on on on, on on on, on on on = 111,111,111 = Octal 7, Octal 7, Octal 7 = 777
rw-, r--, r-- = on on off, on off off, on off off = 110, 100, 100 = Octal 6, Octal 4, Octal 4 =644

So, octal values that are relevant here:
000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6
111 = 7

So, the permissions can also be represented using Octal shorthand, but it only makes sense if you understand that 7 is octal 7 and you can translate octal 7 into binary and get 111 and that you understand that 111 means on on on and that on on on means rwx are all turned on.

Next posting of this series will be about BASH scripting basics.

No comments: