~/.ssh/config: Preferring IPv6 SSH Connections

I was interested in forcing ssh to use IPv6 if the connection is available. I considered writing a bash script that would test connectivity and would invoke ssh with the appropriate command line arguments. After reviewing the ssh configuration file documentation, I determined it was possible to implement what I wanted without standalone scripts.

I updated my ~/.ssh/config file with the following:

IdentityFile ~/.ssh/id_ed25519

Match host stevedoria.net, exec "nc -6z -w 2 stevedoria.net 22"
AddressFamily inet6
User myusername

Host stevedoria.net
AddressFamily inet
User myusername

The above configuration file provides an example of using the ssh_config Match keyword with the host and exec criteria keywords. The configuration file specifies an identity file to use for public key authentication. If the specified host server is “stevedoria.net” and nc is able to connect using the IPv6 address, then IPv6 is used for the SSH connection. Otherwise, if the specified host server is just “stevedoria.net”, then the IPv4 address is used.

With the ssh client configuration file above, I can then log into my web server with the following command from the command line:
$ ssh stevedoria.net

Logging into my server this way is convenient and confirms that I am IPv6-ready.

Questions, comments, and responses are welcomed and appreciated.

Leave a Reply