Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have read the basic instructions here:

I want to sudo ufw default deny and then

sudo ufw allow ssh
sudo ufw allow smtp
sudo ufw allow www
sudo ufw allow https
sudo ufw allow imaps

to allow the services I need, am I missing anything ? I assume allowing ssh will also allow scp ? (heck I will allow sftp as well anyway).

However my problem is I am connecting remotely, so the only way I can do what I want is to actually do a sudo ufw default allow then use a list of the services provided by less /etc/services and deny each service individually? This seems a pain as if I turn on the firewall with default deny it will boot me out of my ssh connection?

share|improve this question
Yes, scp and sftp are all carried over ssh, on port 22. – poolie Sep 14 '11 at 22:45
Can't you enable the services you need (eg ssh) before setting the default policy? – poolie Sep 14 '11 at 22:46

The easiest solution is to find your current IP, and first do the following:

sudo ufw allow in from

Where would be your current IP. This way you don't block yourself out from the machine.

share|improve this answer

This is an old question, but still valid, and the answers given aren't exactly complete, so for those stumbling across this...

The best way to use ufw is to close everything first, then re-open ports one by one as needed. The OP was correct in that you must be careful if you're doing this over SSH (although his solution is backwards): you don't want to lock yourself out, so execute these commands before enabling ufw. As an example:

    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    sudo ufw allow ssh
    sudo ufw allow http
    sudo ufw enable

In summary, set the rules first, including allowing SSH, before enabling them.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.