Skip to content

Instantly share code, notes, and snippets.

@nickgravel
Created March 20, 2024 15:58
Show Gist options
  • Save nickgravel/7844d73e5f288348b405c37b8e80fd36 to your computer and use it in GitHub Desktop.
Save nickgravel/7844d73e5f288348b405c37b8e80fd36 to your computer and use it in GitHub Desktop.
Undo your remote networking config mistakes *after* you’ve been disconnected

You were updating some *nix firewall rules or network configs over a remote connection and changed the wrong setting. Oops, now you’re locked out and feeling helpless.

Next time, follow the steps below to undo your network mistakes and avoid getting locked out permanently. The example commands illustrate how you’d recover from fucking-up a sshd_config change.

1. Backup Your Config(s)

Upon connecting to the remote server, immediately make a backup of the file(s) you need to modify.

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.old

2. Schedule an “Undo” Job

Schedule a job to run later with the at command that reverts the changes you’re about to make, ensuring you set enough time to actually make those changes!

at now + 5 minutes <<< 'cp /etc/ssh/sshd_config.old /etc/ssh/sshd_config; service sshd restart'

In the above example, the backup configuration file will be restored, the service is restarted, and things are back to normal within 5 minutes.

If your changes worked, move on to the next step.

3. If Your Changes Worked, Delete the “Undo” Job

So your changes (finally) worked out, don’t forget to cancel the “undo” job! Commands queued up for later execution via at are listed with the atq command and deleted with the atrm command.

Find the “Undo” Job Number:

atq

Delete the “Undo” Job:

atrm 1

Replace ‘1’ with the relevant job number found via atq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment