Pre-requirements
- Access to your cPanel account or SSH for command line usage.
- A basic understanding of Linux/UNIX file permissions and ownership.
Overview of File Permissions in Linux
As a multi-user system, Linux/UNIX uses defined users, groups, ownership, and permissions to specify who has access to files and directories. This is a built-in security measure designed to ensure that only the person with the right privileges can access or modify a file. Since cPanel is a Linux-based system, the same rules apply to your website directories and files.
Types of Permissions
Files and directories in Linux have the following three permissions for each of the three kinds of owners (user, group, others):
For Files:
- Read: Can view or copy file contents.
- Write: Can modify file content.
- Execute: Can run the file if it is executable.
For Directories:
- Read: Can list and copy files in a directory.
- Write: Can add or delete files in a directory (execute permission is also needed).
- Execute: Can enter the directory using the cd command.
Viewing Permissions with the ls Command
You can view file and directory permissions using the ls -al command:
ls -al
-rw------- 1 username username 1086 Aug 26 06:14 .bash_history -rw-r--r-- 1 username username 18 Aug 30 2021 .bash_logout -rw-r--r-- 1 username username 141 Aug 30 2021 .bash_profile -rw-r--r-- 1 username username 376 Aug 30 2021 .bashrc drwx------ 3 username username 273 Oct 30 12:03 logs drwxr-x--x 5 username username 200 Apr 12 2022 mail --- truncated ---
Viewing File Attributes with the stat Command
The stat command provides detailed information about file or directory attributes, including permissions, ownership, and inode data:
stat public_html
File: public_html Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 10302h/66306d Inode: 1652570144 Links: 12 Access: (0750/drwxr-x---) Uid: ( 1032/ $username) Gid: (65534/ nobody) Access: 2022-10-30 07:01:31.300407834 +0000 Modify: 2022-10-27 21:27:26.498114504 +0000 Change: 2022-10-27 21:27:26.498114504 +0000 Birth: 2021-08-30 22:38:36.741992151 +0000
Setting File Permissions in cPanel
Once you have created a new web hosting account and logged in to your cPanel account, follow these steps to set file permissions:
- Go to the Files section and click on the File Manager icon.
- Once the File Manager loads, navigate to the file or directory you want to modify.
- Select the file or directory and click on the Permissions option in the toolbar.
- In the permissions window, check or uncheck the boxes for read, write, and execute for the user, group, and others as needed.
- Click Save to apply the changes.
Note:
This method works best for modifying permissions for a single file or folder at a time. For bulk modifications or advanced permissions settings, the command line is more efficient.
Modifying Permissions via Command Line
If you have migrated files to your new cPanel hosting account and want to ensure proper permissions, use the following commands to modify directory and file permissions:
1. Set Directory Permissions
To modify directory permissions for all directories inside public_html:
find /home/$username/public_html -type d -exec chmod 755 {} \;
2. Set File Permissions
To modify file permissions for all files inside public_html:
find /home/$username/public_html -type f -exec chmod 644 {} \;
Gotchas to Avoid
- Make sure not to assign too open permissions (e.g., 777), as this can lead to security vulnerabilities.
- Be cautious when setting execute permissions for files that don’t need to be executed.
Correct file permissions are critical for the proper functioning of your website. Whether you're using cPanel's graphical interface or the command line, understanding how to set permissions will ensure that your files and directories are secure and accessible to the right users. Always be cautious with permissions, especially when using chmod on system-critical files or directories.
If you want to add to this tutorial or have found an error in it, please send an email to team+editorial@webhostingm.com