InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Setting Permissions in bash
Bash

Setting Permissions in bash

InfinityCoder February 28, 2017

You want to set permissions in a secure manner.

If you need to set exact permissions for security reasons (or you are sure that you don’t care what is already there, you just need to change it), use chmod with 4-digit octal modes.

1
$ chmod 0755 some_script

If you only want to add or remove permissions, but need to leave other existing permissions unchanged, use the + and – operations in symbolic mode.

1
$ chmod +x some_script

If you try to recursively set permissions on all the files in a directory structure using something like chmod -R 0644 some_directory then you’ll regret it because you’ve now rendered any subdirectories non-executable, which means you won’t be able to access their content, cd into them, or traverse below them.

Use find and xargs with chmod to set the files and directories individually.

1
2
$ find some_directory -type f | xargs chmod 0644 # File perms
$ find some_directory -type d | xargs chmod 0755 # Dir. perms

Of course, if you only want to set permissions on the files in a single directory (nonrecursive), just cd in there and set them.
When creating a directory, use mkdir -m mode new_directory since you not only accomplish two tasks with one command, but you avoid any possible race condition between creating the directory and setting the permissions.

Many people are in the habit of using three-digit octal modes, but we like to use all four possible digits to be explicit about what we mean to do with all attributes.

We also prefer using octal mode when possible because it’s very clear what permissions you are going to end up with.

You may also use the absolute operation (=) in symbolic mode if you like, but we’re  traditionalists who like the old octal method best.
Ensuring the final permissions when using the symbolic mode and the + or – operations is trickier since they are relative and not absolute.

Unfortunately, there are many cases where you can’t simply arbitrarily replace the existing permissions using octal mode.

In such cases you have no choice but to use symbolic mode, often using + to add a permission while not disturbing other existing permissions.

Consult your specific system’s chmod for details, and verify that your results are as you expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls -l
-rw-r--r-- 1 jp users 0 Dec 1 02:09 script.sh
 
# Make file read, write and executable for the owner using octal
$ chmod 0700 script.sh
 
$ ls -l
-rwx------ 1 jp users 0 Dec 1 02:09 script.sh
 
# Make file read and executable for everyone using symbolic
$ chmod ugo+rx *.sh
 
$ ls -l
-rwxr-xr-x 1 jp users 0 Dec 1 02:09 script.sh

Note in the last example that although we added (+) rx to everyone (ugo), the owner still has write (w).

That’s what we wanted to do here, and that is often the case.

But do you see how, in a security setting, it might be easy to make a mistake and allow an undesirable permission to slip through the cracks?

That’s why we like to use the absolute octal mode if possible, and of course we always check the results of our command.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Keeping a Private Stash of Utilities by Adding ~/bin in bash
You have a stash of personal utilities you like to …

Keeping a Private Stash of Utilities by Adding ~/bin in bash

Using More Than Just a Constant String for Default in bash
You need something more than just a constant string as …

Using More Than Just a Constant String for Default in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap