I have created short bash script to run some program 10 times and save the output to a file. While I was setting the executable permissions I thought that it would be nice to remove the write permission, so that the script can not be changed without sudo/root access. But what I got was this:
use@ubuntu:~/some/folder$ sudo chmod -w Run_SIM.sh
chmod: Run_SIM.sh: new permissions are r-xrwxr-x, not r-xr-xr-x
Why?
In short because you're removing the permision for user when you do not specify what to change. You can specify which of the
u
ser,g
roup,o
ther to set permissions for:The u, g, o in front of the permission indicates which of the permission fields to change - for user, group and other.
In addition you have the special
a
ll:A shorthand commonly used by many is the octal permissions, which is built up with three digits, 0-7. First digit represents user, second group and third other.
In the octal system, 1 means expecute, 2 means write, and 4 means read. You add together the modes you want, so read and execute is 5, and read only is 4. To make the file read + execute for all, and write for owner in octal you'd do
chmod 755 filename
. A mode of 740 would be all for owner, read for group and none for other.The good thing about octal modes is that you set all modes at once, so theres no hidden surprises left over.
To reserve editing of a script for root, you'd have to first set the owner of the file to root, using
sudo chown root:root filename
, and thensudo chmod g-w,o-w filename
- or using octalsudo chmod 755 filename
If you go to line 301 of the source code you'll see a the source code responsible for printing this warning message. Basically it calculates what naive users may intend, and print the difference. It's slightly interesting behaviour I was not aware of :)