Skip to content

Instantly share code, notes, and snippets.

@jabenninghoff
Last active December 20, 2015 16:15
Show Gist options
  • Save jabenninghoff/a0a7f0e15dcb1e74b6e1 to your computer and use it in GitHub Desktop.
Save jabenninghoff/a0a7f0e15dcb1e74b6e1 to your computer and use it in GitHub Desktop.
Configure ssh to require public-key based authentication only for external networks

Given: my local network is 10.1.1.0/24, my username is jabenninghoff, my ssh host is ssh.example.com, my host's address is 10.1.1.17, my ssh port is 22, and I'm connecting from either 10.1.1.33 (locally) or 172.16.1.20 (remotely)
When: connecting from the local network
Then: allow default ssh authentication methods

When: connecting from other networks
Then: require key-based authentication

Configure using a Match rule:

Match Address *,!10.1.1.*
    AuthenticationMethods publickey

The Match rule states "match all addresses except 10.1.1.*". As noted in the OpenSSH Cookbook, "Note that for negation a wildcard must be specified first and then the address or range to be excluded following it."

Test sshd config, local connections, remote connections:

sshd -T
sshd -TC user=jabenninghoff,host=ssh.example.com,laddr=10.1.1.17,lport=22,addr=10.1.1.33
sshd -TC user=jabenninghoff,host=ssh.example.com,laddr=10.1.1.17,lport=22,addr=172.16.1.20

The result of the second test should include:
authenticationmethods

The result of the third test should include:
authenticationmethods publickey

Thanks to the FreeBSD Forums for the solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment