[Server & Network General] Ubuntu: ufw(Uncomplicated FireWall)
以下を参考に手を動かして勉強。
[markdown]
※バックナンバーは最新号しか公開されていないようです。
> * [いますぐ実践! Linux システム管理](http://archives.mag2.com/0000149633/)
以下で作成した VM で試します。
> * [Vagrant で Ubuntu をインストールする | Deadwood](https://www.d-wood.com/blog/2016/03/25_7844.html)
## 概要
– ufw(Uncomplicated FireWall)は、Ubuntuで標準的に利用できる「iptablesを簡単に設定するツール」。
– 「ソフトウェアファイアウォールとしての機能を、できるだけ単純なインターフェースで設定する」という設計思想。
> * [第76回 Ubuntuのソフトウェアファイアウォール:UFWの利用(1):Ubuntu Weekly Recipe|gihyo.jp … 技術評論社](http://gihyo.jp/admin/serial/01/ubuntu-recipe/0076)
> * [第77回 Ubuntuのソフトウェアファイアウォール:ufwの利用(2):Ubuntu Weekly Recipe|gihyo.jp … 技術評論社](http://gihyo.jp/admin/serial/01/ubuntu-recipe/0077)
> * [第353回 UbuntuのソフトウェアファイアウォールufwのGUI,Gufw再入門:Ubuntu Weekly Recipe|gihyo.jp … 技術評論社](http://gihyo.jp/admin/serial/01/ubuntu-recipe/0353)
## UFW の有効化・無効化・状態確認
### 有効化
`vagrant ssh` でログイン。
“`prettyprinted
$ sudo ufw status
Status: inactive
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
$ sudo ufw allow OpenSSH
$ exit
“`
### 状態確認
`vagrant ssh` し直してみる。
“`prettyprinted
$ sudo ufw status
Status: active
To Action From
— —— —-
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
— —— —-
22/tcp (OpenSSH) ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)
“`
### 無効化
“`prettyprinted
$ sudo ufw disable
Firewall stopped and disabled on system startup
$ sudo ufw status verbose
Status: inactive
“`
## ルールの追加・削除
### UFW のデフォルト設定
– 入ってくる(incoming)パケットは拒否(deny)
– 出て行く(outgoint)パケットは許可(allow)
### ルールの追加
ルールの追加方法は2つ。
ポートを指定。
“`prettyprinted
$ sudo ufw allow 80/tcp
“`
アプリを指定。
“`prettyprinted
$ sudo ufw app list
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
$ sudo ufw allow Apache
Rule added
Rule added (v6)
“`
### ルールの削除
“`prettyprinted
$ sudo ufw delete allow 80/tcp
“`
### ルールの確認
概要とルールの確認。
“`prettyprinted
$ sudo ufw app info “Apache Full”
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.
Ports:
80,443/tcp
“`
## 設定ファイル
– ufw コマンドで追加したルールは、次回以降のシステム再起動時も有効。
“`prettyprinted
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
— —— —-
22/tcp (OpenSSH) ALLOW IN Anywhere
80/tcp (Apache) ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)
80/tcp (Apache (v6)) ALLOW IN Anywhere (v6)
$ exit
logout
Connection to 127.0.0.1 closed.
“`
再起動。
“`prettyprinted
% vagrant reload
% vagrant ssh
“`
再ログイン後に確認。
“`prettyprinted
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
— —— —-
22/tcp (OpenSSH) ALLOW IN Anywhere
80/tcp (Apache) ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)
80/tcp (Apache (v6)) ALLOW IN Anywhere (v6)
“`
設定ファイルは以下に。
“`prettyprinted
$ ls /lib/ufw/
ufw-init ufw-init-functions user6.rules user.rules
$ ls /etc/ufw/
after6.rules after.init after.rules applications.d before6.rules before.init before.rules sysctl.conf ufw.conf
$ ls /etc/ufw/applications.d/
apache2 apache2-utils.ufw.profile openssh-server
“`
## 追加で復習
### Vagrant のプライベートネットワーク設定
ログアウト後、`vagrant halt` で停止。
> * [How to setup a LAMP development environment in Vagrant](https://www.howtoforge.com/tutorial/vagrant-ubuntu-linux-apache-mysql-php-lamp/)
Vagrantfile を開き、プライベートネットワーク設定のコメントを外す。
“`:Vagrantfile
config.vm.network “private_network”, ip: “192.168.33.10”
“`
### Apache をインストール
`vagrant up`, `vagrant ssh` 後、Apache をインストール・スタート。
“`prettyprinted
$ sudo apt-get -y update
$ sudo apt-get -y install apache2
$ sudo service apache2 start
$ ps aux |grep apache2
“`
### UFW の動作確認
現在は 80 が通っている。
`http://192.168.33.10` へブラウザでアクセス可能。
“`prettyprinted
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
— —— —-
22/tcp (OpenSSH) ALLOW IN Anywhere
80/tcp (Apache) ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)
80/tcp (Apache (v6)) ALLOW IN Anywhere (v6)
“`
Apache のルールを削除。
`http://192.168.33.10` へブラウザでアクセスできない。
“`prettyprinted
$ sudo ufw delete allow Apache
Rule deleted
Rule deleted (v6)
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
— —— —-
22/tcp (OpenSSH) ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)
“`
UFW を無効化。
`http://192.168.33.10` へブラウザでアクセス可能。
“`prettyprinted
$ sudo ufw disable
Firewall stopped and disabled on system startup
$ sudo ufw status verbose
Status: inactive
“`
[/markdown]