Posts tagged Linux

UNIX tips: Learn 10 good UNIX usage habits

0

Good stuff!

Summary: Adopt 10 good habits that improve your UNIX® command line efficiency — and break away from bad usage patterns in the process. This article takes you step-by-step through several good, but too often neglected, techniques for command-line operations. Learn about common errors and how to overcome them, so you can learn exactly why these UNIX habits are worth picking up.

via UNIX tips: Learn 10 good UNIX usage habits.

Getting a handle on linux permissions

0

Back when I was just introduced to linux years and years ago, I remember solving all my permission woes by doing a chmod 777 without regard to its possible repercussions. I was a Windows user at the time and I could make very little to no sense as to why linux permissions must be so damn complicated. As a result, the only thing that got things working became the one and only solution: Permission error? chmod 777! Unable to write to folder? chmod 777! Obviously this should not be and over the year as I’ve grown and became a man I gained a better understanding of all this shenanigans.

I’m by no means an expert, amateur at best, but I’ll try explaining the basic workings of linux file permissions, hopefully someone will find it useful.

Cryptic and sexy is the name of the game

Every file and folder in the linux file system has an associated permission that dictates what actions can and cannot be done to it. In general those actions are:

  • Read
  • Write
  • Execute

Read – This is the action done when the computer needs to access the file system and extract information from it. This action is denoted as “r”. There is a catch here though but I’ll explain it after I’ve gone through the other 2 actions.

Write - When the computer needs to modify a file or a folder in the file system, it is “write”-ing to that memory location. This action is denoted as “w”.

Execute – The contents of a file can sometimes be executed as a series of commands by the computer. This action is denoted as “x”.

In linux, you can easily view the permissions of a file or directory by issuing the ls -l command. For example:

SmacBook-2:smsTest alvin$ ls -l
drwxr-xr-x  20 alvin  staff  680 May 11  2009 applet
-rw-r--r--   1 alvin  staff  623 May 11  2009 smsTest.pde

Note the strange string of characters in the first column of each folder? That string of characters define the permissions for that particular file or folder in the order of owner, group, and others. Take, for example, the first string “drwxr-xr-x” and recall what action each letter stands for from the paragraph above. Confused already? You may be saying “Wtf? You never told us what the ‘d’ stands for… ” Well the “d” means it’s a directory (a folder), if it doesn’t have a “d” as the first letter then that particular item is a file. Anyway, our string of “drwxr-xr-x” can be translated into the following:

The shit you’re interested in is a directory. The owner of this directory can read, write, and execute this directory. The group that owns this directory can only read and execute it. All others can only read and execute it.

It helps to remember the order (type, owner, group, others) and break the string of characters down like this [d][rwx][r-x][r-x], note that owner, group, and others have their own set of [rwx]. If a particular action is NOT allowed for them, it will be shown as a dash “-”. A gotcha here is that a directory needs to have both its read and execute permissions set in order to be accessed. Remember that.

Privacy plz, has u no shame?

I guess it’s hard to explain permissions without touching the subject of users and groups, therefore I’ll just briefly go over that. This draws some parallels with the windows world as basically all manipulation of the file system is done by a user in the operating system. To that effect, a collection of users can also be defined as a group. Recall the following output of the earlier ls -l command:

SmacBook-2:smsTest alvin$ ls -l
drwxr-xr-x  20 alvin  staff  680 May 11  2009 applet
-rw-r--r--   1 alvin  staff  623 May 11  2009 smsTest.pde

The output shows who the file/folder belongs to: in this case both items are owned by the user “alvin” and the group “staff”. This owner and group settings is what the defined permissions look at. Further translation:

The shit you’re looking at is a directory called applet. alvin can read, write, and execute it. Users belonging in the group ‘staff’ may read and execute it. Everyone else may read and execute it.

This is quite powerful as it provides a great deal of flexibility in terms of permissions. It’s quite easy to define what is accessible to who.

777 = rwxrwxrwx? It’s all the same

So how does all this correlate to the numbers we pop in the chmod command. How does 777 equal rwxrwxrwx? Well, to understand that we’ll have to understand some binary numbers! So far we’ve learned that rwxrwxrwx can be broken down into the 3 different permission holders: user, group, and others…

rwx rwx rwx
user group others

First you have to understand that 777 does not mean seven hundred seventy seven. All it means is that the owner’s permission is 7, the group’s permission is 7, and others 7. Put them together and you get 777 just like when you put 3 “rwx” together you get rwxrwxrwx. The number 7 in binary is 111 because it’s equivalent to 22+21+20 = 4 + 2 + 1 = 7. Correlating each bit in the binary form to “wrx” you can obtain a “true” for each 1 and a “false” for each 0. This means that 111 is the same as true for “r”, true for “w”, and true for “x” thus 7 = “rwx”.

More examples:

  • 6 = 110 = rw-
  • 5 = 101 = r-x
  • 750 = 111 101 000 = rwxr-x— (user has full privileges, group has read and execute, others have no privileges)
  • 640 = 110 100 000 = rw-r—– (user has read and write, group has read, others have no privileges)

Knowing this, you can now use the chmod command to change or set permissions of a file or folder in a linux file system. For example, to change the permissions of mytext.txt so that the user has read and write permissions, group has read, and others have none, you can simply do “chmod 640 mytext.txt”. Yup, simple as that… don’t get too excited though…

But numbers are so lame…

Whatever… but there’s actually a much easier way to set permissions that doesn’t require you to remember how to convert binary to “rwx”. You can pair up the chmod commands with these shortcuts:

  • + (add privilege)
  • - (remove privilege)
  • = (explicity set privilege)

Example: “chmod u+x myText.txt” is going to add execute privilege to the user (owner). Cool thing is that you don’t have to change/specify the permissions for group and others, and as such you don’t have to worry about accidentally changing something. More examples “chmod g+rx myText.text” will add read and execute privileges to the group for that particular file. So basically remembering the 3 shortcuts above and the following will get you up and running pretty quickly:

  • u (user/owner)
  • g (group)
  • o (others)

Locking eyes past the candle flame

Umm… Anyway, enjoy and I hope you learned something!

Go to Top