Curious Minds

Clear answers to science and history questions

Edit Root Protected Files Over SFTP with FileZilla

· 5 min read · Science

Editing system files that are owned by root (like /etc/ssh/sshd_config or /etc/nginx/sites-available/default) over SFTP usually fails with a "Permission denied" error. This guide shows you how to temporarily grant your SFTP user write access to a specific file, edit it in FileZilla, and then restore the original permissions. You'll need root access to the server (via SSH) and a basic understanding of Linux file permissions.

What You'll Need

  • Root or sudo access to the server via SSH (terminal).
  • FileZilla Client installed on your local machine.
  • The SFTP login credentials (host, username, password or key).
  • The full path to the file you need to edit.

Step-by-Step

  1. Connect to your server via SSH using a terminal (Linux/macOS) or PuTTY (Windows). Log in as root or with a user that has sudo privileges.
  2. Locate the file's absolute path. For example, if you need to edit sshd_config, the path is usually /etc/ssh/sshd_config. Use ls -l /etc/ssh/sshd_config to confirm it exists and check its current permissions.
  3. Change the file's owner to your SFTP user. If your SFTP username is john, run: sudo chown john /etc/ssh/sshd_config. This changes the owner from root to john, allowing john to read and write the file (if the file's permissions allow).
  4. Ensure the file has write permission for the owner. Run sudo chmod 644 /etc/ssh/sshd_config. This gives the owner read/write (6), group read (4), and others read (4). If the file already has 644, you can skip this step.
  5. Open FileZilla and connect via SFTP. Enter your server's hostname, your SFTP username (john), and your password or key. Use port 22 (default for SFTP). Click Quickconnect.
  6. Navigate to the file's directory. In the remote pane (right side), go to /etc/ssh/ or wherever the file is located.
  7. Download the file to your local machine. Right-click the file and select "Download". This copies it to your local computer.
  8. Edit the file with a text editor. Open the downloaded file in Notepad++, VS Code, or any plain text editor. Make your changes and save.
  9. Upload the edited file back. In FileZilla, navigate to the same remote directory and drag the edited local file into the remote pane. Confirm that you want to overwrite the existing file.
  10. Restore the original owner and permissions. Back in your SSH terminal, run: sudo chown root /etc/ssh/sshd_config and sudo chmod 644 /etc/ssh/sshd_config. This returns the file to its original state.
  11. Verify the file's integrity. Run sudo sshd -t (for sshd_config) or the appropriate syntax check for the service you edited. If it passes, restart the service: sudo systemctl restart sshd (or nginx, etc.).

Common Mistakes

  • Forgetting to restore permissions. Leaving a system file owned by a non-root user can be a security risk and may cause service failures.
  • Editing the file while it's still open in the editor. Always close the file in your text editor before uploading the new version to avoid conflicts.
  • Using the wrong file path. Double-check the path; editing a different file with the same name can cause confusion.
  • Not backing up the original file. Before making changes, copy the file to a backup: sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.
  • Changing permissions on the directory instead of the file. This can lock you out of the directory entirely.

Tips

  • Use sudo only when necessary. For most config files, you can edit them directly with sudo via SSH, but if you prefer a GUI, this method works.
  • Test the configuration before restarting the service. Many services have a syntax check command (e.g., nginx -t). Use it to catch errors.
  • Keep a local backup. Store a copy of the edited file on your local machine for future reference.
  • Use FileZilla's built-in editor. You can set FileZilla to open files with your preferred editor directly (Edit > Settings > File editing). This saves a step, but be careful: FileZilla uploads the file automatically when you save, which can be risky if you make a mistake.
  • Consider using a version control system. If you edit many config files, tracking changes with Git can help you revert mistakes.

Troubleshooting

  • "Permission denied" when uploading. This means the file is still owned by root or the directory doesn't allow write. Re-check the owner and permissions with ls -l.
  • FileZilla says "File is locked" or "in use". Ensure no other process is using the file. Close any editors that have the file open.
  • After editing, the service fails to start. Check the syntax with the service's test command. If it fails, restore the backup and try again.
  • You can't connect via SFTP at all. Verify that the SSH service is running and that your SFTP user has a valid shell (e.g., /bin/bash). Some servers restrict SFTP to certain users.
  • The file appears read-only in FileZilla even after chown. Check the file's permissions again. If the file has 444 (read-only for all), you need to add write permission with chmod 644.

Frequently Asked Questions

Is this method safe for all system files?

It's generally safe if you restore the original owner and permissions immediately after editing. However, for critical files like /etc/passwd or /etc/shadow, it's better to use the command line to avoid accidental corruption.

Can I edit multiple files at once?

Yes, you can change the owner and permissions for multiple files in a directory, but be cautious: changing ownership of a directory recursively can cause issues. It's safer to edit files one by one.

What if I don't have root access via SSH?

You cannot use this method without root or sudo. You'll need to ask your hosting provider or system administrator to edit the file for you.

Why does FileZilla show "Permission denied" when I try to open the file?

Because the file is owned by root and your SFTP user doesn't have read/write permissions. Changing the owner to your user will allow you to open and edit it.

Can I use this method with a Windows server?

No, this is for Linux/Unix servers. On Windows, you'd need to adjust NTFS permissions, which is a different process.

What if I forget to restore the owner?

It's not the end of the world, but it's a security risk. Any process running as your SFTP user could modify the file. Restore it as soon as you remember.

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions