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

A remote file changed while you were editing it: how to avoid overwriting it

How to detect that someone else changed a server file during your edit, compare versions and save without losing either change.

What happens when a remote file changes during your edit?

You download a server configuration, change a timeout and upload it again. Meanwhile, a colleague has changed the listening address in the server copy. Your upload succeeds, but their address change disappears. The server now has a valid file containing an outdated setting. This is a lost update, and a successful transfer does not tell you whether it happened.

The same problem can happen in a remote editor or an SSH terminal. Keeping an editor open does not reserve the file. When an editor reports that the file changed, stop saving and preserve your unsaved text separately. Reloading first can remove the only copy of your work.

The recovery uses three versions: the original you started from, your edited copy and the current server copy. Keep all three until you have checked the merged result. The examples below use a fictional /etc/example.conf on Linux; substitute the actual path only after confirming the server and file.

Why can the file change without another administrator editing it?

Another person is only one possible writer. Before treating the server copy as wrong, establish which system owns the configuration and whether your intended change belongs there at all.

  • Configuration management may regenerate the file from a template. A manual edit can disappear on the next deployment.
  • A package upgrade or installation script may update defaults, add options or install a replacement configuration.
  • The application may save its own settings. Editing its generated output can compete with normal operation.
  • A deployment may replace a symbolic link or rename a new file over the old path. The visible name can stay the same while the underlying file changes.

A modification time is useful context, but it is not a content comparison. Timestamps can be preserved during copying, and two versions can have the same size. A SHA-256 checksum helps compare bytes; ownership, permissions and link destinations require separate checks.

How do you record a reliable starting point?

Run these commands on the Linux server in one shell session. They use GNU utilities, as supplied on Ubuntu, Debian and RHEL, with sudo access to read the protected file. Create a private working directory and an untouched original before editing. Stop if any command fails; an empty or partial copy is not a usable baseline.

umask 077
work=$(mktemp -d)
sudo cat /etc/example.conf > "$work/example.conf.orig"
sha256sum "$work/example.conf.orig"
sudo sha256sum /etc/example.conf
sudo stat -c '%F %N' /etc/example.conf
sudo stat -Lc '%y %U %G %a %s' /etc/example.conf
cp "$work/example.conf.orig" "$work/example.conf.mine"

Compare the two digest values, ignoring their different filenames, and record the metadata output privately. The first stat describes the path, including a symbolic link; the second follows a link to inspect its target. If digests differ, establish a quiet interval and capture a fresh baseline. Even matching values do not stop a later write.

Configuration copies can contain passwords or private keys. Keep originals, edits and diffs in a restricted location. Do not upload them to public comparison services or place backup copies in a directory served by a web server.

If you edit from Windows, keep separate original and working downloads in a restricted local folder. PowerShell can hash either local file using the same algorithm. Compare the original download with the server baseline before editing; hash your edited download separately when verifying the transfer.

Get-FileHash -LiteralPath 'C:\Users\user\Documents\example.conf.orig' -Algorithm SHA256

How do you find out what is writing the file?

Ask colleagues about deployments and check the file header for a generated-file notice. On a systemd Linux server, inspect open handles and relevant recent logs. lsof may need installing. The package log command below applies to Debian and Ubuntu; it is not a RHEL package-history command.

sudo lsof /etc/example.conf
sudo journalctl --since "1 hour ago" --no-pager | grep -F 'example.conf'
sudo grep -F 'example.conf' /var/log/dpkg.log

Treat these as clues. A writer that has already closed the file will not appear in lsof. Ordinary file writes are not automatically recorded in the journal, and dpkg.log does not record every configuration pathname. No output therefore proves neither that the file was untouched nor that nobody else is working on it.

If automation owns the file, make the lasting change in its source template or repository. Coordinate a pause through the normal maintenance process when necessary. Do not disable an unfamiliar service simply to make an overwrite succeed.

How do you compare and merge the three versions?

Preserve your edit as example.conf.mine in the working directory. Fetch the current server version under a new name, without replacing either saved copy. Use the same quiet interval and checksum cross-check as before if writers are still active. Then compare each branch with the untouched original.

sudo cat /etc/example.conf > "$work/example.conf.current"
diff -u "$work/example.conf.orig" "$work/example.conf.current"
diff -u "$work/example.conf.orig" "$work/example.conf.mine"

In unified diff output, minus lines belong to the first file and plus lines to the second. diff returns 1 for differences, which is expected here; 2 indicates trouble. Review both comparisons before deciding what belongs in the result.

GNU diff3 can produce a merged candidate. Its arguments are your edit, the common original and the current server copy, in that order. Use a new output filename, never one of the inputs.

diff3 -m "$work/example.conf.mine" "$work/example.conf.orig" "$work/example.conf.current" > "$work/example.conf.merged"

Exit status 0 means no reported conflicts, 1 means conflicts and 2 means trouble. Resolve any conflict markers in the candidate and review it fully. Even a clean merge can combine incompatible settings on different lines. For the timeout and address example, check that the final file includes both intended changes.

How do you save the merged result without losing another change?

A comparison followed by a save is still two operations. Another writer can intervene between them. Arrange exclusive responsibility for this change, including scheduled automation, before publishing. A lock file helps only when every writer agrees to respect it.

Saving replaces live configuration and can remove someone else's work or break access. Proceed only after the current-version check, a usable backup and validation. For SSH or network settings, keep a working session and console access available until the change is verified.

  1. Validate the merged candidate with the application's documented configuration checker, using a candidate-file option if supported. Review included files and dependencies too.
  2. Recheck the live checksum against example.conf.current, and compare ownership, permissions and link destination with your recorded state. If anything changed, fetch another current copy and review again.
  3. Back up the current live version and its required metadata in a protected location. This rollback version must include changes made since your original download.
  4. For an ordinary regular file, open it with sudoedit /etc/example.conf, replace its contents with the reviewed candidate and save during the agreed interval. For a managed file, publish the source change through its deployment process instead.
  5. Check the saved bytes and metadata before reloading the application. If another unexpected change appears, stop and investigate rather than repeatedly saving over it.

If the path is a symbolic link, confirm the intended target and follow the service's deployment procedure. sudoedit commonly refuses symbolic links by default. Do not bypass that protection without understanding the path. A plain SFTP overwrite also requires the same coordination and final checks.

How do you verify that the right version is active?

After saving, compare the live file with the reviewed merged candidate. Matching digests confirm the bytes you checked are now at that path at the time of reading. Recheck metadata too, especially if an upload created a replacement file.

sha256sum "$work/example.conf.merged"
sudo sha256sum /etc/example.conf
sudo stat -c '%F %N' /etc/example.conf
sudo stat -Lc '%U %G %a' /etc/example.conf

Run the application's configuration test against the installed configuration. Reload only after it passes and only if the service supports reloading these settings. Check service logs and perform a functional request that exercises the changed behavior. A file on disk and a running process using that file are different things.

If verification fails, preserve the failed candidate for diagnosis. Restore the agreed backup through the same controlled procedure and validate it before applying it to the running service. Do not automatically restore the oldest original, which may discard the changes you were trying to preserve.

What mistakes should you avoid, and how can you prevent repeats?

  • Do not force a save merely because your edited file is newer. Newer does not mean it contains the other writer's changes.
  • Do not assume an editor warning is a lock. It can detect a change without preventing another write.
  • Do not assume an atomic rename solves concurrent editing. It can prevent readers from seeing a partially replaced file while still replacing someone else's complete update.
  • Do not hide whitespace or line-ending differences until you understand them. Windows editors can change CRLF, encoding or a byte-order mark and make a small edit affect the whole file.

Keep the authoritative configuration in version control, review changes and deploy through one documented route. A repository records intent and history; coordination still matters when deploying to a live server. Record who owns each generated file and where its source lives so emergency edits do not become permanent drift.

Keep edit sessions short, announce maintenance and retain the original until verification finishes. When an urgent change must be made directly, feed it back into the managed source before the next deployment. Clean up sensitive working copies according to your retention policy once recovery no longer depends on them.

How does CH-J Tools help when a file changes?

The File Manager editor checks the file again when saving. If its content, permissions or link target changed, saving stops and the original remains unchanged. The editor does not merge versions for you.

Loading the current server version discards your unsaved changes. Copy your edited text aside before loading it so that you can compare and merge without losing your work.

For transfers, the app asks before replacing a target that changed or already exists. If the source changes during the transfer, it tells you to start again. Use that interruption to check the versions before deciding which content should be saved.

Frequently asked questions

Can I recover the overwritten version after saving?

Possibly, from backups, editor recovery files, snapshots or the configuration repository. A checksum cannot reconstruct old content. Preserve the remaining copies and avoid further writes while you look for a recovery source.

What if I no longer have the original version?

Keep your edit and download the current server copy separately. Compare them manually and use backups or version history to establish intent. Without a common original, a reliable three-way merge is not available.

Does the same checksum mean permissions are unchanged?

No. A content checksum does not cover ownership, mode bits, ACLs or link destinations. Check the metadata relevant to your service separately, including ACLs or SELinux labels where used.

Will making the file read-only prevent conflicts?

Not reliably. Privileged processes or writers able to replace the directory entry may still change it. Permission changes can also break the application. Coordinate writers and change the authoritative configuration source instead.

Detailed documentation

man: stat →

man: diff →

man: diff3 →

Microsoft Learn: Get-FileHash →

Biometric unlock and security →

All tutorials by category →