iptables规则备份和恢复,任务计划chkconfig工具systemd管理服务

iptables规则备份和恢复

备份

iptables-save > /tmp/ipt.txt

service iptables save 会把规则保存到 /etc/sysconfig/iptables

[root@aminglinux-01 ~]# iptables-save > /tmp/ipt.txt
[root@aminglinux-01 ~]# cat /tmp/ipt.txt 
# Generated by iptables-save v1.4.21 on Sun Sep 17 21:39:26 2017
*nat
:PREROUTING ACCEPT [2844:259375]
:INPUT ACCEPT [217:37076]
:OUTPUT ACCEPT [497:37624]
:POSTROUTING ACCEPT [497:37624]
-A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
COMMIT
# Completed on Sun Sep 17 21:39:26 2017
# Generated by iptables-save v1.4.21 on Sun Sep 17 21:39:26 2017
*filter
:INPUT ACCEPT [2618:240898]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1520:100090]
COMMIT
# Completed on Sun Sep 17 21:39:26 2017
[root@aminglinux-01 ~]# 

恢复刚才备份的规则

iptables-restore < my.ipt

firewalld

  • 打开firewalld

因为之前打开iptables的时候禁掉了firewalld 所以打开firewalld的话,先禁用iptables。

禁用iptables : systemctl disable iptables

然后 :systemctl stop iptables

开启firewalld : systemctl enable firewalld

然后 : systemctl start firewalld

  • firewalld 默认有9个zone

默认zone为public

每个zone都是一个规则集合

查看所有zone : firewall-cmd --get-zones

[root@aminglinux-01 ~]# firewall-cmd --get-zones
work drop internal external trusted home dmz public block

查看默认zone :firewall-cmd --get-default-zone

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

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

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

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

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

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

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

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

internal(内部):用于内部网络。你可以基本信任网络类的其他计算机不会威胁你的计算机。仅仅接收经过选择的链接。

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

firewalld 关于zone的操作

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

firewall-cmd --get-zone-of-interface=ens33 //指定网卡所在的zone

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

[root@aminglinux-01 ~]# firewall-cmd --get-service 
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansyncpa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpassw ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap squid ssh synergy syslog syslog-tls telnet ttp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
[root@aminglinux-01 ~]# 

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

linux任务计划

任务计划的配置文件:/etc/crontab

分,时,日,月,周 user command

[root@aminglinux-01 ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

[root@aminglinux-01 ~]# 

定义任务计划:crontab -e

~                                                                                                                                                   
0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

要保证服务是启动状态 systemctl start crond.service

[root@aminglinux-01 ~]# systemctl start crond.service 
[root@aminglinux-01 ~]# ps aux |grep cron
root        560  0.0  0.1 126224  1680 ?        Ss   22:11   0:00 /usr/sbin/crond -n
root       2389  0.0  0.0 112664   976 pts/0    S+   22:45   0:00 grep --color=auto cron
[root@aminglinux-01 ~]# 

chkconfig 工具

[root@aminglinux-01 ~]# chkconfig 

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@aminglinux-01 ~]# 

systemd管理服务

几个常用的服务相关的命令:systemctl list-units --all --type=service

systemctl enable crond.service //让服务开机启动

systemctl disable crond //不让开机启动

systemctl status crond //查看状态

systemctl stop crond //停止服务

systemctl start crond //启动服务

systemctl restart crond //重启服务

systemctl is-enabled crond //检查服务是否开机启动

  • ls /usr/lib/systemd/system 这里面是系统的所有unit ,分为以下类型。

service 系统服务

target 多个unit组成的组

系统级别

device 硬件设备

mount 文件系统挂载点

automount 自动挂载点

path 文件或路径

scope 不是由systemd 启动的外部进程

slice 进程组

snapshot systemd 快照

socket 进程间通信套接字

swap swap文件

timer 定时器

  • 系统为了方便管理用target来管理unit

列出系统所有的target

systemctl list-unit-files --type=target

systemctl list-dependencies multi-user.target 列出target有哪些unit

systemctl get-default 查看系统默认的target

systemctl set-default multi-user.target 设置默认target

一个service属于一种类型的unit,多个unit组成了一个target,一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service //看[install]部分

[root@aminglinux-01 ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=forking
PIDFile=/var/run/sshd.pid
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@aminglinux-01 ~]# 

转载于:https://my.oschina.net/u/3852961/blog/1830574

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值