跳至主要内容

CentOS 7 Firewalld防火墙说明

理论部分

  • firewalld 以 zone 的区域分割观念来建立,并以动态设定方式执行避免中断的问题,不能同时执行 iptables 跟 firewalld ,因为会造成冲突错误

配置文件

相关路径

/etc/firewalld    设定档位置
/usr/bin/:firewall-cmd 指令所在的位置
/usr/lib/firewalld/:firewall预设的设定资料(xml格式)
defalut zone在文档中有指定

有关zone

  • public:公开场所,不信任网域内所有连线,只有被允许的连线才能进入,一般只要设定这里就可以
  • external:公开的场所,应该是IP是NAT的网络
  • dmz:非军事区,允许对外连线,内部网络只有允许的才能进来
  • work:公司、工作的环境
  • home:家庭环境
  • internal:内部网路,应用在NAT设定时的对内网络
  • trusted:接受所有的连线
  • drop:任何进入的封包都丢弃,只有出去的是允许的
  • block:任何进入的封包全部拒绝,并以ICMP回复对方,只有对外的连线是允许的
--------------------------------------
预设主机是被放在public zone区域,并有开启两个服务dhcpv6-client ssh
在这样的预设下,任何来源都可以通过ssh服务连接到本机,但其他服务的service port都关闭

一些操作

显示目前的设定

# firewall-cmd --list-all

关闭DHCP服务

# sudo firewall-cmd --zone=public --remove-service dhcpv6-client

暂时开启DNS port 53

# sudo systemctl start named
# sudo systemctl enable named
# sudo firewall-cmd --add-service=dns
# sudo firewall-cmd --reload
# firewall-cmd --list-all

永久开启DNS port 53 

# sudo firewall-cmd --add-service=dns --permanent
# sudo firewall-cmd --reload

如何修改主机的预设zone 

前面说预设是public zone,但有些服务需要建立在DMZ下,我们可以透过修改 /etc/firewalld/firewalld.conf来将预设的zone改为DNZ
 # sudo vi /etc/firewalld/firewalld.conf    修改DefaultZone=dmz
 # sudo firewall-cmd --reload

加入自行指定的连接埠 

 # sudo firewall-cmd --add-port=8080/tcp --permanent
 # sudo firewall-cmd --reload
 # sudo firewall-cmd --list-all

设定暂时的rules

 # sudo firewall-cmd --add-port=8888/tcp
 # sudo firewall-cmd --list-all
 当你下一次执行reload时候,设定会被取消

修改服务的预设连接埠

 # sudo cp /usr/lib/firewalld/services/http.xml /etc/firewalld/services    以Web为例
 # sudo vi /etc/firewalld/services/http.xml
 修改对应连接埠 port protocol="tcp" port="8080"
 # sudo firewall-cmd --reload

限制某服务只能从哪些IP进入

 # sudo firewall-cmd --add-rich-rule="rule family="ipv4" source
    address="192.168.12.9" port port="8080" protocol="tcp" accept"

直接指定rule到INPUT chain

 # sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp -s "192.168.12.9" --dport 22 -j ACCEPT
 这样的写法使用 # firewall-cmd --list-all是看不到的,要用 iptables -L -n

查看预设载入的rule 

所有的zone设定档会放在 /etc/firewalld/zones和 /usr/lib/firewalld/zones/ ,你所执行的--permanent参数会放在 /etc/firewalld/zones 对应的zone档案里(如:public.xml)

从 /etc/sysconfig/iptables 转为 firewalld 的 direct 

 假设原有的 /etc/sysconfig/iptables 有规则
 -A INPUT -s 140.113.12.9 -j ACCEPT
 -A INPUT -m state --state NEW -m udp -p udp -s 140.113.0.0/16 --dport 123 -j    
  ACCEPT
 -A INPUT -m state --state NEW -m tcp -p tcp -s 140.114.88.0/24 --dport 161 -j ACCEPT
 要转换到 firewalld 的 direct 规则:
 新增 /etc/firewalld/direct.xml ,如果之前有执行过 # sudo firewall-cmd --permanent --direct ...则这个档案会自动生成

新增/修改 direct.xml 增加对应上面的 rules:
 # sudo vi /etc/firewalld/direct.xml
 <?xml version="1.0" encoding="utf-8"?>
<direct>
   
<rule priority="0" table="filter" ipv="ipv4" chain="INPUT">-p tcp -s 192.168.12.9 --dport 22 -j ACCEPT</rule>
   <rule priority="0" table="filter" ipv="ipv4" chain="INPUT">-s 140.113.12.9 -j ACCEPT</rule>
   <rule priority="0" table="filter" ipv="ipv4" chain="INPUT">-p udp -s 140.113.0.0/16 --dport 123 -j ACCEPT</rule>
   <rule priority="0" table="filter" ipv="ipv4" chain="INPUT">-p tcp -s 140.114.88.0/24 --dport 161 -j ACCEPT</rule>
</direct>
 

从zone移除某项服务 

 # sudo firewall-cmd --zone=public --add-service=http --permanent
 # sudo firewall-cmd --zone=public --remove-service=http --permanent

 # sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
 # sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent

port forward 将从某个port number的封包转送给另外的port或其他主机

 将 80 port 收到的讯息转往 port 8080
 # sudo firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=8080

 将 80 port 收到的讯息转往其他主机的 
 # sudo fierwall-cmd --zone="public" -add-forward-port=port=80:proto=tcp:toport=8080:to addr=140.113.1.1
  




参考:

Popular posts from 产品随想的博客

《Elon Musk》Chapter 1 - 10

To anyone I’ve offended, I just want to say, I reinvented electric cars and I’m sending people to Mars in a rocket ship. Did you think I was also going to be a chill, normal dude? ——Elon Musk, Saturday Night Live, May 8, 2021 PROLOGUE: Muse of Fire Near the end of the first week, the boys were divided into two groups and told to attack each other. “It was so insane, mind-blowing,” Musk recalls. Every few years, one of the kids would die. The counselors would recount such stories as warnings. “Don’t be stupid like that dumb fuck who died last year,” they would say. “Don’t be the weak dumb fuck.” 这种氛围有毒 “I realized by then that if someone bullied me, I could punch them very hard in the nose, and then they wouldn’t bully me again. They might beat the shit out of me, but if I had punched them hard in the nose, they wouldn’t come after me again.” 勇于挑战,哈哈哈 In the emergency room, when they were preparing to stitch him up, he resisted being treated until he was promised that the dog would not ...

Steve Jobs: `There's Sanity Returning', 1998

Nobody can doubt the charisma of Steven P. Jobs. The interim CEO of Apple Computer Inc., who returned to the company last July after his ignominious 1985 ouster, has brought back his legendary vision, impatience, and infectious passion for the Macintosh. Jobs spoke to Business Week Correspondent Andy Reinhardt in Apple's stark, fourth-floor boardroom, just after the company rolled out its new software strategy on May 11. Note: This is an extended, online-only version of the Q&A that appears in the May 25, 1998, issue of Business Week. Q: Now that you've introduced the new, bold-looking iMac, are you going to do some radically different products? A: There's a lot of talk about such things -- about handhelds, set-top boxes. A lot of computer companies have been searching for a consumer product. My view is that the personal computer has been the most successful consumer product of the last 10 years. What we have to do, what the industry stopp...

BG投资十问(Baillie Gifford)

  1,公司是否能正面推动经济、社会或者文化发 展? 如果做不到,BG可能就不会再往下看了。比如军火、赌博类公司再便宜,BG不会去看。 2,公司销售额在5年之内能否翻倍? 为什么要讨论销售额?因为BG关注公司成长,销售额是比较简单直观的指标,而利润可有一些方法调节。所以,BG主要先关注销售额,再看现金流。 3,公司5年之后会怎么样? 这是未来5到10年或者5到15年的中间维度,一是看公司的成长空间究竟还有多大,二是考虑5年之后,公司的短期估值会不会下降,会不会变得成熟。 4,公司的竞争优势到底是什么?随着时间会变 强还是变弱? BG认为,如果公司的竞争优势是不断变弱的,将来在更远期挣回现金流的可能性就更低了。 5,公司是否有非常独特的文化?文化是否能够进化? 毕竟要投资这么长时间,对创始人和公司管理文化是非常关注的。特别喜欢创始人领导的公司,而不是说创始人什么都不干,创始人在做房地产,找了一些人在做新公司,要尽量避免这样的公司。 谈到进化在BG看来,大部分公司都会有大公司病,需要看它怎么能维持比较灵活的管理,变大之后还能够保持非常快速的成长,比如亚马逊的day one文化(注:就是始终处在 创业启动状态,充满迷茫和压力,但是充满创造力和颠覆思想 )。 6,公司过去的回报率怎么样? 一方面是考虑到公司的ROE(净资产收益率),二是考虑公司的Margins(利润),看它的过去是不是值得投资,如果每年ROE都不到10个点,可能长期来看也没什么意思。 7,ROE能不能随着时间增长? 有些公司一开始没有盈利,一直在烧钱,5年之后说不定ROE会到20%或者更高,它是一个动态过程。这样的公司BG也会投资,包括一些早期项目,很多公司都是没有盈利的。 8,公司怎么分配资本? 资本现金流无非就是5种方法:再投资、收购、还债、分红、回购股份,要看公司在某个阶段的资本分配是不是最优的。 如果公司明明可以发展更多,结果乱分红了;或者是明明到了特别成熟的时候,还不愿意分红,都是错误的分配资本。 9,公司怎样才能长成5倍? BG关注的是怎样,而不是能不能。这会强迫BG每个人去设想一些未来的可能性,包括公司现在的底层架构能不能够延伸到更多的业务。 比如,早期的亚马逊或者是阿里,连一个概念都没有,BG会考虑这个公司有没有这种机遇,以后去扩展到可触及的市场。 10,市场对公司有哪些误解,哪些事...

ASC代理中国的拉菲+奔富

1)ASC是之前拉菲、奔富的中国独家代理商 2)2019年,拉菲不再由ASC独家代理,分拆成四家 3)奔富:澳洲品牌红酒──奔富酒庄最好的酒是澳洲酒王 4)奔富的包装、取名、型号代称、代理分销方式(高级到低级) 是一个很好的营销案例; 5)奔富的口感,因为酒精浓度高,偏向于白酒的口味,收到商务人士、土豪老板的追捧,因此有爆发的潜能 To Do: 1) ASC旗舰店,观察销量情况 Update: 1)越来越有趣了,依云水的代理商也是ASC

Linux软件升级

示例一:升级SAMBA服务器 1、寻找samba官方网站,找到最快的镜像服务器 2、下载最新软件包和数字签名 3、校验数字签名     #wget http://us1.smaba.org/samba/ftp/smaba_3.0.6.tar.asc 下载数字签名     #wget http://us1.samba.org/samba/ftp/samba-pubkey.asc 下载公钥文件     #gpg --import samba-pubkey.asc 导入公钥文件     #gzip -d samba-3.0.0.tar.gz 解压缩软件包     #gpg --verify samba-3.0.0.tar.asc 校验数字签名 4.编译安装升级     #tar xvf samba-3.0.0.tar     #cd samba-3.0.6     #cd source     #./configure     #make     #make install 示例二:为Kernel打补丁 1.准备好2.4.26版本的内核文件linux-2.4.26.tar.bz2 2.下载2.4.27版本的补丁patch-2.4.27.bz2和md5校验文件 3.打补丁     #tar xvjf linux-2.4.26.tar.bz2     #bzcat patch-2.4.27.bz2|patch -p0 -p0指使用原始路径     #mv linux-2.4.26 linux-2.4.27 4.编译新内核替换旧内核     #cd linux-2.4.27;make menuconfig     #make dep bzImag...

浏览器隐私泄漏

之前很好奇Tor提示不要全屏操作,会暴露你的屏幕大小信息,现在终于知道为什么,原因在于浏览器能够获取到很多基本信息 这个网站可以探索到一些好玩的 BrowserSpy

Rhino_GH从入门到精通听课笔记_二

一. 基础操作   1. Rhino F6精确更改相机视野   2. 中键和左菜单栏弹出时间都是可以自己设置缩短的 二. 软件技巧   1. GH搭配生成楼梯,柱子,栏杆   2. SU\3DS\OBJ 均可被Rhino识别   3. 已命名工作位置很有用,和和已命名是图标是不一样的   4. Rhino等轴测做法          显示——投影——改为平行          旋转改为0/360          视图——等角视图 三. 奇技淫巧   1. 善用微信搜索,比如“地形制作”,第三讲前半部分讲解利用高程点制作地形   2. Rhino转折剖制作方法,用面split模型,即模型被切成两个,类似于佐罗刀   3. 转折剖模型制作时间很久,工作量比较大 四. Rhino命令   1. worksession  链接文件进来,以一个图层显示,类似于SU里面的另存再载入的操作,可       以节省运算量,比ctrl+c ctrl+v快   2. ctrl+w  局部放大   3. hatch  嵌面

CAD 天正与 AI 工作流

基本步骤   1. 天正导出 T3 .dwg 文件,尽量在2007版本   2. AI 打开,打开时候可以选择比例,因为 AI 画布是基于真实尺度   3. AI 打开 .dwg 后,更改画布到所需要的大小   4. 之后用 PS 打开 AI 文件,可以维持想要的大小和比例   5. 而且 AI 可以读取天正的图层 课外了解   1. ETH 风格平面图   2. 日本创新性的停车方式   3. AI中的实时上色功能去了解