Restore a Backup or Undo a Change? Two Ways to Roll Back a File
Restore a backup or undo the edit? We test both on sshd_config to show which later changes survive a Linux file rollback, and how to check the result.

Imagine you're responsible for maintaining a Linux server.
Your team makes a change to /etc/ssh/sshd_config, the file that controls SSH server settings. The change passes review and is deployed.
Two days later, someone discovers a problem: the emergency root login no longer works on a jump host.
A jump host is a server administrators connect to before accessing other servers.
You need to undo the change.
Fortunately, your team's recovery instructions, also called a runbook, provide two options:
- Restore a backup of the original file, created using the Linux
cpcommand. - Undo the specific change that caused the problem.
Both approaches can work.
But what happens if another administrator modified the same file after your change?
That's where things get interesting.
The question isn't just whether you can restore the file. It's which changes will survive the rollback, and how can you verify the result?
1. Understanding the two rollback methods
Let's start with the difference between restoring a backup and undoing an edit.
Option A: Restore the backup
Before changing a configuration file, an administrator might save a copy:
cp /etc/ssh/sshd_config /root/sshd_config.before
If something goes wrong, they can copy it back:
cp /root/sshd_config.before /etc/ssh/sshd_config
This replaces the current file's contents with the saved version.
The advantage is simple: you know exactly what the original file contained.
The disadvantage is that any changes made to the file after the backup are also lost.
For example, suppose another administrator updated the MaxAuthTries setting after your change. Restoring the old backup removes their update too.
Option B: Undo only your change
Instead of restoring the entire file, you can reverse the specific edit you made.
One way to do this is with a reverse patch.
A patch describes changes to a file. It records which lines were added or removed, along with some unchanged lines around them.
Those surrounding lines are called context. They help the patch command find the correct location.
The command patch -R reverses a patch.
If your change added a line, reversing the patch removes it and restores the previous version of that line.
The advantage is that changes made elsewhere in the file can remain untouched.
However, the reverse patch might fail if someone has modified the same lines or changed the surrounding text too much.
When patch cannot match a section of the original change, it reports a failed hunk. A hunk is one section of changes in a patch.
During a real patch operation, failed hunks can be written to a reject file. During a dry run, the command reports the problem without changing the file.
What about Git?
Some teams manage configuration files through Git instead of manually changing each server.
When something goes wrong, they can use git revert to create a new commit that reverses the original change.
A configuration management tool then brings the servers into alignment with the configuration stored in Git. This process is called convergence.
This approach allows teams to review the rollback and preserve unrelated changes.
The important difference
A backup gives you a known original file that you can compare against.
A reverse patch tries to restore the previous edit while keeping other changes.
That makes verification different.
With a backup, you can compare the restored file's hash against the original hash. A hash is a fingerprint calculated from the file's contents.
With an undo, there isn't necessarily one original hash to compare against. The expected result is the original configuration plus any legitimate changes made afterward.
And unless those later changes were recorded, you may not know exactly what they were.
2. Testing both methods on the same file
To see the difference, we tested both rollback methods on 2026-11-12.
We used a temporary Rocky Linux 9.8 virtual machine (VM) with the following tools:
| Tool | Version | |---|---| | Rocky Linux | v9.8 | | coreutils | v8.32 | | patch | v2.7.6 | | OpenSSH | 9.9p1 |
Step 1: Record the original file information
Before making any changes, we recorded the file's contents and important properties.
sha256sum /etc/ssh/sshd_config stat -c '%a %U:%G' /etc/ssh/sshd_config ls -Z /etc/ssh/sshd_config getfacl /etc/ssh/sshd_config cp /etc/ssh/sshd_config /root/sshd_config.before
Here's what each command does:
sha256sumcalculates a fingerprint of the file's contents.statdisplays the file's permissions, owner, and group.ls -Zdisplays the SELinux security label.getfacldisplays the file's access control list (ACL), which defines access permissions.cpcreates a backup of the file.
The results were:
a7329525af126b8280fd52036f81df62a8f893ce8d917d787ac06ad6d6d1adaf /etc/ssh/sshd_config 600 root:root system_u:object_r:etc_t:s0 /etc/ssh/sshd_config [trimmed: getfacl's note about removing the leading slash] # file: etc/ssh/sshd_config # owner: root # group: root user::rw- group::--- other::---
The permissions 600 mean that the owner can read and write the file, while the group and other users have no access.
The owner and group are both root.
We now have a backup and a record of the file's original state.
Step 2: Make a configuration change
Next, we changed the SSH root-login setting.
sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config
The sed command finds the commented-out line:
#PermitRootLogin prohibit-password
And replaces it with:
PermitRootLogin no
This explicitly disables root login through SSH.
We then recorded the change as a patch and saved another copy of the modified file:
diff -u /root/sshd_config.before /etc/ssh/sshd_config > /root/change.patch cat /root/change.patch cp /etc/ssh/sshd_config /root/sshd_config.after
The diff -u command compares the original and modified files.
Its output was:
[trimmed: the two file-name lines with timestamps] @@ -37,7 +37,7 @@ # Authentication: #LoginGraceTime 2m -#PermitRootLogin prohibit-password +PermitRootLogin no #StrictModes yes #MaxAuthTries 6 #MaxSessions 10
In a diff:
- A line beginning with
-was removed. - A line beginning with
+was added. - Lines without either symbol provide context.
We can now test what happens when another administrator makes additional changes.
3. Case A: another administrator changes a different line
Suppose another administrator changes the maximum number of SSH authentication attempts.
They change the setting from the commented default:
#MaxAuthTries 6
To:
MaxAuthTries 3
This is a separate change from our root-login setting, although it appears only two lines below it.
We make that change with:
sed -i 's/^#MaxAuthTries 6/MaxAuthTries 3/' /etc/ssh/sshd_config
Now let's test both rollback methods.
First, test the reverse patch
patch -R --dry-run /etc/ssh/sshd_config < /root/change.patch
The -R option reverses the original change.
The --dry-run option tests whether the patch can be applied without modifying the file.
The output was:
checking file /etc/ssh/sshd_config Hunk #1 succeeded at 37 with fuzz 2.
This tells us the reverse patch would succeed.
It would restore the original PermitRootLogin line while leaving the colleague's MaxAuthTries 3 setting intact.
But there's an important detail:
Hunk #1 succeeded at 37 with fuzz 2.
What does fuzz 2 mean?
Normally, patch uses the unchanged lines around an edit to find where the change belongs.
In this case, the colleague modified one of those surrounding lines.
Because the context no longer matched exactly, patch tried again using fewer context lines.
It first ignored the outermost context lines, then the next outermost lines.
The default fuzz limit is 2.
The patch succeeded at line 37, where the original diff expected the change.
However, using fewer context lines means the command has less information to confirm that it found the correct location.
The diffutils manual warns that increasing the fuzz factor also increases the chance of applying a patch incorrectly.
In this test, the reverse patch found the intended location.
Now restore the backup
Instead of applying the reverse patch, we restored the original file:
cp /root/sshd_config.before /etc/ssh/sshd_config sha256sum /etc/ssh/sshd_config grep -nE '^(PermitRootLogin|MaxAuthTries)' /etc/ssh/sshd_config; echo "grep exit: $?"
The output was:
a7329525af126b8280fd52036f81df62a8f893ce8d917d787ac06ad6d6d1adaf /etc/ssh/sshd_config grep exit: 1
The SHA-256 hash matches the original hash exactly.
That confirms the file's contents were restored to their original state.
But notice the grep result.
The grep command searched for active PermitRootLogin and MaxAuthTries settings.
An exit code of 1 means it found no matching lines.
That's because the backup contains the original commented-out settings.
The backup removed both our change and the colleague's change.
The restore completed without warning us that the colleague's edit had been lost.
What did Case A teach us?
| Reverse patch | Backup restore |
|---|---|
| Would undo our root-login change | Undid our root-login change |
| Would preserve MaxAuthTries 3 | Removed MaxAuthTries 3 |
| Reported an inexact match using fuzz | Restored the exact original file contents |
| Was tested without modifying the file | Was actually applied |
The reverse patch preserved the unrelated change in its dry-run test.
The backup restored the original file exactly, but it also removed the colleague's work.
4. Case B: another administrator changes the same line
Now let's try a different situation.
This time, another administrator modifies the exact setting we changed.
First, we restore the version containing our original change:
cp /root/sshd_config.after /etc/ssh/sshd_config
Then the colleague changes:
PermitRootLogin no
To:
PermitRootLogin forced-commands-only
We make that edit with:
sed -i 's/^PermitRootLogin no/PermitRootLogin forced-commands-only/' /etc/ssh/sshd_config
Now both administrators have modified the same configuration line.
Test the reverse patch
patch -R --dry-run /etc/ssh/sshd_config < /root/change.patch
The result was:
checking file /etc/ssh/sshd_config Hunk #1 FAILED at 37. 1 out of 1 hunk FAILED
This time, the reverse patch failed.
The patch expected to find PermitRootLogin no, but that line had already been changed to PermitRootLogin forced-commands-only.
It could not safely apply the original reversal.
That failure is useful information.
It tells the administrator that the file has changed in a way that requires a decision.
Now restore the backup
We restored the original file again:
cp /root/sshd_config.before /etc/ssh/sshd_config sha256sum /etc/ssh/sshd_config grep -nE '^#?PermitRootLogin' /etc/ssh/sshd_config
The output was:
a7329525af126b8280fd52036f81df62a8f893ce8d917d787ac06ad6d6d1adaf /etc/ssh/sshd_config 40:#PermitRootLogin prohibit-password
Once again, the original hash matches.
The original PermitRootLogin line is back.
But the colleague's forced-commands-only setting is gone.
And the restore didn't warn us.
What did Case B teach us?
When two administrators modify the same line, a reverse patch may fail and require someone to review the conflict.
A backup restore doesn't need to resolve that conflict. It replaces the current contents with the saved contents.
That makes the backup predictable, but it also means later changes can disappear silently.
5. What a matching hash proves, and what it doesn't
In both tests, restoring the backup produced the same SHA-256 hash as the original file.
That's useful, but it doesn't tell us everything about the file.
The sha256sum command checks the file's contents.
It does not check:
- File permissions
- File ownership
- SELinux security labels
- Access control lists (ACLs)
Two files can contain exactly the same data and have identical hashes while having different permissions or security labels.
After restoring the backup, we checked the file properties again.
The results were:
600 root:root system_u:object_r:etc_t:s0 /etc/ssh/sshd_config
The ACL output also matched the original.
Why did those properties stay the same?
It comes down to how the cp command works.
When cp overwrites an existing file, it normally updates the file's contents in place.
The coreutils manual says a copy onto an existing file leaves its permissions unchanged.
Our test shows the owner and ACLs stayed the same too.
However, creating a new file is different.
When cp creates a new file, it doesn't automatically preserve all the original file's properties, such as ownership, timestamps, or SELinux labels.
To preserve those properties, you can use options such as:
cp -a source destination
Or:
cp --preserve=all source destination
Our experiment showed the owner and ACLs staying the same when copying over an existing file. The points about permissions, creating new files, and preserving SELinux labels come from the manual.
These are properties of the cp command, not Kensa.
6. How Kensa handles rollbacks
So far, we've been using standard Linux commands to compare two rollback methods.
Now let's look at how Kensa handles the same problem.
An important distinction: the SSH configuration tests above demonstrate shell commands. They do not demonstrate Kensa modifying sshd_config.
Kensa uses different rollback methods depending on the type of change it makes.
When Kensa changes SSH settings
Kensa's SSH rules don't make a backup copy of the entire sshd_config file.
On systems where SSH reads configuration files from a drop-in directory, Kensa writes its own drop-in file.
A drop-in file is a separate configuration file that sets options without changes to the main configuration file. The main sshd_config file pulls drop-in files in with an Include line.
During rollback, Kensa removes its own drop-in file.
This means a colleague's changes to the main sshd_config file survive.
However, if someone edits Kensa's own drop-in file, that edit does not survive the rollback.
On systems where Kensa changes the main sshd_config file directly, it changes the individual setting and restores the previous line during rollback.
When Kensa manages an entire file
Other Kensa mechanisms work differently.
For changes involving an entire file, Kensa saves a copy before making the change.
If rollback becomes necessary, it restores that saved copy.
This approach is used by mechanisms that manage things such as:
- Whole files
- Drop-in configuration files
- Pluggable Authentication Modules (PAM) stacks set by
pam_module_configure - Cron entries
- dconf snippets
One mechanism may add only a single line but still save a copy of the entire file.
If Kensa created a file that didn't previously exist, rollback removes that file.
What happens when something goes wrong?
Kensa provides several recovery paths.
1. A check fails right after a change
For the mechanisms that save a copy, a check that fails right after a change makes Kensa restore that copy in the same run.
2. Kensa is killed during a change
An administrator can run kensa recover.
This restores the saved copy from the operation that was killed.
3. A previously committed change needs to be reversed
An administrator can run kensa rollback.
Kensa uses the transaction log created by the original change.
By default, that log is stored at:
.kensa/results.db
This path is relative to the directory where Kensa was run.
Why does Kensa use backups for whole-file changes?
The main benefit is predictability.
For mechanisms that capture the whole file, Kensa can restore the exact bytes that existed before the change.
You can independently verify those bytes using a SHA-256 hash.
But this design has an important cost.
Kensa doesn't compare the current file against the saved copy before restoring it.
That applies both to a rollback during the same run and to a later kensa rollback.
It doesn't ask for confirmation.
It records nothing about a lost edit.
If another administrator changed the file after Kensa saved its copy, those changes are overwritten without warning.
That's exactly the problem we demonstrated in Cases A and B.
And the longer you wait before rolling back, the more opportunity there is for other changes to be made.
7. What exactly does Kensa restore?
Not every Kensa mechanism restores the same file properties.
For the file_content mechanism, Kensa restores:
- File contents
- Permissions
- Ownership
- SELinux label
If the file's contents are restored but its ownership or SELinux label isn't restored correctly, Kensa marks the rollback as failed so an administrator can investigate.
However, file_content does not restore ACLs or other extended attributes.
Other whole-file mechanisms save less information. They record the file's contents and whether it existed before the change. Some also record its permissions.
Does Kensa verify the rollback?
That depends on when the rollback happens.
If Kensa rolls back during the same run, it reads the resulting state and records whether it matches what was captured before the change.
A later kensa rollback doesn't perform that read-back check.
Kensa also doesn't report a SHA-256 hash after restoring a file.
If you want an independent content check, you need to calculate the hash yourself and compare it with the hash recorded before the change.
What about changes to part of a file?
Kensa has four mechanisms that change only part of a file:
config_set
Changes one setting in a configuration file.
mount_option_set
Changes one entry in /etc/fstab, the file that defines filesystem mounts.
pam_module_arg
Changes one argument on a PAM line.
audit_rule_set
Adds rule lines to an audit drop-in.
The first three save the previous line instead of a copy of the whole file, and on rollback Kensa writes that line back in place of the setting. audit_rule_set removes the lines it added.
This means changes to other lines can survive.
However, if another administrator modified the same setting after Kensa's change, their newer value is lost during rollback.
Are there other limitations?
Yes.
Kernel parameters and systemd units restore their own recorded state, along with any files they wrote.
Some operations, such as running a command or performing a manual step, don't record rollback information. Kensa skips those operations during rollback.
There's also a difference when the Kensa helper is disabled with KENSA_NO_AGENT=1.
In that mode, Kensa still uses the saved copy, but whole-file restoration is no longer atomic.
An atomic restore is one that makes the replacement appear as a single completed operation rather than exposing a partially written file.
8. When is undoing the better choice?
Let's return to the original SSH problem.
Your team changed sshd_config, and the emergency root login stopped working.
You have a backup, but other administrators may have updated the file since your change.
Which rollback method should you choose?
It depends on how the file is managed.
| Situation | What to consider | |---|---| | You need the exact original file contents | Restoring a backup provides a known target | | Other administrators have edited different lines | A targeted undo can preserve their changes | | Someone changed the same line | A reverse patch may fail, while a backup can silently overwrite the newer value | | Configuration is managed in Git | Reverting the commit and letting configuration management converge may be preferable | | Rollback happens days after the original change | A backup is more likely to overwrite newer edits |
Consider a jump host where two teams regularly update sshd_config.
One team changes root-login settings. Another team adjusts authentication limits.
If the first team's change needs to be reversed, restoring an old copy of the entire file can remove the second team's work.
A targeted undo is better suited to preserving those unrelated changes.
And if the configuration is managed through Git, reverting the original commit provides a reviewed way to reverse the change while keeping the other updates.
For a fleet of servers managed that way, restoring old file copies may create more problems than it solves.
To try Kensa on a test machine, start with the install guide, then the quickstart.
Final takeaway
Both backup restoration and targeted undo are valid rollback methods. Neither is automatically the best choice for every situation.
Restoring a backup gives you a known result. You can compare the restored file's contents against the original using a hash.
But that certainty comes with a cost: changes made after the backup can disappear without warning.
Undoing an edit is more selective. It can preserve changes made by other administrators, but it may fail when the same lines have been modified. And without a complete record of later changes, there may be no single hash that proves the entire file is correct.
Kensa uses different approaches, depending on the mechanism.
The practical lesson for a system administrator is simple:
Before rolling back a configuration file, understand whether you're restoring the entire file or reversing a specific change. That decision determines which later edits survive, and which ones may be lost.