Uploading directly to a live path can expose a half-written file if the connection drops. A safer pattern is to upload to a separate name, verify its contents, then rename it over the destination. Readers see either the old file or the new one—not the transfer in progress—provided the rename is atomic on the server’s filesystem.
This is useful for a single config file or artifact. It is not a complete deployment system: it does not coordinate multiple files, restart services, or make a service reload the file safely.
1. Choose a staging path
Assume the local file is app.conf and the live remote file is /srv/app/app.conf. Upload to a different name in the same directory:
/srv/app/app.conf.uploading
Keeping staging and destination in the same directory is a straightforward way to keep them on the same filesystem. A rename across filesystems may fail with EXDEV; a tool that falls back to copying would lose the atomic-replacement property.
The example below uses a fixed staging name, so run only one deployment at a time. Concurrent uploads could overwrite each other’s staged file. For parallel deployments, use a unique staging name for each run and ensure the final promotion step selects the intended one.
2. Upload with SFTP
First calculate the local SHA-256 digest:
sha256sum app.conf
Then start an SFTP session and upload the file to its staging path:
sftp user@host
At the sftp> prompt:
put app.conf /srv/app/app.conf.uploading
bye
SFTP clients differ in their interface, but the important detail is that the remote target is the staging name, not /srv/app/app.conf. If the transfer is interrupted, the live file remains untouched; remove the incomplete staging file or upload it again. If you encounter a permission error, check the account’s access to the destination directory as well as the SFTP server’s restrictions. The SFTP permission-denied troubleshooting guide covers common ownership, ACL, and chroot causes.
3. Verify the uploaded bytes
After SFTP finishes, calculate the digest of the staged remote file over SSH:
ssh user@host 'sha256sum -- /srv/app/app.conf.uploading'
Compare the first field of this output with the digest from the local sha256sum app.conf command. They must match exactly. The filenames printed by the two commands can differ; compare the hexadecimal hashes, not the filename column. If the remote system lacks sha256sum, use an available SHA-256 utility such as shasum -a 256, and use it consistently for the comparison.
Do not promote the file if the hashes differ or the remote check fails. Investigate the transfer, upload again, and recheck. A checksum comparison detects a mismatch between the local file and the staged remote file; it does not establish that the file is valid configuration. Run any application-specific syntax or validation checks before promotion when available.
4. Rename the verified file into place
Once the digest matches, run:
ssh user@host 'mv -- /srv/app/app.conf.uploading /srv/app/app.conf'
On a typical Linux filesystem, when both paths are on the same filesystem, the rename replaces the destination as a single namespace operation. A process opening the path sees the old file or the new file, rather than a partially copied replacement. A process that already has the old file open may continue reading that old inode; rename does not force applications to reload files or alter existing file descriptors.
The remote account needs permission to rename entries in /srv/app—usually write and search permission on that directory. It does not necessarily need write permission on the old file itself. If the account cannot perform the rename, do not assume that uploading with SFTP and then running mv as another user will work: the second user must be able to access the staged file and replace the destination. Arrange suitable ownership and directory permissions, or use a controlled privileged deployment step.
For an overview of setting up OpenSSH file transfer on Linux, see how to set up an SFTP server on Linux. The same workflow applies when connecting to an existing SFTP server; you do not need to install a new server just to stage one file.
Check file metadata and service behavior
Replacing a path with a newly uploaded file can change its owner, group, mode, ACLs, or security labels. Check the live file’s requirements before relying on the replacement. For example, inspect the current file with stat and getfacl if installed, and confirm that the service account can still read the new file. SFTP upload permissions are affected by the client, server, and directory defaults; do not assume the staged file will inherit the old file’s metadata.
If the new file needs a particular mode, set it on the staged file before promotion, for example:
ssh user@host 'chmod 0640 /srv/app/app.conf.uploading'
Use the mode appropriate for your service rather than copying this value blindly. Similarly, setting ownership may require elevated privileges. Validate the staged file and its metadata before the rename, then reload or restart the service if its configuration requires that. For a single-file transfer where direct copying is acceptable, SCP upload and download examples explain the alternative; the staging-and-rename step is what protects the live path from an incomplete upload.
Limits of the guarantee
A same-filesystem rename prevents readers from observing a partially transferred file at the destination. It does not guarantee that the data survives sudden power loss: that stronger durability guarantee may require flushing file data and directory metadata, which ordinary SFTP-plus-mv commands do not necessarily ensure. Nor does it make a group of files update atomically.
For one file, though, staging, checksum verification, and a same-filesystem rename provide a simple and auditable improvement over uploading directly over the live path.
I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.
Top comments (0)