How to allowlist your IP address
Allowlisting means "only let these addresses in." It's one of the simplest ways to keep strangers away from a server, database, or admin page. Here's how to do it without locking yourself out.
Step 1: get the right address
You need your public IP, the one the server will see. It's at the top of gopherip.com, and GopherIP also shows ready-to-copy rules for it right below. From a terminal: curl gopherip.com.
Write single addresses in CIDR form: /32 for one IPv4 address, /128 for one IPv6 address. For example, 203.0.113.42/32.
Step 2: add the rule
AWS security group
aws ec2 authorize-security-group-ingress --group-id sg-XXXXXXXX \
--protocol tcp --port 22 --cidr 203.0.113.42/32
Or in the console: EC2, Security Groups, Edit inbound rules, Add rule, Source "My IP." (The console fills it in for you, but it's worth double-checking against GopherIP if you're on a VPN.)
Apache
<Location "/admin">
Require ip 203.0.113.42
</Location>
nginx
location /admin {
allow 203.0.113.42;
deny all;
}
ufw (Ubuntu firewall)
sudo ufw allow from 203.0.113.42 to any port 22 proto tcp
MySQL or MariaDB
CREATE USER 'appuser'@'203.0.113.42' IDENTIFIED BY 'a-strong-password';
The database's own firewall or security group must also allow the address.
Step 3: don't lock yourself out
- Add before you remove. Add your new rule and test it before deleting any wide-open rule like
0.0.0.0/0. - Keep a back door. On AWS, Session Manager or the EC2 serial console still works if you lock SSH. Most hosts have a web console.
- Expect home addresses to change. Your address can change after a router restart (here's why). If you get locked out, check GopherIP first; you probably just have a new address.
- Mind IPv4 vs. IPv6. An IPv4 rule doesn't help if your computer connects over IPv6. See IPv4 vs. IPv6.
- VPNs change your address. If you allowlist while on a VPN, you'll only get in while on that VPN, which is sometimes exactly what you want.
Is allowlisting enough?
It's a strong first layer, not the only one. Keep passwords strong, use SSH keys instead of passwords, and keep software updated. Allowlisting just means far fewer strangers get to knock in the first place.