linux系统管理技巧-日常基础命令二

linux系统管理技巧-日常基础命令二

SElinux

SElinux是linux系统特有的安全机制。

临时关闭SElinux 命令: setenforce 0

永久关闭需要修改配置文件  /etc/selinux/config

把 SElinux=enforcing  改成   SElinux=disabled

配置完成后,重启系统方可生效。getenforce可获取系统SElinux状态

SElinux三种模式:disabled  关闭状态    enforcing    强制模式          permissive   宽容模式

netfilter

Centos版本(5版本和6版本)防火墙为 netfilter  。Centos7防火墙为firewalld

由于我的机器是Centos7的,所以我先关闭firewalld,开启之前版本的iptables。操作如下:

systemctl stop firewalld      --- 关闭firewalld服务

systemctl disabled firewalld    --- 禁止firewalld服务开机自启

yum install -y iptables-services   安装iptables-services,就可以使用之前版本的iptables

systemctl enable iptables   --- 开机自启

systemctl start iptables    --- 启动iptables服务

这样就启动了这个服务,Centos上默认没有iptables规则,这个规则虽然安全,但对于我们并没有用,反而会照成某些影响,建议先清除规则,然后把清除后的规则保存下。

iptables -nvL 查看规则

[root@localhost ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         

   23  1648 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           

    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22

    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         

    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 15 packets, 1412 bytes)

 pkts bytes target     prot opt in     out     source               destination         

[root@localhost ~]# 

-nvL 代表查看规则,-F选项表示清除当前规则,但清除只是临时的,重启系统或者重启iptables服务还会加载已经保存的规则,所以需要使用 services  iptables  save  保存下规则。

[root@localhost ~]# iptables -F ;service iptables save

iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

[root@localhost ~]# 

防火墙规则保存在 /etc/sysconfig/iptables 文件中

[root@localhost ~]# cat /etc/sysconfig/iptables

Generated by iptables-save v1.4.21 on Thu May 31 19:51:48 2018

*filter

:INPUT ACCEPT [2:116]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [1:156]

COMMIT

Completed on Thu May 31 19:51:48 2018

[root@localhost ~]# iptables -nvL

Chain INPUT (policy ACCEPT 104 packets, 8570 bytes)

 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 61 packets, 6244 bytes)

 pkts bytes target     prot opt in     out     source               destination         

[root@localhost ~]# 

firewalld

Centos7版本上的防火墙

由于我在上面的操作中关闭了firewalld服务。现在开启,如下。

[root@localhost ~]# systemctl disable iptables

Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.

[root@localhost ~]# systemctl stop iptables

[root@localhost ~]# systemctl enable firewalld

Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to       /usr/lib/systemd/system/firewalld.service.

Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to   /usr/lib/systemd/system/firewalld.service.

[root@localhost ~]# systemctl start firewalld

[root@localhost ~]# 

firewalld有二个基础概念

1 . zone    --- 每一个zone里有不同的iptables规则,默认一个九个zone,而Centos7默认的zone为public。

2 . service  九种zone,每个zone中都使用了不同的service,而service就是针对一个服务(端口)做的iptables规则。

获取系统所有的zone

[root@localhost ~]# firewall-cmd --get-zones

block dmz drop external home internal public trusted work

获取系统默认的zone

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

public

[root@localhost ~]#    

zone介绍

drop(丢弃): 任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。

block(限制): 任何接收的网络连接都被 IPV4 的 icmp-host-prohibited 信息和 IPV6 的 icmp6-adm-prohibited 信息所拒绝。

public(公共): 在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收进过选取的连接。

external(外部): 特别是为路由器启动了伪装功能的外部网。你不能信任来自网络的其它计算,不能相信它们不会对你的计算机造成危害,仅能接收经过选择的连接。

dmz(非军事区): 用于你的非军事区内的计算机,此区域可公开访问,可以有限地进入你的内部网络,仅仅接收经过选择的连接。

work(工作): 用于工作区。你基本可以相信网络内的其他计算机不会危害你的计算机。仅仅接收经过选择的连接。

home(家庭): 用于家庭网络。你可以基本上信任网络内的其他计算机不会威胁你的计算机。仅仅接收经过选择的连接。

internal(内部): 用于内部网络。你可以基本上信任网络内的其他计算机。仅仅接收经过选择的连接。

trusted(信任): 可接受所有的网络连接。

zone使用

firewall-cmd –set-default-zone=work —- 设定默认的zone为work

[root@localhost ~]# firewall-cmd --set-default-zone=work

success

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

work

[root@localhost ~]# 

firewall-cmd –get-zone-of-interface=ens33 —- 查看指定网卡所在的zone

[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33

public

[root@localhost ~]# 

firewall-cmd –zone=public -add-interface=lo —- 给指定网卡设置zone

firewall-cmd -zone=dmz –change-interface=lo —- 针对网卡更改zone

firewall-cmd -zone=dmz –remove-interface=lo —- 针对网卡删除zone

firewall-cmd –get-active-zones —- 查看系统所有网卡所在的zone

service使用

列出系统所有的service

firewall-cmd  --get-service

[root@localhost ~]# firewall-cmd  --get-service

RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-     testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nfs3 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server   wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server

[root@localhost ~]# 

这些service都是由一个个配置文件定义的,配置文件模板在 /usr/lib/firewalld/services/ 目录下,而真正生效的配置都在 /etc/firewalld/services 目录下面(默认为空):

[root@localhost ~]# ls /usr/lib/firewalld/services/  | tail

tinc.xml

tor-socks.xml

transmission-client.xml

vdsm.xml

vnc-server.xml

wbem-https.xml

xmpp-bosh.xml

xmpp-client.xml

xmpp-local.xml

xmpp-server.xml

[root@localhost ~]# 

firewall-cmd –list-services —- 查看当前zone下有哪些service

[root@localhost ~]# firewall-cmd --list-service

ssh dhcpv6-client

[root@localhost ~]# 

指定zone查看其下的service

[root@localhost ~]# firewall-cmd --zone=public --list-service

ssh dhcpv6-client

[root@localhost ~]# 

给指定zone增加service

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

success

[root@localhost ~]# firewall-cmd --zone=public --list-service

ssh dhcpv6-client http

[root@localhost ~]# 

这个仅在内存中生效,并没有修改配置文件。如想修改配置文件,则如下操作:

[root@localhost ~]# firewall-cmd --zone=public  --add-service=http  --parmanent

一旦修改了某个配置文件,则会在 /usr/lib/firewalld/zones/下面生成对应的zone配置文件(.xml的后缀名)。这样就修改完成了。

zone的配置文件

配置文件目录为  /usr/lib/firewalld/zones/

[root@localhost ~]# ls /usr/lib/firewalld/zones/

block.xml  dmz.xml  drop.xml  external.xml  home.xml  internal.xml  public.xml  trusted.xml     work.xml

[root@localhost ~]# 

IPtables语法

iptables -t nat -nvL 查看规则

[root@localhost ~]# iptables -t nat -nvL | head

Chain PREROUTING (policy ACCEPT 176 packets, 22617 bytes)

 pkts bytes target     prot opt in     out     source               destination         

  328 40135 PREROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

  328 40135 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

  328 40135 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain INPUT (policy ACCEPT 1 packets, 52 bytes)

 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 19 packets, 1435 bytes)

[root@localhost ~]# 

-t 参数 后接表名        

-n  表示不针对ip反解析主机名   

-L 表示列出

-v 表示列出的信息更加详细

-F 清除所有的规则,如果不加 -t 指定表,默认只清除filter表的规则

-Z表示把包以及流量计数器置零

增加规则

[root@localhost ~]# iptables -A INPUT -s 192.168.1.180 -p tcp --sport 1234 -d 192.168.1.181 --dport 80 -j DROP

参数解析

-A/-D : 表示增加/删除一条规则

-I  :  表示插入一条规则,效果如同A一样

-p :  表示指定协议

--dport : 跟-p 一起使用,表示指定目标端口

--sport : 跟-p 一起使用,表示指定源端口

-s  : 表示指定源ip

-d  : 表示指定目的ip

-j  : 后面跟动作,其中ACCEPT表示允许包,DROP表示丢弃包,REJECT表示拒绝包

-i  : 表示指定网卡 

iptables使用

iptables  -I input  -s  1.1.1.1  -j  DROP

iptables  -D input  -s  1.1.1.1  -j  DROP

第一条表示插入一条规则,把来自1.1.1.1的数据包丢掉。第二条则删除刚刚插入的规则。

删除一条规则时,必须和插入的规则一致

iptables  -I INPUT  -s  2.2.2.2  -p  tcp  --dport  80  -j  DROP

上例表示把来自2.2.2.2并且是tcp协议到本机80端口的数据包丢掉

iptables  -I OUTPUT  -d  3.3.3.3  -p  tcp  --dport  22  -j  DROP

上例表示把发送到3.3.3.3的22端口数据包丢掉

iptables  -A  INPUT  -s  192.168.1.0/24  -i  eth0  -j  ACCEPT

上例表示把来自192.168.1.0这个网段且作用在eth0上的包方形



删除一条规则,忘了创建时的规则,可以直接使用如下命令:

[root@localhost ~]# iptables -nvL --line-numbers |head

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

num   pkts bytes target     prot opt in     out     source               destination         

1     1480  117K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate   RELATED,ESTABLISHED

2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           

3      537 64056 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

4      537 64056 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

5      537 64056 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

6        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID

7      536 64004 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

[root@localhost ~]# 

查询到的规则前面加上 数字

删除时   iptables  -D  INPUT  1

iptables  -P  INPUT  DROP

-P选项表示 预设策略 后面跟链名 策略内容为 DROP 或者 ACCEPT  默认是 ACEPT

注:在远程连接服务器时,不要执行此命令,输入此命令回车后,远程会断开。

策略设定后,只能 iptables  -P  INPUT  ACCEPT  才能恢复原始状态

netfilter五个表

filter表主要用于过滤包,是系统预设的表,该表内有三个链:INPUT、OUTPUT、以及FORWARD。INPUT链作用于进入本机的包,OUTPUT作用于本机送出的包,FORWARD作用于本机无关的包

nat表主要用于网络地址转换,它有3个链,PREROUTING链的作用是在包刚刚到达防火墙时改变它的目的地址,OUTPUT链作用是改变本地产生的包的目的地址,POSTROUTING链的作用是在包即将离开防火墙时改变源地址

mangle表主要用于给数据包做标记,然后根据标记去操作相应的包

raw表可以实现不追踪某些数据包,默认系统的数据包都会被追踪

security表在CentOS6中没有,它用于强制访问控制(MAC)的网络规则

netfilter五个链

五个链分别为 PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING。

PREROUTING  : 数据包进入路由表之前

INPUT  : 通过路由表后目的地为本机

FORWARD : 通过路由表后,目的地不为本机

OUTPUT  :  由本机产生,向外转发

POSTROUTING  :  发送到网卡接口之前

selinux教程 http://os.51cto.com/art/201209/355490.htm

selinux pdf电子书 http://pan.baidu.com/s/1jGGdExK

iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html

sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html

iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html http://jamyy.us.to/blog/2006/03/206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值