Overwriting a live file directly with SFTP is risky. If the connection drops during an upload, the destination may already have been truncated or may contain only part of the new content. A service that reads the file at that moment can fail or load invalid configuration.
A safer pattern is to upload to a temporary name in the same remote directory, verify that copy, and then promote it with a rename. The live file stays untouched while the upload is in progress. The rename step needs care: SFTP servers differ in how they handle renaming over an existing destination.
Stage the upload beside the live file
Start an SFTP session:
sftp user@server
Change to the directory containing the live file, then upload under a temporary name:
cd /etc/myapp
put app.conf app.conf.next
ls -l app.conf.next
The staged file should be in the same directory as app.conf. Keeping both names on the same filesystem is important: a same-filesystem rename can be atomic with respect to other processes observing the directory. A rename across filesystems may fail or require a copy-and-delete operation instead, which does not have the same property.
Use a temporary name that will not collide with another deployment. For concurrent deploys, include a release ID or unique suffix, such as app.conf.next.20250308-1530. Ensure the service will not mistake that name for an active configuration file.
Verify the staged bytes before promotion
A successful put tells you the client completed the transfer; it does not establish that the file is the intended version. Download the staged copy and compare checksums:
get app.conf.next /tmp/app.conf.check
In a second local shell, compare it with the source file:
sha256sum app.conf /tmp/app.conf.check
The hashes should match. This verifies the bytes received by the remote server and returned to your client, though it does not validate the configuration’s syntax or whether the application can use it. Run an appropriate local validator before upload where possible, and use a server-side validation command if one is available and safe to run.
If the transfer fails, leave the live file alone. Remove or reuse the incomplete staged file, then upload again. SFTP upload failures can also result from directory ownership, ACLs, or chroot restrictions; diagnose those rather than making the live directory broadly writable. See how to diagnose SFTP permission errors before changing permissions.
Promote carefully: replacement behavior matters
If the destination name does not exist, the ordinary SFTP command is straightforward:
rename app.conf.next app.conf
But during an update, app.conf usually already exists. The standard SFTP rename operation may refuse to replace it. Do not assume that rename means “atomically overwrite” on every server.
OpenSSH’s SFTP client also provides posix-rename, which requests the OpenSSH POSIX rename extension. On a server that supports it, POSIX rename semantics allow replacing an existing destination within the same filesystem:
posix-rename app.conf.next app.conf
The extension is not guaranteed on every SFTP server, and server or filesystem conditions can still make a rename fail. Test the exact command and server behavior on a non-production file first. Check your client’s sftp(1) documentation and server configuration if the command is unavailable or rejected.
Avoid this tempting fallback if uninterrupted access to the live path matters:
rename app.conf app.conf.backup
rename app.conf.next app.conf
It preserves a backup, but between the two commands app.conf does not exist. A process opening the path then can fail. It is not an atomic replacement. If the server does not support replacing the destination, use a deployment mechanism that can perform the replacement safely, or schedule a controlled maintenance window. For ordinary transfer workflows and their verification trade-offs, see SCP file transfer examples.
Backups, permissions, and application reloads
Keep a known-good backup when the configuration is important. Before deployment, record the current file’s contents, checksum, and permissions, or make a backup through a method appropriate to the server. A same-directory backup rename can preserve the old file, but it introduces the missing-path gap described above if you then rename the staged file into place. A backup does not make that sequence atomic.
Also check the staged file’s ownership and mode. The uploaded file may inherit permissions from the client’s transfer behavior or server defaults, and replacing a file can change metadata that the application relies on. Verify the resulting permissions and ownership through an authorized server-side method. If uploads fail, fix the specific access issue rather than loosening permissions on the whole directory; SFTP server setup and directory access have their own constraints, including chroot rules (see the Linux SFTP server setup guide).
A successful rename makes the new pathname visible; it does not automatically validate the application’s configuration, reload the service, or guarantee data has been flushed to durable storage. Validate and reload using the application’s documented process, then check service health. For critical systems, keep a rollback plan and avoid deploying two versions concurrently to the same temporary name.
The key distinction is simple: upload failure should affect only a staged file. Promote only after verification, and use a tested rename operation whose replacement behavior matches the reliability your deployment requires.
I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.
Top comments (0)