{"id":47270,"date":"2026-06-27T22:43:40","date_gmt":"2026-06-27T13:43:40","guid":{"rendered":"https:\/\/raybeam.jp\/docs\/initial-server-setup\/how-to-auto-update-ufw-rules-with-cloudflare-ips\/"},"modified":"2026-06-27T22:44:20","modified_gmt":"2026-06-27T13:44:20","slug":"cach-tu-dong-cap-nhat-quy-tac-ufw-voi-cac-dia-chi-ip-cua-cloudflare","status":"publish","type":"docs","link":"https:\/\/raybeam.jp\/vi\/docs\/initial-server-setup\/how-to-auto-update-ufw-rules-with-cloudflare-ips\/","title":{"rendered":"C\u00e1ch t\u1ef1 \u0111\u1ed9ng c\u1eadp nh\u1eadt c\u00e1c quy t\u1eafc UFW v\u1edbi c\u00e1c \u0111\u1ecba ch\u1ec9 IP Cloudflare"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Khi b\u1ea1n ng\u1ed3i sau Cloudflare, b\u1ea1n n\u00ean <strong>ch\u1ec9<\/strong> cho ph\u00e9p l\u01b0u l\u01b0\u1ee3ng truy c\u1eadp t\u1eeb c\u00e1c d\u1ea3i \u0111\u1ecba ch\u1ec9 IP c\u1ee7a Cloudflare qua c\u00e1c c\u1ed5ng 80 v\u00e0 443. Cloudflare c\u1eadp nh\u1eadt c\u00e1c d\u1ea3i \u0111\u1ecba ch\u1ec9 n\u00e0y theo \u0111\u1ecbnh k\u1ef3, do \u0111\u00f3 c\u00e1c quy t\u1eafc UFW c\u1ea7n \u0111\u01b0\u1ee3c t\u1ef1 \u0111\u1ed9ng l\u00e0m m\u1edbi. T\u1eadp l\u1ec7nh t\u1eeb <strong><a href=\"https:\/\/github.com\/Paul-Reed\/cloudflare-ufw\">Cloudflare-UFW<\/a><\/strong>, c\u1ee7a Paul Reed, ch\u00ednh x\u00e1c l\u00e0 nh\u01b0 v\u1eady.<\/p>\n\n\n\n<h2 id=\"1-download-and-make-the-script-executable\" class=\"wp-block-heading\">1. T\u1ea3i xu\u1ed1ng v\u00e0 thi\u1ebft l\u1eadp quy\u1ec1n th\u1ef1c thi cho t\u1eadp l\u1ec7nh<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo wget -O \/usr\/local\/bin\/cloudflare-ufw.sh \\\n  https:\/\/raw.githubusercontent.com\/Paul-Reed\/cloudflare-ufw\/refs\/heads\/master\/cloudflare-ufw.sh\nsudo chmod +x \/usr\/local\/bin\/cloudflare-ufw.sh\n<\/code><\/code><\/pre>\n\n\n\n<h2 id=\"2-inspect-current-ufw-rules-before-changes\" class=\"wp-block-heading\">2. Ki\u1ec3m tra c\u00e1c quy \u0111\u1ecbnh hi\u1ec7n h\u00e0nh c\u1ee7a UFW (tr\u01b0\u1edbc khi c\u00f3 s\u1ef1 thay \u0111\u1ed5i)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo ufw status verbose\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">B\u1ea1n c\u00f3 th\u1ec3 s\u1ebd th\u1ea5y n\u1ed9i dung nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text<code>H\u00e0nh \u0111\u1ed9ng T\u1eeb\n-- ------ ----\n22\/tcp CHO PH\u00c9P T\u1eeb m\u1ecdi n\u01a1i\n80\/tcp CHO PH\u00c9P T\u1eeb m\u1ecdi n\u01a1i\n443 CHO PH\u00c9P T\u1eeb m\u1ecdi n\u01a1i\n...\n<\/code><\/code><\/pre>\n\n\n\n<h2 id=\"3-remove-the-overlypermissive-80443-rules\" class=\"wp-block-heading\">3. Lo\u1ea1i b\u1ecf c\u00e1c quy t\u1eafc 80\/443 qu\u00e1 tho\u00e1ng<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hi\u1ec7n t\u1ea1i, c\u00e1c quy \u0111\u1ecbnh n\u00e0y cho ph\u00e9p <em>b\u1ea5t k\u1ef3<\/em> \u0110\u1ecba ch\u1ec9 IP \u0111\u1ec3 truy c\u1eadp c\u00e1c c\u1ed5ng web c\u1ee7a b\u1ea1n. Ch\u00fang t\u00f4i s\u1ebd thay th\u1ebf ch\u00fang b\u1eb1ng c\u00e1c quy t\u1eafc ch\u1ec9 \u00e1p d\u1ee5ng cho Cloudflare.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo ufw delete allow 80\/tcp\nsudo ufw delete allow 443\/tcp\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu UFW y\u00eau c\u1ea7u x\u00e1c nh\u1eadn, h\u00e3y nh\u1eadp <code>y<\/code> v\u00e0 nh\u1ea5n ph\u00edm Enter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ki\u1ec3m tra l\u1ea1i tr\u1ea1ng th\u00e1i:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo ufw status verbose\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">B\u1ea1n s\u1ebd kh\u00f4ng c\u00f2n th\u1ea5y <code>80\/TCP<\/code> ho\u1eb7c <code>443<\/code> c\u00f9ng v\u1edbi <code>CHO PH\u00c9P<\/code> t\u1eeb <code>B\u1ea5t c\u1ee9 \u0111\u00e2u<\/code>.<\/p>\n\n\n\n<h2 id=\"4-run-the-cloudflareufw-script\" class=\"wp-block-heading\">4. Ch\u1ea1y t\u1eadp l\u1ec7nh Cloudflare\u2011UFW<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo \/usr\/local\/bin\/cloudflare-ufw.sh\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ch\u1ee9c n\u0103ng c\u1ee7a t\u1eadp l\u1ec7nh:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>L\u1ea5y c\u00e1c d\u1ea3i \u0111\u1ecba ch\u1ec9 IPv4 v\u00e0 IPv6 m\u1edbi nh\u1ea5t c\u1ee7a Cloudflare t\u1eeb <code>https:\/\/www.cloudflare.com\/ips-v4<\/code> V\u00e0 <code>...\/IPv6<\/code>.<\/li>\n\n\n\n<li>X\u00f3a t\u1ea5t c\u1ea3 c\u00e1c quy t\u1eafc UFW hi\u1ec7n c\u00f3 cho c\u00e1c c\u1ed5ng 80 v\u00e0 443 cho ph\u00e9p <code>B\u1ea5t c\u1ee9 \u0111\u00e2u<\/code>.<\/li>\n\n\n\n<li>Th\u00eam m\u1edbi <code>CHO PH\u00c9P<\/code> quy t\u1eafc <strong>ch\u1ec9<\/strong> \u0111\u1ed1i v\u1edbi m\u1ed7i d\u1ea3i \u0111\u1ecba ch\u1ec9 IP Cloudflare tr\u00ean c\u1ed5ng 80 (HTTP) v\u00e0 443 (HTTPS).<\/li>\n\n\n\n<li>T\u00f9y ch\u1ecdn: T\u1ea3i l\u1ea1i UFW \u0111\u1ec3 c\u00e1c quy t\u1eafc m\u1edbi c\u00f3 hi\u1ec7u l\u1ef1c ngay l\u1eadp t\u1ee9c.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Sau khi ho\u00e0n t\u1ea5t, h\u00e3y ki\u1ec3m tra:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo ufw status verbose\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">B\u00e2y gi\u1edd b\u1ea1n s\u1ebd th\u1ea5y nhi\u1ec1u d\u00f2ng nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text<code>Ng\u01b0\u1eddi g\u1eedi Ng\u01b0\u1eddi nh\u1eadn\n-- ------ ----\n80\/tcp CHO PH\u00c9P 173.245.48.0\/20\n80\/tcp CHO PH\u00c9P 103.21.244.0\/22\n...\n443 CHO PH\u00c9P 173.245.48.0\/20\n443 CHO PH\u00c9P 103.21.244.0\/22\n...\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">T\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ecba ch\u1ec9 IP kh\u00e1c \u0111\u1ec1u b\u1ecb t\u1eeb ch\u1ed1i truy c\u1eadp v\u00e0o c\u00e1c c\u1ed5ng 80\/443 theo m\u1eb7c \u0111\u1ecbnh.<\/p>\n\n\n\n<h2 id=\"5-automate-daily-updates-with-cron\" class=\"wp-block-heading\">5. T\u1ef1 \u0111\u1ed9ng h\u00f3a vi\u1ec7c c\u1eadp nh\u1eadt h\u00e0ng ng\u00e0y b\u1eb1ng cron<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Cloudflare c\u00f3 th\u1ec3 thay \u0111\u1ed5i d\u1ea3i \u0111\u1ecba ch\u1ec9 IP c\u1ee7a m\u00ecnh b\u1ea5t c\u1ee9 l\u00fac n\u00e0o. \u0110\u1ec3 \u0111\u1ea3m b\u1ea3o t\u01b0\u1eddng l\u1eeda c\u1ee7a b\u1ea1n lu\u00f4n \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt, h\u00e3y l\u00ean l\u1ecbch ch\u1ea1y t\u1eadp l\u1ec7nh n\u00e0y h\u00e0ng ng\u00e0y (v\u00ed d\u1ee5: l\u00fac 03:00).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ch\u1ec9nh s\u1eeda t\u1ec7p crontab g\u1ed1c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo crontab -e\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u, h\u00e3y ch\u1ecdn tr\u00ecnh so\u1ea1n th\u1ea3o \u01b0a th\u00edch c\u1ee7a b\u1ea1n (v\u00ed d\u1ee5:, <code>nano<\/code>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Th\u00eam d\u00f2ng sau v\u00e0o cu\u1ed1i:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text<code>0 3 * * * \/usr\/local\/bin\/cloudflare-ufw.sh &gt;&gt; \/var\/log\/cloudflare-ufw.log 2&gt;&amp;1\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110i\u1ec1u n\u00e0y s\u1ebd:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ch\u1ea1y t\u1eadp l\u1ec7nh n\u00e0y h\u00e0ng ng\u00e0y v\u00e0o l\u00fac 03:00 theo gi\u1edd m\u00e1y ch\u1ee7.<\/li>\n\n\n\n<li>Ghi k\u1ebft qu\u1ea3 v\u00e0 th\u00f4ng b\u00e1o l\u1ed7i v\u00e0o <code>\/var\/log\/cloudflare-ufw.log<\/code> \u0111\u1ec3 ki\u1ec3m to\u00e1n.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">L\u01b0u v\u00e0 tho\u00e1t kh\u1ecfi tr\u00ecnh so\u1ea1n th\u1ea3o.<\/p>\n\n\n\n<h2 id=\"6-verify-the-cron-job\" class=\"wp-block-heading\">6. Ki\u1ec3m tra t\u00e1c v\u1ee5 cron<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hi\u1ec3n th\u1ecb danh s\u00e1ch crontab c\u1ee7a ng\u01b0\u1eddi d\u00f9ng root \u0111\u1ec3 x\u00e1c nh\u1eadn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo crontab -l\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">B\u1ea1n s\u1ebd th\u1ea5y d\u00f2ng m\u1edbi.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu mu\u1ed1n, b\u1ea1n c\u00f3 th\u1ec3 bu\u1ed9c h\u1ec7 th\u1ed1ng ch\u1ea1y ngay l\u1eadp t\u1ee9c v\u00e0 ki\u1ec3m tra nh\u1eadt k\u00fd:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo \/usr\/local\/bin\/cloudflare-ufw.sh\nsudo tail -n 20 \/var\/log\/cloudflare-ufw.log\n<\/code><\/code><\/pre>\n\n\n\n<h2 id=\"7-ongoing-maintenance\" class=\"wp-block-heading\">7. B\u1ea3o tr\u00ec th\u01b0\u1eddng xuy\u00ean<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u1ec3 l\u00e0m m\u1edbi c\u00e1c quy t\u1eafc theo c\u00e1ch th\u1ee7 c\u00f4ng b\u1ea5t c\u1ee9 l\u00fac n\u00e0o: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo \/usr\/local\/bin\/cloudflare-ufw.sh<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u1ec3 ki\u1ec3m tra nh\u1eadt k\u00fd: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo less \/var\/log\/cloudflare-ufw.log<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu b\u1ea1n c\u1ea7n kh\u00f4i ph\u1ee5c l\u1ea1i ch\u1ebf \u0111\u1ed9 \u201ccho ph\u00e9p \u1edf m\u1ecdi n\u01a1i\u201d (kh\u00f4ng \u0111\u01b0\u1ee3c khuy\u1ebfn ngh\u1ecb trong m\u00f4i tr\u01b0\u1eddng s\u1ea3n xu\u1ea5t): <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<code>sudo ufw delete allow from any to any port 80 proto tcp sudo ufw delete allow from any to any port 443 proto tcp sudo ufw allow 80\/tcp sudo ufw allow 443\/tcp<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">V\u1edbi c\u1ea5u h\u00ecnh n\u00e0y, t\u01b0\u1eddng l\u1eeda UFW c\u1ee7a b\u1ea1n s\u1ebd ch\u1ec9 ch\u1ea5p nh\u1eadn l\u01b0u l\u01b0\u1ee3ng HTTP\/HTTPS t\u1eeb c\u00e1c d\u1ea3i IP hi\u1ec7n t\u1ea1i c\u1ee7a Cloudflare, gi\u00fap gi\u1ea3m \u0111\u00e1ng k\u1ec3 di\u1ec7n t\u00edch t\u1ea5n c\u00f4ng \u0111\u1ed3ng th\u1eddi \u0111\u1ea3m b\u1ea3o c\u00e1c trang web c\u1ee7a b\u1ea1n v\u1eabn c\u00f3 th\u1ec3 truy c\u1eadp \u0111\u01b0\u1ee3c qua Cloudflare.<\/p>","protected":false},"featured_media":0,"parent":47233,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":0,"_seopress_analysis_target_kw":"","pmpro_default_level":"","nav_title":"","positive":"","negative":"","footnotes":""},"docs_category":[],"class_list":["post-47270","docs","type-docs","status-publish","hentry","pmpro-has-access"],"_links":{"self":[{"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/docs\/47270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/comments?post=47270"}],"version-history":[{"count":1,"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/docs\/47270\/revisions"}],"predecessor-version":[{"id":47271,"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/docs\/47270\/revisions\/47271"}],"up":[{"embeddable":true,"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/docs\/47233"}],"wp:attachment":[{"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/media?parent=47270"}],"wp:term":[{"taxonomy":"docs_category","embeddable":true,"href":"https:\/\/raybeam.jp\/vi\/wp-json\/wp\/v2\/docs_category?post=47270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}