Linux E212Medium

Vim 'Permission denied' Error on Linux: Causes and 4 Ways to Fix

The article explains why the 'Permission denied' (E212) error appears in Vim on Linux and provides four proven solutions: using sudo, changing file permissions, changing the owner, or choosing a different editor. Also includes a section on preventive measures.

Updated at February 16, 2026
5-10 minutes
Easy
FixPedia Team
Применимо к:Ubuntu 20.04+Debian 11+CentOS 8+Arch LinuxVim 8.0+

What the code Error Means

Error E212 in Vim means Can't open file for writing. It occurs when you try to save changes to a file (:w or :wq), but the Linux operating system denies write access.

Typical scenario: You are editing a system configuration file (e.g., /etc/hosts, /etc/nginx/nginx.conf) or a file created by another user/process. The message at the bottom of Vim will look like this:

E212: Can't open file for writing

Or, if you are trying to save under a different name in a protected directory:

"/path/to/file" E212: Can't open file for writing

Causes

The error is not related to Vim itself—it is standard Linux system behavior ensuring security. Specific causes:

  1. The file is owned by another user. You are editing a file you do not own (e.g., root). Check with: ls -l /etc/hosts (owner is root).
  2. You lack write permission (w) for the file. Even if you are the owner, the write bit may be removed. Check: in the ls -l output, the owner's permission field lacks a w (e.g., -r--r--r--).
  3. You are trying to save the file in a directory where you lack write permission. For example, you are editing a new file /root/newfile.conf. The problem may be the parent folder, not the file.
  4. The file system is mounted in "read-only" mode. This happens for protected partitions or during disk errors. Check with: mount | grep " / ".
  5. File attributes (chattr) are set as "immutable" (i) or "append-only" (a). Check with: lsattr /etc/hosts (if there is an i or a in the first column).

Solutions

Solution 1: Use sudo to launch Vim (simple, but requires caution)

This is the fastest method if you need to edit a system file just this once.

  1. Exit your current Vim session without saving changes: press Esc, then :q! and Enter.
  2. Launch Vim as the superuser:
    sudo vim /path/to/file
    
    For example: sudo vim /etc/fstab.
  3. Make the necessary changes and save the file normally: Esc, :wq, Enter.

⚠️ Important: Do not leave a terminal with sudo vim running unattended. Avoid using sudo to edit files in your home directories (/home/user/) unless absolutely necessary—it can disrupt ownership permissions.

Solution 2: Change file permissions (chmod)

If you own the file but write permission is removed, restore it.

  1. Exit Vim (:q!).
  2. Grant yourself write permission. The safest option is to add write for the owner (you) only:
    chmod u+w /path/to/file
    
    For example: chmod u+w ~/.config/app/config.ini.
  3. Alternatively, you can set standard permissions 644 (owner: rw-, group: r--, others: r--):
    chmod 644 /path/to/file
    
  4. Reopen the file in Vim (without sudo) and save your changes.

Solution 3: Change file ownership (chown)

If the file is owned by another user (e.g., the www-data web server user) and you need to edit it regularly, change the owner to yourself or your group.

  1. Exit Vim (:q!).
  2. Change the owner. To become the file's owner, run:
    sudo chown $USER:$USER /path/to/file
    
    $USER is an environment variable containing your username. This makes you the owner and sets your default group. If you only need to change the group, leaving the owner: sudo chgrp <group_name> /path/to/file.
  3. You can now edit and save the file in Vim without sudo.

Solution 4: Use a different editor with built-in sudo (for convenience)

Some text editors (e.g., nano or micro) have a built-in ability to save via sudo when permissions are insufficient. However, Vim does not have this feature. If you want to stay in Vim, this solution does not apply. As an alternative:

  • Use sudoedit. This is a secure utility that creates a temporary copy of the file with root privileges, edits it with your default editor (can be configured to use Vim), and then saves it:
    sudoedit /path/to/file
    
    It is preferable to sudo vim because it does not run the editor itself as root.

Prevention

To avoid E212 in the future:

  1. Determine who should own the file. System configs (/etc/) are usually owned by root. Files in /home/ belong to your user. Web server files belong to the server user (www-data, nginx). Edit files knowing their intended ownership.
  2. Apply default permissions. For files in your home directories, permissions are usually 644 (umask 022). If you create a file that needs to be executable, use chmod +x.
  3. Use sudoedit instead of sudo vim. This is a safer method for editing others' files, minimizing risks.
  4. Do not remove protection from system files unnecessarily. If you granted yourself write permission (chmod u+w) on a system file, revert it after editing: chmod u-w /path/to/file. This prevents accidental modifications by other programs.

Frequently Asked Questions (FAQ)

Can I disable permission checks in Vim?

No. This is a fundamental security function of the Linux kernel, not a Vim setting. Any process, including an editor, must operate within the permissions of the user who launched it.

What if chmod or chown also return "Permission denied"?

This means you are neither the owner of the file nor in the sudoers group (you lack administrative privileges). You first need to obtain superuser privileges (log in as the root account or use sudo for the chmod/chown command itself):

sudo chmod 644 /path/to/file

Why did the file become owned by root after using sudo vim and saving?

Because you saved it as the root user. You will now be unable to edit that file without sudo. This is a common mistake. Solution: revert the owner to yourself: sudo chown $USER:$USER /path/to/file.

What if it's a directory, not a file, that is protected?

The error will be similar. You need write permission on the directory (x to access its contents and w to create/delete files). Fix permissions on the directory: sudo chmod u+w /path/to/directory or change its owner.

F.A.Q.

Can I just always run Vim with sudo to avoid the error?
Why do I get an error if I created the file myself under my account?
What is `E212` in the Vim error message?
Can I change file permissions from within Vim?

Hints

Identify the exact cause of the error
Use sudo to write (quick fix)
Change file permissions (recommended)
Change the file owner (if the file isn't yours)

Did this article help you solve the problem?

FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community