Generate an SSH port number based on an SHA-256 digest of the hostname.
The command, ssh-portgen
allows you to calculate a deterministic port number for SSH or any other service based on the name of the host of the service. Primarily, this makes it easy to assign a unique SSH port number to each host, which makes it much more difficult to identify SSH ports during port scans. It is not a high level of server hardening, but it is one additional line of defense to make scripted fingerprinting of your servers more difficult for automated port scans. You can lookup the port for a host by passing the hostname of the host as an argument to the command. The command is cross-platform and should work in any CLI environment on Linux, Mac (darwin), Windows, or the three major flavors of BSD.
To generate a port number for the local host:
> ssh-portgen
Hostname: calamity
SHA2 Digest: a776fba1ad9fd5713cb1d0f4b4c772c4ba4a1f853012444b9e28a800a423a9b6
SSH port number: 1253
To generate/lookup a port number for a remote host:
> ssh-portgen calamity
Hostname: calamity
SHA2 Digest: a776fba1ad9fd5713cb1d0f4b4c772c4ba4a1f853012444b9e28a800a423a9b6
SSH port number: 1253
To select the 3rd and 23rd byte of the digest to generate the port number:
> ssh-portgen -i 3/23
Hostname: calamity
SHA2 Digest: a776fba1ad9fd5713cb1d0f4b4c772c4ba4a1f853012444b9e28a800a423a9b6
SSH port number: 1936
To return only the raw port number for use in a back-tick expression:
> echo `./ssh-portgen -r`
1253
You can compile any platform and architecture that is supported by Go (see the distro list below). You will need Go 1.20 or higher to compile the source code:
go build -o ssh-portgen
Or you can cross-compile for a different OS and/or architecture by setting the GOOS and GOARCH environment variables:
env GOOS=linux GOARCH=amd64 go build -o ssh-portgen
ppc64
386, amd64, arm, arm64
amd64, arm64
amd64
386, amd64, arm, arm64, riscv64
amd64
amd64, arm64
js/wasm
386, amd64, arm, arm64, loong64, mips, mips64, mips64le, mipsle, ppc64, ppc64le, riscv64, s390x
386, amd64, arm, arm64
386, amd64, arm, arm64, mips64
386, amd64, arm
amd64
386, amd64, arm, arm64
Linux: amd64/x86_64, arm64, ppc64le, ppc64, s390x, riscv64
Darwin/Mac: amd64/x86_64, arm64
Windows: amd64/x86_64, arm64
FreeBSD: amd64/x86_64, arm64, riscv64
OpenBSD: amd64/x86_64, arm64, mips64
NetBSD: amd64/x86_64, arm64