Cybersecurity 8 min read

Linux Permissions: Understanding chmod, chown, and File Access Rights

Understand Linux file permissions, chmod numeric modes, chown functionality, and safe practices to prevent files from being exposed to unauthorized users.

Terminal komputer untuk menggambarkan pengelolaan hak akses Linux

Linux file permissions determine who can read, modify, or execute a file. Understanding permissions helps avoid two common problems: applications failing due to overly restrictive permissions, or data becoming exposed to unauthorized users due to overly permissive settings.

Three basic permissions

  • r (read): view file contents or list directory entries.
  • w (write): modify files or add and remove directory entries.
  • x (execute): run program files or access directories to traverse their contents.

Linux applies permissions to three classes: file owner (user), group, and others. The ls -l command displays this information, for example -rw-r--r--. The first character indicates the object type, while the following three groups describe access rights.

Reading numeric modes

Each permission has a value: read = 4, write = 2, execute = 1. Values are summed per class. Mode 755 means the owner has read/write/execute (7), while group and others have read/execute (5). Mode 644 typically means the owner can read and modify the file, while other groups can only read.

chmod usage examples

chmod 644 notes.txt
chmod 755 script.sh
chmod u+x script.sh

chmod changes permission modes. These examples are illustrative; the correct mode depends on the file’s purpose. Scripts that need to run may require execute permission, while sensitive configuration files should not be readable by all users.

The chown function

chown changes the owner and, if needed, the group of a file. An example like sudo chown username:groupname file.txt changes ownership after verifying the account and group names. Avoid running recursive commands on large directories without checking the target first.

Mistakes to avoid

  • Don’t make chmod 777 a universal solution. This permission grants read, write, and execute to all classes for the target object.
  • Don’t use sudo when the operation doesn’t require it.
  • Check paths before recursive commands with options like -R.
  • Use dedicated service accounts and minimum privileges on servers.

File management is also important for web services and storage. When configuring buckets, set permissions according to whether files actually need public access. Read Nazuaf’s article on file storage with Cloudflare R2 for object storage context.

How directory permissions differ from file permissions

Permissions mean different things for files and directories, and confusing the two is a common source of trouble. On a file, read lets you view contents, write lets you modify them, and execute lets you run the file as a program. On a directory, read lets you list its entries, write lets you create or delete entries inside it, and execute lets you access the files and subdirectories within it, provided you know their names.

This distinction explains a situation that puzzles many beginners: a user can open a file inside a directory even without read permission on that directory, as long as they have execute permission on the directory and appropriate permission on the file itself. Conversely, a user with read permission on a directory but no execute permission can see the names of files inside it but cannot open any of them. The two permissions work together, and understanding their interaction prevents both overly open and mysteriously broken setups.

Write permission on a directory deserves extra caution. It controls whether entries can be added or removed, which means a user with write access to a directory can delete files inside it even if they do not own those files and cannot modify their contents. This surprises people who assume that file ownership alone protects a file. If a shared directory needs to allow file creation without allowing deletion of other people’s files, the sticky bit exists precisely for that purpose.

Special modes: setuid, setgid, and the sticky bit

Beyond the basic nine permission bits, Linux supports three special modes that appear in the first position of a numeric mode. The setuid bit, when set on an executable file, causes the program to run with the privileges of the file owner rather than the user who launched it. This is how tools that need elevated access for a specific task work, and it is also why setuid binaries deserve careful review. A setuid program with a flaw can become a path to privileges the user was never meant to have.

The setgid bit behaves similarly for group ownership, and on directories it has a particularly useful effect: files created inside a setgid directory inherit the group of the directory rather than the primary group of the user who created them. This makes shared project directories much easier to manage, because new files automatically belong to the project group. Without setgid, each contributor’s files arrive with their own group, and access becomes inconsistent.

The sticky bit is most familiar from the shared temporary directory, where anyone can create files but only the owner can delete them. When set on a directory, it prevents users from removing or renaming files that belong to someone else, even if the directory itself is writable by everyone. In listings, the sticky bit shows as a letter t in place of the execute bit for others. These three modes are powerful precisely because they change the normal rules, so they should be set deliberately and documented, not applied casually.

Umask and default permissions

New files do not arrive with random permissions; they are shaped by the umask, a setting that removes permission bits from the defaults the system would otherwise apply. A typical umask of 022 means new files are created with mode 644 and new directories with mode 755. The umask subtracts write permission for group and others, which is why a file you create is readable by others but not writable by them unless you change it.

Changing the umask changes the starting point for everything created afterwards in that session or by that user. A more restrictive umask such as 027 removes write for group and all access for others, which suits environments where privacy matters more than sharing. A more permissive umask such as 002 allows group write access, which fits collaborative directories where the group is trusted. The choice depends on the environment, and it should be made consciously rather than copied from a random example.

Note that the umask can only remove permissions, never add them. A program that requests mode 600 for a sensitive file gets 600 regardless of a permissive umask, while a program that requests 666 gets reduced by the umask. This is why the umask is a safety net rather than a complete solution: it prevents accidental over-sharing, but it cannot fix a program that explicitly asks for broad permissions.

Permissions in practice: web servers and shared folders

Web servers are a good place to see permissions in action, because the server process runs as its own user and needs exactly the right access. Content files generally need to be readable by the server user, while directories need execute permission so the server can reach the files inside. Upload directories need write permission for the server user, but they should never allow uploaded files to be executed as programs. A misconfigured upload directory has been the starting point of many server compromises.

Configuration files are the opposite case: they should be readable only by the accounts that genuinely need them. A configuration file holding database credentials has no business being readable by every user on the system. Restricting it to the owner, or to the owner and a specific group, is a simple step that limits the damage if another account is compromised.

Shared folders for a team follow similar principles. Start by deciding who needs to read, who needs to write, and whether new files should be shared automatically. A dedicated group for the project, a setgid directory, and a suitable umask together produce a folder where collaboration works without constant permission repairs. The alternative, repeatedly fixing permissions after every new file appears, is a sign that the setup was never designed; it was assembled by accident.

Recovering from a permission mistake

Permission mistakes happen to everyone, and the recovery matters more than the mistake. The most common error is running a recursive command on the wrong path. A command such as a recursive ownership change pointed at the wrong directory can alter thousands of files in seconds. Before running any recursive permission command, verify the path by listing it first, and consider running the command on a small subdirectory to confirm the effect.

If a mistake has already happened, stop and assess before running more commands. Changing permissions again in a hurry often makes the problem worse, because the original state is no longer known. If backups exist, restoring the affected files may be cleaner than guessing at the correct modes. If the system still boots and services still run, the damage may be limited to data files, which are easier to repair than system directories.

For system directories, a permission error can prevent services from starting or lock users out entirely. In that situation, recovery may require booting from external media or using a rescue mode provided by the hosting service. This is another reason to treat recursive commands with respect: the time spent double-checking a path is trivial compared with the time spent rebuilding a system. A habit of pausing before pressing enter is the cheapest insurance available.

Conclusion

Use ls -l to inspect, chmod to change permissions, and chown to set ownership. Choose the minimum required rights and verify targets before running commands with administrative privileges.

Bagikan artikel ini

No comments yet

    Leave a comment

    Your email address will not be published. Required fields are marked *