Guide of Magento 2 File and Folder Permissions
In this blog, we will understand about files and folder permissions in Magento 2. In Magento, File and Folder Permissions can be set using either the command line(terminal) or a file manager application provided by your hosting provider. Let’s see all topics related to file and folder permission in Magento 2 one by one as below.
1. Magento 2 file permissions :
In Magento when we create a file or folder, magento will automatically provide them default permissions. These default permissions can increase security risks for your important files and folders containing your private or important data . For example,a text file or folder has 777 permissions, which means everyone gets read, write and execute permissions. So, anyone can change or fetch your data from that file or folder.
To See the file and folder permissions, we have to run the ls-li command in the command line(terminal) to see the Permissions of all the files and folders available in that directory.
As You can see in the above image,the permission of all the folders and files inside the one directory. By Running this command we get all the information about file and folder like permission flags, owner name, group name, size of file or folder, last modification date and time etc.
In the above image, the 2nd Column is the Permission Flag Column. For e.g. drwxrwxrwx this flag is giving all Permission information about a particular directory.
Position | Meaning |
1 | Here,"d" if flag of directory, "-" if flag of normal file |
2,3,4 | read(r), write(w), execute(x) permission for user (owner) of file |
5,6,7 | read(r), write(w), execute(x) permission for group |
8,9,10 | read(r), write(w), execute(x) permission for other (world) |
There are two types of commands are available to set or change permissions of files and folders in magento 2 : 1) UMASK Commands 2) CHMOD Commands
We will understand about this Commands one by one as below :
1) UMASK Commands
The chmod (chmod means change mode) command is used to change the permission flags on existing files. It can be applied recursively using the -R option. It can be invoked with either an octal value representing the permission flags, or with symbolic representations of the flags. All chmod commands are as below :
-
UMASK VALUES FILE PERMISSIONS FOLDER PERMISSIONS 002 -rw-rw-r-- drwxrwxr-x 007 -rw-rw---- drwxrwx--- 002 -rw-r--r-- drwxr-xr-x 027 -rw-r----- drwxr-x--- 077 -rw------- drwx------ When a file is created in the system, the permission flags are set according to the file mode creation mask, which can be set using the umask command. The Umask is a three-digit octal value whose nine bits relate to fields 2-10 of the permission flags. Some usually used umask values are as below :
-
Octal Digit Binary Representation (rwx) Permissions 0 000 none 1 001 execute only(x) 2 010 write only(w) 3 011 write and execute(w+x) 4 100 read only(r) 5 101 read and execute(r+x) 6 110 read and write(r+w) 7 111 read, write, and execute (r+w+x)
2) CHMOD Commands
2. Magento 2 file permissions command
In Magento, all files should have 660 permissions. Such a permission means that the owner and the group can read and write, but other users have no permissions.
-
In some cases, 660 cannot be used. Then, you should use 644 instead.
-
Run the below command to apply new permission for files :
-
find . -type f -exec chmod 644 {} \;
-
3. Magento 2 folder permissions command
In Magento, all directories should have 770 permissions. This permission gives read, write and execute permission to the owner and to his group, but no permissions to anyone else.
-
In some cases, 770 cannot be used. Then, you should use 755 instead.
-
Run the below command to apply new permissions for folders :
-
find . -type d -exec chmod 755 {} \;
4. Magento 2 pub/static permission command
In Magento all temporary and media directories (/var, /pub/media, /pub/static) and files should have public access (777 permission).
-
find ./var -type d -exec chmod 777 {} \;
-
find ./pub/media -type d -exec chmod 777 {} \;
-
find ./pub/static -type d -exec chmod 777 {} \;
5. Bin/magento permission denied
Usually, we get this error when we are running some script/file that does not have execute permissions. All we need to do is to change file permissions and add an executive one for that particular file..
-
To fix the bash permission denied error, follow below steps :
-
Open terminal (shell)
-
Navigate to the folder with the script
-
Run below command to change file permission settings:
-
chmod +x path_to_file/file_name
6. Magento 2 file permission Check failed
When permissions are not set properly, the Web Setup Wizard displays this error. It usually happens when directories in the Magento file system are not writable by the web server user, and the Magento file system owner should also have all permissions.
-
To solve this error, first you have to keep up with the following prerequisites :
-
1. check magento is installed at /var/www/html/ this path.
-
2. You should have command-line access.
-
If you have satisfied above conditions, Then Follow Below Steps :
-
Open terminal (shell)
-
Navigate to root directory of your magento by below command
-
cd /var/www/html/magento
-
Then change permission with below command
-
sudo chmod 777 -R var/ generated/ pub/
7. Magento 2 reset file permissions
To Reset Permissions for Magento 2 you have to Run Below Commands in Terminal after navigate to Your magento root directory :
-
cd <your Magento install dir>
-
find . -type f -exec chmod 644 {} \;// 644 permission for files
-
find . -type d -exec chmod 755 {} \;// 755 permission for folders
-
chmod 644 ./app/etc/*.xml
-
chown -R :<web server group>
-
chmod u+x bin/magento
Conclusion :
We hope the above blog will help you to understand Magento 2 file and folder permissions. If you have any query then you contact us or let us know in the comment section.