Chapter 3: Linux Basic Commands
3.5 Permissions
Friends, most "403 Forbidden" and "Permission denied" problems come from this very topic, so pay attention. Once r, w and x are clear, half of your troubleshooting is already done.
मित्रांनो, "403 Forbidden" आणि "Permission denied" चे बहुतेक प्रॉब्लेम्स याच टॉपिकमधून येतात, म्हणून लक्ष द्या. एकदा r, w आणि x नीट समजले की तुमचं अर्धं ट्रबलशूटिंग आधीच झालं.
दोस्तों, "403 Forbidden" और "Permission denied" के ज़्यादातर प्रॉब्लम्स इसी टॉपिक से आते हैं, इसलिए ध्यान दो. एक बार r, w और x ठीक से समझ आ गए तो आपकी आधी ट्रबलशूटिंग पहले ही हो गई.
Every file has an owner (u), a group (g) and others (o). Each can have read (r), write (w) and execute (x) permission.
| Permission | On a file | On a directory | Value |
|---|---|---|---|
| r (read) | View contents | List files (ls) |
4 |
| w (write) | Modify contents | With search, add, remove or rename entries, subject to additional access rules | 2 |
| x (execute) | Run as program/script | Search the directory and traverse it on the way to another path | 1 |
Numeric (octal) notation — add the values for each of user, group, others:
| Octal | Symbolic | Meaning | Typical use |
|---|---|---|---|
| 777 | rwxrwxrwx |
Everyone everything | Avoid — insecure |
| 755 | rwxr-xr-x |
Owner full, others read+enter | Directories, scripts, web folders |
| 750 | rwxr-x--- |
Owner full, group read and search or execute, others nothing | Private app directories |
| 700 | rwx------ |
Only owner | ~/.ssh directory |
| 644 | rw-r--r-- |
Owner write, others read | Web files (HTML, CSS), configs |
| 640 | rw-r----- |
Owner write, group read | Restricted config when the owning group is authorized to read it |
| 600 | rw------- |
Only owner read/write | ~/.ssh/authorized_keys |
| 400 | r-------- |
Only owner read | .pem private key |
The lab below uses mode changes on a script that you create. chmod changes mode. Use chown to request an ownership change and chgrp to request a group change. For EC2 SSH from macOS or Linux, AWS shows chmod 400 for the private key file on the client computer. Verify the exact key path before changing its mode.
Check the web worker's access
Find the worker identity and the exact file in its error log. Check search permission on every parent and read permission on the file. A public static directory might use 755 and a public static file might use 644. A site with a dedicated serving group can use narrower modes such as 750 and 640 when that group can traverse the path. Choose the permissions with the deployment owner.
For the Mauli Dairy website you need these three numbers every day: 755 for folders, 644 for HTML and CSS files, and, for the key on your laptop, .pem gets 400. All other numbers are just variations around these.
Mauli Dairy च्या वेबसाइटसाठी हे तीन नंबर्स रोज लागतात: फोल्डर्स 755, HTML आणि CSS फाइल्स 644, आणि तुमच्या लॅपटॉपवर .pem की 400. बाकी सगळे नंबर्स यांच्याच आजूबाजूचे आहेत.
Mauli Dairy की वेबसाइट के लिए ये तीन नंबर्स रोज़ चाहिए: फ़ोल्डर्स 755, HTML और CSS फ़ाइलें 644, और आपके लैपटॉप पर .pem की 400. बाकी सारे नंबर्स इन्हीं के आस-पास के हैं.
Ravindra Bagale's Tip
Before changing permissions on a live server, always run ls -l first and note down the original values. And please, never use chmod -R 777 to "fix" an error – it only hides the real problem and opens a security hole. Find out which user needs access, and give only that much access. That's all.
Ravindra Bagale's Tip – मराठी
Live server वर permissions बदलण्याआधी नेहमी आधी ls -l चालवा आणि original values लिहून ठेवा. आणि please, error "fix" करायला कधीच chmod -R 777 वापरू नका – ते फक्त खरा problem लपवतं आणि security hole उघडतं. कोणत्या user ला access पाहिजे ते शोधा, आणि तेवढाच access द्या. बस इतना ही.
Ravindra Bagale's Tip – हिंदी
Live server पर permissions बदलने से पहले हमेशा पहले ls -l चलाओ और original values लिख लो. और please, error "fix" करने के लिए कभी chmod -R 777 इस्तेमाल मत करो – वो सिर्फ़ असली problem छुपाता है और security hole खोल देता है. किस user को access चाहिए वो ढूँढो, और उतना ही access दो. बस इतना ही.
Ravindra Bagale's Tip
A folder at 644 has no search bit, so an ordinary process cannot traverse it. If a web worker needs that path, this can cause a read denial. Check the worker identity, each directory with ls -ld, and the file with ls -l. Choose the narrowest permissions that serve the site.
Ravindra Bagale's Tip – मराठी
Folder ला 644 दिलं की search साठी लागणारा x bit नसतो. Web worker त्या path मधून जाऊ शकत नसेल तर file वाचता येणार नाही. Worker identity, प्रत्येक directory ls -ld ने आणि file ls -l ने check करा. साइटला जेवढी permission लागते तेवढीच द्या.
Ravindra Bagale's Tip – हिंदी
Folder को 644 देने पर search के लिए ज़रूरी x bit नहीं रहता. Web worker उस path को पार नहीं कर पाए तो file पढ़ नहीं पाएगा. Worker की identity देखो, हर directory को ls -ld और file को ls -l से check करो. साइट को जितनी permission चाहिए उतनी ही दो.
Lab
Chala, create a fresh directory for your sample script. Check the printed path before continuing.
script_lab=$(mktemp -d "$HOME/mode-script-lesson.XXXXXXXX") &&
cd -- "$script_lab" &&
printf '%s\n' '#!/bin/sh' 'printf "%s\n" "Practice script"' > ./deploy.sh &&
pwd && ls -l ./deploy.sh
In that directory, set the script to 750, remove its owner execute bit, then restore that bit. Run ls -l after each command. Calculate the number for rwxr-x--- independently and compare it with your result.
chmod 750 ./deploy.sh
ls -l ./deploy.sh
chmod u-x ./deploy.sh
ls -l ./deploy.sh
chmod u+x ./deploy.sh
ls -l ./deploy.sh
To clean up, check your location and the listing, remove just ./deploy.sh, return to your home directory and remove the empty directory using rmdir -- "$script_lab".
Got it? r=4, w=2, x=1 – once you can read these three numbers and the output of ls -l, most permission problems get solved. Now let's move on to users and sudo.
समजलं का? r=4, w=2, x=1 – हे तीन नंबर्स आणि ls -l चा आउटपुट वाचता आला की परमिशन्सचे बहुतेक प्रॉब्लेम्स सुटतात. आता पुढे जाऊया यूजर्स आणि sudo कडे.
समझ आया? r=4, w=2, x=1 – ये तीन नंबर्स और ls -l का आउटपुट पढ़ना आ गया तो परमिशन्स के ज़्यादातर प्रॉब्लम्स सुलझ जाते हैं. अब आगे चलते हैं यूज़र्स और sudo की तरफ़.
Give each worker access to the part of the job they must do
Mauli Dairy has three people involved when it publishes the milk collection times. Asha maintains the timetable. The website program copies the approved text into a response when a visitor opens the page. A support colleague checks a private shift note when a driver calls. All three jobs involve files. Only one of them requires editing the timetable. If the website process can rewrite the timetable, a programming error could change the published pickup time. If the support colleague can read every private document, a simple lookup gives that account much more access than the job requires. Permissions let us express these differences on the machine where the files live.
Imagine the public timetable is served from a directory called published. Asha owns its files. A group called site-readers includes the account that runs the website worker. The group is a named collection of users. We can assign a group to a file and give members of that group the access they need. A private shift note belongs under a separate directory. Keeping it outside the published path reduces the chance that a configuration mistake can present it as public content. The fact that the timetable is public on the website and the fact that an account can open the local file are separate questions. Web visitors request a reply from a process. That process uses its Linux identity to open a file.
Now the page works in Asha's terminal. She reads the timetable and sees the right text. A visitor still gets 403 Forbidden. In a browser, 403 means the server refused the request. Web configuration, application authorization and filesystem permissions can all contribute to that result. If the server log shows a file open failed with Permission denied, we have a concrete place to start. Identify the account that tried to open it, the full path it used and the action it wanted to perform. Only then choose a permission change. Her own successful read proves something useful about her account and the file she selected. We need a second piece of evidence about the actual website worker.
Keep this problem in mind throughout the lesson. The tabletop exercise will give us one owner who can edit, one group that can read, and other accounts that have no permission on those objects. In the terminal practice, you will use only your current account and disposable text. You can directly observe how changing a single permission causes a particular operation to fail. The roles in the dairy story let you reason about a second identity without creating another user, modifying an existing group or touching a running site.
Read one permission line from left to right
Linux stores information about a file alongside its contents. This information includes the type of object, the user ID of its owner, the group ID and the permission bits. Running a long listing on the intended path lets you see those fields before you change anything. In the following example, the names, timestamp and size are made up. Treat the listing as evidence to interpret.
ls -ld -- published published/collection.txt
drwxr-x--- 2 asha site-readers 4096 Nov 22 08:00 published
-rw-r----- 1 asha site-readers 44 Nov 22 08:01 published/collection.txt
The first character says which type of object you are looking at. A d here means directory. A dash in that first position means an ordinary file. Read the next nine characters in three groups of three. The first group gives the owner permissions, the second is the group class and the last is other. In the simple case with no additional access control entries, the name in the owning group column tells us which membership the group class represents. The names asha and site-readers are the user and group owner. The numbers just before them are link counts. A link count is outside the information you use for this permission calculation.
For the timetable line, divide the nine permission characters into rw-, r-- and ---. Asha can read and write that file. A process that is a member of the owning group can read the file. Others have none of these mode permissions. The missing execute bit on a timetable makes sense because it is text to display. For the directory, read rwx, r-x and ---. Asha can list and search the directory and can change its entries. The worker's group can list and search the directory. Others have no access through that directory in the ordinary case. We will check what search means next.
The -l option requests the long format. The -d option makes a directory argument display as a single object. Without it, a command such as ls -l published normally displays the objects inside that directory. This difference matters. A student may see the timetable's readable mode while overlooking the mode of its parent. Both are involved in a file open. If the called operation is changing the permissions of the directory, use the listing that describes the directory itself. Keep the path in the command and output, especially when two releases contain the same filename.
The user and group concepts describe an object's ownership and the identity attempting access. The owner can also be a member of the file's group. For the ordinary three-class check, a process matching the owner uses the owner class. If the owner lacks a particular bit, extra bits available to the group or to other do not make up for it. Otherwise a process with a matching group uses the group class, and a process in neither category uses the other class. ACLs extend that algorithm. A dependable first question is always which identity made this request, before reading bits on its behalf.
Separate reading the paper from changing the folder entries
When we call read, write and execute permissions by the letters r, w and x, the letters are the same for every object type. The operations they allow depend on the type. For an ordinary file, read permission allows opening the contents for reading. Write permission allows writing the contents. Execute permission is part of the check when you ask the system to run that file directly. The file has to be in a format the system knows how to run, such as a supported executable or a script with an appropriate interpreter line. A text document does its job with read permission. It gains no useful website feature from the execute bit.
In a directory, the items you see are names that point to objects. A directory read lets you enumerate the names. Directory search, represented by the execute bit, lets the kernel look up a particular name in that directory. You need search on the directories you walk through, whether you use cd or pass an absolute path to cat. A directory can allow listing of names while blocking access to the objects those names refer to. A long listing may display a name but fail to get other information underneath it. A directory can also allow search while blocking an ordinary listing. A known filename inside it may be openable if all other checks succeed.
Directory write is involved when you add, remove or rename entries. To create a file in the directory, you ordinarily need both write and search permission on the containing directory. To remove a name, you ordinarily need write and search on its parent. The remove operation changes the parent directory's list of entries. It does not require rewriting the contents of the file. A read-only timetable can therefore be removed by somebody allowed to change the entries of its parent, subject to additional rules. Similarly, an owned, writable timetable may be edited in place but resist renaming if the parent lacks write permission for that account.
Suppose Asha lowers the file to owner-read-only before publishing. Her website worker can still be granted group read. If she leaves the containing directory writable by the worker, the worker may be able to rename or remove the public name anyway. A small plan that protects the file contents while leaving the publication directory open for unrelated writers misses a required control. Look at this example of a same-directory rename. Before it, the directory contains collection.txt with known text. After a successful rename to collection.previous.txt, that name points to the same file. Its contents and ordinary read mode remain as before. What changed is the directory entry used to find it.
For a rename between two directories on the same filesystem, check the paths and the parent directories on both sides. A move across filesystems can require copying and then removing the original. That means reading the original and creating the destination can become part of the operation. Additional rules also matter for special directories. For example, a sticky directory generally limits removal and rename of entries by otherwise permitted users to the appropriate file owner, directory owner or privileged process. Linux commonly uses this for a shared temporary directory. Treat the normal parent rule as a starting point and then look for the additional controls that apply.
Build a numeric mode one digit at a time
Three values are enough for ordinary read, write and execute calculations. Read is four. Write is two. Execute is one. Each digit is the sum of the bits for a single class. Start with three small questions for each class. Does it need to see the contents? Does it need to modify them? Does it need to run this file or search this directory? Add only the values that correspond to your answers. Zero means none of the three. The positions are owner, group and other, in that order. The digits are base eight, often called octal. Each position supports zero through seven.
On the timetable, Asha needs read and write, so the owner digit is four plus two, which gives six. The serving group needs only read, so its digit is four. The other class gets zero. Now we can write 640 and decode it back to rw-r-----. On published, Asha needs all three directory permissions, giving seven. The worker group needs read and search, giving four plus one or five. Others get zero. The directory is 750, which reads as rwxr-x---. The letter and number forms tell the same story about each class.
| Item | Owner | Owning group | Other | Mode |
|---|---|---|---|---|
| Timetable text | Read and write | Read | None | 640 |
| Published directory | List, search and change names | List and search | None | 750 |
| Private shift note | Read and write | None | None | 600 |
| Private shift directory | List, search and change names | None | None | 700 |
Try to reason about 644 without memorizing a chart. It gives six to the owner and four each to the group and other class. This is reasonable for some local files intended to be readable by all relevant accounts. A public website does not by itself require that all local accounts be able to read the file. If the chosen serving group can open it under 640, that meets the immediate goal with more limited local access. If you apply 644 to the containing directory, nobody in the ordinary classes gets a search bit. Seeing some filenames from the directory is then very different from successfully opening them.
The first three ordinary digits answer our current question. You may sometimes see a leading digit for special permissions. For example, the sticky bit we discussed is part of that special place. In symbolic listings, special bits overlap execute positions and can appear as letters such as t or s. If the file or directory has a special bit, record it and learn who relies on it before issuing a mode change. The basic table is a tool for your ordinary file and ordinary directory. Real application trees sometimes contain files with intentionally different modes.
Change a selected permission or set a known ordinary mode
Use chmod when the operation you need is a change of mode. You can specify the desired ordinary permissions numerically for each of the three classes. You can also express a targeted change in symbols. Start with the class letter, follow it with the operator and finish with the bits. The letters u, g and o choose the owner, group class or other. a chooses all three. A plus adds a permission to the chosen class. A minus removes it. An equals sign assigns the selected class's permissions according to the requested bits. Explicitly name the classes you mean, especially when comparing changes across shells with different masks.
chmod 640 ./published/collection.txt
chmod g-r ./published/collection.txt
ls -l -- ./published/collection.txt
chmod g+r ./published/collection.txt
ls -l -- ./published/collection.txt
In this sequence the first command starts from a known mode. Removing group read changes the timetable from rw-r----- to rw-------. Adding it changes that one bit back. The owner remains Asha in the imagined production listing and remains you in your personal practice copy. The group name also remains what it was. If it is the wrong group for the planned service, changing its group read bit does not give the intended worker access. In the ordinary case a group's identity is what makes that permission relevant to the process.
We use chown to change the owning user, with a group component available as well. There is also chgrp to request a group ownership change. These have their own authorization requirements. In a managed service, ask the owner of the deployment or filesystem who should be able to publish and which service account should read. An application that can rewrite everything it serves has more power than a read-only web worker needs. Assigning every file to the web worker can therefore change a deliberately designed boundary. Reading the owner and group columns before deciding whether either needs to change will help you keep the two roles separate.
Suppose a small training script currently has 640 and you want to run it as its owner. chmod u+x adds one to the owner's digit, leaving you with 740. If you use chmod 755 instead, you set the ordinary bits in all three places. People in the other class now have read and execute bits too. Those two requests can both allow you to run the script, but the group and other experiences differ. Practise making the smallest change that satisfies the exact role and action you wrote down. Recheck the target with a long listing immediately after the edit.
Create a controlled permission lab
Use an ordinary user shell on your own Unix-like practice system. This exercise depends on the normal denial you get as a non-root process. No elevated command, network access, real key or second login is needed. The shell can be Bash on a Linux practice instance or a local terminal with equivalent commands. Start in a new Bash session if you want to keep it separate from your other work. Run the following setup and read its output before continuing. If it reports that you are root, or if any line reports an error, stop this lab and switch to a normal writable workspace.
if [ "$(id -u)" -eq 0 ]; then
printf '%s\n' 'Open a normal user shell for this lab.'
else
permissions_lab=$(mktemp -d "$HOME/permission-lesson.XXXXXXXX") &&
cd -- "$permissions_lab" &&
mkdir -- published private &&
printf '%s\n' 'Milk collection starts at 06:30.' > ./published/collection.txt &&
printf '%s\n' 'Practice note for the next shift.' > ./private/shift.txt &&
chmod 750 ./published &&
chmod 640 ./published/collection.txt &&
chmod 700 ./private &&
chmod 600 ./private/shift.txt &&
pwd && ls -ld -- ./published ./published/collection.txt ./private ./private/shift.txt
fi
The temporary directory has a randomly chosen suffix and is intended to be private to you. Check the path printed by pwd before every change block. The fictional group in the worked story was site-readers. In this fixture, the owner and group names will reflect the account and parent directory on your own host. The operations below run as the owner and demonstrate the resulting access for that identity. The exercise does not test a different user just because the middle characters show group permissions.
cat -- ./published/collection.txt
chmod g-r ./published/collection.txt
ls -l -- ./published/collection.txt
cat -- ./published/collection.txt
chmod g+r ./published/collection.txt
ls -l -- ./published/collection.txt
The first and second reads should both print the same collection time. We removed a bit from the group class. The current process owns the file in this lab, so its read still comes from the owner class. This is a common reason students believe a permission change had no effect. A test using the same identity may exercise a different class from the one they changed. The recorded listing changes from the expected group read form to one with no group bits and then back. That is the correct immediate evidence for our controlled owner test. Testing group behavior with a proper second identity takes a separate approved setup.
Block one directory search and explain the exact error
Predict what the shell will do when you remove the owner's search permission from published. The file underneath still has its owner read bit. You know its exact name. Can the kernel open it before it has passed through the directory? Write your prediction and run the next commands individually at the lab root.
chmod u-x ./published
ls -ld -- ./published
ls -1 -- ./published
cat -- ./published/collection.txt
chmod u+x ./published
cat -- ./published/collection.txt
The long listing describes the directory itself. The owner now has read and write without search. Because this is a simple lab and the owner has read on the directory, a plain listing of names should still show collection.txt. Some listing implementations report an additional lookup warning. A shell alias that asks for file details or type marks can also trigger extra lookups. If that happens, use command ls -1 -- ./published to run the command directly. The next attempt to open the known file should report permission denied. The name is in the directory. The process lacks the permission required to find the object it names through that directory. Some systems may show error wording different from the English phrase.
Put back the owner's search bit on the directory. The final read should display the same timetable without changing its contents or its own mode. The directory returns to the intended 750 mode. This gives you a clean before-and-after test. The failing and successful commands are the same file open, through the same path and as the same identity. The changed bit is the one on the parent. When a website works with a copy in one release directory but fails with an identical copy in another release, this type of experiment tells you where to look.
Optional follow-up in this same disposable directory. Try removing the owner's read bit while leaving search and write, then open the known filename. A simple attempt to list all entries should be refused while a read of the known timetable can succeed. Immediately restore read on the directory with chmod u+r ./published. Compare the ordinary read operation on the file with the list operation on the directory. You should be able to describe why issuing ls and receiving an error does not settle whether a process can open a particular known path.
Prove the parent directory controls renaming and removal
Our next test changes a directory entry. Start with pwd and ls -ld -- ./published to confirm the exact lab scope and that the directory is back to its starting permissions. Then remove write from the owner of the private directory while keeping read and search. The note inside it remains owner-writable. The first command below tries to write the existing file's contents directly. Shell redirection is the operation that opens the file for append. The following move instead asks the system to give the same file a new name. Predict the result of each action.
chmod u-w ./private
printf '%s\n' 'Shift check complete.' >> ./private/shift.txt
mv -- ./private/shift.txt ./private/shift-reviewed.txt
ls -ld -- ./private
cat -- ./private/shift.txt
chmod u+w ./private
The appended text should be present because search on the directory and write on the existing file are both allowed. The rename should fail over the missing directory write bit. The directory listing should show dr-x------ in the ordinary permission positions. Confirm the contents from the old path before restoring directory write. A file editor may save another way by writing a temporary copy and renaming it over the old one. Such an editor can fail at saving despite a simple append succeeding. Learning the operation a program actually performs will often explain a permissions report that seems inconsistent at first.
For the reverse test, make the timetable readable by its owner and group, with no file write at all. The owner of published still has permission to change its entries. Rename the timetable using two exact names in the same lab directory. You can see that the read-only mode travels with the timetable when you list the result. This is a reliable demonstration that directory write and file write serve different purposes.
chmod 440 ./published/collection.txt
ls -l -- ./published/collection.txt
mv -- ./published/collection.txt ./published/collection-retired.txt
ls -l -- ./published/collection-retired.txt
cat -- ./published/collection-retired.txt
rm -i -- ./published/collection-retired.txt
ls -1 -- ./published
The renamed path should show -r--r----- and the original text. Answer yes to the interactive removal prompt only if it names the lab path you just checked. An empty directory listing afterwards proves that this name was removed without granting write on the file. If you decline, the name will remain and the later directory cleanup will stop. GNU tools can ask for confirmation around write-protected files, even when the kernel permits the parent directory operation. That question belongs to the command's user interface. The directory permissions still govern whether the underlying removal can happen.
Predict what a newly created file starts with
You might create a new private note and discover a read bit already available to another class. You might create another file on a different machine and get a different starting mode. The program that creates an object requests a mode. The session's file creation mask, usually called the umask, removes permission bits from that request in the ordinary case. A common regular text creation request is 666. A directory creation request is often 777. The file request already lacks execute bits. The mask can only take away from that request, so the example does not suddenly make a text file executable.
Consider the mask 027. The zero keeps all requested owner bits, the two turns off group write, and the seven turns off all requested other access. For a file requested as 666, the usual result is 640. For a directory requested as 777, the result is 750. Notice that you remove the named bits rather than subtracting a number as ordinary decimal arithmetic. For the directory, removing group write still leaves the requested group search bit. For the regular file, its original request had no search or execute bit to keep. With 077, other and group request bits are both removed. The corresponding examples become file 600 and directory 700.
Verify the difference with two short subshells inside the lab. A subshell lets you change settings for commands inside its parentheses and leave the current shell's mask as it was. Check these paths are absent before running the block. Creating over an existing file with redirection can preserve its existing mode, which would make it the wrong test for an initial mode. A filesystem with an inherited default ACL can also produce different modes. Keep the demonstration anchored to the evidence the local listing shows.
umask
(
umask 027
printf '%s\n' 'Group read sample' > ./mask-group.txt &&
mkdir -- ./mask-group
)
(
umask 077
printf '%s\n' 'Owner only sample' > ./mask-owner.txt &&
mkdir -- ./mask-owner
)
ls -ld -- ./mask-group.txt ./mask-group ./mask-owner.txt ./mask-owner
umask
The final mask printed should match the first. The listings should normally show -rw-r----- and drwxr-x--- for the group sample. They should show -rw------- and drwx------ for the owner sample. A mode change after creation uses chmod. A mask tunes the starting permissions for new objects created by commands in that environment. If you have to explain why a colleague's newly copied text is unexpectedly private, compare how each copy was created, what mode the program requested, and what the process mask and directory inheritance were.
Return the practice directory to an empty state
Before clearing the lab, print the working directory. Check it has the unique permission-lesson name from setup and matches the variable you kept in the same shell. If you chose the optional search experiment, make sure the permission on the lab's publication directory was restored. The only text left there should be the retired file if you declined the prompt. List the named directories first. The cleanup below mentions the exact exercise files and uses rmdir for empty directories. An unexpected extra item causes the directory removal to stop, leaving it available for inspection.
pwd
printf '%s\n' "$permissions_lab"
ls -la -- ./published ./private
if [ -n "$permissions_lab" ] && [ "$(pwd -P)" = "$(cd -- "$permissions_lab" && pwd -P)" ]; then
rm -i -- ./private/shift.txt ./mask-group.txt ./mask-owner.txt &&
rmdir -- ./published ./private ./mask-group ./mask-owner &&
cd -- "$HOME" &&
rmdir -- "$permissions_lab"
fi
Answer the prompts only for the exact samples you recognise. The final removal runs after you move out of the lab, so your shell has a valid current directory afterwards. If a directory is not empty, look at the name reported and compare it with that directory's contents. Typical causes are a declined deletion or a path created during the optional experiment. You can choose to keep that artifact and its containing directory. If you choose to clear it, remove only the sample you identify and repeat the relevant empty directory removals. You can then unset the variable using unset permissions_lab when your work is done.
Follow the failing path before you repair a web page
Return to the dairy site. The support request says the timetable cannot be read by the web worker. Start with the reported route and server identity. Compare the requested hostname and path with the corresponding virtual host configuration and the error log line. A read refusal on a particular path is a stronger clue than the browser code by itself. If the log names yesterday's release while you inspect today's release, correct your investigation before touching modes. If the HTTP access rule explicitly rejects the route, the application configuration is where the inquiry belongs. Once an operating system denial names a path, follow it from the outside in.
List each directory component using ls -ld, then list the target file. On Linux systems with the util-linux tool installed, namei -l can expand a path component by component, including symlinks. A symlink is a name referring to another path. A public tree may follow a symlink to a release directory or a new storage mount. The worker must be able to traverse the resulting path in its own environment. The identity you see in your shell is available through id. The worker's real identity and groups can be determined from the configured service and the process it launched. Before a group change, confirm you are checking the running process and remember that already-running processes may retain earlier supplementary group memberships.
Compare the required operations against the modes. A worker reading a known file needs search through the directory chain and read access to the file. It may need directory read as part of other behavior, such as looking at a directory listing. A publisher that installs a new file also needs appropriate directory write and search. The planned group on the example worker gets read and search on the publication directory and read on the timetable. If a parent path belongs to Asha's private home directory and gives only her search permission, a correctly readable timetable deeper in the tree will still be out of reach. Deploy the approved file under the site's intended publication tree, with its appropriate permissions, rather than opening the private directory to unrelated readers.
Check for access rules beyond the ordinary bits when they matter. An ACL can grant access to named users or groups. Its mask can limit some of those entries. Linux listings can mark additional access control information after the permission characters, commonly with a plus sign for an ACL. On a supported filesystem with the relevant tools, getfacl helps show the entries and their effective rights. SELinux can impose an additional decision based on process and file labels. A mount can disallow direct execution or be read-only. Some filesystem attributes can restrict changes. If the normal mode and identity checks predict one result and the process sees another, that disagreement is the reason to inspect the additional policy. Record the symptom and the first mismatch, then follow the team's approved access path for further checks.
After a justified change in the publication tree, repeat the specific read or request that failed. Keep the hostname, route, worker identity and resolved filename attached to your evidence. Verify that the test note outside the public tree remains outside the response. This teaches a useful habit even when the right change is only a single group read bit. Confirm the desired access and the boundary around it. Local SSH private-key protection follows the same principle at a different location. An EC2 private key stays on the client computer and should be readable only by the owner. AWS shows chmod 400 for the client key file on macOS or Linux. Check the exact client path and its owner before applying the change. Editing permissions on a different server cannot repair an overly accessible client key.
Interview questions with worked reasoning
A file shows mode 640. Can a group member definitely read it?
The group class in that mode has read permission. I would next check whether the process's active groups match the file's owning group, or whether the process matches the owner and uses that class instead. It needs search access through every directory in the resolved path. Additional policy can also apply. For the simple dairy example, the correct group, a traversable parent chain and a matching regular file establish why the expected read succeeds. The numeric mode by itself leaves those pieces of evidence open.
Can you rename a read-only file?
Yes, when the access checks for changing the directory entries allow the rename. For our same-directory example, the owner could rename the timetable after removing its file write bit because the parent allowed write and search. The renamed entry still led to read-only contents. A sticky directory or another security control may restrict the change. If the move crosses filesystems, the copy and removal steps can introduce additional access checks.
Someone can edit a note with append redirection but their editor refuses to save. What would you inspect?
I would identify the save operation. A direct append modifies the existing file. An editor that writes a replacement in the same folder and then renames it needs access to create and rename directory entries. I would check both the existing note and its parent, along with the precise error and exact path. Our lab reproduces the distinction with an owner-writable note in an owner-readable and searchable directory whose write bit was temporarily removed.
What changes when you run chmod u+x on an ordinary text script?
It adds owner execute to the ordinary mode. For a starting mode of 640, the result is 740. The owner and group assignments remain the same. Direct execution also requires a supported program format or script interpreter, and it can be limited by other policy. Before running a script, read the code you intend to run and confirm the interpreter it expects. For a normal file containing data, owner execute is unnecessary.
Why is a 403 response a poor reason to change every file mode?
It identifies a declined web request and leaves the failing layer open. The denial might be an application decision. If a log instead records a filesystem open error, I would reproduce the exact path and identify the worker, check search access on parents and read on the target, then check other relevant access policy. A correct repair has a specific reason and a specific retest. Changing an entire tree can grant unintended permissions and make a subsequent diagnosis harder by removing evidence of the original issue.
Check your understanding
1. The publisher may list a folder, but cannot open a known readable file inside it. Which missing directory permission could cause this?
A. Write
B. Search
C. File group write
Answer and reasoning
B. Directory search is represented by execute. Listing names uses directory read. The directory lookup must also succeed before Linux can check the file read permission.
2. The owner should have read and write. The owning group needs read. Others get no access. Which ordinary mode fits a regular note?
A. 750
B. 604
C. 640
Answer and reasoning
C. The required digits are six for owner read and write, four for group read and zero for other. Mode 604 would instead make the file readable by the other class.
3. A note shows mode 400. Which observation best explains why a same-directory rename succeeded?
A. The process has write and search on the parent, with no additional barrier.
B. Read permission on the note automatically allows directories to change.
C. The file became world-writable during rename.
Answer and reasoning
A. The operation changes the directory entry. Opening and writing the contents is a different operation. Look at both listings if you need to keep the distinction visible.
4. A program requests mode 666 for a new regular file under mask 027. There is no inherited ACL. What is the expected result?
A. 640
B. 750
C. 027
Answer and reasoning
A. The mask removes group write and removes the offered other bits. No execute bits were requested, so none appear in the result.
5. You grant group read to a timetable. Your own successful read proves which fact if you own the file?
A. Every process can read it.
B. The worker's group is definitely correct.
C. Your owner access and path worked for that read.
Answer and reasoning
C. Use the listing to verify the group bit change, and check the worker's identity or appropriately retest the worker operation for group access.
Revision cards
Read the object type first
The first character of a long listing distinguishes a directory, ordinary file or another type. Read the next nine positions in owner, group and other sets of three. Include the owning identity and resolved path in your explanation.
Directories have their own actions
Directory read lists names. Directory search looks up a known name. Directory write participates in adding or removing names. Ordinary file bits control access to its contents and direct execution.
Two different changes
Use a targeted symbolic mode when you need to add or remove one bit. Use a known numeric mode when you intend to define the ordinary permissions for the three classes. Review ownership separately.
The file's parent can explain a move
To remove or rename an entry, investigate the permissions on the affected directory or directories. A read-only contents mode alone cannot lock the name in place.
A mask applies to creation
Start from the program's requested mode and remove masked bits. Check the result on a newly created path and consider directory inheritance. A mode on an existing file is separate.
Match the test identity
Record who ran the failing operation, the specific path and the action it tried. Confirm a repair using the same relevant identity and operation. A read from your shell exercises your own user and groups.
Official references
- GNU Coreutils manual for file permissions, ls, chmod, chown, rm and mv
- Linux inode metadata and special mode bits
- Linux pathname resolution and directory search
- Linux unlink behavior and directory permission checks
- Linux rename behavior and permission checks
- Linux new object creation masks and ACL inheritance
- Linux ACL entries and access checks
- SELinux policy modes and file labeling
- Amazon EC2 connection prerequisites and client key permissions