CH-J Toolsserver management over SSH
Menu
All guides and articles →

How to edit files in /etc over SFTP and sudo

Why SFTP gets permission denied in /etc, and safe ways to change a configuration file without opening up root access.

Why can you read a file in /etc but not save it?

You connect to server.example as user, open /etc/nginx/nginx.conf over SFTP, change one directive and receive permission denied when saving. The connection and download worked; replacing the protected configuration did not. This is normally an account permission boundary, rather than a broken transfer or an editor problem.

Most system configuration is owned by root. Many files allow ordinary users to read them, while files containing credentials may allow neither reading nor writing. A successful SSH login does not grant administrative file access. An account allowed to use sudo still needs to invoke it through an approved operation.

The examples below target Linux servers using GNU utilities, with NGINX installed at /etc/nginx and, where shown, systemd. They apply to typical Ubuntu, Debian and RHEL installations; inspect your actual paths and policy first. A Windows desktop can be your SFTP client, but these commands run on the Linux server.

Why does sudo in SSH not elevate SFTP?

An SFTP session operates with the permissions of its authenticated account. Standard SFTP has no sudo request. Running sudo in a separate SSH terminal changes the privileges of that command, not the identity of an existing file transfer. Reconnecting SFTP with the same account does not change this.

  • Writing an existing file requires permission on that file. An editor that saves by creating a temporary file and renaming it also needs suitable directory permissions.
  • Directory traversal, access control lists (ACLs) and SELinux policy can affect access as well as the familiar owner, group and mode bits.
  • A read-only filesystem or an immutable attribute can block changes even when ordinary permissions look correct. Elevation alone does not resolve every denial.

Use the failure message to choose the next check. Permission denied during upload suggests access restrictions; a read-only filesystem message suggests a mount or storage problem. Do not remount a filesystem or remove protective attributes simply to make one edit succeed.

What should you check and back up first?

In the SSH terminal, confirm the account, allowed sudo operations and target metadata. These commands inspect the existing file without changing it. If sudo rejects an operation, ask the administrator for the specific access needed; permission to run one command does not imply permission to run every command below.

id
sudo -l
ls -ld /etc /etc/nginx
sudo ls -l /etc/nginx/nginx.conf
sudo stat -c '%U %G %a %h %F' /etc/nginx/nginx.conf

For example, stat might report the following. It means root ownership, group root, mode 644, one hard link and a regular file. These are example values, not requirements for every configuration file. A symbolic link or multiple hard links needs investigation before replacement.

root root 644 1 regular file

For a confirmed regular file, create a separate backup directory under /root, outside configuration include paths. Run these commands in the same terminal used for the remaining work, check that each succeeds, and record the printed directory. A new directory avoids overwriting an older backup.

backup_dir=$(sudo mktemp -d /root/nginx-edit.XXXXXXXX)
sudo cp -a -- /etc/nginx/nginx.conf "$backup_dir/nginx.conf"
printf 'Backup directory: %s\n' "$backup_dir"

Before changing SSH access or sudo policy, keep an authenticated session open and confirm a working console or other recovery route. An invalid configuration or an unintended access rule can lock you out even when the file was saved successfully.

How do you edit safely with sudoedit?

For an interactive terminal edit, sudoedit makes temporary copies, runs the editor as your ordinary account and copies modified content back after the editor exits. The editor itself does not run as root. Your sudo policy must authorize sudoedit for the target; ordinary sudo command permissions may differ.

  1. Confirm the backup from the previous section exists and coordinate with anyone else editing the same configuration.
  2. Open the intended file, make the smallest necessary change, save and exit the editor.
  3. Read any sudoedit warning before proceeding. If copying back failed, do not assume the live file contains your changes.
  4. Validate the installed configuration using the service-specific checks in the verification section.
sudoedit /etc/nginx/nginx.conf

The editor depends on your environment and sudo policy. Do not disable sudoedit safety checks to follow a symbolic link or edit through a user-writable directory. Confirm the intended real target with the administrator. For sudoers, use visudo instead because it provides checks designed for that policy.

How do you install a copy edited locally?

Choose this route instead of sudoedit when you prefer a local editor. Download an authorized copy, preserve Linux line endings and the original encoding, and avoid leaving credentials in shared folders or editor backups. Create a private staging directory on the server, then upload nginx.conf.new into the printed path using SFTP.

stage_dir=$(mktemp -d "$HOME/nginx-edit.XXXXXXXX")
printf 'Upload directory: %s\n' "$stage_dir"

Keep this terminal open so stage_dir and backup_dir remain available. After upload, inspect the difference and check the destination has not changed since you downloaded it. A diff exit status of 1 means differences were found, not a transfer failure. Diff output may expose secrets, so do not paste it into public tickets.

sudo diff -u -- /etc/nginx/nginx.conf "$stage_dir/nginx.conf.new"

Use the following install example only after confirming root:root and 644 are correct for this existing regular file. It is not a recipe for private keys or secret configuration. If getfacl is available, inspect ACLs before replacement; do not use this simple recipe for files with special ACLs or other required extended attributes without a metadata preservation plan.

The next command overwrites the destination. Confirm the backup succeeded and the reviewed upload is complete. The install command sets owner and mode explicitly; it does not promise to preserve the original ACLs or all extended attributes.

sudo install -o root -g root -m 644 -- "$stage_dir/nginx.conf.new" /etc/nginx/nginx.conf
sudo stat -c '%U %G %a' /etc/nginx/nginx.conf

On SELinux-enabled systems, commonly RHEL, inspect the label and preview policy-based relabeling. Run restorecon without -n only if the proposed context matches the intended policy. A deliberate custom label needs its established policy configuration, not an automatic reset. Skip these commands on systems without SELinux tools.

sudo ls -Z /etc/nginx/nginx.conf
sudo restorecon -n -v /etc/nginx/nginx.conf
sudo restorecon -v /etc/nginx/nginx.conf

How do you validate, apply and roll back the change?

Saving changes does not necessarily change the running service. For a standard systemd NGINX installation, test the configuration before reloading. The && operator prevents the reload if validation fails. If your service uses a custom configuration path, test that same configuration rather than the default.

sudo nginx -t && sudo systemctl reload nginx
systemctl is-active nginx
sudo journalctl -u nginx -n 30 --no-pager

Check the actual website and intended behavior as well as service status. A syntax check cannot tell whether you selected the correct upstream or accidentally changed access rules. If validation fails, leave the running service alone and correct the named file, including any referenced configuration fragment.

For SSH configuration, validate with sshd -t before applying changes. On Ubuntu and Debian the usual systemd unit is ssh; on RHEL it is sshd. After a successful check, reload the applicable unit and test a fresh login from another window. For sudo policy, edit with visudo and then check the complete policy, not just one fragment.

sudo /usr/sbin/sshd -t
sudo visudo -f /etc/sudoers.d/example
sudo visudo -c

These SSH and sudoers commands are separate examples, not extra NGINX steps. If rollback is needed for the NGINX example, first inspect the recorded backup. Use the original terminal variables or substitute the exact recorded backup path. Restore only after confirming it is the intended original.

sudo stat -- "$backup_dir/nginx.conf"
sudo diff -u -- "$backup_dir/nginx.conf" /etc/nginx/nginx.conf

Restoring the backup replaces the current file and discards intervening changes. Coordinate with other administrators before proceeding, then repeat metadata checks and service validation.

sudo cp -a -- "$backup_dir/nginx.conf" /etc/nginx/nginx.conf
sudo nginx -t && sudo systemctl reload nginx

Which shortcuts create lasting problems?

  • Do not apply chmod 777 or recursively change permissions under /etc. Configuration writable by ordinary accounts can let them influence privileged services.
  • Do not change a system file owner to your SSH account to enable saving. Restore the intended ownership if an earlier workaround already did this.
  • Do not enable root password login or unrestricted passwordless sudo just to avoid one upload failure. Request a narrowly defined administrative workflow.
  • Do not leave backup files inside directories read through broad include patterns. An extra configuration copy can become active unexpectedly.
  • Do not assume sudo echo with shell redirection elevates the write. The calling shell handles redirection; use the reviewed editing or installation workflow instead.

Do not disable SELinux to hide a label error. Diagnose the path, expected context and relevant denial instead. Also avoid repeated restarts while testing: a currently running service may still be using a good configuration even though the file on disk is broken.

How do you make future edits predictable?

Record the target path, original metadata, backup location, intended change and validation result. Coordinate edits so a stale local download does not overwrite another administrator's work. Re-download or merge deliberately when the destination has changed; a successful upload does not resolve a conflicting edit.

Prefer supported configuration fragments where the service documents them, and check the effective include order. If automation manages the file, change its source of truth or your manual edit may disappear at the next deployment. For recurring work, document the approved command sequence and recovery procedure.

After successful verification, remove only the specific staged and local copies you no longer need, according to your retention policy. Treat configuration backups as potentially sensitive. Retain a recoverable original for the agreed period rather than collecting untracked copies across home directories.

How does CH-J Tools help with sudo editing?

File Manager offers to open a file with sudo when the SSH account cannot read it and warns that saving will require sudo. On save, data is transferred to the account's private temporary directory. After verification, sudo moves it into place while keeping the owner, permissions, ACL and SELinux context. An optional backup stores the original before replacement.

The password goes only to sudo on the server and is not stored on disk or in logs. If sudo policy requires a terminal through requiretty, the app does not bypass that requirement. The app does not validate configuration content, so you still need to run the service check yourself before applying the change.

Frequently asked questions

Can SFTP use sudo automatically?

Standard SFTP has no sudo operation. A client may implement a separate privileged editing workflow, but the server must allow the required sudo commands. Logging in as an ordinary account does not make its transfers privileged.

Why does sudoedit refuse a symbolic link?

Its safety rules normally reject symbolic link targets. Check the intended real file and obtain authorization for that path rather than disabling the protection.

Should every file in /etc be root-owned with mode 644?

No. Some configurations need another group or stricter access, especially those containing secrets. Inspect the original and service documentation before choosing installation metadata.

Does saving a configuration file restart the service?

Usually not, although some services watch files automatically. Follow the service documentation, validate first and use its supported reload or restart procedure. Successful saving alone proves neither syntax nor behavior.

Detailed documentation

man: sudo / sudoedit →

man: install →

man: visudo →

man: restorecon →

NGINX: command-line parameters →

NGINX: beginner's guide →

Biometric unlock and security →

All tutorials by category →