特殊:Badtitle/NS100:UFW:修订间差异
小 创建新页面为 '{{From|https://help.ubuntu.com/community/UFW}} {{Languages|UbuntuHelp:UFW}} #title Uncomplicated Firewall - UFW == Introduction == For an introduction to firewalls, please see [...' |
(没有差异)
|
2009年11月17日 (二) 21:00的版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/UFW }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/af | • {{#if: UbuntuHelp:UFW|Afrikaans| [[::UFW/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ar | • {{#if: UbuntuHelp:UFW|العربية| [[::UFW/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/az | • {{#if: UbuntuHelp:UFW|azərbaycanca| [[::UFW/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/bcc | • {{#if: UbuntuHelp:UFW|جهلسری بلوچی| [[::UFW/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/bg | • {{#if: UbuntuHelp:UFW|български| [[::UFW/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/br | • {{#if: UbuntuHelp:UFW|brezhoneg| [[::UFW/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ca | • {{#if: UbuntuHelp:UFW|català| [[::UFW/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/cs | • {{#if: UbuntuHelp:UFW|čeština| [[::UFW/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/de | • {{#if: UbuntuHelp:UFW|Deutsch| [[::UFW/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/el | • {{#if: UbuntuHelp:UFW|Ελληνικά| [[::UFW/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/es | • {{#if: UbuntuHelp:UFW|español| [[::UFW/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/fa | • {{#if: UbuntuHelp:UFW|فارسی| [[::UFW/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/fi | • {{#if: UbuntuHelp:UFW|suomi| [[::UFW/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/fr | • {{#if: UbuntuHelp:UFW|français| [[::UFW/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/gu | • {{#if: UbuntuHelp:UFW|ગુજરાતી| [[::UFW/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/he | • {{#if: UbuntuHelp:UFW|עברית| [[::UFW/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/hu | • {{#if: UbuntuHelp:UFW|magyar| [[::UFW/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/id | • {{#if: UbuntuHelp:UFW|Bahasa Indonesia| [[::UFW/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/it | • {{#if: UbuntuHelp:UFW|italiano| [[::UFW/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ja | • {{#if: UbuntuHelp:UFW|日本語| [[::UFW/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ko | • {{#if: UbuntuHelp:UFW|한국어| [[::UFW/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ksh | • {{#if: UbuntuHelp:UFW|Ripoarisch| [[::UFW/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/mr | • {{#if: UbuntuHelp:UFW|मराठी| [[::UFW/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ms | • {{#if: UbuntuHelp:UFW|Bahasa Melayu| [[::UFW/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/nl | • {{#if: UbuntuHelp:UFW|Nederlands| [[::UFW/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/no | • {{#if: UbuntuHelp:UFW|norsk| [[::UFW/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/oc | • {{#if: UbuntuHelp:UFW|occitan| [[::UFW/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/pl | • {{#if: UbuntuHelp:UFW|polski| [[::UFW/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/pt | • {{#if: UbuntuHelp:UFW|português| [[::UFW/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ro | • {{#if: UbuntuHelp:UFW|română| [[::UFW/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/ru | • {{#if: UbuntuHelp:UFW|русский| [[::UFW/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/si | • {{#if: UbuntuHelp:UFW|සිංහල| [[::UFW/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/sq | • {{#if: UbuntuHelp:UFW|shqip| [[::UFW/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/sr | • {{#if: UbuntuHelp:UFW|српски / srpski| [[::UFW/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/sv | • {{#if: UbuntuHelp:UFW|svenska| [[::UFW/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/th | • {{#if: UbuntuHelp:UFW|ไทย| [[::UFW/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/tr | • {{#if: UbuntuHelp:UFW|Türkçe| [[::UFW/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/vi | • {{#if: UbuntuHelp:UFW|Tiếng Việt| [[::UFW/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/yue | • {{#if: UbuntuHelp:UFW|粵語| [[::UFW/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/zh | • {{#if: UbuntuHelp:UFW|中文| [[::UFW/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/zh-hans | • {{#if: UbuntuHelp:UFW|中文(简体)| [[::UFW/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:UFW | UbuntuHelp:UFW | {{#if: | :}}UFW}}/zh-hant | • {{#if: UbuntuHelp:UFW|中文(繁體)| [[::UFW/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:UFW|:UFW|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :UFW/zh | | {{#ifexist: UFW/zh | | {{#ifeq: {{#titleparts:UFW|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:UFW|1|-1|}} | zh | | }}
- title Uncomplicated Firewall - UFW
Introduction
For an introduction to firewalls, please see Firewall.
UFW - Uncomplicated Firewall
The default firewall configuration tool for Ubuntu is ufw
. Developed to ease iptables
firewall configuration, ufw
provides a user friendly way to create an IPv4 or IPv6 host-based firewall.
UFW by default is initially disabled.
Basic Syntax and Examples
Set Default Rule
Setting the default mode of ufw is recommended before turning it on Set Default Deny:
sudo ufw default deny
Set Default Allow:
sudo ufw default allow
Enable and Disable
Enable ufw
To turn UFW on:
sudo ufw enable
Unless you have used set the default to deny when you initially enable ufw it is in ALLOW mode, and will allow everything incoming and outgoing until you make rulesets.
Disable ufw
To disable ufw use:
sudo ufw disable
Allow and Deny
Allow
sudo ufw allow <port>/<optional: protocol>
example: To allow incoming tcp and udp packet on port 53
sudo ufw allow 53
example: To allow incoming tcp packets on port 53
sudo ufw allow 53/tcp
example: To allow incoming udp packes on port 53
sudo ufw allow 53/udp
Deny
sudo ufw deny <port>/<optional: protocol>
example: To deny tcp and udp packets on port 53
sudo ufw deny 53
example: To deny incoming tcp packets on port 53
sudo ufw deny 53/tcp
example: To deny incoming udp packets on port 53
sudo ufw deny 53/udp
Delete Existing Rule
To delete a rule, simply prefix the original rule with delete. For example, if the original rule was:
ufw deny 80/tcp
Use this to delete it:
sudo ufw delete deny 80/tcp
Services
You can also allow or deny by service name since ufw reads from /etc/services To see get a list of services:
cat /etc/services | less
Allow by Service Name
sudo ufw allow <service name>
example: to allow ssh by name
sudo ufw allow ssh
Deny by Service Name
sudo ufw deny <service name>
example: to deny ssh by name
sudo ufw deny ssh
Status
Checking the status of ufw will tell you if ufw is enabled or disabled and also list the current ufw rules that are applied to your iptables. To check the status of ufw:
sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 22:tcp ALLOW 192.168.0.0/24 22:udp ALLOW 192.168.0.0/24
if ufw was not enabled the output would be:
sudo ufw status Status: inactive
Logging
To enable logging use:
ufw logging on
To disable logging use:
ufw logging off
Advanced Syntax
You can also use a fuller syntax, specifying the source and destination addresses and ports.
Allow Access
This section shows how to allow specific access.
Allow by Specific IP
sudo ufw allow <ip address>
example:To allow packets from 207.46.232.182:
sudo ufw allow from 207.46.232.182
Allow by Subnet
You may use a net mask :
sudo ufw allow from 192.168.1.0/24
Allow by specific port and IP address
sudo ufw allow from <ip address> to <protocol> port <port number>
example: allow ip address 192.168.0.4 access to port 22 for all protocols
sudo ufw allow from 192.168.0.4 to any port 22
Enable PING
Note: Security by obscurity may be of very little actual benefit with modern cracker scripts. By default, UFW blocks ping requests. You may find you wish to enable ping to diagnose networking problems. You need to edit /etc/ufw/before.rules and remove the commment on this line (remove the # in the front)
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT
Deny Access
Deny by specific IP
sudo ufw deny from <ip address>
example:To block packets from 207.46.232.182:
sudo ufw deny from 207.46.232.182
Deny by specific port and IP address
sudo ufw deny from <ip address> to <protocol> port <port number>
example: deny ip address 192.168.0.1 access to port 22 for all protocols
sudo ufw deny from 192.168.0.1 to any port 22
Advanced Blocking Rules
Blocking IP addresses is not so straight forward if you have an existing set of rules as IPTABLES matches in order. So if you started with default deny and added in port 80 for a public server :
sudo ufw allow 80
But then find IP address 111.222.3.44 is hacking your server :
sudo ufw deny 111.222.3.44
will do nothing (you allowed access with your first rule). You need to edit /etc/ufw/before.rules and add a section "Block IP" after "Drop INVALID packets" :
-A ufw-before-input -s 111.222.3.44 -j DROP #Assuming no loging is desired of course) # drop INVALID packets # uncomment to log INVALID packets #-A ufw-before-input -m conntrack --ctstate INVALID -j LOG --log-prefix "[UFW B$ -A ufw-before-input -m conntrack --ctstate INVALID -j DROP # Block IP # This it is efective :) -A ufw-before-input -s 111.222.3.44 -j DROP
Advanced Example
Scenario: You want to block access to port 22 from 192.168.0.1 and 192.168.0.7 but allow all other 192.168.0.x IPs to have access to port 22
sudo ufw deny from 192.168.0.1 to any port 22 sudo ufw deny from 192.168.0.7 to any port 22 sudo ufw allow from 192.168.0.0/24 to any port 22
This puts the specific rules first and the generic second. Once a rule is matched the others will not be evaluated (see manual below) so you must put the specific rules first. As rules change you may need to delete old rules to ensure that new rules are put in the proper order. To check your rules orders you can check the status; for the scenario the output below is the desired output for the rules to work properly
sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 22:tcp ALLOW 192.168.0.0/24 22:udp ALLOW 192.168.0.0/24
Scenario change: You want to block access to port 22 to 192.168.0.3 as well as 192.168.0.1 and 192.168.0.7.
sudo ufw delete allow from 192.168.0.0/24 to any port 22 sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 sudo ufw deny 192.168.0.3 to any port 22 sudo ufw allow 192.168.0.0/24 to any port 22 sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 22:tcp DENY 192.168.0.3 22:udp DENY 192.168.0.3 22:tcp ALLOW 192.168.0.0/24 22:udp ALLOW 192.168.0.0/24
If you simply add the deny rule the allow would have been above it and been applied instead of the deny
Other Resources
- For instructions on using ufw first see the official server guide.
- The most recent syntax and manual can be retrieved by getting the man page. Otherwise open a terminal window and type:
man ufw
- Firewall - wiki homepage for firewall related documentation.
- Iptables - interface to the netfilter subsystem in the Linux kernel.
- UbuntuWiki:UbuntuFirewall - UFW Project wiki page.