chmod vs chown: What's the Difference?

When configuring servers, deploying web applications, or resolving terminal access errors in Linux, developers frequently encounter two core commands: chmod and chown. Because both commands interact directly with file security metadata and are often executed back-to-back during troubleshooting sessions, many engineers confuse their specific responsibilities. The distinction between them is fundamental to Unix architecture: chown defines who owns the resource (the identity), while chmod defines what operations those owners are permitted to perform (the access rights). Mastering the interplay between ownership and permissions ensures clean, secure server administration without resorting to dangerous shortcuts like chmod 777.

Advertisement

What chmod Changes

The name chmod stands for change mode. In Unix terminology, the "file mode" refers to the nine standard permission bits governing read (r), write (w), and execute (x) rights across the three user classes (owner, group, and others), plus special bits like setuid and the sticky bit. Executing chmod modifies these permission bits without altering the identity of the user or group associated with the file:

# Check initial permissions and ownership
$ ls -l script.sh
-rw-r--r-- 1 developer staff 1024 Jan 15 10:00 script.sh

# Use chmod to make the file executable (chmod 755)
$ chmod 755 script.sh

# The ownership (developer:staff) is UNCHANGED, but the mode bits changed!
$ ls -l script.sh
-rwxr-xr-x 1 developer staff 1024 Jan 15 10:00 script.sh

You can execute chmod on any file that you personally own without requiring superuser (sudo) elevation. Standard modes configured via chmod include chmod 755 for directories and scripts, chmod 644 for static assets, and chmod 600 for private credentials like SSH keys.

What chown Changes

The name chown stands for change owner. This command modifies the user ID (UID) and group ID (GID) metadata recorded on the file inode. While chmod dictates whether a file can be read or written, chown designates which specific accounts belong to the user and group classes:

# Check current file ownership
$ ls -l index.html
-rw-r--r-- 1 root root 2048 Jan 15 10:05 index.html

# Transfer ownership to webmaster and group www-data
$ sudo chown webmaster:www-data index.html

# The permissions (-rw-r--r--) remain IDENTICAL, but the owners changed!
$ ls -l index.html
-rw-r--r-- 1 webmaster www-data 2048 Jan 15 10:05 index.html

Unlike chmod, regular users cannot use chown to transfer ownership of their files to other accounts (a security restriction known as "chown restriction" to prevent users from bypassing disk quotas or framing other users). Therefore, running chown almost always requires sudo administrative privileges. The basic syntax supports updating the user alone (chown user file), the group alone (chown :group file, identical to chgrp), or both simultaneously (chown user:group file).

Common Combinations

In production web operations, chown and chmod are frequently paired to initialize new application deployments, fix broken upload directories, or establish collaborative environments. Here are the three most common standard patterns:

# Pattern 1: Initializing a clean web server document root
sudo chown -R www-data:www-data /var/www/html
find /var/www/html -type d -exec chmod 755 {} +
find /var/www/html -type f -exec chmod 644 {} +

# Pattern 2: Setting up a private SSH key directory
sudo chown -R developer:developer /home/developer/.ssh
chmod 700 /home/developer/.ssh
chmod 600 /home/developer/.ssh/id_ed25519
chmod 644 /home/developer/.ssh/id_ed25519.pub

# Pattern 3: Shared team project directory with group collaboration
sudo chown -R deployer:engineers /opt/project
chmod 2775 /opt/project

In Pattern 1, chown ensures that the Nginx or Apache web server account (www-data) owns the web root, while chmod guarantees that directories can be traversed (chmod 755) and files can be served (chmod 644) without granting write access to public visitors. In Pattern 2, ownership is anchored to the developer, while chmod 700 and chmod 600 ensure that OpenSSH strict permission checks are satisfied. In Pattern 3, chown links the folder to the engineers group, while chmod 2775 enforces group write capabilities and group inheritance for all new files.

Which One Fixes My Problem?

When you encounter a "Permission Denied" error in the Linux terminal, how do you know whether you need to run chmod or chown? Follow this decision framework:

  • Is the file owned by the wrong user? If a deployment script created files as root inside a developer home directory, or if an Nginx upload failed because a directory is owned by root:root, you need chown. Assign ownership to the process executing the command: sudo chown -R www-data:www-data /var/www/uploads.
  • Is the file owned by the right user, but the action is blocked? If you own a script (e.g., deploy.sh) but cannot execute it via ./deploy.sh, the ownership is correct, but the permission mode is missing the execute bit. You need chmod: chmod +x deploy.sh (or chmod 755).
  • Are colleagues in your group unable to edit a shared file? If ownership belongs to your team group, but members cannot save edits, the group class lacks write permission. You need chmod: chmod g+w filename (or chmod 664).

Never attempt to resolve ownership mismatches by making files world-writable with chmod 777. Aligning proper ownership via chown combined with accurate, minimal permissions via chmod creates an unbreachable, professional server architecture.

Advertisement

Frequently Asked Questions

Can chmod change file ownership?

No. The chmod command only alters file permission modes (read, write, execute). To change the user or group owner of a file, you must use the chown or chgrp command.

Which command requires sudo: chmod or chown?

Regular users can use chmod on files they personally own without sudo. However, changing file ownership with chown almost always requires superuser (sudo) privileges.

Should I run chown before or after chmod?

Typically, you run chown first to establish correct user and group ownership, followed by chmod to enforce the appropriate read, write, and execute permissions.

Advertisement