Linux Commands

Understanding Chmod +X – Making Files Executable

Understanding Chmod +X - Making Files Executable

The “chmod” command in Linux-based Distributions like Ubuntu is used to change the access permissions of files and folders for users. In Ubuntu, getting unauthorized access to other users may endanger the overall security of the system. To prevent those security risks, certain file and folder permissions have to be changed.

This article briefly discusses the “chmod” command and its use in making files executable.

Understanding Chmod +X – Making Files Executable

With “chmod +X”, the permissions of files and folders can be changed and those files are also made executable. The “X” command specifies and sets the execute permission on the file. The “+” sign means adding permission. The syntax for using the “chmod” command is:

chmod [options] [mode] [filename]

Types of Permissions

The permissions are of 3 types:

  • Read (r): Allowing Users to view the file.
  • Write (w): Allowing Users to write to the file.
  • Execute (x): Granting Execute Permission to a file.

Types of Users

These permissions can affect four types of users on Ubuntu. These types are:

  • Owners (u): Permissions only apply to Owners.
  • Groups (g): Permissions only apply to users in a specific group.
  • All Users (a): Permissions apply to Every User.
  • Others (o): Permissions only apply to other people outside of a specific group or a specific type of user.

Permission Operators

The Permission Operators are of 3 types:

  • +”: Add User Permission
  • ”: Remove User Permission
  • =”: Set Permission to Specific Users

To manage file permission and make files executable for users, the “chmod” command is used in the Terminal of Ubuntu.

Using the Command Line or Terminal to Make Files Executable

The Command line can be used to make any file executable. To make an executable file in Ubuntu, first you need to make a script file with the “.sh” extension using the “nano” command:

nano <fileName>.sh

In our case, use the following command to make a script file “ScriptFileA.sh” with the nano command:

sudo nano ScriptFileA.sh

Once you hit “enter”, the text editor will open:

In the text editor, add the script that is to be executed. In our case, the following script will be added:

#!/bin/bash

echo "Hello World!"

Once the script is added, “write out” the script using the “ctrl+o” key:

It will ask for confirmation, hit “enter” to save it and then close the Text Editor using the “ctrl+x” shortcut key. The Script File will be created and can be ensured using the “ls” command:

ls

In our Documents Directory, the Script File “ScriptFileA.sh” is created:

Before executing the file, change the permission of the Script File using the “chmod” command:

sudo chmod +x ScriptFileA.sh

The “+x” means the File can now be executed by any user. The cursor moves to the next line indicating the permission was added:

Now, to Execute the File, the “./” command can be used:

./<fileName>.sh

In our case, the file name is “ScriptFileA.sh”:

./ScriptFileA.sh

This will return the executed output in the terminal:

Understanding File Permission

The file permissions can be useful for ensuring which users can access the file. The “ls” command followed by “-l” is used to list all the files with details including the permissions. In our case, the “ls -l” command will be used to list the files with details in the Documents Directory:

ls -l

The command line displays all the files with details:

The first column of the detailed file list provides information about the file permission. In our case, the Script File “ScriptFileA.sh” has the “-rwxr-xr-x” permission meaning all users have the read, write, and execute permission:

In our case, the permission string consists of three sets, i.e. “-rwx”, “r-x”, and “r-x”.

These three sets are referred to as the “owner(o)”, “group(g)”, and “others(o)”. The first set informs about the “owner” permission, the second set informs the “group of users” permission, and the third set informs the “other users” permission. In our case, the “owner” has “rwx” permission meaning the owner can read the file, edit the file, and can also run or execute the file. The group of owners has the read and execute permission, and the file has only the read and execute permission for other users.

Making Files Executable for All Users

Multiple files can be made executable as well using the “chmod” command:

chmod +x <fileName>

With “+x” every user can execute the file. In our case, we have a “ScriptA.sh” file having the read and write permission for the current user:

To change the File Permission, the “chmod” command will be:

sudo chmod +x ScriptA.sh

The “+x” will ensure that the file is executable for every user. To confirm, the “ls -l” command can be used to display the permission.

The permission changed allowing all users to execute the script file. To change the execute permission of the file only for the user, use the below command:

chmod u+x <fileName>

The “u+x” indicates the file is only executable for the user. In our case, the file “abc.txt” currently has the read and write permissions only:

Now to change the permission and make the file “abc.txt” executable for the user only, the command used is:

sudo chmod u+x abc.txt

The “-rwx” in the first set indicates it is executable for the user:

Making Directory Executable for All Users

The permission for directories inside Ubuntu can also be changed using the “chmod” command. The “chmod” command to change directory permission is the same as it is used for changing the file permission:

chmod +x <directoryName>

In our case, the “NewDir” has no permissions for any user:

To make the Directory executable for All users, the command used will be:

sudo chmod +x NewDir

This will make the Directory Executable for all users:

Making Multiple Files Executable

Multiple Files can also be executed using the “chmod” command. The syntax to make multiple files executable is:

chmod +x <file1> <file2>

In our case, the files “fileA.txt” and “fileB.txt” has only the read and write permission currently:

To change the permission of both files and make them executable for all the users, the “chmod” command will be used as follows:

sudo chmod +x fileA.txt fileB.txt

This will make both files executable for all users:

Restrict Execution Permission Using “chmod”

Instead of only adding the execute permissions on files and folders, permissions can also be removed restricting users from certain access or management. To remove permission or restrict execution permission specifically, the “-” symbol is used with the execute “x” operation:

chmod -x <fileName>

In our case, the Script file “ScriptFileA.sh” has the execute permission for all users:

To remove the execution permission from all users, the command used is:

sudo chmod -x ScriptFileA.sh

This will remove the execution permission from ScriptFileA.sh File:

Conclusion

The “chmod” command is useful for managing the file permissions of files and folders in Ubuntu. The chmod command is used with permission operators and user permissions to specify certain user permissions for a file or folder. It can also be used to limit access or permissions of files from users. This article explained all the different permission restrictions and additional methods with the “chmod” command.

Similar Posts