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?
2 Answers
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
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: