Linux日常运维管理技巧(三)iptables规则备份和恢复、firewalld的9个zone、任务计划cron、chkconfig系统服务管理、添加服务命令、systemctl管理服务
目錄
?
Linux防火墻-netfilter
iptables規(guī)則備份和恢復
?
Linux防火墻-firewalled
firewalld的9個zone
firewalld關于zone的操作
firewalld關于service的操作
Linux任務計劃cron
Linux系統(tǒng)服務管理-chkonfig工具
systemd管理服務
unit介紹
target介紹
Linux防火墻-netfilter
iptables規(guī)則備份和恢復
service iptables save ##會把規(guī)則保存到/etc/sysconfig/iptables配置文件中
把iptables規(guī)則備份到自定義my.ipt文件中:命令iptables-save > my.ipt?
[root@zyshanlinux-001 ~]# iptables -t nat -A PREROUTING -d 192.168.43.32 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22 [root@zyshanlinux-001 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.43.32 [root@zyshanlinux-001 ~]# iptables-save > /tmp/myipt.txt ##把前面2條nat規(guī)則保存到自定義文件 [root@zyshanlinux-001 ~]# cat !$ ##查看保存規(guī)則的內(nèi)容 cat /tmp/myipt.txt # Generated by iptables-save v1.4.21 on Tue Jun 12 22:40:22 2018 *nat :PREROUTING ACCEPT [0:0] :INPUT ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] -A PREROUTING -d 192.168.43.32/32 -p tcp -m tcp --dport 1122 -j DNAT --to-destination 192.168.100.100:22 -A POSTROUTING -s 192.168.100.100/32 -j SNAT --to-source 192.168.43.32 COMMIT # Completed on Tue Jun 12 22:40:22 2018恢復剛才備份的規(guī)則:命令iptables-restore < my.ipt
[root@zyshanlinux-001 ~]# iptables -nvL ##filter表是空的 Chain INPUT (policy ACCEPT 0 packets, 0 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 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? [root@zyshanlinux-001 ~]# iptables -t nat -F ##清空nat表 [root@zyshanlinux-001 ~]# iptables -t nat -nvL ##確認nat表是空的 Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? ? Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? ? Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? ? Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? [root@zyshanlinux-001 ~]# iptables-restore < /tmp/myipt.txt ##從自定義文件備份規(guī)則中恢復規(guī)則 [root@zyshanlinux-001 ~]# iptables -t nat -nvL ##確實是把nat表的規(guī)則恢復 Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 DNAT ? ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 192.168.43.32 ? ? ? tcp dpt:1122 to:192.168.100.100:22 ? Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? ? Chain OUTPUT (policy ACCEPT 1 packets, 76 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? ? Chain POSTROUTING (policy ACCEPT 1 packets, 76 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 SNAT ? ? ? all -- * ? ? * ? ? ? 192.168.100.100 ? ? 0.0.0.0/0 ? ? ? ? ? to:192.168.43.32?
Linux防火墻-firewalled
firewalld的9個zone
打開firewalld,需要把前面開啟的netfilter先關閉,再啟動firewalled.
[root@zyshanlinux-001 ~]# systemctl disable iptables Removed symlink /etc/systemd/system/basic.target.wants/iptables.service. [root@zyshanlinux-001 ~]# systemctl stop iptables ? [root@zyshanlinux-001 ~]# 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@zyshanlinux-001 ~]# systemctl start firewalld [root@zyshanlinux-001 ~]#firewalld默認有9個zone,zone是firewalld的單位,每個zone相當于一個規(guī)則集合。
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zones block dmz drop external home internal public trusted work默認zone為public
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-default-zone public每個zone的含義
?
firewalld關于zone的操作
1、更改默認zone 命令firewall-cmd --set-default-zone=work
[root@zyshanlinux-001 ~]# firewall-cmd --set-default-zone=work success [root@zyshanlinux-001 ~]# firewall-cmd --get-default-zone work2、查看指定網(wǎng)卡用的是什么zone 命令firewall-cmd --get-zone-of-interface=lo
[root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens33 work [root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens37 work [root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=lo no zone [root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens33:0 no zone3、給指定網(wǎng)卡設置zone 命令firewall-cmd --zone=public --add-interface=lo
##新增網(wǎng)卡沒有zone## [root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens37 no zone ##到路徑/etc/sysconfig/network-scripts/去## [root@zyshanlinux-001 ~]# cd /etc/sysconfig/network-scripts/ [root@zyshanlinux-001 network-scripts]# ls ifcfg-ens33 ? ifdown-ippp ? ifdown-sit ? ? ? ifup-bnep ifup-plusb ? ifup-TeamPort ifcfg-ens33:0 ifdown-ipv6 ? ifdown-Team ? ? ifup-eth ? ifup-post ? ifup-tunnel ifcfg-lo ? ? ? ifdown-isdn ? ifdown-TeamPort ifup-ippp ifup-ppp ? ? ifup-wireless ifdown ? ? ? ? ifdown-post ? ifdown-tunnel ? ifup-ipv6 ifup-routes init.ipv6-global ifdown-bnep ? ifdown-ppp ? ? ifup ? ? ? ? ? ? ifup-isdn ifup-sit ? ? network-functions ifdown-eth ? ? ifdown-routes ifup-aliases ? ? ifup-plip ifup-Team ? network-functions-ipv6 ##復制ifcfg-ens33文件,改為ifcfg-ens37,修改名字、IP等參數(shù)## [root@zyshanlinux-001 network-scripts]# cp ifcfg-ens33 ifcfg-ens37 [root@zyshanlinux-001 network-scripts]# ls ifcfg-ens33 ? ifdown-ippp ? ifdown-Team ? ? ifup-ippp ? ifup-routes ? ? ? network-functions ifcfg-ens33:0 ifdown-ipv6 ? ifdown-TeamPort ifup-ipv6 ? ifup-sit ? ? ? ? network-functions-ipv6 ifcfg-ens37 ? ifdown-isdn ? ifdown-tunnel ? ifup-isdn ? ifup-Team ifcfg-lo ? ? ? ifdown-post ? ifup ? ? ? ? ? ? ifup-plip ? ifup-TeamPort ifdown ? ? ? ? ifdown-ppp ? ? ifup-aliases ? ? ifup-plusb ifup-tunnel ifdown-bnep ? ifdown-routes ifup-bnep ? ? ? ifup-post ? ifup-wireless ifdown-eth ? ? ifdown-sit ? ? ifup-eth ? ? ? ? ifup-ppp ? init.ipv6-global [root@zyshanlinux-001 network-scripts]# vim ifcfg-ens37 ##再重啟firewalld服務## [root@zyshanlinux-001 network-scripts]# systemctl restart firewalld ##仍然沒有zone## [root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=ens37 no zone ##用命令賦予zone## [root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=dmz --add-interface=ens37 success ##zone設置完成## [root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=ens37 dmz ##給網(wǎng)卡lo設置zone## [root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=public --add-interface=lo success [root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=lo public4、針對某個網(wǎng)卡更改zone 命令firewall-cmd --zone=dmz --change-interface=lo
[root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=drop --change-interface=lo success [root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=lo drop5、針對某個網(wǎng)卡刪除zone 命令firewall-cmd --zone=dmz --remove-interface=lo
[root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=drop --remove-interface=lo success ##如果該網(wǎng)卡有默認的zone,就恢復默認zone;沒就會刪除zone [root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=lo no zone6、查看系統(tǒng)所有網(wǎng)卡所在的zone 命令firewall-cmd --get-active-zones
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-active-zones dmzinterfaces: ens33:0 workinterfaces: ens33 publicinterfaces: ens37 lo?
firewalld關于service的操作
service是zone下的一個子單元。
1、查看所有的services 命令firewall-cmd --get-services(s可加可不加)
[root@zyshanlinux-001 ~]# firewall-cmd --get-services 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 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@zyshanlinux-001 ~]# 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 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-server2、查看當前zone下有哪些service 命令firewall-cmd --list-services
[root@zyshanlinux-001 ~]# firewall-cmd --get-default-zone work [root@zyshanlinux-001 ~]# firewall-cmd --list-services ssh dhcpv6-client [root@zyshanlinux-001 ~]# firewall-cmd --list-service ssh dhcpv6-client3、指定某個zone下?lián)碛械膕ervices 命令firewall-cmd --zone=public --list-services
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --list-services ssh dhcpv6-client [root@zyshanlinux-001 ~]# firewall-cmd --zone=block --list-services ? [root@zyshanlinux-001 ~]# firewall-cmd --zone=trusted --list-services ? [root@zyshanlinux-001 ~]#4、把http和ftp添加到public zone下面
命令firewall-cmd --zone=public --add-service=http
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --add-service=http success [root@zyshanlinux-001 ~]# firewall-cmd --zone=public --add-service=ftp success [root@zyshanlinux-001 ~]# firewall-cmd --zone=public --list-services ssh dhcpv6-client http ftp5、把http和ft從public zone下面移除
命令firewall-cmd --zone=public --remove-service=http
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --remove-service=http success [root@zyshanlinux-001 ~]# firewall-cmd --zone=public --remove-service=ftp success [root@zyshanlinux-001 ~]# firewall-cmd --zone=public --list-service ssh dhcpv6-client6、前面2步都是在內(nèi)存上操作,想把新的service永久加載到zone下,需要更改配置文件
?命令firewall-cmd --zone=public --add-service=http --permanent
?之后會在/etc/firewalld/zones目錄下面生成配置文件
?zone的配置文件模板在這個路徑下/usr/lib/firewalld/zones/
?service的配置文件模板在這個路徑下/usr/lib/firewalld/services/
[root@zyshanlinux-001 ~]# ls /usr/lib/firewalld/zones/ ##zone的配置文件模板## block.xml dmz.xml drop.xml external.xml home.xml internal.xml public.xml trusted.xml work.xml [root@zyshanlinux-001 ~]# ls /etc/firewalld/zones public.xml public.xml.old ##添加"http"到public zone配置文件下## [root@zyshanlinux-001 ~]# firewall-cmd --zone=public --add-service=http --permanent success [root@zyshanlinux-001 ~]# ls /etc/firewalld/zones public.xml public.xml.old [root@zyshanlinux-001 ~]# cat /etc/firewalld/zones/public.xml <?xml version="1.0" encoding="utf-8"?> <zone><short>Public</short><description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description><service name="ssh"/><service name="dhcpv6-client"/><service name="http"/> ##添加的"http"## </zone> ##添加"ftp"到public zone配置文件下## [root@zyshanlinux-001 ~]# firewall-cmd --zone=public --add-service=ftp --permanent success [root@zyshanlinux-001 ~]# cat /etc/firewalld/zones/public.xml <?xml version="1.0" encoding="utf-8"?> <zone><short>Public</short><description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description><service name="ssh"/><service name="dhcpv6-client"/><service name="http"/><service name="ftp"/> ##添加的"ftp"## </zone>7、需求:ftp服務自定義端口1121,需要在work zone下面放行ftp
[root@zyshanlinux-001 ~]# ls /etc/firewalld/zones/ public.xml public.xml.old [root@zyshanlinux-001 ~]# ls /etc/firewalld/services/ ##把services中的ftp.xml模板拷貝到配置文件的services中## [root@zyshanlinux-001 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services [root@zyshanlinux-001 ~]# ls /etc/firewalld/services/ ftp.xml [root@zyshanlinux-001 ~]# vi /etc/firewalld/services/ftp.xml ? <?xml version="1.0" encoding="utf-8"?> <service><short>FTP</short><description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description><port protocol="tcp" port="1121"/> ##把21端口改為1121端口##<module name="nf_conntrack_ftp"/> </service> ? ##把zones中的work.xml模板拷貝到配置文件的zones中## [root@zyshanlinux-001 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ [root@zyshanlinux-001 ~]# ls /etc/firewalld/zones/ public.xml public.xml.old work.xml [root@zyshanlinux-001 ~]# vim /etc/firewalld/zones/work.xml ? <?xml version="1.0" encoding="utf-8"?> <zone><short>Work</short><description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description><service name="ssh"/><service name="dhcpv6-client"/><service name="ftp"/> ##添加一行,把"ftp"加進去## </zone>重新加載firewall服務
[root@zyshanlinux-001 ~]# firewall-cmd --reload success [root@zyshanlinux-001 ~]# firewall-cmd --zone=work --list-services ##確認生效## ssh dhcpv6-client ftp?
Linux任務計劃cron
需求:在凌晨備份數(shù)據(jù),啟動服務,操作過程可以時shell腳本或一個單獨的命令
任務計劃的配置文件:
[root@zyshanlinux-001 ~]# 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@zyshanlinux-001 ~]#格式:分 時 日 月 周 user command
分范圍0-59,時范圍0-23,日范圍1-31,月范圍1-12,周0-6
在配置文件中可以把計劃的輸出結果輸出到指定文件,方便追溯。
[root@zyshanlinux-001 ~]# crontab -e no crontab for root - using an empty one ? 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log為什么沒有年?它用周來區(qū)別唯一性,每年的某一天日期所在的周都是不同的。
可用格式1-5表示一個范圍1到5
可用格式1,2,3表示1或者2或者3
可用格式*/2表示被2整除的數(shù)字,比如小時,那就是每隔2小時
如何定義后面的命令呢,與vi同樣操作,*代表所有(每天),用命令Crontab ?–u指定用戶、 -e編輯、 -l列出、 -r刪除
[root@zyshanlinux-001 ~]# crontab -l no crontab for root [root@zyshanlinux-001 ~]# crontab -e no crontab for root - using an empty one crontab: installing new crontab [root@zyshanlinux-001 ~]# crontab -l 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log [root@zyshanlinux-001 ~]# crontab -r [root@zyshanlinux-001 ~]# crontab -l no crontab for root [root@zyshanlinux-001 ~]# crontab -u root -l no crontab for root要保證服務是啟動狀態(tài):systemctl start crond.service?
有cron這個進程,就證明上面的命令成功啟動了服務.
查看該服務的啟動與停止狀態(tài)
查看cron計劃里的命令,命令保存在/var/spool/cron/root路徑下,以用戶的名字命名.
可能出現(xiàn)問題:計劃沒執(zhí)行,原因可能是沒用絕對命令而是用了命令,該命令并沒有在PATH環(huán)境變量里面。養(yǎng)成寫腳本時用絕對路徑命令的習慣。計劃命令寫正確錯誤輸出方便后期追溯。
?
Linux系統(tǒng)服務管理-chkonfig工具
該工具是在centos6中使用的,在centos7中已經(jīng)不用了,但向之前版本兼容,所以還能用,過度作用。只剩2個服務了netconsole,network,這2服務在/etc/init.d/服務腳本下.
chkconfig --list
[root@zyshanlinux-001 ~]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:開 4:開 5:開 6:關 [root@zyshanlinux-001 ~]# ls /etc/init.d/ functions netconsole network README0關機,1單用戶,2多用戶但少了nfs服務(網(wǎng)絡文件系統(tǒng)),3多用戶少了圖型,4保留的級別暫時沒用,5帶圖形多用戶,6級別重啟.
僅關閉級別3 命令chkconfig –level 3 network off
[root@zyshanlinux-001 ~]# chkconfig --level 3 network off [root@zyshanlinux-001 ~]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:關 4:開 5:開 6:關把級別345都關閉 命令chkconfig –level 345 network off
[root@zyshanlinux-001 ~]# chkconfig --level 345 network off [root@zyshanlinux-001 ~]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:關 4:關 5:關 6:關把級別345都開啟 命令chkconfig –level 345 network on
[root@zyshanlinux-001 ~]# chkconfig --level 345 network on [root@zyshanlinux-001 ~]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:開 4:開 5:開 6:關添加服務命令 chkconfig –add networkd
[root@zyshanlinux-001 ~]# ls /etc/init.d/ functions netconsole network README ##到腳本目錄下## [root@zyshanlinux-001 ~]# cd !$ cd /etc/init.d/ [root@zyshanlinux-001 init.d]# ls functions netconsole network README ##拷貝服務文件## [root@zyshanlinux-001 init.d]# cp network 123 [root@zyshanlinux-001 init.d]# ls -l 總用量 48 -rwxr-xr-x 1 root root 7293 6月 14 23:12 123 -rw-r--r--. 1 root root 17500 5月 ? 3 2017 functions -rwxr-xr-x. 1 root root 4334 5月 ? 3 2017 netconsole -rwxr-xr-x. 1 root root 7293 5月 ? 3 2017 network -rw-r--r-- 1 root root 1160 4月 11 15:36 README ##拷貝的服務文件沒在chkconfig里面## [root@zyshanlinux-001 init.d]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:開 4:開 5:開 6:關 ##必須用下面這個命令才能把123服務添加到chkconfig## [root@zyshanlinux-001 init.d]# chkconfig --add 123 [root@zyshanlinux-001 init.d]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? 123 ? ? ? ? ? 0:關 1:關 2:開 3:開 4:開 5:開 6:關 netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:開 4:開 5:開 6:關 [root@zyshanlinux-001 init.d]# ls 123 functions netconsole network READMEvim 123 該服務文件
其中最重要的規(guī)則和描述
# chkconfig: 2345 10 90 # description: Activates/Deactivates all network interfaces configured to \ # start at boot time.把服務從chkconfig中刪除 命令chkconfig –del networkd
[root@zyshanlinux-001 init.d]# chkconfig --del 123 [root@zyshanlinux-001 init.d]# chkconfig --list ? 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數(shù)據(jù) 可能被原生 systemd 配置覆蓋。 ?要列出 systemd 服務,請執(zhí)行 'systemctl list-unit-files'。查看在具體 target 啟用的服務請執(zhí)行'systemctl list-dependencies [target]'。 ? netconsole ? ? 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network ? ? ? 0:關 1:關 2:開 3:開 4:開 5:開 6:關?
systemd管理服務
查看所有的unit和service,相對來說是第2條命令比較有序。
systemctl是RHEL 7 的服務管理工具中主要的工具,它融合之前service和chkconfig的功能于一體。可以使用它永久性或只在當前會話中啟用/禁用服務。
[root@zyshanlinux-001 init.d]# systemctl list-unit-files [root@zyshanlinux-001 init.d]# systemctl list-units --all --type=service幾個常用的服務相關的命令?
- systemctl enable crond.service //讓服務開機啟動?
- systemctl disable crond //不讓開機啟動?
- systemctl status crond //查看狀態(tài)?
- systemctl stop crond //停止服務?
- systemctl start crond //啟動服務?
- systemctl restart crond //重啟服務?
- systemctl is-enabled crond //檢查服務是否開機啟動
-
systemctl list-unit-files|grep enabled //查看已啟動的服務列表
-
systemctl --failed //查看啟動失敗的服務列表
systemd有系統(tǒng)和用戶區(qū)分;系統(tǒng)(/user/lib/systemd/system/)、用戶(/etc/lib/systemd/user/).
一般系統(tǒng)管理員手工創(chuàng)建的單元文件建議存放在/etc/systemd/system/目錄下面。
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target[Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true[Install] WantedBy=multi-user.target配置文件內(nèi)容,實際上是一個軟連接
[root@zyshanlinux-001 ~]# systemctl disable crond [root@zyshanlinux-001 ~]# systemctl enable crond.service Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service. [root@zyshanlinux-001 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service [Unit] Description=Command Scheduler After=auditd.service systemd-user-sessions.service time-sync.target[Service] EnvironmentFile=/etc/sysconfig/crond ExecStart=/usr/sbin/crond -n $CRONDARGS ExecReload=/bin/kill -HUP $MAINPID KillMode=process[Install] WantedBy=multi-user.target[root@zyshanlinux-001 ~]# ls -l !$ ls -l /etc/systemd/system/multi-user.target.wants/crond.service lrwxrwxrwx 1 root root 37 6月 14 23:43 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service啟動就創(chuàng)建一個軟連接,關閉就是把軟連接刪除.
[root@zyshanlinux-001 ~]# ls -l /usr/lib/systemd/system/crond.service -rw-r--r--. 1 root root 284 8月 3 2017 /usr/lib/systemd/system/crond.service [root@zyshanlinux-001 ~]# systemctl disable crond Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. [root@zyshanlinux-001 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service ls: 無法訪問/etc/systemd/system/multi-user.target.wants/crond.service: 沒有那個文件或目錄?
unit介紹
ls /usr/lib/systemd/system //系統(tǒng)所有unit,分為以下類型
- service 系統(tǒng)服務
- target 多個unit組成的組?
- device 硬件設備?
- mount 文件系統(tǒng)掛載點?
- 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 --type=service//列出狀態(tài)為active的service?
- systemctl is-active crond.service //查看某個服務是否為active
?
target介紹
系統(tǒng)為了方便管理用target來管理unit
systemctl list-unit-files --type=target? // 列出所有的target
systemctl list-dependencies multi-user.target? // 查看指定target下面有哪些unit
[root@zyshanlinux-001 ~]# systemctl list-dependencies multi-user.target multi-user.target ● ├─auditd.service ● ├─brandbot.path ● ├─chronyd.service ● ├─dbus.service ● ├─firewalld.service ● ├─irqbalance.service ● ├─kdump.service ● ├─network.service ● ├─NetworkManager.service ● ├─plymouth-quit-wait.service ● ├─plymouth-quit.service ● ├─postfix.service ● ├─rsyslog.service ● ├─sshd.service ● ├─sysstat.service ● ├─systemd-ask-password-wall.path ● ├─systemd-logind.service ● ├─systemd-readahead-collect.service ● ├─systemd-readahead-replay.service ● ├─systemd-update-utmp-runlevel.service ● ├─systemd-user-sessions.service ● ├─tuned.service ● ├─vmtoolsd.service ● ├─basic.target ● │ ├─microcode.service ● │ ├─rhel-autorelabel-mark.servicesystemctl get-default //查看系統(tǒng)默認的target
[root@zyshanlinux-001 ~]# systemctl get-default multi-user.targetsystemctl set-default multi-user.target //設置默認的target
[root@zyshanlinux-001 ~]# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. [root@zyshanlinux-001 ~]#一個service屬于一種類型的unit
多個unit組成了一個target
一個target里面包含了多個service
cat /usr/lib/systemd/system/sshd.service //看[install]部分
[root@zyshanlinux-001 ~]# 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 $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s[Install] WantedBy=multi-user.target ##這里看sshd.service是屬于multi-user.target##?
擴展?
提供一個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?
總結
以上是生活随笔為你收集整理的Linux日常运维管理技巧(三)iptables规则备份和恢复、firewalld的9个zone、任务计划cron、chkconfig系统服务管理、添加服务命令、systemctl管理服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线上环境HBASE-1.2.0出现old
- 下一篇: vue 项目抛出警告