In this Linux quick tip we will discuss using command line options to force the SSH client to use IPv4 or IPv6 specifically. We will also show you how to set which IP protocol you would like to use in your SSH client configuration file.

Command Line Options to Force SSH Client to Use IPv4

There are two, very easy to remember, command line options to force specific versions of the internet protocol. Simply adding a -4 option on the command line will force the SSH client to use IPv4.

Here is an example:

ssh -4 [email protected]

Command Line Options to Force SSH Client to Use IPv6

You probably guessed it by now, but there is also a -6 option that allows you to force the use of IPv6.

Here is an example:

ssh -6 [email protected]

Setting IPv4 or IPv6 in Configuration Files

In an earlier article titled "Configure Host Specific SSH Settings" we discuss building configuration files for different hosts. If you would like to force IPv4 on a host without adding the command line option every time, just create or edit your ~/.ssh/config file and add one of the following lines under your host.

Set IPv4 Connection in SSH Client Configuration File

Here we use inet to specify we want to use IPv4 for the SSH connection.

AddressFamily inet

Set IPv6 Connection in SSH Configuration File

In this example, we use inet6 to specify that we want to use IPv6 for the SSH connection.

AddressFamily inet6

Here is an example of an SSH client configuration file with a host using IPv6 on port 2222 and using SSH keys for authentication.

Host putorius.net
   IdentityFile ~/.ssh/id_putorius_rsa
   IdentitiesOnly yes
   ForwardX11 yes
   Port 2222
   AddressFamily inet6

Conclusion

It's simple to force IPv4 or IPv6 for your SSH connections. It is one of those times when the command line option seems so simple, but hard to guess.

If you are anything like me you are now wondering why inet? Inet simply stands for Internet Protocol family. It is widely used in Linux network connections of many services. You can read more about inet in the links below.