# IP restrictions: server, port and user

"Who may connect from where" can be said in four places. Each answers a different question, and a connection has to pass all of them.

## The four places

| Place | Question it answers | Set in |
|---|---|---|
| the **server** | which addresses may connect to this server at all? | [Banning](Prefs-Banning.md#ip-restrictions-and-bans): the permanent list |
| a **port** | which addresses may use this listener? | [IP restrictions of a port](Ports-IP-Restrictions.md) |
| a **user** | from where may this account log in? | [IP restrictions of a user](Users-Restrictions-IP.md) |
| **administration** | from where may the server be administered? | **IPs allowed to administer**, in [Banning](Prefs-Banning.md#ip-restrictions-and-bans) |

Beside them stand the bans the server sets by itself ([Bans and hammering](Security-Bans.md)) and the rules by country ([Banning by country](Prefs-Banning-Countries.md)).

## The order

1. **Server.** Temporary bans, then the permanent list.
2. **Port.** The rules of the listener the connection came in on.
3. **Country**, for public addresses.
4. The connection is accepted, and the client says who it is.
5. **User.** The account's own rules. A login from an address they do not allow fails like a wrong password.
6. **Administration.** What an administrator's account tries to do from an address that is not on the list is refused.

A connection that fails at 1 to 3 is closed before a word is said. One that fails at 5 or 6 has reached the login.

## How a list is read

All of them work the same way: rules of the kind Allow or Deny, each for a range of addresses, read from the top. **The first rule the address falls into decides.** An address that fits no rule of a list that has rules is refused. An empty list allows everybody.

So every list is either "everybody but these", with a rule at the end that allows all, or "nobody but these", with Allow rules only. The details are on [IP restrictions of a port](Ports-IP-Restrictions.md#how-the-rules-are-read).

## Which place for which goal

| Goal | Place |
|---|---|
| keep a hostile network away from everything | server: a Deny on the permanent list |
| the server is for our partners only, and we know their addresses | server: Allow rules only. Or the firewall in front, which is better at it. |
| SFTP for partners, the web interface for everybody | port: rules on the SFTP listener |
| the admin pages on a port of their own, for the office | port: rules on that listener, **and** administration: the office networks |
| this account belongs to a script on one machine | user: that machine's address |
| this partner's accounts may only come from the partner | user: on the partner's template |
| an administrator's password alone must not be enough | administration: your own networks only. This is the single most useful setting on this page. |

## Administration from known addresses only

On a new server, **IPs allowed to administer** holds `127.0.0.1` and `*`: everywhere. Take `*` out and put in your networks, such as `10.0.1.*`. `127.0.0.1`, the server machine itself, is always allowed, so you cannot lock yourself out of the machine.

An attacker who then gets hold of an administrator's password, or finds a hole in an admin function, can do nothing with it from outside.

The check is made for every admin action, not only at login.

## Behind a load balancer or proxy

All of this works on the address the server sees. Behind a balancer that is the balancer's, for everybody, unless the real address is passed on:

- for all protocols: [The PROXY protocol on a port](Ports-PROXY-Protocol.md);
- for the web: the `X-Forwarded-For` header, see [Behind a proxy](Ports-Behind-a-Proxy.md).

Until one of them works, rules by address are worse than useless: a Deny for one user's address denies nobody, and an Allow for the balancer allows everybody.

## IPv6

Rules can name IPv6 addresses and ranges. A rule that allows all of IPv4, `0.0.0.0` to `255.255.255.255`, allows all of IPv6 as well. A server that is reachable over IPv6 needs its rules checked for both: a careful list of IPv4 ranges does not restrict IPv6 clients in the way you may expect.

## Things to know

- A firewall in front of the server does the coarse work better: it drops packets before they cost the server anything. Use the server's lists for what the firewall does not know: users, ports of the server, administration.
- Addresses of partners change. A rule with a **Notes** text that says whose address it is, and since when, is one you can still maintain in two years.
- The [Telnet client](Admin-Telnet.md) of Server Admin and the [connection rate line](Security-Login-Load.md#who-connects-how-often) in the log help to find out which address somebody really comes from.
