DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 4

How to Grant Executable Permissions to a Script

Why Permissions Matter

In a Linux or Unix-like operating system, file permissions are a fundamental security feature. They control who can read, write, or execute a file. This is crucial for system stability and security, as it prevents unauthorized users from running scripts that could harm the system or access sensitive information.

When you create a new script (like a .sh file), it usually doesn't have executable permissions by default. This is a safety measure. Before you can run it, you must explicitly tell the system that the file is safe to execute.


The chmod Command

The command used to change file permissions is chmod, which stands for "change mode." There are two main ways to use it: with symbolic notation and with octal (numeric) notation.

1. Symbolic Notation

This method is often the easiest to remember. You use a combination of letters to specify the permissions you want to change.

  • u = user (owner)

  • g = group

  • o = others

  • a = all (user, group, and others)

  • + = add a permission

  • - = remove a permission

  • = = set a permission

  • r = read

  • w = write

  • x = execute

To grant all users executable permissions for the /tmp/wycliffe.sh script, you would use:

chmod a+x /tmp/wycliffe.sh
Enter fullscreen mode Exit fullscreen mode

This command literally means "change mode for all users, add the execute permission."

2. Octal (Numeric) Notation

This method uses a three-digit number to set permissions for the user, group, and others. Each digit is a sum of the permissions:

  • 4 = read (r)
  • 2 = write (w)
  • 1 = execute (x)

So, to grant read and execute permissions, you'd use 4 + 1 = 5. To grant all three, you'd use 4 + 2 + 1 = 7.

A common permission set for scripts is 755. This means:

  • 7 (user): read, write, and execute permissions
  • 5 (group): read and execute permissions
  • 5 (others): read and execute permissions

To apply this to your script, you'd run:

chmod 755 /tmp/wycliffe.sh
Enter fullscreen mode Exit fullscreen mode

Verifying the Change

After you've run the command, you can confirm the change by using the ls -l command.

ls -l /tmp/wycliffe.sh
Enter fullscreen mode Exit fullscreen mode

The output will show a string of permissions at the beginning. If the script is executable for all, it will look something like this:

-rwxr-xr-x 1 user group 1234 Aug 7 09:00 /tmp/wycliffe.sh

The x in the output confirms that the execute permission has been successfully granted.

Top comments (0)