Changing File Permissions Linux
The chmod command is used to change the permissions of a file or directory. To use it, you specify the desired permission settings and the file or files that you wish to modify. There are two ways to specify the permissions, but I am only going to teach one way.
It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here’s how it works:
rwx rwx rwx = 111 111 111 rw- rw- rw- = 110 110 110 rwx --- --- = 111 000 000
rwx = 111 in binary = 7 rw- = 110 in binary = 6 r-x = 101 in binary = 5 r-- = 100 in binary = 4
(rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
(rwxr-xr-x) The file’s owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
(rwx——) The file’s owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
(rw-rw-rw-) All users may read and write the file.
(rw-r—r—) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
(rw——-) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.
Directory permissions
The chmod command can also be used to control the access permissions for directories. In most ways, the permissions scheme for directories works the same way as they do with files. However, the execution permission is used in a different way. It provides control for access to file listing and other things. Here are some useful settings for directories:
(rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.
(rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.
(rwx——) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.
Use this command on terminal
to grant all access (Read, Write, Execute).
If you don’t need execution access and need only right access then
In general, chmod commands take the form:
chmod options permissions filename
If no options are specified, chmod modifies the permissions of the file specified by filename to the permissions specified by permissions.
permissions defines the permissions for the owner of the file (the user ), members of the group who owns the file (the group ), and anyone else ( others ). There are two ways to represent these permissions: with symbols (alphanumeric characters), or with octal numbers (the digits 0 through 7).
Here the digits 7, 5, and 4 each individually represent the permissions for the user, group, and others, in that order. Each digit is a combination of the numbers 4, 2, 1, and 0:
- 4 stands for «read»,
- 2 stands for «write»,
- 1 stands for «execute», and
- 0 stands for «no permission.»
So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1 (read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute).
I think, you got the idea. For more detail read man entry of chmod (this page).
Use chmod command, as a example ;
To change the owner of the file use chown as in
Old post, but maybe somebody will still find useful my answer.
If you want to give access to all users (or «other than owner» — meaning last digit ) you can run next command:
cd sudo find . -perm 750 -exec chmod 757 <> +;
It will find all files with permission 750 (read + write + execute for owner user / read + execute for group owner / nothing for other users) and by changing just last digit, you give the same permission as owner, to all other users. (last 7 meaning read + write + execute for other users)
Of course, you can run the command with different digits, but just replace last digit with the value of first on. For example, run next commands to ensure that all file that are read + write + executable for owner , will have the same right access for ‘other’:
sudo find . -perm 700 -exec chmod 707 <> +; sudo find . -perm 701 -exec chmod 707 <> +; sudo find . -perm 702 -exec chmod 707 <> +; sudo find . -perm 703 -exec chmod 707 <> +; sudo find . -perm 704 -exec chmod 707 <> +; sudo find . -perm 705 -exec chmod 707 <> +; sudo find . -perm 706 -exec chmod 707 <> +;
If you will continue in this way (having one line for every combination 7xx), you will end up running 77 commands. Which is a bit too much. right?
Luckily, there can be done a trick. You can use «-» in front of the number that meaning that the file has «at least» that number. For example, running:
will return all files that have permission 775, 776 and 777. Another example is running:
that will return files with permision 770, 771 . 777.
In our case, we are care only about first digit and we want to change last digit with its value, so there can used:
sudo find . -perm 700 -exec chmod 777 <> +;
**> This will work only for files on which owner has read + write +
execute. If you will try above command with 600 instead of 700, keep in mind that it will return also files with permission 6xx and files with permission 7xx.**
Chmod Command – How to Change File Permissions in Linux
Daniel Rosa
One of the first commands I learned on Linux was the touch command that creates a file using the command line.
If you ever try to create, for instance, a text file, all you have to do is type touch filename.txt , press Enter, and the file is created for you in the directory you are in. You can then write whatever you want in it using the file editor of your choice.
However, when it comes to creating scripts, things can get a little more complicated. Why is that?
Let me try to show you this with another example. Suppose you want to create a script using touch. Type touch example.sh , press Enter, and there you have it. Once more, you can write in it using a file editor.
With everything ready, all that’s left to do is to test it. You type ./sample.sh and press Enter to see your script in action and…but what is this message telling me?
Why do we need permissions?
I am the admin! How come I don’t have permission to run a script I wrote myself seconds ago?
There is actually a reason for that – and, to be honest, most users should be thankful for it, since not being able to execute scripts without knowing what you’re doing often prevents you from putting your system at risk.
Let’s have a quick chat about permissions first. Then we’ll move on to discovering how to change them.
In order to get some more information about your file, we will use the command that lists the files in a directory: ls .
After typing ls and pressing Enter, this is what we get in the command line:
What it does is list all the visible files in the directory you are at the moment. By adding the flag -l to it, it provides you with more information on the files in the directory. This is the result when we type ls -l and press Enter:
We see the same file(s), but with plenty of information before it/them. To begin with, we have a sequence of ten dashes and letters that may seem meaningless at first. This is actually the mapping of permissions your file has.
The first character can be a dash ( — , for a common file), a d (for a directory), or an l (for a soft link). For the sake of simplicity, I will focus on the simple files, although permissions are valid for all these kinds of files/folders.
After the first character, the other 9 can be divided in groups of three. The first trio show the permissions for the current user. The next shows the permissions for this group. The last three are the permissions for all users that don’t fit this category.
For our explanation, let’s focus on the first three permissions, since we are not going to change groups or anything of the sort.
Time to understand what these are. There are three things you can normally do with a file: read it, write in it, and execute it. That is, basically, what those letters mean.
The first r stands for the reading permission. Then we have w for the writing permission. Finally, a dash, meaning whatever should be there is not. What should be there is an x , standing for eXecutable.
So, talking about the current user (me), the permissions I have for this file are the reading and the writing permissions. I can’t, however, execute the sample.sh file. That’s why, when trying to execute the file, I got that ‘permission denied’ message.
How can I, then, execute this file? That’s where the chmod command comes into play.
What does chmod do?
Well, I’ve been mentioning ‘permissions’ since the beginning of the article. Let’s extend this explanation a little bit to say that they are ‘permissions to access a file in a certain mode. This means that r denotes the permission to access the file in reading mode, w denotes the permission to access the file in writing mode, and x denotes the permission to access the file in executable mode.
Why am I telling you that? Because of the command this article is all about. chmod stands for ‘change mode’. In other words, when you use this command, you are changing a file’s mode to whatever mode you want to use.
How to Use Operators with cmod
It is necessary to use an operator with the chmod command. It serves to specify the kind of change you want to do on the permissions.
For instance, + is the operator you use to add a permission to the ones the file already has. — removes a permission from the list. There is also the = operator, which resets the permissions so you can define them again.
In our case, by typing chmod -w sample.sh , what I am asking the command to do is to remove the writing permission. So what I would have to do to add the executable permission is to type chmod +x sample.sh .
If I now try to execute the file, whatever I put in the script is now going to be executed.
Using ls -l , this is what I would have now.
Who gets the permission?
Something else worth pointing out is who is getting this permission. You will see that the ‘x’ is given to all users, file owner, group, and others. If this is not what you are going for, maybe it is a good thing to remove the executable permission again with chmod -x sample.sh .
In order to enable the permission only for the owner of the file (me, in this case), we should add a ‘u’ before the ‘+x’, like this:
Typing ls -l , that’s what you have:
If you wanted to give the permission for both the owner and its group, then the command would be chmod ug+x sample.sh .
Great! I think this covers what I would like to show you. There are other things that could be of interest to you, like how to use chmod with octal or binary values to represent the permissions. But these are modes we use to have the same results and I believe the letters are an easier way to achieve these results.
In case you want more information on the command, one thing you can do is type chmod —help , which will give you a briefing on what the command can do. An even more detailed description can be achieved by typing man chmod .
I hope this article was helpful to you. For more articles on Linux commands, check freecodecamp.org/news.