PDA

View Full Version : command: chmod



gcawood
11-04-2011, 03:33 PM
chmod is a Linux command that will let you "set permissions" (aka, assign who can read/write/execute) on a file.

Usage:
chmod permissions file
OR:
Usage:
chmod permission1_permission2_permission3 file

When using chmod, you need to be aware that there are three types of Linux users that you are setting permissions for. Therefore, when setting permissions, you are assigning them for "yourself", "your group" and "everyone else" in the world. These users are technically know as:



● Owner
● Group
● World


Therefore, when setting permissions on a file, you will want to assign all three levels of permissions, and not just one user.

Think of the chmod command actually having the following syntax...


chmod owner group world FileName


Now that you understand that you are setting permissions for THREE user levels, you just have to wrap your head around what permissions you are able to set!

There are three types of permissions that Linux allows for each file.



● read
● write
● execute


Putting it all together:
So, in laymen terms, if you wanted a file to be readable by everyone, and writable by only you, you would write the chmod command with the following structure.


COMMAND : OWNER : GROUP : WORLD : PATH
chmod read & write read read FileName
chmod 6 4 4 myDoc.txt


Wait! What are those numbers?!?
Computers like numbers, not words. Sorry. You will have to deal with it. Take a look at the following output of `ls -l`


[root@demo]$ ls -l
-rw-r--r-- 1 gcawood iqnection 382 Dec 19 6:49 myDoc.txt


You will need to convert the word read or write or execute into the numeric equivalent (octal) based on the table below.



● 4 – read (r)
● 2 – write (w)
● 1 – execute (x)


Practical Examples



chmod 400 mydoc.txt – read by owner
chmod 040 mydoc.txt – read by group
chmod 004 mydoc.txt – read by anybody (other)
chmod 200 mydoc.txt – write by owner
chmod 020 mydoc.txt – write by group
chmod 002 mydoc.txt – write by anybody
chmod 100 mydoc.txt – execute by owner
chmod 010 mydoc.txt – execute by group
chmod 001 mydoc.txt – execute by anybody


Wait! I don't get it... there aren't enough permissions to do what I want!
Good call. You need to add up the numbers to get other types of permissions...

So, try wrapping your head around this!!
7 = 4+2+1 (read/write/execute)
6 = 4+2 (read/write)
5 = 4+1 (read/execute)
4 = 4 (read)
3 = 2+1 (write/execute)
2 = 2 (write)
1 = 1 (execute)



chmod 666 mydoc.txt – read/write by anybody! (the devil loves this one!)
chmod 755 mydoc.txt – rwx for owner, rx for group and rx for the world
chmod 777 mydoc.txt – read, write, execute for all! (may not be the best plan in the world...)


Good luck! Hope this helps.
(ps, never set things to 777 unless you have a really good reason to do so.)

tomfmason
11-04-2011, 04:01 PM
very nice write up. It is also work noting that you can use shortcuts like the following
chmod ug+rw file
chmod o-r file
chmod ugo=rwx file

enhu
11-05-2011, 12:09 AM
nice guide. thanks for reminding by the way.

because every time i can't edit a file inside a directory, i just do chmod 777 * so i can edit files. :D the reason is that i don't exactly know which command to execute and its easier to me. but this is just something one would do if you know files aren't that important, the computer is yours and no one uses it except you. :D

MustangV10
11-11-2011, 02:26 PM
I've never CHMODD'ed via SSH..I always use the cPanel file manager (I have cPanel installed on my server). Guess it's important to know the 'get your hands dirty' method as well though.

Thanks!

mrnothersan
01-15-2012, 01:40 PM
I don't usually CHMOD via SSH, unless there is a problem. I always use the cPanel file manager, or through an FTP client.

sbircris.jr
02-21-2012, 02:21 PM
ah this was one of the questions for my interview :D
Thanks for this great post ... i`ll save it, because is very nicely explained

lanlanlangzi
03-29-2012, 03:51 AM
I have used it several times.It is very useful.:)

Darwin
05-02-2012, 07:09 AM
I don't usually CHMOD via SSH, unless there is a problem. I always use the cPanel file manager, or through an FTP client.

This would be for doing things from scratch, it is handy to know for noobs. I tend to use my CHMOD via SSH so having a general path to follow is handy even if this is a quick rundown of how things work.

Stefano Messicano
05-03-2012, 02:47 PM
Very nice tutorial! An example if you want to change the permissions of the file "participants" so that everybody has full access to it, you would enter:
chmod 777 participants.

sam300
05-09-2012, 12:28 AM
chmod is a quite useful command, thanks for the description of chmod.

forestplay
05-09-2012, 12:50 PM
(ps, never set things to 777 unless you have a really good reason to do so.)

Knowing chmod is very handy. It's second nature to me.

The two most common values are

755 - wide open for user, read and executable for everyone else
644 - read and writable for user, readable by everyone else

If you want to change one bit, use this variation:

chmod u+x myfile

This makes the file executable by the user who owns the file. There are variations of that too.

chmod ug+x myfile (allow execute permission for user and group)
chmod u+rwx myfile (allow read and write to user)
chmod a-x myfile (disallow execute permission for all)

u = user
g = group
o = other
a = all

r = read
w = write
x = execute

'+' = allow permission
'-' = disallow permission

Sometimes using this syntax is easier than doing math. Have both techniques in your toolbox.

-Bob

dale
05-10-2012, 08:38 AM
On top of it, one could append "1" to the three digits for sticky, e.g.: chmod 1777 some_file

vickyonl9
05-20-2012, 02:55 PM
Hi,

Can you actually explain what do you mean by " using CHMOD via SSH " ?

If you are talking about logging into some other machine through SSH and then changing the permissions, can you please tell me , why do you find it unsafe to CHMOD using SSH? And also, can you tell me more about cPANEL?

vickyonl9
05-20-2012, 02:57 PM
what does sticky actually mean?

dale
05-20-2012, 10:35 PM
Sticky means only owner can remove or rename the file or link.

And after double checking to make sure, I realised that Linux does not recognise this use. So it is irrelevant to Linux. (I did not know this, so I also learnt something new. :))

JimBobIII
05-20-2012, 11:08 PM
I'm almost certain Linux machines can use the sticky bit; do you have a valid source that says otherwise?

dale
05-20-2012, 11:38 PM
Wikipedia (http://en.wikipedia.org/wiki/Sticky_bit#Usage). And on reading it again, it seems like I have misread. Go me on Monday. :)

So it seems like Linux ignores the traditional function of sticky bit, whilst retaining its effect on renaming and deletion?

I think this explanation from Happy Hacker Digests (http://www.happyhacker.org/hhlist/digest12.shtml) sums it up nicely.


3. Re: t bit of chmod
From: Christopher Hicks <chicks@chicks.net>

On Fri, 4 Jul 1997, Meino Christian Cramer wrote:
> I've been struggled over the "t" and "T" bit of the chmod-command.
>
> The man-pages said for the t-bit "save program text on swap device (t)"
> (and what's about directories???)
>
> If I do a chmod 7552 I will get a "T" instead of "t" -- also no answer
> from the man-pages.

The t flag (a.k.a. the sticky bit) has an ancient purpose when applied to
files which is mainly of historical amusement on modern machines. It also
has a quite practical purpose when applied to directories.

First, the amusement. Back in the days when UNIX ran on machines with 16k
or less of RAM (!), it was a significant performance improvement to be
able to keep some programs accessible more quickly. Widely used programs
(such as vi) had their sticky bit set, so they'd 'stick' around. This
meant that even if noone was using vi at the moment, it would at least
stay in swap since swap was often a faster device and there's less
computation to swap something in than to start it up from scratch. With
modern OS's such as linux which dynamically allocate disk cache on
machines with up to a gigabyte of RAM, there is little value to the
sticky bit. For FILES, that is.

But the sticky bit is not just an anachronism. When applied to
directories it causes a quite valuable behaviour. It prevents people from
deleting or moving files they don't own. You might think "but UNIX
handles that anyway". Well, sort of. Deleting and renaming are dependant
on the permissions of the DIRECTORY. Since everyone has their own home
directory and has their own files in it, why would anyone care? Because
of tmp directories. Tmp directories are world writable. This would
normally allow anyone to delete or rename files in them -- whether they
owned the file or not. But that would make temporary directories
significantly less useful. So it was decided that the sticky bit would be
'overloaded' with a special behaviour when it was set on directories.

If you're interested in seeing all the sticky files and directories on
your system, try
find / -perm -1000 -ls
(This may not work on some commercial systems that use non-gnu versions
of find.)

</chris>

Free software isn't free, but expensive software is expensive
~*-,._.,-*~'`^`'~*-,._.,-*~'`^`'~*-,._.,-*~'`^`'~*-,._.,-*~'`^`'~*-,._.,-*~
"Unix is hacker crack." -- Unix-Haters Handbook

Sorry for the confusion.

JimBobIII
05-21-2012, 12:53 AM
Ah, well that's significantly more correct-sounding. You learn something new every day, I suppose.

ljepilo
05-29-2012, 08:00 AM
Wikipedia (http://en.wikipedia.org/wiki/Sticky_bit#Usage). And on reading it again, it seems like I have misread. Go me on Monday. :)

So it seems like Linux ignores the traditional function of sticky bit, whilst retaining its effect on renaming and deletion?

I think this explanation from Happy Hacker Digests (http://www.happyhacker.org/hhlist/digest12.shtml) sums it up nicely.



Sorry for the confusion.

Its not a problem ,now its much more clear :).

linbgs
06-09-2012, 09:28 PM
Sticky means only owner can remove or rename the file or link.

And after double checking to make sure, I realised that Linux does not recognise this use. So it is irrelevant to Linux. (I did not know this, so I also learnt something new. :))

I learn something new everyday on here.
Thanks for explaining, because I certainly was lost.

Victor Leigh
06-10-2012, 03:57 AM
I have tried all possible combinations of chmod but I still cannot access one partition of my hard drive where I keep all my data. I am using Ubuntu 10.04.

I have to boot up Puppy Linux to change the permissions. Then after I boot up Windows 8 to play WoW, and come back to Ubuntu, I am locked out of that data partition again. Has this got anything to do with the way Windows 8 takes ownership of the partition?

jasko
08-03-2012, 04:12 AM
Did you try with Ubuntu live cd to change rights?

georgecyriac989
08-19-2012, 05:48 AM
Linux commands are really amazing