Cara Mengkonfigurasi iptables di CentOs
首先确认CentOS版本,CentOS 6使用iptables默认,CentOS 7/8使用firewalld,默认需安装iptables-services以启用传统iptables;2. 在CentOS 7/8上需停止并禁用firewalld,安装iptables-services后启动并启用iptables服务;3. 使用iptables命令配置规则,包括查看规则、清空规则、设置默认策略、允许回环、已建立连接、SSH、HTTP、HTTPS及ICMP流量;4. 通过service iptables save命令保存规则至/etc/sysconfig/iptables以确保重启后生效;5. 可选使用iptables-restore从文件恢复规则,需确保文件格式正确;6. 可编写脚本自动化配置基本安全规则并保存;7. 操作时需谨慎避免SSH断连,建议通过控制台备份访问方式,频繁验证规则并定期备份配置文件,正确配置后iptables仍为强大可靠的防火墙工具。
Configuring iptables in CentOS depends on the version you're using, as newer versions (CentOS 7 and later) use firewalld
by default instead of directly managing iptables
. However, you can still configure iptables
manually if needed. Below is a guide for both scenarios: using iptables-service
(for traditional iptables) and managing rules directly.

1. Check Your CentOS Version and Default Firewall
First, confirm your CentOS version:
cat /etc/centos-release
- CentOS 6: Uses
iptables
by default. - CentOS 7/8: Uses
firewalld
, butiptables
is still available.
If you want to use classic iptables
instead of firewalld
, disable firewalld
and install iptables-services
.

2. Switch to Iptables (on CentOS 7 or 8)
Stop and disable firewalld:
sudo systemctl stop firewalld sudo systemctl disable firewalld
Install iptables-services:
sudo yum install iptables-services -y
On CentOS 8, use
dnf
instead ofyum
:sudo dnf install iptables-services -y
Start and enable iptables:
sudo systemctl start iptables sudo systemctl enable iptables
Now the system will use traditional iptables
at boot.

3. Basic Iptables Configuration
You can manage rules using the iptables
command. Here are common examples:
View current rules:
sudo iptables -L -n -v
Flush all rules (be careful):
sudo iptables -F
⚠️ Warning: This removes all rules. You may lose SSH access if not careful.
Set default policies:
sudo iptables -P INPUT DROP sudo iptables -P FORWARD DROP sudo iptables -P OUTPUT ACCEPT
This sets a default deny policy on incoming and forwarded traffic.
Allow loopback traffic:
sudo iptables -A INPUT -i lo -j ACCEPT sudo iptables -A OUTPUT -o lo -j ACCEPT
Allow established connections:
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Allow SSH (port 22):
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Tip: Change the SSH port first if possible, then adjust the rule accordingly for security.
Allow HTTP and HTTPS:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Allow ping (ICMP):
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
4. Save Iptables Rules
Rules added with iptables
commands are temporary unless saved.
On CentOS 7/8 with iptables-service:
Save rules:
sudo service iptables save
Or:
sudo /usr/libexec/iptables/iptables.init save
This saves rules to /etc/sysconfig/iptables
.
On CentOS 6:
sudo service iptables save
Rules are automatically saved to /etc/sysconfig/iptables
.
5. Restore Rules from File (Optional)
If you have a rules file:
sudo iptables-restore < /etc/sysconfig/iptables
Make sure the format is correct (use iptables-save
to generate it).
6. Example: Basic Secure Iptables Script
Create a script to set up basic rules:
#!/bin/bash # Flush existing rules iptables -F iptables -X # Set default policies iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # Allow loopback iptables -A INPUT -i lo -j ACCEPT # Allow established connections iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow SSH (change port if needed) iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow HTTP/HTTPS iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # Allow ICMP (ping) iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT # Log dropped packets (optional) iptables -A INPUT -j LOG --log-prefix "IPTables-Dropped: " # Save rules service iptables save echo "Firewall configured and saved."
Save it as firewall.sh
, make it executable, and run:
chmod +x firewall.sh sudo ./firewall.sh
Test carefully—especially SSH rules—to avoid lockout.
7. Troubleshooting Tips
- If you get locked out, use the console (via VPS provider) to restore access.
- Always allow SSH before setting
INPUT DROP
. - Use
iptables -L -n -v
frequently to verify rules. - Backup
/etc/sysconfig/iptables
regularly.
Basically, while CentOS 7+ defaults to firewalld
, switching to classic iptables
gives you fine-grained control. Just remember to save your rules and avoid dropping SSH access. With proper care, iptables
remains a powerful and reliable firewall tool.
Atas ialah kandungan terperinci Cara Mengkonfigurasi iptables di CentOs. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undress AI Tool
Gambar buka pakaian secara percuma

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Stock Market GPT
Penyelidikan pelaburan dikuasakan AI untuk keputusan yang lebih bijak

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



ToConfigureanntpseronCentosconingChrony, firstInstallandenableChronyDWithSudoyMinstallChrony-y, thenstartandenableitviasystemctlstartChRonyDandSystemCtLenableChronyD;

Pertama, sahkan versi CentOS. CentOS6 menggunakan iptables secara lalai. Centos7/8 menggunakan Firewalld. Secara lalai, perkhidmatan iptables mesti dipasang untuk membolehkan iptables tradisional; 2. Pada Centos7/8, firewalld mesti dihentikan dan dilumpuhkan. Selepas memasang perkhidmatan iptables, mulakan dan aktifkan perkhidmatan iptables; 3. Gunakan arahan iptables untuk mengkonfigurasi peraturan, termasuk peraturan tontonan, peraturan penjelasan, menetapkan dasar lalai, yang membolehkan loopbacks, sambungan yang ditetapkan, SSH, HTTP, HTTPS dan trafik ICMP; 4. Lulus ServiceIptableSave

InstallCifs-utilsandoptionallysamba-clientusingyumordnf.2.CreateamountPointlike/mnt/windows-share.3.ManuallymountTheWindowsShareingthemountTheMountTheMountTheMountTheMountThemountTheMountTheMountThemountTheMountThemountThemountTheMountThemountThemountThemountThemountThemountThemountTheMandWithWithWithPropriate,

Gunakan kucing/etc/centos-release untuk melihat versi CentOS secara langsung, dan output seperti centoslinuxrelease7.9.2009 (teras); 2. Gunakan CAT/ETC/OS-RELEASE untuk menjadi kaedah standard silang platform, dan versi dikenal pasti melalui medan versi, version_id atau cantik_name; 3. Gunakan HostNamectl untuk memaparkan maklumat sistem termasuk versi sistem operasi, di mana garis "Operatingsystem" menunjukkan versi CentOS; 4. Dalam sistem yang lebih tua seperti centos6 dan versi terdahulu, rpm-qcentos-releas boleh digunakan untuk digunakan

Mengenal pastiThenetworkInterfaceing "iPaddr", 2.EditTheConfigurationFile "/etc/sysconfig/rangkaian-skrip/ifcfg-" withatexteditor, 3.SetBootProto = staticandConfigureipaddr, netmask, dandnssettings, 4.restarThenetWorks

Kemas kini sistem dan mulakan semula; 2. Tetapkan nama domain penuh sebagai nama tuan rumah; 3. Muat turun dan jalankan skrip pemasangan CWP (CentOS7/8 sepadan dengan versi yang berbeza); 4. Akses http: // Server IP: 2030 atau: 2082 Log masuk melalui penyemak imbas; 5. Konfigurasikan firewall untuk membuka pelabuhan 2030 dan 2082; 6. Selepas selesai, ubah kata laluan lalai MySQL, melindungi phpMyAdmin, konfigurasikan nama domain, membolehkan SSL dan buat akaun pengguna biasa; 7. Beri perhatian kepada penyelenggaraan CWP yang tidak aktif, yang hanya disyorkan untuk pembelajaran atau ujian. Persekitaran pengeluaran harus menggunakan alternatif yang lebih selamat seperti ISPConfig atau cyberpanel, dan langkah -langkah keselamatan sistem mesti diperkuat, seperti mengemas kini sistem, menggunakan kata laluan yang kuat, dan melumpuhkan root

UseryncforfullsystembackupsbymountingAtargetDriveArdrunningRsyncwitharchive, ACL, andExtendedAttributePreservationWhileExcludingVirtualFileSystems, thenunmountaFterCompletion.2.CreateCompressedSystemBackWithTarbySwithTem

Untuk memasang dan mengkonfigurasi pelayan VNC pada CentOS7, anda mesti terlebih dahulu mengemas kini sistem dan memasang tigervnc, 1. Buat pengguna bukan akar dan tetapkan kata laluan VNC, 2. Menggunakan terowong SSH untuk meningkatkan keselamatan. Selepas melengkapkan konfigurasi, antara muka grafik boleh diakses dari jauh melalui klien VNC, dan keseluruhan proses memastikan fungsi keselamatan dan lengkap.
