การเปิดปิด Port Firewall ใน CentOS

wk
1 min readFeb 5, 2020

--

ตัวอย่างการเปิดปิด Port HTTP 8000 ใน CentOS ด้วยคำสั่ง firewall-cmd

1. ติดตั้ง CentOS ด้วย Vagrant

  • เพิ่มไฟล์ Vagrantfile
  • Boot VM ด้วยคำสั่ง vagrant up
  • Login เข้า CentOS ด้วยคำสั่ง vagrant ssh

2. เปิดใช้งาน Firewall

sudo yum install -y firewalld
sudo systemctl enable firewalld
sudo systemctl start firewall

3. เปิด Port

sudo firewall-cmd --permanent --add-port=8000/tcp
sudo firewall-cmd --reload

ทดสอบ โดยเปิด HTTP Server ใน CentOS

python -m SimpleHTTPServer 8000

ลอง Connect Port 8000 จากเครื่อง Client ด้วย curl จะเห็นว่าโปรแกรม สามารถ Connect ไปยัง Server ผ่าน Port 8000 และแสดงผลลัทธ์ ดังภาพ

4. ปิด Port

sudo firewall-cmd --remove-port=8000/tcp
sudo firewall-cmd --runtime-to-permanent
sudo firewall-cmd --reload

ทดสอบ จากเครื่อง Client อีกครั้ง ควาวนี้จะเกิด Error Connection refused เนื่องจาก Port ถูกปิดไปแล้ว

5. แสดง Port ที่เปิดอยู่

sudo firewall-cmd --list-allpublic (active)
target: default
icmp-block-inversion: no
interfaces: ens192
sources:
services: dhcpv6-client http https ssh
ports: 80/tcp 443/tcp 3306/tcp 8080/tcp 4000/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

--

--

No responses yet