Files, permissions and the nano editor
nano in 30 seconds
nano myfile.txt # your own file
sudo nano /etc/apache2/apache2.conf # a system file needs sudo
| Keys | Does |
|---|---|
| Ctrl+O, Enter | Save |
| Ctrl+X | Exit (asks to save: Y then Enter) |
| Ctrl+W | Search |
| Ctrl+K / Ctrl+U | Cut line / paste |
| Alt+/ (or Ctrl+End) | Jump to the end of the file |
| Ctrl+_ | Go to a line number (useful when an error says "line 42") |
| Alt+U | Undo |
[ Error writing ...: Permission denied ] when saving = you opened a system file without sudo. Save a copy to your home folder (Ctrl+O, change the name to /home/pi/backup.conf), exit, and reopen with sudo nano.
Before editing a config file, keep a copy: sudo cp file file.bak.
Reading permissions
ls -l /var/www/html
-rw-r--r-- 1 root root 10701 Mar 26 14:55 index.html
drwxr-xr-x 2 www-data www-data 4096 Mar 26 14:55 uploads
- rw- r-- r-- root root
│ │ │ │ │ └── group owner
│ │ │ │ └── user owner
│ │ │ └── others (everyone else)
│ │ └── group
│ └── owner
└── type: - file, d directory, l link
| Letter | On a file | On a folder |
|---|---|---|
r | read it | list what is inside |
w | change it | create/delete files inside |
x | run it as a program | enter it (needed to reach anything inside) |
The numbers
Each digit is r=4 + w=2 + x=1, for owner, group, others:
| Number | Means | Typical for |
|---|---|---|
644 | rw-r--r-- | Normal files, web pages |
755 | rwxr-xr-x | Folders, scripts |
600 | rw------- | Private files (SSH keys) |
775 | rwxrwxr-x | Shared folders (owner + group can write) |
777 | everyone can do everything | Never. It "fixes" errors by removing all protection. |
chmod 644 page.php
chmod -R 755 /var/www/html/myfolder # -R = everything inside too
chmod +x script.sh # make runnable
Owners
sudo chown pi file.py # change the owner
sudo chown pi:www-data file.php # owner and group
sudo chown -R pi:pi /home/pi # everything in your home back to you
Why Apache says 403 Forbidden
Apache runs as www-data. To show /var/www/html/project/page.php, www-data needs:
xon every folder on the way (/var,/var/www,/var/www/html,project), andron the file.
Check the whole path at once:
namei -l /var/www/html/project/page.php
Letting pi edit the web folder
sudo chown -R pi:www-data /var/www/html
sudo chmod -R 775 /var/www/html
Now pi can save there (from Thonny too) and Apache can still read everything.
Files that suddenly belong to root
If you run nano, Thonny or Python with sudo on your own files, the files they create belong to root, and later normal saves fail with Permission denied. Spot it with ls -l (owner root in your home folder) and fix:
sudo chown -R pi:pi /home/pi
Copying and moving without surprises
cp -a source dest # copy keeping owners, permissions and dates (use for config and backups)
cp -r folder dest # copy a folder
mv old new # move or rename
rm -r folder # delete a folder: no recycle bin, no undo
Common errors
| Error | Meaning and fix |
|---|---|
Permission denied | Not allowed. ls -l to see the owner and permissions; use sudo for system files, or fix ownership. |
Operation not permitted | Even root may not do that here (e.g. on the FAT boot partition, chown does nothing). |
Read-only file system | The card is damaged and was locked: repair. |
No such file or directory | Wrong name, case or folder. pwd and ls. |
Is a directory | You gave a folder where a file was expected. |
bash: ./script.sh: Permission denied | Not executable: chmod +x script.sh (or run it with bash script.sh). |
Directory not empty | Use rm -r (carefully). |