Error opening file: Permission denied – backup creation failure in nano
0 reputation · 14 Feb 2023, 17:46 UTC
0 reputation · 14 Feb 2023, 17:46 UTC
When nano is invoked with the -B or --backup flag on a file that it cannot write to, the editor reports the message Error opening file: Permission denied and terminates the editing session. The manual documents the backup option but does not clarify what should happen when the backup write fails.
The aborting behavior leaves users uncertain whether nano should instead continue editing without creating a backup, or whether it should provide a configurable fallback. This design choice is not explicitly defined in the documentation.
35275 reputation · 14 Feb 2023, 20:59 UTC
When nano is started with -B (or --backup) and it cannot create the backup file, it exits with Error opening file: Permission denied. Nano’s design is to avoid writing a file that has no backup, which could leave the file in an inconsistent state.
file~ is created in the same directory as the target file.open(2) call fails with EACCES, and nano aborts.ls -l /path/to/file and ls -ld $(dirname /path/to/file) to check ownership and write permission on the directory.sudo nano /path/to/file (be careful to restore original ownership after editing).-b or disable backups in ~/.nanorc with set backup 0.ls -l and that a backup file file~ exists if backups were enabled.The --backup description in man nano states that a backup file named file~ is created in the same directory. It does not mention the abort, but the source code shows the abort is intentional to protect against writing a file without a backup.
Is the file located in a system directory such as /etc or a user‑writable location? Knowing the directory context helps decide whether sudo is appropriate or a permission tweak suffices.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 15 Feb 2023, 01:00 UTC
If you want to keep nano’s automatic ~ backups but lack write permission in the file’s directory, you can redirect the backup location to a writable path via ~/.nanorc. Adding the line
set backupdir ~/.nano-backupstells nano to create
file~(orfile.~if you also setset backupsuffix) inside~/.nano-backupsinstead of alongside the edited file. As long as that directory is writable by your user, the backup succeeds and nano will not abort withError opening file: Permission denied. This preserves the safety net of a backup while sidestepping directory‑permission issues.