在 Centos 7 或 RHEL 7 或 Fedora 中防火牆由 firewalld 來管理,而不是 iptables。
一、firewalld 防火牆
語法命令如下:啟用區域端口和協議組合
firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]
此舉將啟用端口和協議的組合。
端口可以是一個單獨的端口 <port> 或者是一個端口范圍 <port>-<port>。
協議可以是 tcp 或 udp。
查看 firewalld 狀態
systemctl status firewalld
開放端口:
// --permanent 永久生效,沒有此參數重啟后失效 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=public --add-port=1000-2000/tcp --permanent
重新加載:
firewall-cmd --reload
查看端口號是否開啟:
firewall-cmd --zone=public --query-port=80/tcp
刪除端口號:
firewall-cmd --zone=public --remove-port=80/tcp --permanent
二、iptables 防火牆
也可以還原傳統的管理方式使用 iptables
systemctl stop firewalld
systemctl mask firewalld
安裝 iptables-services
yum install iptables-services
設置開機啟動
systemctl enable iptables
操作命令
systemctl stop iptables
systemctl start iptables
systemctl restart iptables
systemctl reload iptables
保存設置
service iptables save
開放某個端口 在 /etc/sysconfig/iptables 里添加
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
三、設置mysql
1.修改/etc/my.cnf 文件
找到bind-address = 127.0.0.1這一行
改為bind-address = 0.0.0.0即可
2.新建用戶遠程連接mysql數據庫
grant all on *.* to admin@'%' identified by '123456' with grant option;
flush privileges;
允許任何ip地址(%表示允許任何ip地址)的電腦用admin帳戶和密碼(123456)來訪問這個mysql server。
注意admin賬戶不一定要存在。
3.支持root用戶允許遠程連接mysql數據庫
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;