drwxr-xr-x Mean Wetin? Linux Permissions Explain
Understand drwxr-xr-x one character at a time, see why e map to 755, wetin x mean for directory, and why chmod 777 almost never be the right fix.
Wetin drwxr-xr-x mean
drwxr-xr-x describe directory wey e owner fit change, while every other user fit read am and pass through am without changing anything. For octal, that mode na 755. Linux dey print these ten characters for the beginning of every file entry for ls -l output, and dem always mean the same thing for the same order. So, if you learn one string, you don learn all of dem.
One rule dey come before the others because e decide whether your own test go show you anything. Root normally get the CAP_DAC_OVERRIDE capability (discretionary access control override). This capability bypass file read and write checks, plus directory traverse checks. To execute a regular file, at least one execute bit still dey required, and root process fit get restrictions on its capabilities. Log in as normal user when you want watch how the rules dey work.
The ten characters, one at a time
Make one directory and one file wey you fit inspect. Nothing for here go touch anything outside the new directory.
mkdir -p ~/permdemo/inner
printf 'hello\n' > ~/permdemo/inner/notes.txt
ls -ld ~/permdemo ~/permdemo/inner ~/permdemo/inner/notes.txtWith the common default umask of 022 and no parent default ACL, newly created directory lines go start with drwxr-xr-x and the newly created file line go start with -rw-r--r--.
Character 1 na file type, e no be permission. d na directory. - na regular file. l na symbolic link. c and b na character and block device nodes. s na socket, while p na named pipe. E dey outside the octal value, na why drwxr-xr-x become 755 instead of something wey start with d.
The nine characters after am dey in three groups of three, and dem no dey change order.
- Characters 2 to 4 na the owner triad, the bits wey apply to the user wey own the file.
- Characters 5 to 7 na the group triad, the bits wey apply to the file group, or the ACL mask when one dey present.
- Characters 8 to 10 na the other triad, the bits wey apply to everybody else.
Inside one triad, the slots always be r, then w, then x, and dash mean say that bit dey off. The letters no dey change position. r-x mean read without write. -w- mean write without read, and this one legal but e rare.
So drwxr-xr-x split like this: d for directory, then rwx for owner, then r-x for group, then r-x for other.
Some systems dey print an eleventh character. A trailing dot, drwxr-xr-x., mean say the file get security context but no other alternate access method, and this one normal for SELinux distributions like Fedora and Rocky. A trailing plus, drwxr-xr-x+, mean say another alternate access method, like a POSIX ACL (access control list), dey apply to the file. Read those extra rules with getfacl <path>.
r, w and x dey do different work for directory
Na this be the first rule wey beginners dey get wrong. The letters na the same for file and directory. But the powers wey dem give no be the same.
rfor file go read the contents.rfor directory go list the names inside am, and na wetin plainlsneed.wfor file go change the contents.wfor directory, together withx, go allow you add and remove entries inside am. To delete file dey change the directory wey hold am, so e need write and traverse permission for that directory, subject to restrictions like sticky bit; the file own read and write bits no decide am.xfor file go run am as program.xfor directory go traverse am, meaning say kernel fit resolve one name inside am during path lookup.
Traverse na the part wey dey surprise people. x for directory no execute anything. To open /srv/site/index.html, you need x for /, then x for /srv, then x for /srv/site, and finally r for the file. If one directory for that chain no get x for you, the lookup go stop there, and caller go receive Permission denied for the whole path even when the file for the end dey readable by everybody. namei -l /srv/site/index.html go print every step for the chain with its mode and owner, so you fit see which link stop am.
Directory wey get r and no get x na strange half state wey you need recognise. Normal user fit list the names, because na r grant that permission, but e no fit stat any entry. So ls -l go fill the size and mode columns with question marks and print ls: cannot access ...: Permission denied for each one.
Wetin make the letters turn 755
Each triad na three-bit number. r na 4, w na 2, and x na 1. Add the bits wey dey on.
rwxna 4 + 2 + 1 = 7rw-na 4 + 2 = 6r-xna 4 + 1 = 5r--na 4---na 0
So, drwxr-xr-x na 7 for the owner, 5 for the group, and 5 for other users: 755. -rw-r--r-- na 6, 4, 4: 644. drwxrwxr-x na 7, 7, 5: 775, wey be 755 plus write permission for the group. You no need count the characters by hand, because stat dey print both forms together.
stat -c '%A %a %U %G %n' ~/permdemo ~/permdemo/inner/notes.txt%A na the letter form, %a na the octal form, while %U and %G show the owning user and owning group.
Permission strings wey people dey search for
Na these modes you go meet for real server, together with their octal value and where each one dey show up.
-rw-r--r--na 644. Ordinary files wey service only dey read, like config file or HTML page.-rw-------na 600. Secrets: SSH private key, or application.envfile.-rw-rw-r--na 664. File wey dey inside directory shared by group, where teammates need write access.-rwxr-xr-xna 755. Scripts and binaries, like/usr/local/bin/backup.shand most of/usr/bin.-rwx------na 700. Private script wey only the owner fit run.drwxr-xr-xna 755. Almost every system directory, and website document root.drwx------na 700.~/.ssh, and home directories for locked-down box.drwxrwxr-xna 775. Directory wey owner's group fit write into.drwxrwsr-xna 2775. Na the same thing, plus setgid bit, so new files inside go inherit the directory group.drwxrwxrwtna 1777./tmp. The finaltna sticky bit, so unprivileged user fit remove or rename file only if dem own the file or the directory; privileged processes fit bypass this restriction.-rwsr-xr-xna 4755. Setuid binary wey dey run as the owner, like/usr/bin/passwdand/usr/bin/sudo.-rw-rw-rw-na 666, anddrwxrwxrwxna 777. World writable, and for server this nearly always na mistake.lrwxrwxrwxna wetin ordinary symbolic links dey show. Linux dey ignore their mode bits and check the target instead, so this string no get meaning for access checks; special magic links fit show different modes.
Which triad apply to you
Na here beginners dey make the second mistake. For ordinary permission bits without extended ACL, kernel go pick exactly one triad and stop there.
If your user ID match the file owner, you go get owner triad, and kernel no go check group and other bits. If e no match, but file group dey among your groups, you go get group triad. If not, you go get other triad.
Two things follow from this. Owner triad still apply even when na the most restrictive one. File wey get mode 0466, and wey -r--rw-rw- print, go give owner read only while everybody else fit write, because owner check match first and kernel no read anything after that. This one legal, but e dey confuse everybody the first time dem see am.
File group na wetin determine group triad, no be the full list of groups wey you belong to. ls -l print two names for each line: owner first, then group. Na only that second group get say for that file. Belonging to twenty groups go help only when file carry one of dem.
id
stat -c '%U %G %A %n' ~/permdemo/inner/notes.txtid print your user and every group wey you dey inside. stat print file owner and group. Compare the two, and you go know which triad kernel go use for you.
Na why shared directory usually get group and setgid bit. sudo chmod 2775 /srv/shared print as drwxrwsr-x, and files wey dem create inside go inherit directory group instead of creator personal group. New subdirectories too go get setgid bit. Setgid dey set group, e no dey set mode. With umask of 022, no default ACL, and creation request of 0666, new file inside go come out as 644. Teammates fit read am, but dem no fit write am. For creation request of 0666, umask of 002 preserve group write and produce 664; 007 too preserve am and produce 660. Suitable default ACL na another option, but program requested mode still limit am. You fit also change permissions after creation. Give each service its own account na the other half of this matter, and e dey covered for one Linux user per service on a VPS.
umask and default ACLs dey decide permissions wey new files get
If parent directory no get default ACL, new file go take initial mode from program wey create am, minus permission bits wey process umask clear. umask na mask of bits to remove, so when you set bit for am, e go remove that permission.
Most distributions dey ship with 022. Program wey create regular file usually dey request 0666. Program wey create directory usually dey request 0777. If parent directory no get default ACL, umask go clear 022 from both requests. So files go get 644, and directories go get 755. Na exactly this pair you dey see for fresh VPS.
umask
umask -S
touch ~/permdemo/new.txt && mkdir -p ~/permdemo/newdir
ls -ld ~/permdemo/new.txt ~/permdemo/newdirumask -S go print the same value as letters, and e easier to read pass 0022. Set umask 027 inside ~/.profile for stricter default: 640 for files and 750 for directories. This one mean say your group fit read your work, but nobody else fit. These results assume the usual 0666 and 0777 requests and say parent directory no get default ACL.
Two limits dey important. umask fit only clear bits; e no fit add dem. File wey program request as 0666, na wetin touch dey ask for, no fit ever become executable, no matter wetin you set. Also, program wey ask for owner-only access go keep am: mktemp go create files as 600 and directories as 700 under umask 022 or 027. systemd service no dey automatically read your shell profile, so set the value inside the unit file instead.
[Service]
UMask=0027One situation dey skip umask completely. When parent directory get default ACL, umask no dey apply. New file permission bits go come from inherited ACL, but permissions wey no dey inside the mode wey program request go remain turned off.
Why web files dey 644 and web directories dey 755
Web server dey run under im own account, www-data for Debian and Ubuntu, nginx for Rocky and Alma. That process need read permission for the files wey e serve and traverse permission for the directories above dem. E no get reason to write to dem, and static site no suppose ever allow am.
644 for file give owner write permission and give everybody read permission, so deploy user fit publish and web user fit serve. 755 for directory give owner write permission and everybody traverse permission, so web user fit follow the path but e no fit add or delete anything. If bug dey inside application, e no fit rewrite the pages wey e dey serve.
The traverse rule na the part wey dey cause problem here. If site dey inside /home/deploy/site, /home/deploy na 750, and web user no dey inside the directory group, web user no fit enter the home directory at all. The request go end with HTTP 403 and line like this for /var/log/nginx/error.log:
open() "/home/deploy/site/index.html" failed (13: Permission denied), client: 203.0.113.5That 13 na EACCES, wey be the kernel permission refusal. Network no get problem: port dey listen and request don arrive. Na this one dey confuse person when you still dey learn how listening ports dey work for Linux. Run namei -l /home/deploy/site/index.html and read down the chain to find the first directory wey no get x for other users.
Directory wey application dey write into, like upload path, na the exception. Grant access through ownership instead of using wider mode: sudo chown -R www-data:www-data /srv/site/uploads and leave the mode at 755. Keep write access only for the one directory wey need am.
chmod, without flattening the whole tree
chmod get two forms. Octal go set all nine bits at once: chmod 644 notes.txt. Symbolic go change only wetin you name and leave the rest as e be: chmod u+x deploy.sh add execute for the owner, while chmod go-w notes.txt remove write from group and other.
Recursion na where tree fit spoil. chmod -R 755 . dey mark every image and every config file as executable, because chmod no fit know difference between script and JPEG. Use capital X instead.
chmod -R u=rwX,go=rX ~/permdemo
stat -c '%a %n' ~/permdemo ~/permdemo/inner/notes.txtCapital X dey apply execute to directories, and to files wey already get execute bit somewhere. Directories go come out as 755, ordinary files go come out as 644, and scripts wey already executable go remain so. When one file already get mode wey you trust, chmod --reference=good.sh other.sh go copy am across.
The messages wey you go see when permission get problem
bash: ./deploy.sh: Permission denied get different causes: x bit dey miss for the triad wey apply to you, one directory for the path no get x, interpreter access dey denied, or filesystem mount as noexec. chmod u+x deploy.sh go fix the first case.
bash: ./deploy.sh: cannot execute: required file not found na another fault with confusing name. The x bit dey correct, but the interpreter wey dem name for the first line no dey. Windows line endings na the usual cause, so the kernel dey look for interpreter wey dem call /bin/bash\r. Use sed -i 's/\r$//' deploy.sh repair am.
Permissions 0644 for '/home/deploy/.ssh/id_ed25519' are too open. dey come from the SSH client. E no go use private key wey other accounts fit read. The key need 600, while ~/.ssh need 700. Full key handling dey for how to manage SSH keys and their file permissions.
Authentication refused: bad ownership or modes for directory /home/deploy/.ssh dey show for the server journal when home directory or its .ssh dey writable by group. The sshd StrictModes setting dey reject the key. From the client side, this one go look like unexpected password prompt without explanation.
sudo: /etc/sudoers is world writable, followed by sudo: no valid sudoers sources found, quitting, mean say sudo check the mode of its own config and refuse to run. That file suppose be 0440, and e must never be writable by everybody. This one na the classic result of broad recursive chmod. E fit happen together with the sshd message above, and then provider console go remain the only way to enter the server again.
Why 777 no be fix
777 dey give every account for the machine write access, plus every process wey dey run under any of those accounts. Server dey run services with their own users, so “everybody” cover far more for VPS than e cover for laptop. If somebody breach a service, e fit write anywhere wey 777 allow.
For web root, the harm dey direct. If directory world writable and server dey serve am too, file upload flaw fit become way to plant script, then request the script back.
777 almost always be wrong answer to ownership question. The symptom be “app no fit write to this directory”. The cause be say wrong user own the directory. sudo chown -R appuser:appuser /srv/app/storage with mode 755 solve am: application fit write there, while every other account still fit list and enter the directory but no fit write inside am. Creating those accounts before anything deploy belong for the first ten minutes for new VPS.
World write legitimate for one familiar place, /tmp, and the spelling be drwxrwxrwt. The write bits make directory writable by everybody; the final t be sticky bit, wey restrict removal and renaming to file owner, directory owner, and privileged processes. Without that bit, any account fit delete another account temporary files.
Read mode before you change am
These commands dey only read state, so e safe to run dem anywhere.
id
umask
stat -c '%A %a %U %G %n' ~/permdemo ~/permdemo/inner/notes.txt
namei -l ~/permdemo/inner/notes.txt
find ~/permdemo -perm -0002find <path> -perm -0002 dey list everything under a path wey get world write bit set. Na the fastest way to audit a box after person don use chmod 777 repair things.
To check whether particular service account fit enter directory, ask as that account. sudo -u www-data test -x /srv/site && echo yes || echo no dey print yes when that user get traverse permission for the directory, and no when e no get am. If you ask as root with CAP_DAC_OVERRIDE, e bypass directory mode checks, so e no show whether the service account fit enter.
FAQ
Wetin drwxr-xr-x mean for Linux?
Na directory, and the first d show am. E get mode 755. Owner triad na rwx, so owner get every permission for am. Group triad na r-x, while other triad na r-x. So everybody else fit list the names wey dey inside and pass through the directory, but na owner alone fit add or remove anything. Confirm any path with stat -c '%A %a %U %G %n' <path>. E go print the letter form and octal form side by side.
Why web files dey 644 and web directories dey 755?
Web server dey run under another account, www-data for Ubuntu. E need read permission for the files wey e dey serve and traverse permission for every directory above dem. E no get reason to write to either one. 644 give owner write and everybody else read. 755 give owner write and everybody else traverse. If application genuinely need write access to a directory, hand the directory over with chown to that application's user, instead of widening the mode for everybody.
The x bit mean say I fit execute directory?
No. For directory, x mean traverse. Na the right to resolve one name inside the directory as kernel dey walk through a path. cd need am, and every attempt to open a file underneath the directory need am too. Every directory along the path need x. So, file wey get mode 644 still no go reachable if directory above am no get x for you. namei -l /path/to/file print the mode of every directory for the chain, so you fit see where lookup stop.
chmod 777 ever be correct fix?
Almost never for server. E give every account for the machine write access, including accounts wey services dey use to run. So, one breached service fit rewrite the file. When application no fit write to a directory, the real problem usually na ownership. sudo chown -R appuser:appuser /srv/app/storage with mode 755 give the application write access, while other accounts still fit read the directory but no fit write inside am. The common exception na /tmp at 1777. For there, sticky bit restrict removal and renaming to the file owner, directory owner, and privileged processes.
Why ls dey print dot or plus after permissions?
That eleventh character describe rules beyond the nine permission bits. Dot, as for drwxr-xr-x., mean say security context dey attached, but no other alternate access method dey apply. This one normal for Fedora and Rocky. Plus, as for drwxr-xr-x+, mean say another alternate access method apply, such as POSIX ACL (access control list). Run getfacl <path> to list those extra entries.