You can restrict VPS incoming connections within your VPS provider’s admin page (at a network-level), but if you can’t restrict outgoing connections, or you prefer to use a firewall at a server-level. you can use ufw.

This is an example of how ufw works. We are going to restrict our incoming connections to a specific IP. Replace “IP” with your chosen IP.

You may need to start and enable ufw service. You need to be a root user or use sudo to run these commands.

ufw default deny incoming
ufw default allow outgoing
ufw allow from IP
ufw enable # you only need to run this once

To allow a specific port:

ufw allow 1050

Disable the firewall:

ufw disable

If you use Docker, -p flag makes the selected port available to everyone regardless of your ufw configuration, so you need to use --network host instead.

docker run -d --network host nginx