Change stored remote host names in terminal application en mass
I have been using the simple hostname to ssh into remote hosts, around 100 systems. There has been a change in configuration of the network that will require FQDNs.
Rather than recreating all my remote system names (stored in terminal and shown when pressing shift-command-k )
Can I edit the stored data to add the domain information to the existing entries?
The host names are stored in the Terminal settings .plist in the users /Library/Preferences
folder:
~/Library/Preferences/com.apple.Terminal.plist
Edit the .plist
The easiest way to edit a batch of these is by editing the plist with a tool like VSCode (free) with the Binary Plist Extension or your favorite text editor with plist support.1 You can also convert the plist to XML (and back to binary) using plutil
.2 This will allow you to use any text editor like TextEdit that doesn't natively support plist formats.
Important: I suggest making a backup copy of the plist before you start editing in case you want to revert back to the original.
You can find all of your servers in the array under the PermanentServers key as shown below. Simply go in and fix the FDQN as needed. If the domain suffixes are all the same, a simple Find/Replace should speed through the task.
Save the file and reload Terminal. For this example, I added the FQDN .newFQDN
to each entry and restarted Terminal. Here's the result:
Alternate Method using defaults read/write
You can get the names of the server with the defaults read
command:
$ defaults read -app Terminal PermanentServers
(
"host1.FQDN",
"host2.FQDN",
"host3.FQDN",
"host4.FQDN"
)
You can set your new servers by overwriting that array with a new one as follows:
$ defaults write -app Terminal PermanentServers -array '(host1.newFQDN, host2.newFQDN, host3.newFQDN, host4.newFQDN)'
You can then reissue the defaults read
command to check the results. The upside to this is that you don't need TextWrangler and you can script it. However, it's difficult to do a large number on the command line so, you'll want to edit it first in a text editor to make sure you have no mistakes.
1 In the first version of this answer, I originally referred to TextWrangler as it was a tool I originally used to view/edit plist files. However, I've been transitioning to VSCode as it's cross platform - macOS, Windows, and even FreeBSD, I discovered a plist plugin that allowed me to finally get rid of TextWrangler, not that it's "bad" in any way; I'm just trying to streamline my dev tools.
2 To convert a binary plist to XML for editing, issue the command plutil -convert xml1 foobar.plist
. Once you're finished editing, save it then convert it back to binary with the command plutil -convert binary1 foobar.plist
If all you're worried about is being able to ssh
in, you might not need to change how you connect at all.
You can make aliases in ~/.ssh/config
which refer to other hosts. For example, if you want to be able to do ssh shortie
instead of ssh somehostname.plus.fqdn
you would use this in ~/.ssh/config
:
Host shortie
HostName somehostname.plus.fqdn
HostKeyAlias somehostname.plus.fqdn
The HostKeyAlias
tells ssh
which hostname to use in ~/.ssh/known_hosts
for verifying this connection, so you might want to use the FQDN for that.
I would try that with one of your hosts and see if it works for you. If it does, editing ~/.ssh/config
is certainly the easiest fix, and you can copy it to other computers if you want/need.
You can also assign multiple aliases, too.
The nice thing about doing this as the ssh
level is that it will work for Terminal.app, iTerm, and any other tools that use ssh.