Skip to main content

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
KeysDoes
Ctrl+O, EnterSave
Ctrl+XExit (asks to save: Y then Enter)
Ctrl+WSearch
Ctrl+K / Ctrl+UCut 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+UUndo

[ 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
LetterOn a fileOn a folder
rread itlist what is inside
wchange itcreate/delete files inside
xrun it as a programenter it (needed to reach anything inside)

The numbers​

Each digit is r=4 + w=2 + x=1, for owner, group, others:

NumberMeansTypical for
644rw-r--r--Normal files, web pages
755rwxr-xr-xFolders, scripts
600rw-------Private files (SSH keys)
775rwxrwxr-xShared folders (owner + group can write)
777everyone can do everythingNever. 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:

  • x on every folder on the way (/var, /var/www, /var/www/html, project), and
  • r on 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​

ErrorMeaning and fix
Permission deniedNot allowed. ls -l to see the owner and permissions; use sudo for system files, or fix ownership.
Operation not permittedEven root may not do that here (e.g. on the FAT boot partition, chown does nothing).
Read-only file systemThe card is damaged and was locked: repair.
No such file or directoryWrong name, case or folder. pwd and ls.
Is a directoryYou gave a folder where a file was expected.
bash: ./script.sh: Permission deniedNot executable: chmod +x script.sh (or run it with bash script.sh).
Directory not emptyUse rm -r (carefully).