CentOS 7 คอนฟิก Firewall เบื้องต้นด้วย firewall-cmd

สิ่งที่เปลี่ยนไปอีกอย่างใน CentOS 7 หรือ Red Hat Enterprise 7 เมื่อเทียบกับเวอร์ชันเดิม (5, 6) คือเปลี่ยนมาใช้ firewalld เพื่อช่วยให้การคอนฟิก firewall ในลีนุกซ์ทำได้ง่ายขึ้น โดยมีการจัดแบ่งเป็นโซน (zone) จัดกลุ่มพอร์ต (port) เป็นเซอร์วิส (service) และอื่นๆ

จริงๆ แล้ว เบื้องหลัง firewalld ก็ไปเรียกคำสั่ง iptables เพื่อใช้คอนฟิก Netfilter ซึ่งเป็นโมดูลอยู่ในเคอร์เนลลีนุกซ์ ในการจัดการควบคุมแพ็กเกจ (packet filtering) เข้าออกเครื่อง

ในที่นี้ขอยกตัวอย่างการใช้คำสั่ง firewall-cmd เพื่อเพิ่มเซอร์วิสเช่น http ให้เครื่องอื่น สามารถมาเรียกใช้เซอร์วิสเว็บในเครื่องของเราได้

firewalld จะติดตั้งมาให้โดยดีฟอลต์อยู่แล้ว และโดยคอนฟิกดีฟอลต์จะอนุญาตให้เครื่องอื่นๆ เรียกใช้เซอร์วิสที่รันอยู่ในเครื่องของเราได้แค่ ssh เท่านั้น คือให้เครื่องอื่นๆ สามารถรีโมดเข้ามาเครื่องเราโดยใช้คำสั่งหรือโปรแกรมประเภท ssh เท่านั้น

วิธีการคอนฟิก firewalld สามารถทำได้โดยสองแบบคือ

  • firewall-config เป็นโปรแกรมกราฟิกรันบน X Window จำเป็นต้องลง GNOME หรือ KDE
  • firewall-cmd รันเป็นคำสั่งรูปแบบเท็กซ์ ซึ่งจะใช้ในบทความนี้

ก่อนอื่น ลองตรวจสอบสถานะการรัน firewalld บนเครื่อง

ใช้คำสั่ง systemctl status เพื่อดูสถานะการรันของ firewalld

[root@cent7 ~]# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Sun 2015-06-28 21:24:24 ICT; 59min ago
 Main PID: 421 (firewalld)
   CGroup: /system.slice/firewalld.service
     └─421 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jun 28 21:24:23 cent7.example.com systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 28 21:24:24 cent7.example.com systemd[1]: Started firewalld - dynamic firewall daemon.

แสดงสถานะ active (running) คือยังทำงานอยู่

หรือจะใช้คำสั่ง firewall-cmd ระบุออปชัน –state เพื่อดูสถานะการรันของ firewalld

[root@cent7 ~]# firewall-cmd --state
running

โดยดีฟอลต์ จะมีการจัดกลุ่มโซน (zone) ให้ทุกอินเตอร์เฟซ (interface) ของเครื่องอยู่ในกลุ่ม public

[root@cent7 ~]# firewall-cmd --get-default-zone
public

ใช้คำสั่ง firewall-cmd ออปชัน –get-active-zones เพื่อดูโซนและอินเตอร์เฟซที่มีการคอนฟิกไว้

[root@cent7 ~]# firewall-cmd --get-active-zones
public
  interfaces: enp0s3 enp0s8

จากตัวอย่างข้างบน เครื่องนี้มีอยู่สองอินเตอร์เฟซ enp0s3, enp0s8 ซึ่งทั้งสองจัดอยู่ในโซน public

ใช้คำสั่ง firewall-cmd ระบุออปชัน –list-services เพื่อดูสถานะการอนุญาตให้มีการเรียกใช้เซอร์วิสของโซนที่ระบุในออปชัน –zone

[root@cent7 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client ssh

โดยดีฟอลต์จะอนุญาตให้ใช้เซอร์วิส ssh เข้ามาทางอินเตอร์เฟซที่อยู่ในโซน public เท่านั้น

หมายเหตุ ในที่นี้ขอไม่อธิบายเซอร์วิส dhcpv6-client

หากต้องการเพิ่มให้สามารถใช้เซอร์วิส http ของเครื่องเราในโซนที่ระบุได้ ก็สามารถทำได้โดยระบุออปชัน –add-service

[root@cent7 ~]# firewall-cmd --zone=public --add-service=http
success

หากไม่ทราบชื่อเซอร์วิส สามารถใช้คำสั่ง firewall-cmd ระบุออปชัน –get-services เพื่อดูรายชื่อเซอร์วิสที่กำหนดไว้ได้

[root@cent7 ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https

หากต้องการดูรายละเอียดของเซอร์วิส ก็ดูได้จากไฟล์ .xml ที่อยู่ในไดเรกทอรี /usr/lib/firewalld/services/

ตัวอย่างเช่น เซอร์วิส http จะหมายถึง tcp port 80

[root@cent7 ~]# cat /usr/lib/firewalld/services/http.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>WWW (HTTP)</short>
  <description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description>
  <port protocol="tcp" port="80"/>
</service>

หลังจากการเพิ่มเซอร์วิสแล้ว ใช้ออปชัน –list-services เพื่อดูคอนฟิกเซอร์วิสปัจจุบันของโซนที่เปิดไว้

[root@cent7 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client http ssh

สังเกตว่าจะมีชื่อเซอร์วิส http เพิ่มเข้ามา

ณ ตอนนี้ เครื่องอื่นๆ ก็สามารถเข้ามาเรียกใช้เซอร์วิส http หรือเว็บที่รันอยู่ในเครื่องของเราได้แล้ว

คอนฟิก firewall ที่แก้ไขไป จะอยู่ชั่วคราวเท่านั้น ถ้ามีการรีสตาร์ตเครื่องใหม่ คอนฟิกที่เพิ่ม จะหายไป

ต้องระบุออปชัน –permanent เพื่อให้บันทึกการคอนฟิกลงในไฟล์

[root@cent7 ~]# firewall-cmd --permanent --zone=public --add-service=http
success

ไฟล์คอนฟิกของโซนจะเป็นไฟล์ .xml ในไดเรกทอรี /etc/firewalld/zones/

[root@cent7 ~]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="http"/>
  <service name="ssh"/>
</zone>

ทดลองรีสตาร์ตเครื่องใหม่ คอนฟิกที่แก้ไขไปตอนระบุออปชัน –permanent ก็ยังคงอยู่

แต่ถ้าระบุออปชัน –permanent ตอนเพิ่มเซอร์วิสเลย คอนฟิกที่เพิ่มจะยังไม่มีผล

ตัวอย่างเช่น ระบุออปชัน –permanent ตอนเพิ่มเซอร์วิส https

[root@cent7 ~]# firewall-cmd --permanent --zone=public --add-service=https
success

ลองใช้ออปชัน –list-services แสดงเซอร์วิสที่เปิดอยู่ จะยังไม่มี https

[root@cent7 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client http ssh

แต่คอนฟิกในไฟล์ zones ได้เปลี่ยนไปแล้ว

[root@cent7 ~]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
  <zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="http"/>
  <service name="ssh"/>
  <service name="https"/>
</zone>

ถ้าต้องการให้คอนฟิกที่เพิ่มด้วยออปชัน –permanent มีผล ต้องรันคำสั่ง โดยการระบุออปชัน –reload

[root@cent7 ~]# firewall-cmd --reload
success

หลังการรีโหลดคอนฟิก

[root@cent7 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client http https ssh

หากต้องการลบเซอร์วิสออก ก็ทำโดยระบุออปชัน –remove-service

[root@cent7 ~]# firewall-cmd --zone=public --remove-service https
success
[root@cent7 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client http ssh

เช่นเดียวกัน หากต้องการบันทึกการแก้ไขลงในไฟล์คอนฟิก firewall ก็ต้องระบุออปชัน –permanent

[root@cent7 ~]# firewall-cmd --permanent --zone=public --remove-service https
success

ข้อมูลอ้างอิง

One thought on “CentOS 7 คอนฟิก Firewall เบื้องต้นด้วย firewall-cmd”

  1. ขอบคุณมากครับ ทำบทความดีๆออกมาให้ศึกษาอีกนะครับ

Leave a Reply

Your email address will not be published.