1

I have a file called test.sh. I've used chown to set it's owner to user1.

chown user1 test.sh

Then I play with chmod options to see if I understand them correctly.

chmod 400 test.sh

I can read the file but cannot modify or execute it.

chmod 200 test.sh

I can modify the file through command line but cannot read or execute it.

chmod 100 test.sh

I should be able to execute but not modify or read the file. But I get a permission denied error everytime I try to execute without sudo.

What am I doing wrong here?

3
1

Basic permissions:

  • Read: r–– → 4
  • Write: –w– → 2
  • Execute: ––x → 1

Most used combinations:

  • Read: r–– → 4+0+0 = 4
  • Read and exec: r–x → 4+0+1 = 5
  • Read and write: rw– → 4+2+0 = 6
  • Read, write and exec: rwx → 4+2+1 = 7

Further reading:

3
  • I was forcing the terminal to execute the file without being able to read it. Can't believe I didn't see that. Thanks. – zindarod Aug 3 '17 at 11:36
  • Please add the link provided by steeldriver to your answer. It will clear most of the confusion. – zindarod Aug 3 '17 at 11:45
  • @Zindarod, done. I intend to elaborate this answer later today. – pa4080 Aug 3 '17 at 11:50
3

You can also try this.

This will add execute permission to owner

chmod u+x file

This will remove execute permission from owner if he had it.

chmod u-x file

first character means who will be affected by this change.

  • u = user - owner
  • g = group - owning group
  • o = other - anyone
  • a = all - same as ugo

second character means if you will add or remove permissions

  • + = add permission
  • - = remove permission
  • = = set permission and overwrite

third character means which permission to apply

for files:

  • r = read - display content of file, copy
  • w = write - change content of file, remove, rename
  • x = execute - run script, program ...

for folders:

  • r = read - display content of folder
  • w = write - create, remove files from directory
  • x = execute - cd into directory

If you want to add write permission for owner to all files in folder, run

chmod u+w -R folder

You can also combine them, so following are valid

chmod ugo+r file
chmod ug+rx file
2
  • Advantages of this approach: 1) We don't need to make any calculations and pay attention which number or which place must be - more human readable; 2) When we using octal we changing the entire file's or folder's permissions, while in this way we just tweak the desired part of the permissions... At all, when this approach is applicable, we do avoid dumb mistakes. – pa4080 Aug 3 '17 at 12:48
  • Yes you are right but I think this method is easier to understand for an absolute beginner. Also to avoid mistakes you can use = instead of + or -. But I agree octal version is more powerfull – FK-VH Aug 3 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.