Table of Contents
- What is chmod? A Simple Definition
- How File Permissions Work in Linux
- Numeric vs. Symbolic Modes
- The Difference Between chmod and sudo chmod
- Common chmod Commands You Should Know
- Security Risks: Why 777 is Dangerous
- Best Practices for File Permissions
- common-errors and fixes while using chmod command
- Frequently Asked Questions (20 FAQs)
What is chmod command? A Simple Definition
Have you ever encountered the chmod command on a mac, linux, or other unix-based computer? Chmod command is the gatekeeper of your file system, determining who has access to view, edit, and execute a file.
You can think of your computer like an office building – certain areas of the building are accessible to everyone (public files), while other areas are for employees only (user files), and still other areas are restricted for use by only the security team (system files). Chmod command allows you to place locks on these “doors”.
How File Permissions Work in Linux
To understand the concept of chmod, we must first be clear on the three different parties who have access to our files:
- User (u) – This is the individual who owns the file.
- Group (g) – This is a set of users that can share access to a file.
- Others (o) – This is everyone else that has access to the system (the entire world).
Each of these types of users will have three different types of permissions:
- Read (r) – This means that a user has permission to read the data stored within the file.
- Write (w) – This means that a user has permission to write to or delete data from the file.
- Execute (x) – This means that a user can run the file (this could be a script or an application).
Numeric vs. Symbolic Modes
There are two methods to alter your permissions: numeric and alphabetic.
1. The Numeric (Octal) Method
In most situations, the numeric method is used to determine what permission each file or directory has. Every type of permission has a numerical representation, as shown here:
- 4 = Read
- 2 = Write
- 1 = Execute
- 0 = No Permission
Adding these numbers together will give you a single digit value, which represents the permissions for each user/group combination.
For example, if you have both Read and Write permissions for someone, then the numeric representation is 4 + 2 = 6.
Therefore, if your permission level for your file was “755”, this would indicate that the Owner has 7 (Read+Write+Execute), the Group has 5 (Read+Execute), and Everyone Else has 5 (Read+Execute).
The Difference Between chmod and sudo chmod
“Super User DO” (sudo) is an abbreviation of the administrative capability of executing commands on a computer system by an end user.
When you own the file as indicated by file ownership (e.g., a document in your personal folder) you should use chmod command.
When you need to change the permission of a file on a system or configure file located in etc and/or have permission denied from a designated user, you must execute the command using sudo permission because you may receive a “Permission Denied” response from your operating system without sufficient permissions.
Common chmod Command You Should Know
Below are some commands used for permissions by Developers and Webmasters frequently:
chmod 777 file.txt: FILE CAN BE ACCESSED BY ANYONE WITH FULL CONTROL (unsafe).chmod 755 script.sh: Owner HAS FULL CONTROL, Other Users Can ONLY READ AND EXECUTE.chmod 644 index.php: Owner CAN READ AND WRITE, Other Users CAN ONLY READ FILE (web file default).chmod -R 755 folder/: Sets PERMISSIONS FOR THE FOLDER AND ALL OF ITS FILES (recursive).
Security Risks: Why 777 is Dangerous
- Everyone that uses your system has access to view all of the sensitive system configuration files that contain passwords and API keys.
- Malicious scripts are injected into your source code that allows an attacker to compromise your application through backdoors or distributed malware.
- Attackers can remove any number of critical files. They can permanently delete your entire website or database.
- There are many world-writable directories on your server, so an attacker will have plenty of options for where to store the malicious files they upload.
- Bots can automatically scan for and exploit 777 permissions on your entire server.
- All legitimate users will also be able to overwrite any important files that they were supposed to share with their group members.
- Any processes that run under different user accounts may potentially corrupt one another’s files.
- Privilege escalation is very easy; an attacker can elevate their privileges from a low-privilege account to full administrative privileges.
- Since there is no difference between the access levels of users in an audit log, the audit trail becomes meaningless.
- Due to excessive file permissions granted to all users, there is a high risk of a violation of compliance standards (e.g., GDPR, PCI-DSS).
Common errors and fixes while using chmod command
-
“Operation not permitted” blocks everything
You’re trying to fix permissions but get slapped with
Operation not permitted. Happens when you don’t own the file.Quick Fix: Run
sudo chmod 755 yourfileor check who owns it first withls -la. -
sudo suddenly vanishes
sudo: command not foundappears out of nowhere.Quick Fix: Become root with
su -or beg your server admin for sudo access. -
File disappears mid-chmod
No such file or directorywhen the file was just there.Quick Fix: Use
pwdto confirm your location and tab-complete filenames. -
Symlinks turn into garbage
Recursive chmod breaks all your symlinks.
Quick Fix: Add
-h:sudo chmod -R -h 755 folder/. -
WordPress plugins refuse to install
After setting 777 everywhere, nothing works anymore.
Quick Fix: Reset with
find . -type d -exec chmod 755 {} \; && find . -type f -exec chmod 644 {} \;. -
Apache throws 403 errors everywhere
Your site loads but files return Forbidden.
Quick Fix:
sudo chown -R www-data:www-data /var/www/ && chmod -R 755 /var/www/. -
SSH keys stop working completely
Sudden
Permission denied (publickey)after permission tweaks.Quick Fix:
chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_rsa. -
Cron jobs mysteriously die
Scheduled tasks fail silently because scripts aren’t executable.
Quick Fix:
chmod 755 /path/to/your/script.sh. -
Docker containers can’t read volumes
Containers complain about mounted folder permissions.
Quick Fix:
sudo chown -R 1000:1000 /your/docker/volume. -
Git goes haywire on repos
remote: Permission deniedbreaks your workflow.Quick Fix:
sudo chown -R $USER:$USER .git. -
Nginx chokes on wp-config.php
500 errors after touching WordPress config files.
Quick Fix:
chmod 600 wp-config.php && chown www-data:www-data wp-config.php. -
MySQL socket becomes unreachable
Database connections fail with socket permission errors.
Quick Fix: Temporary:
sudo chmod 777 /var/run/mysqld/mysqld.sock. -
PHP-FPM can’t write sessions
Sessions break because FPM user lacks folder access.
Quick Fix:
sudo chown www-data:www-data /var/lib/php/sessions. -
SFTP uploads get rejected
550 Permission denied kills file transfers.
Quick Fix:
sudo chown $USER:$USER ~/webroot && chmod 755 ~/webroot. -
Email delivery suddenly stops
Mail server can’t write to spool directories.
Quick Fix:
sudo chown mail:mail /var/mail && chmod 660 /var/mail. -
npm global packages won’t install
EACCES errors plague Node.js development.
Quick Fix:
mkdir ~/.npm-global && npm config set prefix '~/.npm-global'. -
Redis refuses socket connections
Cache layer can’t access Unix socket files.
Quick Fix:
sudo chmod 770 /var/run/redis/redis.sock. -
Backup scripts can’t read files
Automated backups fail due to missing read permissions.
Quick Fix:
chmod +r /path/to/source && chown backupuser:backupuser /path/to/source. -
SELinux silently blocks everything
Permissions keep reverting no matter what you do.
Quick Fix:
sudo setsebool -P httpd_can_network_connect 1. -
Disk fills during recursive chmod
No space left on deviceduring mass permission changes.Quick Fix: Monitor with
watch df -hand usechmod -R --changes 755 dir/.
Frequently Asked Questions
- What does chmod 755 do? It allows the owner to read, write, and execute, while others can only read and execute.
- Is sudo chmod command safe? It is safe if you know what you are changing. Using it on vital system files can break your OS.
- How do I see current permissions? Type
ls -lin your terminal to see a list of files and their current permission strings. - What is the “R” flag in chmod command? It stands for Recursive, applying the command to all subfolders and files.
- Why can’t I edit my WordPress config file? You likely need
sudo chmodto give yourself write permissions. - What is octal notation? It’s the number system (0-7) used to represent permissions in the command line.
- What does chmod +x do? It makes a file “executable,” allowing you to run it as a program.
- Can I use chmod on Windows? No, Windows uses a different system called ACLs, though WSL (Windows Subsystem for Linux) supports chmod.
- What is the difference between 644 and 664? In 664, the group also has permission to write/edit the file.
- How do I reset permissions? There is no “reset” button; you must manually change them back to the desired numbers.
- What is a “Superuser”? This is the root account that has total control over the file system.
- What does chmod 400 do? It makes the file read-only for the owner and grants no access to anyone else. Great for SSH keys!
- Why is my website showing a 403 Forbidden error? This often happens because your file permissions are too restrictive for the web server to read.
- What is “u+g”? This is symbolic notation meaning “User plus Group.”
- Does chmod change the file content? No, it only changes the metadata/access rules.
- Can a group be a single person? Yes, in Linux, every user usually has their own private group.
- How do I change permissions for only directories? You can use the
findcommand combined with chmod. - What is the “Other” category? It refers to any user who is not the owner and not in the assigned group.
- Is 775 better than 777? Yes, because it still restricts “the world” (others) from writing to your files.
- Can I use chmod in a FTP client? Yes, most FTP clients like FileZilla allow you to right-click and “Change Permissions.”
20 chmod Best Practices
Managing your file system via the command line is a superpower. To keep your system secure:
- Use the 777 option only when absolutely required, for testing purposes, for a very short period of time.
- Folders should be assigned user and group permissions of 755.
- Files should have permissions of 644 assigned.
- Before using
sudo chmod -R, always verify the path you are using. - To verify current file or folder permissions, use
ls -la. - Always apply the Principle of Least Privilege (PoLP). To apply PoLP, only assign the minimum amount of file/folder permissions required.
- For web files, permissions for file ownership should be 644, and for directories, permissions should be 755, to provide maximum security when using Apache or Nginx.
- Do not make the WordPress
wp-config.phpfile world-readable (assign 600 only for this configuration). - Use a permission setting of 600 for your
~/.ssh/id_rsaSSH key file. If necessary, stricter permissions can be used. - Do not recursively
chmod/or any parent directories. - Use
chownprior to usingchmodto ensure you have assigned the correct file and folder ownership. - Test any permission changes in a non-production environment first.
- Document your permission schemes for team members to collaborate on.
- Conduct regular auditing of permissions using the
findcommand. - Group permissions for a shared team directory should be at least 750.
- Use 755 only for executable scripts that need to be executed by other users.
- Always create a backup before performing a major
chmodoperation. - After making any permission changes, monitor your logs for any unauthorized access attempts.
- Create an alias for your most frequently utilized safe permission pattern.
Stay Connected with My Coding Journey
Don’t let scammers stop your professional growth. Join our community for more tech safety tips!
For more tutorials and guides, check out: CodingJourney.co.in







