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

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

保存和备份iptables规则

用户自设定的防火墙规则只保存在内存中,并未保存至文件中,当系统重启后,以前设定的规则就没有了,所以,你设定的规则可以先保存下:

service iptables save —保存规则

[root@localhost ~]# service iptables save

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

[root@localhost ~]# 

由上可看出 规则保存至  /etc/sysconfig/iptables 文件内,这个文件就是iptables的规则,所以如果需要备份防火墙规则的话,可以直接复制一个这个文件的副本即可。

停止防火墙服务

service  iptables  stop

但是如果你重新设定规则,哪怕只有一条,也会自动开启防火墙。

备份防火墙演示

[root@localhost ceshi]# iptables-save > my.ipt 

[root@localhost ceshi]# cat my.ipt 

Generated by iptables-save v1.4.21 on Fri Jun  1 00:42:58 2018

*filter

:INPUT ACCEPT [171:13932]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [107:11100]

-A INPUT -s 192.168.1.0/24 -p tcp

COMMIT

Completed on Fri Jun  1 00:42:58 2018

[root@localhost ceshi]# 

备份完毕,现在来看下我的备份文件

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

Generated by iptables-save v1.4.21 on Fri Jun  1 00:42:49 2018

*filter

:INPUT ACCEPT [136:11416]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [83:8828]

-A INPUT -s 192.168.1.0/24 -p tcp

COMMIT

Completed on Fri Jun  1 00:42:49 2018

[root@localhost ceshi]# 

接下来我在my.ipt里增加了一条规则

[root@localhost ceshi]# cat my.ipt 

Generated by iptables-save v1.4.21 on Fri Jun  1 00:42:58 2018

*filter

:INPUT ACCEPT [171:13932]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [107:11100]

-A INPUT -s 192.168.1.0/24 -p tcp

-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

COMMIT

Completed on Fri Jun  1 00:42:58 2018

[root@localhost ceshi]# 

现在我去删除现有的规则,然后保存。

[root@localhost ceshi]# iptables -F

[root@localhost ceshi]# iptables -nvL

Chain INPUT (policy ACCEPT 24 packets, 1718 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 17 packets, 1528 bytes)

 pkts bytes target     prot opt in     out     source               destination         

[root@localhost ceshi]# service iptables save

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

[root@localhost ceshi]# iptables -nvL

Chain INPUT (policy ACCEPT 90 packets, 6718 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 68 packets, 6916 bytes)

 pkts bytes target     prot opt in     out     source               destination         

接下来使用命令 iptables-restore < my.ipt 恢复备份的防火墙规则

[root@localhost ceshi]# iptables-restore < my.ipt 

[root@localhost ceshi]# iptables -nvL

Chain INPUT (policy ACCEPT 11 packets, 758 bytes)

 pkts bytes target     prot opt in     out     source               destination         

   10   724            tcp  --  *      *       192.168.1.0/24       0.0.0.0/0           

    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes)

 pkts bytes target     prot opt in     out     source               destination         

[root@localhost ceshi]# 

恢复后可查看到规则。

firewalld操作

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 ~]# 

需求:在服务器上配置一个ftp服务,端口为1121,且在work zone 下放行ftp。操作如下

[root@localhost ceshi]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/

[root@localhost ceshi]# vim /etc/firewalld/services/ftp.xml 

[root@localhost ceshi]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

[root@localhost ceshi]# vim /etc/firewalld/zones/work.xml 

[root@localhost ceshi]# firewall-cmd --reload

FirewallD is not running

[root@localhost ceshi]# systemctl  start firewalld

[root@localhost ceshi]# firewall-cmd --reload

success

[root@localhost ceshi]# firewall-cmd --zone=work  --list-services

ssh ftp dhcpv6-client

[root@localhost ceshi]# 

/usr/lib/firewalld/services/  目录下存放的是service的模板配置文件

/etc/firewalld/services/  目录下的配置文件才会生效

先拷贝模板文件去 service 配置文件目录下,然后进去修改 ftp.xml 文件 把端口改为1121

再拷贝模板文件去 zones配置文件目录下    进去修改work.xml里 增加一项内容。<service name=“ftp”>

其次使用命令 firewall-cmd  --reload   //重新加载

最后可以查看使用命令  firewall-cmd --zone=work  --list-services  查看是否生效!

linux系统任务计划

命令crontab

常用参数:

-u:表示指定哪个用户,不加-u选项则为当前用户。

-e:表示制定计划任务。

-l :表示列出计划任务。

-r :表示删除计划任务。

格式: 分  时  日  月  周  user  command

crontab -e 其实是打开了 /var/spool/cron/username 文件 username 代表你的用户名

[root@localhost ceshi]# cat /var/spool/cron/root

01 10 05 06 3 echo 'OK' > /tmp/cron.log

[root@localhost ceshi]#

crontab配置文件

[root@localhost ceshi]# 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@localhost ceshi]# 

crontab命令演示:

[root@localhost ceshi]# crontab -e

no crontab for root - using an empty one

crontab: installing new crontab

[root@localhost ceshi]# crontab -l

01 10 05 06 3 echo 'OK' > /root/ceshi/cron.log

[root@localhost ceshi]# crontab -r

[root@localhost ceshi]# crontab -l

no crontab for root

[root@localhost ceshi]# 

上述命令表示在6月5号10点01分且当天必须是周三  时执行 echo 'OK' > /root/ceshi/cron.log

做完任务计划后,需要去查看crond服务是否启动。

[root@localhost ceshi]# systemctl status crond

● crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since 三 2018-05-30 00:29:58 CST; 2 days ago

 Main PID: 516 (crond)

   CGroup: /system.slice/crond.service

          └─516 /usr/sbin/crond -n

5月 30 00:29:58 localhost.localdomain systemd[1]: Started Command Scheduler.

5月 30 00:29:58 localhost.localdomain systemd[1]: Starting Command Scheduler...

5月 30 00:29:58 localhost.localdomain crond[516]: (CRON) INFO (RANDOM_DELAY will be scaled   with fac...d.)

5月 30 00:29:59 localhost.localdomain crond[516]: (CRON) INFO (running with inotify support)

Hint: Some lines were ellipsized, use -l to show in full.

[root@localhost ceshi]# 

active状态一行    active(running) 代表启动          inactive(dead)表示 未启动

linux系统服务管理

chkconfig服务管理工具

CentOS6上的服务管理工具为chkconfig,linux系统所有的预设服务都可以通过查看/etc/init.d/目录得到

[root@localhost ceshi]# ls /etc/init.d/

functions  netconsole  network  README

[root@localhost ceshi]# 

只有屈指可数的几个文件,因为CentOS7已经不再延续CentOS6的服务管理方案了,但是chkconfig还是可以使用的。

列出所有的服务及其每个级别的状态

[root@localhost ceshi]# chkconfig --list

注:该输出结果只显示 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@localhost ceshi]#     

级别介绍:

  • 等级0表示:表示关机
  • 等级1表示:单用户模式
  • 等级2表示:多用户模式,少nfs服务
  • 等级3表示:多用户模式,不带图形
  • 等级4表示:是一种保留的级别
  • 等级5表示:带图形界面的多用户模式
  • 等级6表示:重新启动

chkconfig命令演示:

[root@localhost ceshi]# chkconfig --level 3 network off

[root@localhost ceshi]# chkconfig --list |grep network

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。 

 要列出 systemd 服务,请执行 'systemctl list-unit-files'。

  查看在具体 target 启用的服务请执行

  'systemctl list-dependencies [target]'。

network         0:关 1:关 2:开 3:关 4:开 5:开 6:关

[root@localhost ceshi]# 

–level 指定级别 后面是服务名,然后跟状态是 off 或者 on

可以指定多个级别

[root@localhost ceshi]# chkconfig --level 345 netconsole on

[root@localhost ceshi]# chkconfig --list | grep netconsole

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。 

  要列出 systemd 服务,请执行 'systemctl list-unit-files'。

 查看在具体 target 启用的服务请执行

'systemctl list-dependencies [target]'。

netconsole      0:关 1:关 2:关 3:开 4:开 5:开 6:关

[root@localhost ceshi]# 

省略级别操作

[root@localhost ceshi]# chkconfig netconsole on

[root@localhost ceshi]# chkconfig --list | grep netconsole

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。 

  要列出 systemd 服务,请执行 'systemctl list-unit-files'。

 查看在具体 target 启用的服务请执行

 'systemctl list-dependencies [target]'。

netconsole      0:关 1:关 2:开 3:开 4:开 5:开 6:关

[root@localhost ceshi]# 

省略级别操作  默认针对2.3.4.5级别操作得。

删除服务/加入服务 操作

[root@localhost ceshi]# chkconfig --del network

[root@localhost ceshi]# chkconfig --list | grep network

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。 

  要列出 systemd 服务,请执行 'systemctl list-unit-files'。

  查看在具体 target 启用的服务请执行

  'systemctl list-dependencies [target]'。

[root@localhost ceshi]# chkconfig --add network

[root@localhost ceshi]# chkconfig --list | grep network

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。 

要列出 systemd 服务,请执行 'systemctl list-unit-files'。

 查看在具体 target 启用的服务请执行

  'systemctl list-dependencies [target]'。

network         0:关 1:关 2:开 3:开 4:开 5:开 6:关

[root@localhost ceshi]# 

systemd服务管理

systemd是centos7管理的一个服务机制

  systemd支持多个服务并发启动,而sysV只能一个个启动。导致systemd方式启动会快上许多

列出系统所有的服务:

[root@localhost ceshi]# systemctl list-units --all --type=service |head

  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION

  auditd.service                                        loaded    active   running Security Auditing Service

  brandbot.service                                      loaded    inactive dead    Flexible Branding Service

  chronyd.service                                       loaded    active   running NTP client/server

  cpupower.service                                      loaded    inactive dead    Configure CPU power related  settings

  crond.service                                         loaded    active   running Command Scheduler

  dbus.service                                          loaded    active   running D-Bus System Message Bus

● display-manager.service                               not-found inactive dead    display-manager.service

  dracut-shutdown.service                               loaded    inactive dead    Restore /run/initramfs

  ebtables.service                                      loaded    inactive dead    Ethernet Bridge Filtering tables

[root@localhost ceshi]#      

由于太多,只列出前十

列出服务对应的启动脚本文件:

[root@localhost ceshi]# ls /usr/lib/systemd/system/ | head

arp-ethers.service

auditd.service

autovt@.service

basic.target

basic.target.wants

blk-availability.service

bluetooth.target

brandbot.path

brandbot.service

chrony-dnssrv@.service

[root@localhost ceshi]# 

常用服务相关命令

  • systemctl enable crond.service //让服务开机启动
  • systemctl disable crond //不让开机启动
  • systemctl status crond //查看状态
  • systemctl stop crond //停止服务
  • systemctl start crond //启动服务
  • systemctl restart crond //重启服务
  • systemctl is-enabled crond //检查服务是否开机启动

unit介绍

ls /usr/lib/systemd/system //系统所有unit:

  • service 系统服务
  • target 多个unit组成的组
  • device 硬件设备
  • mount 文件系统挂载点
  • automount 自动挂载点
  • path 文件或路径
  • scope 不是由systemd启动的外部进程
  • slice 进程组
  • snapshot systemd快照
  • socket 进程间通信套接字
  • swap swap文件
  • timer 定时器

unit相关命令

systemctl list-units //列出正在运行的unit 

systemctl list-units --all //列出所有,包括失败的或者inactive的

systemctl list-units --all --state=inactive //列出inactive的unit

systemctl list-units --all --type=service //列出所有状态的service

systemctl list-units --type=service //列出状态为active的service

systemctl is-active crond.service //查看某个unit是否为active

target介绍

target是多个unit的组合,就是系统用target来管理unit

列出当前系统所有的target

[root@localhost ceshi]# systemctl list-unit-files --type=target |head

UNIT FILE                 STATE   

basic.target              static  

bluetooth.target          static  

cryptsetup-pre.target     static  

cryptsetup.target         static  

ctrl-alt-del.target       disabled

default.target            enabled 

emergency.target          static  

final.target              static  

getty.target              static  

[root@localhost ceshi]# 

由于过多,只列出了十个。

查看一个target包含的unit

[root@localhost ceshi]# systemctl list-dependencies multi-user.target |head

multi-user.target

● ├─auditd.service

● ├─brandbot.path

● ├─chronyd.service

● ├─crond.service

● ├─dbus.service

● ├─firewalld.service

● ├─httpd.service

● ├─irqbalance.service

● ├─kdump.service

[root@localhost ceshi]# 

以树形方式列出来。清晰直观。由于过多,只列出了十个。

设置默认的target

[root@localhost ceshi]# systemctl set-default multi-user.target  

列出系统默认target

[root@localhost ceshi]# systemctl get-default

multi-user.target

[root@localhost ceshi]# 

service、unit以及target之间的联系:

  1. 一个service属于一种unit;
  2. 多个unit一起组成了一个target;
  3. 一个target里包含了多个service,可以查看/usr/lib/systemd/system/sshd.service里面 [install]部分的内容,其内容是定义了该service属于哪一个target。
    [root@localhost ceshi]# 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=notify
    EnvironmentFile=/etc/sysconfig/sshd
    ExecStart=/usr/sbin/sshd -D OPTIONSExecReload=/bin/killHUP O P T I O N S E x e c R e l o a d = / b i n / k i l l − H U P MAINPID
    KillMode=process
    Restart=on-failure
    RestartSec=42s
    [Install]
    WantedBy=multi-user.target
    [root@localhost ceshi]#

扩展内容

iptables系列文章

https://www.zsythink.net/archives/tag/iptables/page/2/

anacron https://www.jianshu.com/p/3009a9b7d024?from=timeline

systemd自定义启动脚本  http://www.jb51.net/article/100457.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值