Linux permissions: who can read, write and run a file

Every Linux file has an owner, a group and three sets of rwx bits. Reading them is the first skill on any server.

Time O(1)Space O(1)Code C#
Watch it animatedStep through it, with the running line highlighted. Free with an account.

The idea

A permission string like rwxr-xr-- is three groups of three bits: owner, group and everyone else. r = 4, w = 2, x = 1, so each group is one octal digit. The kernel checks only the group that applies to you: owner first, then group, then other.

Code

// What "chmod 754 deploy.sh" means, decoded the way the kernel checks it
static string Decode(string octal)
{
    string who = "";
    foreach (char d in octal)
    {
        int bits = d - '0';
        who += (bits & 4) != 0 ? "r" : "-";
        who += (bits & 2) != 0 ? "w" : "-";
        who += (bits & 1) != 0 ? "x" : "-";
    }
    return who; // owner, group, other
}

Tips

In the .NET library

On Linux, ASP.NET Core apps usually run as a dedicated service user under systemd or in a container with a non-root user, so a bug in the app cannot rewrite system files.