How to ping and ssh with IPv6 Link Local in /etc/hosts

My laptop’s /etc/hosts file has entries like this:

fe80::46a8:42ff:fe2d:c255 monarcheno1
# eno2 - 10.0.10.1
fe80::46a8:42ff:fe2d:c256 monarcheno2
# eno4 10.0.20.1
fe80::46a8:42ff:fe2d:c258 monarcheno4
# ens5 10.0.40.1
fe80::6a05:caff:fe4a:8ac7 monarchens5

These are link-local IPv6 addresses, useful only when connected to a WiFi access point that bridges those link-local addresses, a WiFi access point that does not do NAT. Luckily, that’s how I’ve set up my WiFi access points.

When I try to ssh or ping one of these hostnames, I’ve had problems.

Using the host name in /etc/hosts doesn’t work with ping. These commands fail, albeit for different reasons:

ping monarcheno4 
ping monarcheno4%wlp0s20f3

The first command fails because a link-local address is considered “non-routeable”. You have to route it manually, hence the %wlp0s20f3 suffix of the second command.

For some reason, the esoteric command line argument (argc and argv) processing of the ping command considers the entire string “monarcheno4%wlp0s20f3” a single host name in the second command.

ping will recognize the colon-hex text format as IPv6 addresses:

ping fe80::46a8:42ff:fe2d:c258%wlp0s20f3
ping -I wlp0s20f3 fe80::46a8:42ff:fe2d:c258
ping  -I wlp0s20f3 monarcheno4

The third ping command routes whatever the DNS resolver code finds in /etc/hosts through interface wlp0s20f3.

ssh has some similar issues, but confusingly uses a different flag to indicate “route through this interface”.

ssh -p 4999 fe80::46a8:42ff:fe2d:c258%wlp0s20f3
ssh -p 4999 -B wlp0s20f3  monarcheno4

That particular machine has sshd listening on port 4999 because I run endlessh to mess up SSH-scanners. Since I’m writing this post for my own consumption later, I’m leaving TCP port selection in.