crond系統級別 :定時文件清理,日志切割,定時收集系統的狀態
用戶級別 :同步系統時間,定時備份數據
[root@qls ~]#
[root@qls ~]# ll /etc/cron* -d
drwxr-xr-x. 2 root root 21 Aug 14 15:11 /etc/cron.d
drwxr-xr-x. 2 root root 42 Aug 14 15:12 /etc/cron.daily #系統每天執行定時任務
-rw-------. 1 root root 0 Apr 11 2018 /etc/cron.deny #黑名單
drwxr-xr-x. 2 root root 22 Aug 14 15:11 /etc/cron.hourly #系統每小時
drwxr-xr-x. 2 root root 6 Jun 10 2014 /etc/cron.monthly #系統每個月
-rw-r--r--. 1 root root 451 Jun 10 2014 /etc/crontab #定時任務的主配置文件
drwxr-xr-x. 2 root root 6 Jun 10 2014 /etc/cron.weekly #系統每周/var/spool/cron/ #用戶定時任務存放地址[root@qls ~]# ll /var/log/cron #定時任務日志[root@qls ~]# 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* #所有,每
- #連續的時間,1-5
, #不連續的時間,1,5
*/5 #每個5個單位
00 02 * * * #每天的凌晨2點整執行 00 02 1 * * #每月的1號的凌晨2點整執行 00 02 14 2 * #每年2月份的14號的凌晨2點整執行 00 02 * * 7 #每周日的的凌晨2點整執行
?
00 02 * 6 5 #每年的6月份每周五的凌晨2點整執行
?
00 02 14 * 7 #每個月14號且是周日的凌晨2點整執行 00 02 14 2 7 #每年的2月份14號且是周日的凌晨2點整執行
?
*/10 02 * * * #每天凌晨2點每隔十分鐘執行一次
?
* * * * * # 每分鐘執行一次
?
00 00 14 2 * # 每年的2月份14號凌晨12點整執行一次
?
*/5 * * * *
?
00 02 * 1,5,8 *
?
00 02 1-8 * *
?
00 21 * * *
?
45 4 1,10,22 * *
?
45 4 1-10 * *
?
3,15 8-11 */2 * *
?
0 23-7/2 * * *
?
15 21 * * 1-5
crontab #定時任務的命令選項-e #編輯定時任務 vim /var/spool/cron/username-l #查看定時任務 cat-r #刪除定時任務文件-u #指定用戶,默認是當前用戶
#安裝軟件
[root@qls ~]# yum install ntpdate -y#命令行測試是否可以同步時間
[root@qls ~]# date
Wed Aug 21 09:17:11 CST 2019
[root@qls ~]# ntpdate ntp.aliyun.com
21 Aug 09:17:59 ntpdate[8071]: adjust time server 203.107.6.88 offset 0.031011 sec
[root@qls ~]# date
Wed Aug 21 09:18:04 CST 2019
[root@qls ~]# date -s 2019/08/01
Thu Aug 1 00:00:00 CST 2019
[root@qls ~]# ntpdate ntp.aliyun.com
21 Aug 09:18:39 ntpdate[8074]: step time server 203.107.6.88 offset 1761503.866272 sec
[root@qls ~]# date
Wed Aug 21 09:18:45 CST 2019#編寫定時任務
[root@qls ~]# crontab -l
#sync time
* * * * * ntpdate ntp.aliyun.com#查看日志,看是否執行成功
[root@qls ~]# tailf /var/log/cron
Aug 21 09:20:43 qls crontab[8077]: (root) REPLACE (root)
Aug 21 09:20:43 qls crontab[8077]: (root) END EDIT (root)
Aug 21 09:20:49 qls crontab[8080]: (root) LIST (root)
Aug 21 09:21:01 qls CROND[8083]: (root) CMD (ntpdate ntp.aliyun.com)Aug 1 00:00:47 qls CROND[8095]: (root) CMD (ntpdate ntp.aliyun.com)#檢查,時間沒有同步成功
[root@qls ~]# date
Thu Aug 1 00:00:58 CST 2019#查看郵件
[root@qls ~]# tailf /var/mail/root
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20190731160201.2D69620C1941@qls.localdomain>
Date: Thu, 1 Aug 2019 00:02:01 +0800 (CST)/bin/sh: ntpdate: command not found #發現命令找不到#重新編寫定時任務
[root@qls ~]# crontab -e
#sync time
* * * * * /usr/sbin/ntpdate ntp.aliyun.com#再次檢查發現,定時任務成功執行
[root@qls ~]# date
Wed Aug 21 09:26:32 CST 2019#但是,系統會發送郵件
[root@qls ~]# ll /var/mail/root
-rw-------. 1 root mail 10447 Aug 21 09:30 /var/mail/root
You have new mail in /var/spool/mail/root#停掉郵件服務
[root@qls ~]# systemctl stop postfix#發現系統生成大量的小文件
[root@qls ~]# ll /var/spool/postfix/maildrop/
total 12
-rwxr--r--. 1 root postdrop 600 Aug 21 09:32 2250C400DE75
-rwxr--r--. 1 root postdrop 599 Aug 21 09:33 4D709400DE76
-rwxr--r--. 1 root postdrop 599 Aug 21 09:31 E884E400DE61#再次修改定時任務
[root@qls ~]# crontab -e
#sync time
* * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null #發現系統不再生成小文件
#啟動郵件服務
[root@qls ~]# systemctl start postfix#郵件也不會再次發送了。
[root@qls ~]# ll /var/mail/root
-rw-------. 1 root mail 14945 Aug 21 09:36 /var/mail/root總結:
有注釋
使用絕對路徑
把結果追加到空或者追加到文件中
[root@qls ~]# crontab -l
#sync time
* * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
#
* * * * * /usr/bin/date +%F_%T >> /root/time.txt #查看執行記錄,發現+后面的內容沒有識別
[root@qls ~]# tailf /var/log/cron
Aug 21 10:03:01 qls crond[7032]: (root) RELOAD (/var/spool/cron/root)
Aug 21 10:03:01 qls CROND[8583]: (root) CMD (/usr/bin/date +)
Aug 21 10:03:01 qls CROND[8586]: (root) CMD (/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null )#重新編寫定時任務
[root@qls ~]# crontab -e
#sync time
* * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
#
* * * * * /usr/bin/date +\%F_\%T >> /root/time.txt#定時任務被成功的執行
[root@qls ~]# tailf /var/log/cron
Aug 21 10:05:01 qls crond[7032]: (root) RELOAD (/var/spool/cron/root)
Aug 21 10:05:01 qls CROND[8614]: (root) CMD (/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null )
Aug 21 10:05:01 qls CROND[8615]: (root) CMD (/usr/bin/date +%F_%T >> /root/time.txt )總結:定時任務中,有些特殊符號,比如%,會不被識別
要求
備份 /etc/
etc_2019-08-21_10-09.tar.gz
var_2019-08-21_10-09.tar.gz
統一存放一個備份目錄
刪除三天前的備份數據[root@qls ~]# vim back.sh
#!/bin/bash
Time=$(date +%F_%H-%M)
mkdir -p /backup
cd /
tar czPf /backup/etc_${Time}.tar.gz etc/find /backup -type f -mtime +3 -delete[root@qls ~]# for i in {15..21} ;do date -s "2019/08/$i" && sh back.sh ;done[root@qls ~]# crontab -e
#sync time
* * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
#
* * * * * /usr/bin/date +\%F_\%T >> /root/time.txt
#backup /etc
* * * * * /bin/sh /root/back.sh &>/dev/null[root@qls ~]# ll /backup/
total 9980
-rw-r--r--. 1 root root 10215613 Aug 21 10:25 etc_2019-08-21_10-25.tar.gz#腳本中會出現命令找不到的情況
[root@qls ~]# vim back.sh
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Time=$(date +%F_%H-%M)
mkdir -p /backup
cd /
tar czPf /backup/etc_${Time}.tar.gz etc/find /backup -type f -mtime +3 -delete
/var/spool/cron/root #備份次文件即可,如果有腳本,需要把腳本進行備份
echo 'oldboy' >>/etc/cron.deny
1.每分鐘打印你的名字到oldboy.txt中。
#注釋
* * * * * command2.每周六的早上8點到12點,執行/scripts/test.sh腳本。
#注釋00 8-12 * * 6 command3.每個月的1號,執行/scripts/oldboy.sh腳本00 01 1 * * command4.每年的10月份1號到7號,執行/scripts/holiday.sh腳本00 01 1-7 10 * command5.每天的9點和11點,執行/scripts/test.sh腳本00 9,11 * * * command
#安裝郵件服務
yum install -y mailx
# 編輯配置文件 vi /etc/mail.rc
set from=xxx@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=xxx@qq.com
set smtp-auth-password='授權碼'
set smtp-auth=login
set smtp=smtps://smtp.qq.com:465
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/# 命令行發送
echo '你好啊' |mail -s 'Hello' xxx@qq.com
[root@qls ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND第一列:進程運行的用戶第二列:進程pid號碼第三列:占用CPU的百分比第四列: 占用內存的百分比第五列: 占用虛擬內存第六列: 占用的物理內存第七列:終端第八列:狀態第九列:進程啟動時間第十列:進程占用CPU的時間第11列: 執行的命令[root@qls ~]# top
top - 11:46:57 up 3:14, 2 users, load average: 0.68, 0.24, 0.12 #系統負載 1 5 15 分鐘
Tasks: 106 total, 1 running, 105 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 5.4 sy, 0.0 ni, 94.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2028088 total, 1097584 free, 113832 used, 816672 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1709956 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 第一行: 11:46:57 當前系統時間 3:14, 當前系統運行時間 2 users 當前登錄的用戶數量第二行:任務,總任務的數量,關于進程的命令ps #查看進程top #查看性能的命令htop #升級版topglances #顯示系統性能iotopiostatnetstat -lntp #查看端口號ss #用法一樣#查看網關路由[root@qls ~]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
[root@qls ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
172.16.1.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1#查看tcp的11種狀態
[root@qls ~]# netstat -ant
[root@qls ~]# ss -ant#掃描端口
[root@qls ~]# nmap -p1-100 10.0.0.100進程管理
[root@qls ~]# kill 10466 #kill 根據進程pid進行終止進程 -9 強制殺掉 -1 重載如果進程不存在,會提示[root@qls ~]# killall vim #killall根據進程名稱去終止如果進程不存在,會提示[root@qls ~]# pkill top #模糊殺手,包含進程的所有進程都是終止
沒有任何提示
screen #后臺管理選項 -S #給會話窗口起個命令-r #進入子窗口
ctrl鍵a +d 臨時退出
1. 遠程連接
2. 數據加密傳輸Telnet 不加密 tcp/23 不支持root用戶登錄
ssh 加密 tcp/22 支持root用戶登錄測試使用Telnet#安裝服務端[root@qls ~]# yum install -y telnet-server#啟動服務[root@qls ~]# systemctl start telnet.socket#查看端口號[root@qls ~]# netstat -lntp |grep 23#創建測試用戶
[root@qls ~]# useradd test
[root@qls ~]# echo '123' |passwd --stdin test
Changing password for user test.
passwd: all authentication tokens updated successfully.#連接測試
[root@qls ~]# telnet 10.0.0.100
Trying 10.0.0.100...
Connected to 10.0.0.100.
Escape character is '^]'.Kernel 3.10.0-957.el7.x86_64 on an x86_64
qls login: root
Password:
Login incorrectqls login: root
Password:
Login incorrectqls login: test
Password:
Last login: Wed Aug 21 14:53:34 from ::ffff:10.0.0.1
[test@qls ~]$
scp #ssh遠程傳輸的命令ssh #遠程登錄的命令ssh-keygen #生成秘鑰的命令ssh-copy-id #分發公鑰的命令#推
[root@qls ~]# scp -rp nginx-1.16.1.tar.gz root@10.0.0.7:/root
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:UJfXPNHA1tsUI9NsyZWwf2/IF/NcEErY/iJ74f4NyR4.
ECDSA key fingerprint is MD5:c9:76:b4:5e:4f:51:5a:00:e0:ba:f0:d6:c5:17:b9:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
nginx-1.16.1.tar.gz [root@qls ~]# ll
total 1016
-rw-------. 1 root root 1614 Aug 14 15:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 1032630 Aug 14 01:01 nginx-1.16.1.tar.gz#拉
[root@qls ~]# scp -rp root@10.0.0.100:/root/nginx-1.16.1 /root#使用遠程連接端口號傳輸
[root@qls ~]# scp -P22 -rp nginx-1.16.1.tar.gz root@10.0.0.7:/root
root@10.0.0.7's password:
nginx-1.16.1.tar.gz 100% 1008KB 92.8MB/s 00:00#限速傳輸, 單位kb
[root@qls ~]# scp -rp -l 8096 swap.txt root@10.0.0.7:/root#修改主機名
[root@qls ~]# hostnamectl set-hostname web01
[root@qls ~]# bash
[root@web01 ~]#
[root@web01 ~]#
[root@web01 ~]# cat /etc/hostname
web01#命令行連接
[root@qls ~]# ssh root@10.0.0.100 -p22#本地連接
[C:\~]$ ssh root@10.0.0.100 22#遠程執行命令
[root@web01 ~]# ssh root@10.0.0.100 "ifconfig eth0"
root@10.0.0.100's password:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255inet6 fe80::52f8:a673:eea3:dc47 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:0d:42:09 txqueuelen 1000 (Ethernet)RX packets 47639 bytes 43560570 (41.5 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 70530 bytes 131220281 (125.1 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#1.生成秘鑰對
[root@qls ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:K/fUKgI3bNlQKbirzmPz3+UFWgHcr+j2OAiDBh8UghI root@qls
The key's randomart image is:
+---[RSA 2048]----+
|E... . ..o |
|o.. . . +.. |
|.. . o .. |
|. . . . .. |
| o o o +S.o. |
| + = * o+.o |
| . . *.++ o o |
| .= o+=* o |
| oo+...oo+= |
+----[SHA256]-----+#2.分發公鑰
[root@qls ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.7's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@10.0.0.7'"
and check to make sure that only the key(s) you wanted were added.#3.連接測試
[root@qls ~]# ssh root@10.0.0.7
Last login: Wed Aug 21 15:21:32 2019 from 10.0.0.1
[root@web01 ~]#
Port 6666 # 變更SSH服務遠程連接端口
PermitRootLogin no # 禁止root用戶直接遠程登錄
PasswordAuthentication no # 禁止使用密碼直接遠程登錄
UseDNS no # 禁止ssh進行dns反向解析,影響ssh連接效率參數
GSSAPIAuthentication no # 禁止GSS認證,減少連接時產生的延遲
o install the new keys
root@10.0.0.7’s password:
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘root@10.0.0.7’”
and check to make sure that only the key(s) you wanted were added.
#3.連接測試
[root@qls ~]# ssh root@10.0.0.7
Last login: Wed Aug 21 15:21:32 2019 from 10.0.0.1
[root@web01 ~]#
### ssh優化```bashPort 6666 # 變更SSH服務遠程連接端口
PermitRootLogin no # 禁止root用戶直接遠程登錄
PasswordAuthentication no # 禁止使用密碼直接遠程登錄
UseDNS no # 禁止ssh進行dns反向解析,影響ssh連接效率參數
GSSAPIAuthentication no # 禁止GSS認證,減少連接時產生的延遲
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态