{"id":176354,"date":"2020-04-21T08:24:03","date_gmt":"2020-04-21T00:24:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=176354"},"modified":"2020-04-09T09:25:56","modified_gmt":"2020-04-09T01:25:56","slug":"linux-ufw","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-ufw.html","title":{"rendered":"\u6709\u5173Linux\u9632\u706b\u5899ufw\u7684\u89c1\u89e3\u53ca\u547d\u4ee4"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u4eec\u6765\u7814\u7a76\u4e0b Linux \u4e0a\u7684 ufw\uff08\u7b80\u5355\u9632\u706b\u5899\uff09\uff0c\u4e3a\u4f60\u66f4\u6539\u9632\u706b\u5899\u63d0\u4f9b\u4e00\u4e9b\u89c1\u89e3\u548c\u547d\u4ee4\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

ufw\uff08\u7b80\u5355\u9632\u706b\u5899Uncomplicated FireWall\uff09\u771f\u6b63\u5730\u7b80\u5316\u4e86 iptables\uff0c\u5b83\u4ece\u51fa\u73b0\u7684\u8fd9\u51e0\u5e74\uff0c\u5df2\u7ecf\u6210\u4e3a Ubuntu \u548c Debian \u7b49\u7cfb\u7edf\u4e0a\u7684\u9ed8\u8ba4\u9632\u706b\u5899\u3002\u800c\u4e14 ufw \u51fa\u4e4e\u610f\u6599\u7684\u7b80\u5355\uff0c\u8fd9\u5bf9\u65b0\u7ba1\u7406\u5458\u6765\u8bf4\u662f\u4e00\u4e2a\u798f\u97f3\uff0c\u5426\u5219\u4ed6\u4eec\u53ef\u80fd\u9700\u8981\u6295\u5165\u5927\u91cf\u65f6\u95f4\u6765\u5b66\u4e60\u9632\u706b\u5899\u7ba1\u7406\u3002<\/p>\n

ufw \u4e5f\u6709 GUI \u5ba2\u6237\u7aef\uff08\u4f8b\u5982 gufw\uff09\uff0c\u4f46\u662f ufw \u547d\u4ee4\u901a\u5e38\u5728\u547d\u4ee4\u884c\u4e0a\u6267\u884c\u7684\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u4f7f\u7528 ufw \u7684\u547d\u4ee4\uff0c\u5e76\u7814\u7a76\u4e86\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u3002<\/p>\n

\u9996\u5148\uff0c\u5feb\u901f\u67e5\u770b ufw \u914d\u7f6e\u7684\u65b9\u6cd5\u662f\u67e5\u770b\u5176\u914d\u7f6e\u6587\u4ef6 \u2014\u2014 \/etc\/default\/ufw\u3002\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u5176\u914d\u7f6e\uff0c\u4f7f\u7528 grep \u6765\u6291\u5236\u4e86\u7a7a\u884c\u548c\u6ce8\u91ca\uff08\u4ee5 # \u5f00\u5934\u7684\u884c\uff09\u7684\u663e\u793a\u3002<\/p>\n

$ grep -v '^#\\|^$' \/etc\/default\/ufw\r\nIPV6=yes\r\nDEFAULT_INPUT_POLICY=\"DROP\"\r\nDEFAULT_OUTPUT_POLICY=\"ACCEPT\"\r\nDEFAULT_FORWARD_POLICY=\"DROP\"\r\nDEFAULT_APPLICATION_POLICY=\"SKIP\"\r\nMANAGE_BUILTINS=no\r\nIPT_SYSCTL=\/etc\/ufw\/sysctl.conf\r\nIPT_MODULES=\"nf_conntrack_ftp nf_nat_ftp nf_conntrack_netbios_ns\"<\/pre>\n

\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684\uff0c\u9ed8\u8ba4\u7b56\u7565\u662f\u4e22\u5f03\u8f93\u5165\u4f46\u5141\u8bb8\u8f93\u51fa\u3002\u5141\u8bb8\u4f60\u63a5\u53d7\u7279\u5b9a\u7684\u8fde\u63a5\u7684\u5176\u5b83\u89c4\u5219\u662f\u9700\u8981\u5355\u72ec\u914d\u7f6e\u7684\u3002
\nufw \u547d\u4ee4\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff0c\u4f46\u662f\u8fd9\u4e2a\u6982\u8981\u5e76\u4e0d\u610f\u5473\u7740\u4f60\u53ea\u9700\u8981\u8f93\u5165 ufw \u5c31\u884c\uff0c\u800c\u662f\u4e00\u4e2a\u544a\u8bc9\u4f60\u9700\u8981\u54ea\u4e9b\u53c2\u6570\u7684\u5feb\u901f\u63d0\u793a\u3002<\/p>\n

ufw [--dry-run] [options] [rule syntax]<\/pre>\n

--dry-run \u9009\u9879\u610f\u5473\u7740 ufw \u4e0d\u4f1a\u8fd0\u884c\u4f60\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u4f46\u4f1a\u663e\u793a\u7ed9\u4f60\u5982\u679c\u6267\u884c\u540e\u7684\u7ed3\u679c\u3002\u4f46\u662f\u5b83\u4f1a\u663e\u793a\u5047\u5982\u66f4\u6539\u540e\u7684\u6574\u4e2a\u89c4\u5219\u96c6\uff0c\u56e0\u6b64\u4f60\u8981\u505a\u6709\u597d\u591a\u884c\u8f93\u51fa\u7684\u51c6\u5907\u3002<<\/p>\n

\u8981\u68c0\u67e5 ufw \u7684\u72b6\u6001\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002\u6ce8\u610f\uff0c\u5373\u4f7f\u662f\u8fd9\u4e2a\u547d\u4ee4\u4e5f\u9700\u8981\u4f7f\u7528 sudo \u6216 root \u8d26\u6237\u3002<\/p>\n

$ sudo ufw status\r\nStatus: active\r\n\r\nTo Action From\r\n-- ------ ----\r\n22 ALLOW 192.168.0.0\/24\r\n9090 ALLOW Anywhere\r\n9090 (v6) ALLOW Anywhere (v6)<\/pre>\n

\u5426\u5219\uff0c\u4f60\u4f1a\u770b\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

$ ufw status\r\nERROR: You need to be root to run this script<\/pre>\n

\u52a0\u4e0a verbose \u9009\u9879\u4f1a\u63d0\u4f9b\u4e00\u4e9b\u5176\u5b83\u7ec6\u8282\uff1a<\/p>\n

$ sudo ufw status verbose\r\nStatus: active\r\nLogging: on (low)\r\nDefault: deny (incoming), allow (outgoing), disabled (routed)\r\nNew profiles: skip\r\n\r\nTo Action From\r\n-- ------ ----\r\n22 ALLOW IN 192.168.0.0\/24\r\n9090 ALLOW IN Anywhere\r\n9090 (v6) ALLOW IN Anywhere (v6)<\/pre>\n

\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8f7b\u677e\u5730\u901a\u8fc7\u7aef\u53e3\u53f7\u5141\u8bb8\u548c\u62d2\u7edd\u8fde\u63a5\uff1a<\/p>\n

$ sudo ufw allow 80 <== \u5141\u8bb8 http \u8bbf\u95ee\r\n$ sudo ufw deny 25 <== \u62d2\u7edd smtp \u8bbf\u95ee<\/pre>\n

\u4f60\u53ef\u4ee5\u67e5\u770b \/etc\/services \u6587\u4ef6\u6765\u627e\u5230\u7aef\u53e3\u53f7\u548c\u670d\u52a1\u540d\u79f0\u4e4b\u95f4\u7684\u8054\u7cfb\u3002<\/p>\n

$ grep 80\/ \/etc\/services\r\nhttp 80\/tcp www # WorldWideWeb HTTP\r\nsocks 1080\/tcp # socks proxy server\r\nsocks 1080\/udp\r\nhttp-alt 8080\/tcp webcache # WWW caching service\r\nhttp-alt 8080\/udp\r\namanda 10080\/tcp # amanda backup services\r\namanda 10080\/udp\r\ncanna 5680\/tcp # cannaserver<\/pre>\n

\u6216\u8005\uff0c\u4f60\u53ef\u4ee5\u547d\u4ee4\u4e2d\u76f4\u63a5\u4f7f\u7528\u670d\u52a1\u7684\u540d\u79f0\u3002<\/p>\n

$ sudo ufw allow http\r\nRule added\r\nRule added (v6)\r\n$ sudo ufw allow https\r\nRule added\r\nRule added (v6)<\/pre>\n

\u8fdb\u884c\u66f4\u6539\u540e\uff0c\u4f60\u5e94\u8be5\u518d\u6b21\u68c0\u67e5\u72b6\u6001\u6765\u67e5\u770b\u662f\u5426\u751f\u6548\uff1a<\/p>\n

$ sudo ufw status\r\nStatus: active\r\n\r\nTo Action From\r\n-- ------ ----\r\n22 ALLOW 192.168.0.0\/24\r\n9090 ALLOW Anywhere\r\n80\/tcp ALLOW Anywhere <==\r\n443\/tcp ALLOW Anywhere <==\r\n9090 (v6) ALLOW Anywhere (v6)\r\n80\/tcp (v6) ALLOW Anywhere (v6) <==\r\n443\/tcp (v6) ALLOW Anywhere (v6) <==<\/pre>\n

ufw \u9075\u5faa\u7684\u89c4\u5219\u5b58\u50a8\u5728 \/etc\/ufw \u76ee\u5f55\u4e2d\u3002\u6ce8\u610f\uff0c\u4f60\u9700\u8981 root \u7528\u6237\u8bbf\u95ee\u6743\u9650\u624d\u80fd\u67e5\u770b\u8fd9\u4e9b\u6587\u4ef6\uff0c\u6bcf\u4e2a\u6587\u4ef6\u90fd\u5305\u542b\u5927\u91cf\u89c4\u5219\u3002<\/p>\n

$ ls -ltr \/etc\/ufw\r\ntotal 48\r\n-rw-r--r-- 1 root root 1391 Aug 15 2017 sysctl.conf\r\n-rw-r----- 1 root root 1004 Aug 17 2017 after.rules\r\n-rw-r----- 1 root root 915 Aug 17 2017 after6.rules\r\n-rw-r----- 1 root root 1130 Jan 5 2018 before.init\r\n-rw-r----- 1 root root 1126 Jan 5 2018 after.init\r\n-rw-r----- 1 root root 2537 Mar 25 2019 before.rules\r\n-rw-r----- 1 root root 6700 Mar 25 2019 before6.rules\r\ndrwxr-xr-x 3 root root 4096 Nov 12 08:21 applications.d\r\n-rw-r--r-- 1 root root 313 Mar 18 17:30 ufw.conf\r\n-rw-r----- 1 root root 1711 Mar 19 10:42 user.rules\r\n-rw-r----- 1 root root 1530 Mar 19 10:42 user6.rules<\/pre>\n

\u672c\u6587\u524d\u9762\u6240\u4f5c\u7684\u66f4\u6539\uff0c\u4e3a http \u8bbf\u95ee\u6dfb\u52a0\u4e86\u7aef\u53e3 80 \u548c\u4e3a https \u8bbf\u95ee\u6dfb\u52a0\u4e86\u7aef\u53e3 443\uff0c\u5728 user.rules \u548c user6.rules \u6587\u4ef6\u4e2d\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n

# grep \" 80 \" user*.rules\r\nuser6.rules:### tuple ### allow tcp 80 ::\/0 any ::\/0 in\r\nuser6.rules:-A ufw6-user-input -p tcp --dport 80 -j ACCEPT\r\nuser.rules:### tuple ### allow tcp 80 0.0.0.0\/0 any 0.0.0.0\/0 in\r\nuser.rules:-A ufw-user-input -p tcp --dport 80 -j ACCEPT\r\nYou have new mail in \/var\/mail\/root\r\n# grep 443 user*.rules\r\nuser6.rules:### tuple ### allow tcp 443 ::\/0 any ::\/0 in\r\nuser6.rules:-A ufw6-user-input -p tcp --dport 443 -j ACCEPT\r\nuser.rules:### tuple ### allow tcp 443 0.0.0.0\/0 any 0.0.0.0\/0 in\r\nuser.rules:-A ufw-user-input -p tcp --dport 443 -j ACCEPT<\/pre>\n

\u4f7f\u7528 ufw\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8f7b\u677e\u5730\u963b\u6b62\u6765\u81ea\u4e00\u4e2a IP \u5730\u5740\u7684\u8fde\u63a5\uff1a<\/p>\n

$ sudo ufw deny from 208.176.0.50\r\nRule added<\/pre>\n

status \u547d\u4ee4\u5c06\u663e\u793a\u66f4\u6539\uff1a<\/p>\n

$ sudo ufw status verbose\r\nStatus: active\r\nLogging: on (low)\r\nDefault: deny (incoming), allow (outgoing), disabled (routed)\r\nNew profiles: skip\r\n\r\nTo Action From\r\n-- ------ ----\r\n22 ALLOW IN 192.168.0.0\/24\r\n9090 ALLOW IN Anywhere\r\n80\/tcp ALLOW IN Anywhere\r\n443\/tcp ALLOW IN Anywhere\r\nAnywhere DENY IN 208.176.0.50 <== new\r\n9090 (v6) ALLOW IN Anywhere (v6)\r\n80\/tcp (v6) ALLOW IN Anywhere (v6)\r\n443\/tcp (v6) ALLOW IN Anywhere (v6)<\/pre>\n

\u603b\u800c\u8a00\u4e4b\uff0cufw \u4e0d\u4ec5\u5bb9\u6613\u914d\u7f6e\uff0c\u800c\u4e14\u4e14\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

ufw\uff08\u7b80\u5355\u9632\u706b\u5899Uncomplicated FireWall\uff09\u771f\u6b63\u5730\u7b80\u5316\u4e86 iptables\uff0c\u5b83\u4ece\u51fa\u73b0\u7684\u8fd9\u51e0 […]<\/p>\n","protected":false},"author":323,"featured_media":176247,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-176354","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/176354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=176354"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/176354\/revisions"}],"predecessor-version":[{"id":176357,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/176354\/revisions\/176357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/176247"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=176354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=176354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=176354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}