- Change folder permissions and ownership
- 9 Answers 9
- Learn to Use Chmod Command With These Examples
- Chmod command in Linux
- Chmod command examples
- Change permission on all the files in a directory recursively
- chmod 777: Everything for everyone
- chmod +x or chmod a+x: Execution for everyone
- chmod 755: Only owner can write, read and execute for everyone
- chmod 700: Everything for owner only
- chmod 666: No one executes
- chmod 644: Everyone can read, only owner can write
- chmod 600: Owner can read and write, nothing else for anyone
- chmod command examples in symbolic mode
- A word of warning!
Change folder permissions and ownership
I would like the user to have full rights on this folder (as well as all sub-directories and files in it):
currently owned by root. I have found numerous posts (in this forum and elsewhere) on how to do this for files but I can’t find a way to do it for whole folders.
9 Answers 9
Use chown to change ownership and chmod to change rights.
As Paweł Karpiński said, use the -R option to apply the rights for all files inside of a directory too.
Note that both these commands just work for directories too. The -R option makes them also change the permissions for all files and directories inside of the directory.
sudo chown -R username:group directory
will change ownership (both user and group) of all files and directories inside of directory and directory itself.
sudo chown username:group directory
will only change the permission of the folder directory but will leave the files and folders inside the directory alone.
As enzotib mentioned, you need to use sudo to change the ownership from root to yourself.
Note that if you use chown : (Note the left-out group), it will use the default group for that user.
If you want to change only the group, you can use:
BEWARE of recursively taking ownership of ANY directory. Think before you leap. Don’t be chown copypastin’ from the internet, kids. Just because you want to install a node package and it won’t let you, don’t sudo chown -R just because the fist hit from googling the error message says to. Reckless sudo chown -R -ing can kill your OS.
It needs to be said that using the -R option only applies to files and folders that exist in the directory already. It does NOT apply to future creations. For example, if you create another folder or file as root within the folder you’ve already changed permissions on, you will have the same experiences you’re having now until you chmod\chown them again.
Make the current user own everything inside the folder (and the folder itself):
very helpful for newbies (like me) when don’t know what to type in ‘usergroup’ for sudo chown
If you prefer, this can be done with a GUI as well. You will need to open Nautilus as root to do so. Press Alt + F2 to access the «Run Applications» dialog and enter gksu nautilus
Next, browse to and right click on the folder you would like to modify. Then, select «Properties» from the context menu. You can now select the user or group that you would like to be the «Owner» of the folder as well as the permissions you would like to grant them. Finally, press «Apply Permissions to Enclosed Files» to apply the changes recursively.
Though it seems this does not always work for some operations in a deep folder tree. If it does not work use the appropriate terminal command.
Learn to Use Chmod Command With These Examples
This article will teach you how to change permissions in Linux with practical examples of chmod command.
Sooner or later in the Linux world, you will have to change the permission on a file or directory. This is done with the chmod command.
In this article, I’ll share with you some of the practical examples of chmod command. I’ll also explain some the popular terms like chmod 777 or chmod 755 or chmod -r.
Before you see the chmod examples, I would strongly advise you to learn the basics of file permissions in Linux. Using chmod command will be a lot easier once you understand the permissions.
Chmod command in Linux
What is chmod? chmod stands for change mode. This command is used for changing the mode of access.
But wait! Is it not meant for changing the permission? Actually, in early Unix days, permissions were called mode of access. This is why this particular command was named chmod.
chmod command has the following syntax:
Before you see how to use chmod, you should know its options.
- -v : output a diagnostic for every file processed
- -c : like verbose but report only when a change is made
- –reference=FILE : use FILE’s mode instead of MODE values
- –R : change permissions recursively
Note that using -v option report if change were made or if nothing needed to be done. When combined with -R option, -v can produce a lot of output. –reference=FILE let you use the current permission mode of FILE as the permissions to set on the target file. Note this option requires a double-dash prefix (–) not (-).
Chmod command examples
Using chmod command is very easy if you know what permissions you have to set on a file.
For example, if you want the owner to have all the permissions and no permissions for the group and public, you need to set the permission 700 in absolute mode:
You can do the same in symbolic mode.
If you want an easy way to know the Linux file permission in numeric or symbolic mode, you can use this chmod calculator. Just select the appropriate permissions and it will tell you the permissions in both absolute and symbolic mode.
Change permission on all the files in a directory recursively
chmod has the recursive option that allows you to change the permissions on all the files in a directory and its sub-directories.
chmod 777: Everything for everyone
You might have heard of chmod 777. This command will give read, write and execute permission to the owner, group and public.
If you want to change the mode to 777, you can use the command like this:
chmod 777 is considered potentially dangerous because you are giving read, write and execute permission on a file/directory to everyone (who is on your system). You should totally avoid it.
chmod +x or chmod a+x: Execution for everyone
Probably one of the most used case of chmod is to give a file the execution bit. Often after downloading an executable file you will need to add this permission before using it. To give owner, group and everyone else permission to execute file:
chmod 755: Only owner can write, read and execute for everyone
This next command will set the following permission on file: rwxr-xr-x. Only the owner will be allowed to write to the file. Owner, group members and everyone else will have read and execute permission.
chmod 700: Everything for owner only
This command will give read, write and execute permission to the owner. Group and others will have no permissions, not even read.
chmod 666: No one executes
To give owner, group and everyone else read and write permission on file.
chmod 644: Everyone can read, only owner can write
With this next one, owner will have read and write while group and everyone else have read permission.
chmod 600: Owner can read and write, nothing else for anyone
With this next one, owner will have read and write while group and everyone else will have no permissions whatsoever.
chmod command examples in symbolic mode
In the above examples, I use bitmask to set the new MODE. Those are easy to calculate. Simple addition is required. Consider the following:
You can now easily see where I got the 755, 666, 640 from. You don’t have to use bitmask to set new permission. A more human readable way is available. This second format looks like this:
chmod OPTIONS /path/to/file
While this may seem complicated, it is quite simple. You first start with typing chmod and the OPTIONS that you want. Then, ask yourself: Who am I changing permissions for? User, Group, Others. This will give you the first section of the command:
The next step to complete the command, you either decide to add permissions bits (+), remove permissions (-), or set permission (=). This last one will add or remove permissions as needed to set permission as you requested.
The next section is where you decide the permission MODE to apply(+), remove (-) or match (=). You can specify any combination of rwx.
This next example will apply read/write permission to file for the owner. The verbose option will cause chmod to report on the action.
This next one will set the group’s write permission on directory and all its content recursively. It will report only on changes.
chmod -cR g+w /path/to/directory
You can combine multiple operation to be done on permission like this next example. It will make sure owner has read/write/execute, also add write permission for group and remove execution for everyone else:
chmod u=rwx,g+w,o-x /path/to/file
This last one will use rFile as a reference to set permission on file. When completed, the permission of file will be exactly as they are for rFile
chmod --reference=/path/to/rFile /path/to/file
There are more options and MODE that can be used with chmod that are not covered or mentioned here. I wanted to keep this to basic and hopefully help a few new Linux user.
A word of warning!
With chmod and sudo you now have to power to change permission on almost any files. This does NOT mean you should. Permissions outside your home directory are set the way they are for a reason. Changing them is rarely the appropriate solutions to any problems.
I hope these chmod command examples were helpful for you. Got a question or suggestion? Please leave a comment below.