Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Limit access to openssh features with the Match option

Published: 24-11-2012 | Author: Remy van Elst | Text only version of this article


Table of Contents


OpenSSH has a lot of nice features which let you control how it is used. Forexample, you can disallow the root account to login, set the port number,protocol version and a lot of other features. This tutorial will show you how toenable certain features for certain hosts, users, groups and addresses with theMatch keyword in sshd_config. And as a bonus it also covers the iptablesfirewall.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Securing sshd

First a few general tips about securing ssh.

Examples

You of course have disabled password authentication and you use public/privatekey based authentication, right? Buy, you might have that one host which is forwhatever reason is not able to use ssh keys. The below config line disablespassword authentication for everyone, and then it enables it for the IP address1.2.3.4:

PasswordAuthentication no### this should be on the bottom of the config file### Enable password authentication for IP 1.2.3.4Match Address 1.2.3.4    PasswordAuthentication yes

Or you might have a user which needs to use a graphical application on a server.But all the other users do not have to use that. For example, you might haveMatlab for one user. You can install a desktop for them, but you can also letthem use X forwarding. The below config allows X forwarding for the user John,but disallows it for everyone else.

X11Forwarding no### add this to the bottom of the sshd_configMatch User John    X11Forwarding yes

But lets say John can only use matlab (X-forwarding) from the internal networkand you want to X forwarding for every other user, only allowing it from thelocal 172.16.1.* network, you might want to use this config lines:

X11Forwarding no### add this to the bottom of the sshd_configMatch User John Address 172.16.1.*     X11Forwarding yes

And what if you want to allow a few IP addresses and one hostname to login witha password, as root? Note that this is a bad thing to do, you should not allowroot to login but use su or sudo, and preferably all users should login with sshkeys.

PaswordAuthentication noPermitRootLogin no### Add this to the end of the config fileMatch Address 10.20.30.40,80.90.100.200 Host dispatch.raymii.org    PasswordAuthentication Yes    PermitRootLogin yes

Restrict access via the firewall

Using iptables is also a good way to restrict access to a few IP addresses. Thebelow example allows the IP addresses 2.3.4.5, 3.4.5.6 and 10.2.3.40 to talk toport 22, and discards all the other traffic.

iptables -I INPUT -s 2.3.4.5 -p tcp -m tcp --dport 22 -j ACCEPTiptables -I INPUT -s 3.4.5.6 -p tcp -m tcp --dport 22 -j ACCEPTiptables -I INPUT -s 10.2.3.40 -p tcp -m tcp --dport 22 -j ACCEPTiptables -I INPUT -p tcp -m tcp --dport 22 -j REJECT

All the options

The list below are all the options supported in an SSH Match pattern:

More information

Source link used for this article
Forum topic on the Arch Linux bbs

Tags: limits, match, openssh, ssh, tutorials