Day 18
作者:翟玉龍
歸檔:課堂筆記
2019/3/25
快捷鍵:
Ctrl? + 1??? 標(biāo)題1
Ctrl? + 2??? 標(biāo)題2
Ctrl? + 3??? 標(biāo)題3
Ctrl? + 4??? 實(shí)例
Ctrl? + 5??? 程序代碼
Ctrl? + 6??? 正文
格式說(shuō)明:
藍(lán)色字體:注釋
黃色背景:重要
綠色背景:注意
老男孩教育教學(xué)核心思想6重:重目標(biāo)、重思路、重方法欺矫、重實(shí)踐仰剿、重習(xí)慣、重總結(jié)
學(xué)無(wú)止境,老男孩教育成就你人生的起點(diǎn)!
聯(lián)系方式:
網(wǎng)站運(yùn)維QQ交流群:
Linux?385168604架構(gòu)師390642196
Python 29215534大數(shù)據(jù)421358633
官方網(wǎng)站:
目錄
學(xué)無(wú)止境,老男孩教育成就你人生的起點(diǎn)皿渗!... 1
第1章 如何調(diào)試定時(shí)任務(wù)... 1
1.查看錯(cuò)誤日志? /var/log/cron?? **********. 1
1.2.1 No space left on device. 1
2.9用戶(hù)環(huán)境變量及bash -4.2解決方法... 10
[if !supportLists]第1章 [endif]如何調(diào)試定時(shí)任務(wù)
[if !supportLists]1.1 [endif]?
1.查看錯(cuò)誤日志?/var/log/cron?? **********
學(xué)會(huì)最小化排除:
>/var/log/cron
先清空然后再測(cè)試和觀察斩芭。
[if !supportLists]1.???? [endif]增加執(zhí)行任務(wù)頻率調(diào)試任務(wù)
把頻率調(diào)快。************
[if !supportLists]2.???? [endif]把定時(shí)任務(wù)執(zhí)行時(shí)間比當(dāng)前時(shí)間拖后五分鐘
00 03
執(zhí)行乐疆,假設(shè)當(dāng)前五點(diǎn)划乖,就調(diào)成05
05,有個(gè)五分鐘的提前量挤土。用來(lái)測(cè)試琴庵。
[if !supportLists]3.???? [endif]調(diào)整系統(tǒng)時(shí)間,把系統(tǒng)時(shí)間往前調(diào)整仰美。
[if !supportLists]4.???? [endif]通過(guò)腳本日志輸出調(diào)試定時(shí)任務(wù)注意點(diǎn):有時(shí)候程序只能執(zhí)行一次迷殿,一定要在測(cè)試環(huán)境測(cè)試好,然后直接復(fù)制到正式
代碼發(fā)布流程:
辦公室測(cè)試環(huán)境==èIDC機(jī)房測(cè)試環(huán)境==èIDC機(jī)房正式環(huán)境
防止在正式環(huán)境發(fā)生錯(cuò)誤咖杂,從而影響用戶(hù)體驗(yàn)庆寺,業(yè)務(wù)穩(wěn)定。
[if !supportLists]1.2 [endif]常見(jiàn)企業(yè)故障案例
[if !supportLists]1.2.1 [endif]No space
left on device
磁盤(pán)滿(mǎn)的報(bào)錯(cuò)诉字,原因往往是inode沾滿(mǎn)了
因?yàn)槎〞r(shí)任務(wù)沒(méi)有加&>/dev/null懦尝,定時(shí)任務(wù)執(zhí)行的時(shí)候吧輸出給系統(tǒng)root發(fā)郵件,郵件服務(wù)器postfix服務(wù)器默認(rèn)不開(kāi)壤圃,這些給系統(tǒng)發(fā)的郵件都會(huì)堆在臨時(shí)目錄(大量小文件存在)
/var/spool/clientmqueue/
/var/spool/Postfix/maildrop
[if !supportLists]1.2.2 [endif]Export變量生產(chǎn)案例
?定時(shí)任務(wù)在執(zhí)行腳本的時(shí)候陵霉,多數(shù)情況沒(méi)辦法加載環(huán)境變量,特別是(/etc/profile)
登錄后怎么操作都對(duì)伍绳,但是就是放到定時(shí)任務(wù)不行踊挠,
命令行:bash登錄方式
[if !supportLists]1.???? [endif]手動(dòng)登錄后,加載所有環(huán)境變量(~./bash_profile,~/.bash_rc,/etc/profile)
[if !supportLists]2.???? [endif]定時(shí)任務(wù)執(zhí)行腳本屬于非登錄狀態(tài)(/etc/bashrc)
所有運(yùn)維都會(huì)把變量放到此文件/etc/profile冲杀。吧這個(gè)文件里的變量定義在執(zhí)行的腳本中重新定義止毕。
具體為crond執(zhí)行shell是只能識(shí)別為數(shù)不多地系統(tǒng)環(huán)境變量,普通環(huán)境變量一般是無(wú)法識(shí)別的漠趁,如果在編寫(xiě)的腳本中需要使用變量,最好使用export重新聲明下該變量忍疾,以確保腳本正確執(zhí)行闯传。以后要將其作為一個(gè)開(kāi)發(fā)基本規(guī)范寫(xiě)上。
1[if !vml]
[endif]
1)在每周6的凌晨3:15執(zhí)行/home/shell/collect.pl卤妒,并將標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出到/dev/null設(shè)備甥绿,請(qǐng)寫(xiě)出crontab中的語(yǔ)句。
######oldboy...
15 03 * * 6 /bin/perl /home/shell/collect.pl>/dev/null 2>&1
2)crontab在11月份內(nèi)则披,每天的早上6點(diǎn)到12點(diǎn)中共缕,每隔2小時(shí)執(zhí)行一次/usr/bin/httpd.sh,
怎么實(shí)現(xiàn)士复?
00 6-12/2 * 11 * /bin/sh /usr/bin/httpd.sh&>/dev/null
[if !supportLists]第2章 [endif]用戶(hù)管理
[if !supportLists]2.1 [endif]用戶(hù)相關(guān)文件
/etc/passwd? 用戶(hù)所在文件
/etc/shadow? 密碼所在文件
Useradd oldboy 添加用戶(hù)實(shí)際就是修改上述兩個(gè)文件
Passwd oldboy? 改密碼實(shí)際上就是改密碼所在文件
用戶(hù)組相關(guān)文件
?/etc/group?用戶(hù)組所在文件
?/etc/gshadow?用戶(hù)組密碼所在文件(廢棄)
?Useradd oldboy添加用戶(hù)實(shí)際上也會(huì)修改上述兩個(gè)文件(因?yàn)橐獎(jiǎng)?chuàng)建同名用戶(hù)組)
Groupadd sa 添加用戶(hù)組就是修改上述兩個(gè)文件
[if !supportLists]2.2 [endif]/etc/passwd
[if !vml]
[endif]Linux是命令行管理图谷,平時(shí)輸入ls,cp翩活,誰(shuí)識(shí)別,幫我么吧我們想要的輸出呢便贵?
這個(gè)工具就是Bash (命令行解釋器)
CentOS7 默認(rèn)解釋器是bash
[if !vml]
[endif]
[if !supportLists]2.3[endif] Shadow
[if !vml]
[endif]
[if !supportLists]2.4[endif] Group
[if !vml]
[endif]
[if !supportLists]2.5 [endif]Gshadow
[if !vml]
[endif]
[if !supportLists]2.6 [endif]_shadow
[if !vml]
[endif]
[root@oldboyedu?~]#?useradd?gongli1?-c?"beautify?woman"?-d?/tmp?-e?'2020/10/20'
useradd:?warning:?the?home?directory?already?exists.
Not?copying?any?file?from?skel?directory?into?it.
[root@oldboyedu?~]#?tail?-1?/etc/passwd
gongli1:x:1005:1008:beautify?woman:/tmp:/bin/bash
[root@oldboyedu?~]#?chage?-l?gongli1
Last?password?change :?Mar?25,?2019
Password?expires :?never
Password?inactive :?never
Account?expires :?Oct?20,?2020??=================
Minimum?number?of?days?between?password?change:?0
Maximum?number?of?days?between?password?change:?99999
Number?of?days?of?warning?before?password?expires:?7
給開(kāi)發(fā)等人員添加用戶(hù)菠镇,盡量給截止時(shí)間。
/etc/default/useradd 文件
Useradd命令的配置文件
為什么默認(rèn)的shell就是/bin/bash
為什么默認(rèn)的家目錄在home下
為什么默認(rèn)的家目錄/home承璃、用戶(hù)名下面有很多隱藏文件利耍,從哪來(lái)呢
[if !vml]
[endif]
修改方法:
Useradd -D -s /bin/sh?? 相當(dāng)于vim編輯文件
[if !supportLists]2.7 [endif]MD5
md5sum 給文件設(shè)置指紋(計(jì)算和檢查MD5數(shù)字信息)
[root@oldboyedu ~]# md5sum /etc/passwd/etc/shadow /etc/group /etc/gshadow
2fe9f002726ed0a138d67cd44722f1a6? /etc/passwd
c2ca41415dca17f1a3dc3c286a9b9bff? /etc/shadow
c475144a13d87a400b5e16fe6bd70baf? /etc/group
659ef4f533df0fa7e457f87755fb1c27? /etc/gshadow
[root@oldboyedu ~]# useradd bingbing
文件發(fā)生變化
[root@oldboyedu ~]# md5sum /etc/passwd/etc/shadow /etc/group /etc/gshadow
39d60eff90ecd0326fe59e6464b464ad? /etc/passwd
0aa8d3b157f042876ec0190a2808a377? /etc/shadow
cee8bc6e85f9dfb9dc0a04e452e834f4? /etc/group
cdfd26351f409ee32fca53a22ad15912? /etc/gshadow
[root@oldboyedu ~]# grep bingbing? /etc/passwd /etc/shadow /etc/group/etc/gshadow
/etc/passwd:bingbing:x:1004:1006::/home/bingbing:/bin/bash
/etc/shadow:bingbing:!!:17980:0:99999:7:::
/etc/group:bingbing:x:1006:
/etc/gshadow:bingbing:!::
/etc/passwd文件:
[if !supportLists]2.8 [endif]Bash命令行解釋器
Linux是命令行管理,平時(shí)輸入ls,cp盔粹,誰(shuí)識(shí)別隘梨,幫我們把我們想要的輸出呢?
這個(gè)工具就是bash(命令行解釋器)舷嗡。 * 轴猎?[abc]
用戶(hù)登錄:輸入命令,希望哪個(gè)解釋器解釋?zhuān)ńY(jié)尾列決定)
/etc/passwd:bingbing:x:1004:1006::/home/bingbing:/bin/bash
CentOS7默認(rèn)解釋器是bash咬崔。
/etc/passwd里的解釋器
[root@oldboyedu ~]# awk -F ":"'{print $NF}' /etc/passwd|sort|uniq -c
?????6 /bin/bash
?????1 /bin/sync
?????1 /sbin/halt
????18 /sbin/nologin
?????1 /sbin/shutdown
[root@oldboyedu ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
bash是sh的擴(kuò)展税稼,sh是bash的軟連接。
[root@oldboyedu ~]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Mar? 4 11:15 /bin/sh -> bash
/bin/bash /server/scripts/bak.sh
[root@oldboyedu ~]# tail -1 /etc/shadow
bingbing?用戶(hù)
:!!??????密碼
:17980???最近更改密碼的時(shí)間
:0???????禁止修改密碼的天數(shù)
:99999???用戶(hù)必須更改口令的天數(shù)
:7???????警告更改密碼的期限
:???????????? ? 在用戶(hù)密碼過(guò)期之后到禁用賬戶(hù)的天數(shù)
:???????????? ? 從1970年1月1日起垮斯,到用戶(hù)被禁用的天數(shù)
:???????????? ? 保留
/etc/group
/etc/gshadow
[root@oldboyedu ~]# useradd gongli -u 888-s /sbin/nologin -M
[root@oldboyedu ~]# tail -1 /etc/passwd
gongli:x:888:1007::/home/gongli:/sbin/nologin
bingbing:x:1004:1006::/home/bingbing:/bin/bash
[root@oldboyedu ~]# useradd gongli1 -c"beautify woman" -d /tmp -e '2020/10/20'
useradd: warning: the home directory alreadyexists.
Not copying any file from skel directoryinto it.
[root@oldboyedu ~]# tail -1 /etc/passwd
gongli1:x:1005:1008:beautifywoman:/tmp:/bin/bash
[root@oldboyedu ~]# chage -l gongli1
Last password change?????????????????????????????????? : Mar 25, 2019
Password expires??????????????????????????? : never
Password inactive????????????????????????????????? : never
Account expires???????????????????????????????????? : Oct 20, 2020? =================
Minimum number of days between passwordchange???????????? : 0
Maximum number of days between passwordchange???????????? : 99999
Number of days of warning before passwordexpires?????? : 7
給開(kāi)發(fā)等人員添加用戶(hù)郎仆,盡量給截止時(shí)間。
[root@oldboyedu ~]# useradd gongli2 -gincahome
[root@oldboyedu ~]# id gongli2
uid=1006(gongli2) gid=1004(incahome)groups=1004(incahome)
/etc/default/useradd文件 useradd命令的配置文件
默認(rèn)shell就是/bin/bash
為什么默認(rèn)的家目錄在home下
為什么默認(rèn)的家目錄/home/用戶(hù)名下面有很多隱藏文件兜蠕,從哪來(lái)的扰肌。
就是/etc/default/useradd文件配置的。
[root@oldboyedu ~]# cat/etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
修改方法:
useradd -D -s /bin/sh?? 相當(dāng)于vim編輯/etc/default/useradd文件熊杨。
[root@oldboyedu ~]# useradd -D -s /bin/sh
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# cat/etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@oldboyedu ~]# useradd chenglong1
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# tail -1 /etc/passwd
chenglong1:x:1007:1009::/home/chenglong1:/bin/sh
[root@oldboyedu ~]# chage -l chenglong1
Last password change?????????????????????????????????? : Mar 25, 2019
Password expires??????????????????????????? : never
Password inactive????????????????????????????????? : never
Account expires???????????????????????????????????? : Oct 21, 2020
Minimum number of days between passwordchange???????????? : 0
Maximum number of days between password change???????????? : 99999
Number of days of warning before passwordexpires?????? : 7
[if !supportLists]2.9 [endif]用戶(hù)環(huán)境變量及bash -4.2解決方法
/etc/skel目錄曙旭,創(chuàng)建用戶(hù)默認(rèn)就會(huì)把此目錄下的文件拷貝到/home/用戶(hù)名下。
/etc/skel 為每個(gè)用戶(hù)提供用戶(hù)環(huán)境變量的目錄晶府。
[root@oldboyedu ~]# ls /etc/skel -A
.bash_logout? .bash_profile?.bashrc
===================================================
.bash_logout? .bash_profile?.bashrc用戶(hù)環(huán)境變量
登錄系統(tǒng)后桂躏,命令行出現(xiàn)如下提示:為什么?
[root@oldboyedu /home/chenglong1]# su -chenglong1
-sh-4.2$
-sh-4.2$
解答:用戶(hù)的環(huán)境變量缺失導(dǎo)致的川陆。
執(zhí)行如下命令解決
-sh-4.2$ cp /etc/skel/.bash* .
-sh-4.2$ source ./.bash_profile ./.bashrc
[chenglong1@oldboyedu ~]$
[chenglong1@oldboyedu ~]$
-sh-4.2$ exportPS1='[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\]\w\[\e[0m\]]\$'
[chenglong1@oldboyedu ~]$
[if !supportLists]2.10 [endif]Useradd和usermod
添加用戶(hù)要用的文件
/etc/login.defs 了解
添加用戶(hù)要用的文件
/etc/login.defs? /etc/skel /etc/default/useradd
編輯/etc/passwd,通過(guò)注釋來(lái)刪除剂习。
#chenglong1:x:1007:1009::/home/chenglong1:/bin/sh
"/etc/passwd" 30L, 1362Cwritten?????????????????????????????????????????????????????????????
[root@oldboyedu ~]# su - chenglong1
su: user chenglong1 does not exist
[root@oldboyedu ~]# useradd -u 9999 -s
/bin/sh -M -g sa -c "老男孩" -e "2019/5/1" zongsheng
[root@oldboyedu ~]# tail -1 /etc/passwd
zongsheng:x:9999:1003:老男孩:/home/zongsheng:/bin/sh
[root@oldboyedu ~]# id zongsheng
uid=9999(zongsheng) gid=1003(sa)groups=1003(sa)
[root@oldboyedu ~]# chage -l zongsheng
Last password change?????????????????????????????????? : Mar 25, 2019
Password expires??????????????????????????? : never
Password inactive????????????????????????????????? : never
Account expires???????????????????????????????????? : May 01, 2019
Minimum number of days between password change???????????? : 0
Maximum number of days between passwordchange???????????? : 99999
Number of days of warning before passwordexpires?????? : 7
usermod -u 8888 -s /bin/bash -m
/home/zongsheng -g incahome -c "男孩" -e "2020/5/1"
[root@oldboyedu ~]# usermod -u 8888 -s
/bin/bash -g incahome -c "男孩" -e "2020/5/1" zongsheng
[root@oldboyedu ~]# chage -l zongsheng
Last password change?????????????????????????????????? : Mar 25, 2019
Password expires??????????????????????????? : never
Password inactive????????????????????????????????? : never
Account expires???????????????????????????????????? : May 01, 2020
Minimum number of days between passwordchange???????????? : 0
Maximum number of days between passwordchange???????????? : 99999
Number of days of warning before passwordexpires?????? : 7
[root@oldboyedu ~]# id zongsheng
uid=8888(zongsheng) gid=1004(incahome)groups=1004(incahome)
[root@oldboyedu ~]# tail -1 /etc/passwd
zongsheng:x:8888:1004:男孩:/home/zongsheng:/bin/bash
You have new mail in /var/spool/mail/root
usermod -u 8888 -s /bin/bash -g incahome -c
"男孩" -e"2020/5/1" zongsheng
usermod -m /home/zongsheng zongsheng
[if !supportLists]2.11 [endif]不交互設(shè)置密碼:
方法1:
[root@oldboyedu ~]# echo 123456|passwd--stdin oldgirl
Changing password for user oldgirl.
passwd: all authentication tokens updatedsuccessfully.
方法2:
[root@oldboyedu ~]# echo 123456 >pass
[root@oldboyedu ~]# cat pass
123456
[root@oldboyedu ~]# passwd --stdin oldgirl
Changing password for user oldgirl.
passwd: all authentication tokens updatedsuccessfully.
[root@oldboyedu ~]# tail -4 /etc/passwd|awk-F ":" '{print $1":oldboy"}' >user.log
[root@oldboyedu ~]# cat user.log
gongli1:oldboy
gongli2:oldboy
chenglong1:oldboy
zongsheng:oldboy
chpasswd對(duì)密碼文件的要求是上述user.log
方法1:
[root@oldboyedu ~]# chpasswd
方法2:
[root@oldboyedu ~]# tail -4 /etc/passwd|awk-F ":" '{print $1":oldboy"}'|chpass
[if !vml]
[endif]
???????????????????????????????????????????????????????????????????????????? 編輯/etc/passwd ,通過(guò)注釋來(lái)刪除
[if !vml]
[endif]