Day18
定時(shí)任務(wù)要領(lǐng):
* * * * * ?/bin/sh /server/scripts/bak.sh &>/dev/null
?
如何調(diào)試定時(shí)任務(wù)?
1刚照、查看錯誤日志*****
/var/log/cron
學(xué)會最小化排除:
先清空无畔,>/var/log/cron
然后在測試和觀察浑彰。
2闸昨、增加執(zhí)行任務(wù)頻率調(diào)試任務(wù) *****
把頻率調(diào)快饵较。
?
3循诉、把定時(shí)任務(wù)執(zhí)行時(shí)間比當(dāng)前時(shí)間拖后5分鐘
00 03執(zhí)行茄猫,當(dāng)前5:00划纽,咱們就把時(shí)間調(diào)整 05 05,有個5分鐘提前量勇劣。
4比默、調(diào)整系統(tǒng)時(shí)間(測試環(huán)境還湊合)
00 03執(zhí)行盆犁,把系統(tǒng)時(shí)間調(diào)整到2:55谐岁。
?
5伊佃、通過腳本日志輸出調(diào)試定時(shí)任務(wù)
定時(shí)任務(wù)輸出
* * * * * ?/bin/sh /server/scripts/bak.sh &>/tmp/bak.log
腳本輸出:
tar zcvf oldboy.tar.gz ?./data &>/tmp/tar.log
?
注意點(diǎn):
有時(shí)候程序只能執(zhí)行一次锭魔。一定要在測試環(huán)境測試好迷捧,然后直接復(fù)制到正式漠秋。
?
代碼發(fā)布流程:
辦公室測試環(huán)境===>IDC機(jī)房測試環(huán)境===>IDC機(jī)房正式環(huán)境
?
防止在正式環(huán)境發(fā)生錯誤庆锦,從而影響用戶體驗(yàn),業(yè)務(wù)穩(wěn)定尿扯。
?
?
?
企業(yè)故障案例:No space left on device常見企業(yè)故障案例
磁盤滿的報(bào)錯衷笋,原因往往是inode被占滿了辟宗。
df -i
因?yàn)槎〞r(shí)任務(wù)沒有加&>/dev/null泊脐,定時(shí)任務(wù)執(zhí)行的時(shí)候把輸出給系統(tǒng)root發(fā)郵件容客。
郵件服務(wù)器postfix服務(wù)默認(rèn)不開耘柱,這些給系統(tǒng)發(fā)的郵件就會堆在臨時(shí)目錄(大量小文件存在)
/var/spool/clientmqueue/ ?????6
/var/spool/postfix/maildrop/ ?7
?
?
企業(yè)故障案例:程序通過手動可以執(zhí)行调煎,放定時(shí)任務(wù)里不執(zhí)行士袄。
export變量生產(chǎn)案例
?
定時(shí)任務(wù)在執(zhí)行腳本的時(shí)候娄柳,多數(shù)情況沒辦法加載環(huán)境變量赤拒,特別是/etc/profile挎挖。
登錄后怎么操作都對蕉朵,但是就是放到定時(shí)任務(wù)不行始衅。
?
命令行:bash登錄方式
1.手工登錄后蝙茶,加載所有環(huán)境變量(~/.bash_profile,~/.bash_rc,/etc/profile,/etc/bashrc)
2.定時(shí)任務(wù)執(zhí)行腳本屬于非登錄狀態(tài)(/etc/bashrc)尸闸。
實(shí)踐檢驗(yàn),定時(shí)任務(wù)能否自動加載/etc/bashrc和/etc/profile文件畸肆。
?
所有運(yùn)維都會把變量放到此文件/etc/profile轴脐,把這個文件里的變量定義
在執(zhí)行的腳本中重新定義大咱。
?
具體為crond執(zhí)行Shell時(shí)只能識別為數(shù)不多的系統(tǒng)環(huán)境變量碴巾,普通環(huán)境變量一般是無法識別的厦瓢,如果在編寫的腳本中需要使用變量煮仇,最好使用export重新聲明下該變量,以確保腳本正確執(zhí)行夹姥。以后要將其作為一個開發(fā)基本規(guī)范寫上佃声。
?
?
1)在每周6的凌晨3:15執(zhí)行/home/shell/collect.pl圾亏,并將標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤輸出到/dev/null設(shè)備志鹃,請寫出crontab中的語句缰趋。
######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
?
?
老男孩Linux58期day18
用戶管理知識:
?
用戶相關(guān)文件:
/etc/passwd用戶所在文件
/etc/shadow密碼所在文件
useradd oldboy添加用戶實(shí)際上就是修改上述兩個文件
passwd oldboy改密碼實(shí)際上就是修改密碼所在文件
?
?
用戶組相關(guān)文件:
/etc/group用戶組所在文件
/etc/gshadow用戶組密碼所在文件(廢棄狀態(tài))
useradd oldboy添加用戶實(shí)際上也會修改上述兩個文件(因?yàn)橐獎?chuàng)建同名的用戶組)
groupadd sa添加用戶組就是修改上述兩個文件
?
?
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文件:
?
Linux是命令行管理,平時(shí)輸入ls,cp些侍,誰識別岗宣,幫我們把我們想要的輸出呢耗式?
這個工具就是bash(命令行解釋器)。 * 娱挨? [abc]
?
?
用戶登錄:輸入命令跷坝,希望哪個解釋器解釋(結(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用戶
:!!密碼
:17980最近更改密碼的時(shí)間
:0禁止修改密碼的天數(shù)
:99999用戶必須更改口令的天數(shù)
:7警告更改密碼的期限
: 在用戶密碼過期之后到禁用賬戶的天數(shù)
: 從1970年1月1日起,到用戶被禁用的天數(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 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
?
給開發(fā)等人員添加用戶袖订,盡量給截止時(shí)間洛姑。
?
?
[root@oldboyedu ~]# useradd gongli2 -g incahome
[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/用戶名下面有很多隱藏文件参咙,從哪來的蕴侧。
?
就是/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 password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
?
?
/etc/skel目錄敏储,創(chuàng)建用戶默認(rèn)就會把此目錄下的文件拷貝到/home/用戶名下妥箕。
/etc/skel為每個用戶提供用戶環(huán)境變量的目錄矾踱。
[root@oldboyedu ~]# ls /etc/skel -A
.bash_logout ?.bash_profile ?.bashrc
===================================================
.bash_logout ?.bash_profile ?.bashrc用戶環(huán)境變量
?
登錄系統(tǒng)后呛讲,命令行出現(xiàn)如下提示:為什么?
[root@oldboyedu /home/chenglong1]# su - chenglong1
-sh-4.2$
-sh-4.2$
解答:用戶的環(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$ export PS1='[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\$'
[chenglong1@oldboyedu ~]$
?
添加用戶要用的文件
/etc/login.defs了解
?
添加用戶要用的文件
/etc/login.defs ?/etc/skel /etc/default/useradd
?
?
編輯/etc/passwd,通過注釋來刪除。
#chenglong1:x:1007:1009::/home/chenglong1:/bin/sh
"/etc/passwd" 30L, 1362C written ?????????????????????????????????????????????????????????????
[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 password change : 99999
Number of days of warning before password expires : 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 password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 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
?
不交互設(shè)置密碼:
方法1:
[root@oldboyedu ~]# echo 123456|passwd --stdin oldgirl
Changing password for user oldgirl.
passwd: all authentication tokens updated successfully.
?
方法2:
[root@oldboyedu ~]# echo 123456 >pass
[root@oldboyedu ~]# cat pass
123456
[root@oldboyedu ~]# passwd --stdin oldgirl <pass
Changing password for user oldgirl.
passwd: all authentication tokens updated successfully.
?
?
[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對密碼文件的要求是上述user.log
?
方法1:
[root@oldboyedu ~]# chpasswd<user.log
方法2:
[root@oldboyedu ~]# tail -4 /etc/passwd|awk -F ":" '{print $1":oldboy"}'|chpasswd
?
?
作業(yè):批量創(chuàng)建10個用戶锐朴,oldboy01-oldboy10,請?jiān)O(shè)置和用戶名相同的密碼焚志。
?
?
下節(jié)命令:
chage和passwd對比
groupadd
groupdel
id whoami ?w ?last lastlog
su sudo visudo
?
Day19
?[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : never
Password inactive : never
Account expires : never
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
[root@oldboyedu ~]# chage -E "2020/10/1" oldboy
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : never
Password inactive : never
Account expires : Oct 01, 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
范例14-17:創(chuàng)建新用戶range,要求該用戶7天內(nèi)不能更改密碼壶谒,
60天以后必須修改密碼佃迄,過期前10天通知用戶呵俏,過期后30天后禁止用戶登錄。
chage -m7 -M60 -W10 -I30 oldboy
聯(lián)系英文:
Options:
? -d, --lastday LAST_DAY? ? ? ? set date of last password change to LAST_DAY
? -E, --expiredate EXPIRE_DATE? set account expiration date to EXPIRE_DATE
? -h, --help? ? ? ? ? ? ? ? ? ? display this help message and exit
? -I, --inactive INACTIVE? ? ? set password inactive after expiration
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? to INACTIVE
? -l, --list? ? ? ? ? ? ? ? ? ? show account aging information
? -m, --mindays MIN_DAYS? ? ? ? set minimum number of days before password
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? change to MIN_DAYS
? -M, --maxdays MAX_DAYS? ? ? ? set maximim number of days before password
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? change to MAX_DAYS
? -R, --root CHROOT_DIR? ? ? ? directory to chroot into
? -W, --warndays WARN_DAYS? ? ? set expiration warning days to WARN_DAYS
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : never
Password inactive : never
Account expires : Oct 01, 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
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# chage -m7 -M60 -W10 -I30 oldboy
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : Dec 06, 2020
Password inactive : Jan 05, 2021
Account expires : Oct 01, 2020
Minimum number of days between password change : 7
Maximum number of days between password change : 60
Number of days of warning before password expires : 10
passwd -n70 -x600 -w100 -i300 oldboy
[root@oldboyedu ~]# passwd -n70 -x600 -w100 -i300 oldboy
Adjusting aging data for user oldboy.
passwd: Success
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : May 30, 2022
Password inactive : Mar 26, 2023
Account expires : Oct 01, 2020
Minimum number of days between password change : 70
Maximum number of days between password change : 600
Number of days of warning before password expires : 100
-n, --minimum DAYS
? ? ? ? ? ? ? This? will? set? the? minimum password lifetime, in days, if the user's account supports password life‐
? ? ? ? ? ? ? times.? Available to root only.
-x, --maximum DAYS
? ? ? ? ? ? ? This will set the maximum password lifetime, in days, if the user's? account? supports? password? life‐
? ? ? ? ? ? ? times.? Available to root only.
-w, --warning DAYS
? ? ? ? ? ? ? This? will? set? the number of days in advance the user will begin receiving warnings that her password
? ? ? ? ? ? ? will expire, if the user's account supports password lifetimes.? Available to root only.
-i, --inactive DAYS
[root@oldboyedu ~]# su - oldboy -c pwd
/home/oldboy
[root@oldboyedu ~]# su - oldboy -c whoami
oldboy
怎么用su。
1啤斗、先登錄普通用戶钮莲,沒事不允許登錄root崔拥。
只有執(zhí)行的任務(wù)需要root權(quán)限的時(shí)候才允許你登錄root链瓦。
系統(tǒng)維護(hù)通道。
用su管理肥卡,必須知道root密碼,安全隱患璃哟。
10個運(yùn)維,都得知道root密碼铐伴,安全隱患当宴。
需求:
1户矢、不用知道root密碼還能管理服務(wù)器捌年。
2礼预、最小化管理服務(wù)器托酸,想關(guān)機(jī)获高,就只給你halt權(quán)限。
sudo命令:
可以以最小化的權(quán)限(單個命令)摊趾,執(zhí)行命令時(shí)擁有root用戶的權(quán)限
SUID 針對命令砾层,任何用戶執(zhí)行命令都有root身份。? ? 任何用戶執(zhí)行某個命令:模糊
SUDO 針對用戶侨糟,給某個用戶以root身份執(zhí)行某個命令秕重。指定用戶執(zhí)行某個命令:具體。
如何編輯配置sudo凳兵?
sudo是一個提權(quán)的命令(對應(yīng)權(quán)限通過讀取/etc/sudoers(嚴(yán)格語法)文件實(shí)現(xiàn)的)
配置/etc/sudoers可以使用visudo命令留荔,或vim /etc/sudoers(不推薦)
[oldboy@oldboyedu ~]$ ls /root
ls: cannot open directory /root: Permission denied
完成上面的動作杰妓。
給oldboy用戶巷挥,針對ls設(shè)置權(quán)限倍宾。
visudo進(jìn)入編輯狀態(tài),100G
## Allow root to run any commands anywhere
root? ? ALL=(ALL)? ? ? ALL
oldboy? ALL=(ALL)? ? ? /usr/bin/ls,/bin/cp? #<===增加一行
用戶? ? ? ? 主機(jī)=(角色)? ? ? 命令
注意:
1怔锌、路徑要全路徑:
which cp查
2、不要vim /etc/sudoers編輯岛杀,如果非要用編輯完visudo -c檢查語法
oldboy是管理員类嗤,并且不要密碼:
oldboy? ALL=(ALL)? ? ? NOPASSWD: ALL
登錄后切到root運(yùn)行土浸。
[oldboy@oldboyedu ~]$ sudo su - root
Last login: Tue Mar 26 10:32:57 CST 2019 on pts/3
[root@oldboyedu ~]#
為了安全禁止root遠(yuǎn)程連接linux泪酱。
工作中如何登錄墓阀?使用普通用戶登錄然后利用sudo提權(quán)到root经伙。
Wecomle to oldboy training 58期帕膜。
[oldboy@oldboyedu ~]$ whoami
oldboy
[oldboy@oldboyedu ~]$ sudo ls /root
a.txt? c.txt? data1? etc? ? ? oldboy ? ? oldboy_b ? ? oldboy_soft_link? pass? test.txt ? user.log
b.txt? d? ? ? d.txt? grep.txt? oldboy_1.txt? oldboyedu.txt? oldboy.txt ? ? ? test? test.txt.ori
[oldboy@oldboyedu ~]$
[oldboy@oldboyedu ~]$
[oldboy@oldboyedu ~]$ sudo su -
Last login: Tue Mar 26 11:42:09 CST 2019 on pts/0
[root@oldboyedu ~]#
[root@oldboyedu ~]# su - oldboy
Last login: Tue Mar 26 10:44:08 CST 2019 on pts/3
[oldboy@oldboyedu ~]$ ls /root
ls: cannot open directory /root: Permission denied
[oldboy@oldboyedu ~]$ sudo -l
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
? ? #1) Respect the privacy of others.
? ? #2) Think before you type.
? ? #3) With great power comes great responsibility.
[sudo] password for oldboy:
Matching Defaults entries for oldboy on oldboyedu:
User oldboy may run the following commands on oldboyedu:
? ? (ALL) /bin/ls
[oldboy@oldboyedu ~]$ sudo ls /root
a.txt? c.txt? data1? etc? ? ? oldboy ? ? oldboy_b ? ? oldboy_soft_link? pass? test.txt ? user.log
b.txt? d? ? ? d.txt? grep.txt? oldboy_1.txt? oldboyedu.txt? oldboy.txt ? ? ? test? test.txt.ori
[oldboy@oldboyedu ~]$ ls /root
ls: cannot open directory /root: Permission denied
[oldboy@oldboyedu ~]$
練習(xí):給普通用戶oldgirl增加添加用戶和刪除用戶權(quán)限垮刹。
root用戶找回密碼
https://baijiahao.baidu.com/s?id=1614000108255096283&wfr=spider&for=pc
Day20
企業(yè)生產(chǎn)環(huán)境主流磁盤的相關(guān)信息對比:
企業(yè)生產(chǎn)場景普及程度SAS>SSD>SATA
單位容量對比性能和價(jià)格:SSD>SAS>SATA(一塊SSD和一塊SATA)
單位價(jià)格購買磁盤容量:SATA>SAS>SSD
512字節(jié)*每磁道扇區(qū)數(shù)=磁道大小
磁道大小*磁道數(shù)=盤面大小
盤面大小*磁頭數(shù)=磁盤容量
磁盤容量=盤面大小*磁頭數(shù)
磁盤容量=磁道大小*磁道數(shù)*磁頭數(shù)
磁盤容量=512字節(jié)*每磁道扇區(qū)數(shù)*磁道數(shù)*磁頭數(shù)
磁盤容量=柱面大小*柱面數(shù)(磁道數(shù))
柱面大小=磁道大小*磁頭數(shù)
磁道大小=512字節(jié)*每磁道扇區(qū)數(shù)
磁盤容量=512字節(jié)*每磁道扇區(qū)數(shù)*柱面數(shù)(磁道數(shù))*磁頭數(shù)
磁盤容量=512字節(jié)*每磁道扇區(qū)數(shù)*磁道數(shù)*磁頭數(shù)
磁盤容量=512字節(jié)*每磁道扇區(qū)數(shù)*柱面數(shù)(磁道數(shù))
*磁頭數(shù)磁盤容量=512*63*2610*255字節(jié)
day21
Raid是廉價(jià)冗余磁盤陣列(Redundant Array of Inexpensive Disk)的簡稱, 有時(shí)也簡稱磁盤陣列(Disk Array)达吞。
RAID分為兩類:
軟RAID,系統(tǒng)層面實(shí)現(xiàn)的荒典,性能差。
硬RAID寺董,硬件層面實(shí)現(xiàn)的覆糟,性能好。
? ? ? ? 主板板載RAID:功能弱遮咖,0滩字,1
獨(dú)立RAID卡:功能強(qiáng),0,1,5,10 *****工作選擇盯滚。
LVM全稱(Logic Volume Management (Manager))邏輯卷管理,它的最大用途是可以【靈活的管理磁盤的容量】踢械,讓磁盤分區(qū)可以隨意放大或縮小,便于更好的應(yīng)用磁盤的剩余空間魄藕,如果過于強(qiáng)調(diào)性能與備份内列,那么還是應(yīng)該使用RAID功能,而不是LVM背率。
LVM是軟件層面實(shí)現(xiàn)的话瞧,性能太低。性能降低5-10%寝姿。
買服務(wù)器插滿磁盤交排,分區(qū)規(guī)劃好,永遠(yuǎn)都不需要LVM饵筑。
RAID1又稱為Mirror或Mirroring(鏡像)埃篓,
它的宗旨是最大限度的保證用戶數(shù)據(jù)的可用性和可修復(fù)性。
RAID1的操作方式是把用戶寫入一個磁盤的數(shù)據(jù)百分之百地自動復(fù)制到另外
一個磁盤上根资,從而實(shí)現(xiàn)存儲雙份的數(shù)據(jù)架专。
RAID5描述 中庸
RAID5是一種存儲性能、數(shù)據(jù)安全和存儲成本兼顧的存儲解決方案玄帕。
RAID5需要三塊或以上的物理磁盤部脚,可以提供熱備盤實(shí)現(xiàn)故障的恢復(fù);
采用【奇偶校驗(yàn)】裤纹,可靠性強(qiáng)委刘,且只有同時(shí)損壞兩塊硬盤時(shí)數(shù)據(jù)才會完全損壞,
只損壞一塊硬盤時(shí),系統(tǒng)會根據(jù)存儲的奇偶校驗(yàn)位重建數(shù)據(jù)锡移,臨時(shí)提供服務(wù)呕童;此時(shí)如果有熱備盤,系統(tǒng)還會自動在熱備盤上重建故障磁盤上的數(shù)據(jù)罩抗;
分區(qū)知識:
回憶:
(1)什么是分區(qū)拉庵?
磁盤分區(qū)就相當(dāng)于給磁盤打隔斷。
(2)磁盤和分區(qū)在Linux里的命名套蒂。
IDE /dev/hda hdb
SCSI sda sdb
分區(qū)數(shù)字表示:sda1 sda2 sda3
(3)磁盤分區(qū)類型和特點(diǎn):
1钞支、主分區(qū)(primary)P
? 1)系統(tǒng)中必須要存在的分區(qū),系統(tǒng)盤選擇主分區(qū)安裝操刀。
? 2)數(shù)字編號只能是1-4.sda1烁挟、sda2、sda3骨坑、sda4撼嗓。
? 3)主分區(qū)最多四個,最少一個欢唾。
2且警、擴(kuò)展分區(qū)(extend)E
? 1)相當(dāng)于一個獨(dú)立的小磁盤。獨(dú)立的分區(qū)表礁遣,不能獨(dú)立存在斑芜。
? 2)有獨(dú)立的分區(qū)表。
? 3)不能獨(dú)立存在祟霍,即不能直接存放數(shù)據(jù)杏头。
? 4)必須在擴(kuò)展分區(qū)上建立邏輯分區(qū)才能存放數(shù)據(jù)。
? 5)占用主分區(qū)的編號(主分區(qū)+擴(kuò)展分區(qū))之和最多4個沸呐。
? 6)擴(kuò)展分區(qū)可以沒有醇王,最多只能有一個。
3崭添、邏輯分區(qū)(logic)L
? 2)數(shù)字編號只能是從5開始寓娩。
? 3)存放于擴(kuò)展分區(qū)之上。
? 4)存放任意普通數(shù)據(jù)呼渣。
磁盤分區(qū)注意事項(xiàng)要點(diǎn)
一塊硬盤的分區(qū)方式只能為如下組合之一:
(1)任意多個主分區(qū)根暑,但要求1≤主分區(qū)數(shù)量≤4。
例如:一個硬盤可以分為4個主分區(qū)3個主分區(qū)2個主分區(qū)或1個主分區(qū)徙邻。
(2)擴(kuò)展分區(qū)可以和主分區(qū)組合,但要求2≤(主分區(qū)+擴(kuò)展分區(qū))數(shù)量≤4)畸裳。
例如:3個主分區(qū)+1個擴(kuò)展分區(qū)或2個主分區(qū)+1個擴(kuò)展分區(qū)或1個主分區(qū)+1個擴(kuò)展分區(qū)缰犁。
當(dāng)總分區(qū)的數(shù)量大于4個的時(shí)候,必須提前分一個擴(kuò)展分區(qū),擴(kuò)展分區(qū)最多只能有一個帅容。
(3)如果要分成四個磁盤分區(qū)的話颇象,那么最多就是可以:
P + P + P + P
P + P + P + E
問題:如果給一個磁盤分6個分區(qū)有哪些方案,同時(shí)寫出分區(qū)/dev/sda(數(shù)字)并徘。
3P+1E(3L)? 1 2 3 5 6 7
2P+1E(4L)? 12 5678
1P+1E(5L)? 1 56789
分區(qū)分完了遣钳,空間還有剩余浪費(fèi)掉空間。
P + P + P + P
(4)磁盤分區(qū)工作原理:
磁盤是按柱面分區(qū)的麦乞。
磁盤分區(qū)登記的地點(diǎn)蕴茴,磁盤分區(qū)表。
磁盤分區(qū)表存放分區(qū)結(jié)果信息的姐直。
磁盤分區(qū)表位置倦淀,0磁道0磁頭1扇區(qū)(512字節(jié))
占用1扇區(qū)的前446字節(jié)(系統(tǒng)引導(dǎo)信息的)后面的64字節(jié)(分區(qū)表),
剩下2個字節(jié)分區(qū)結(jié)束標(biāo)志声畏。
磁盤分區(qū)表的容量是有限的撞叽,64字節(jié),一個分區(qū)固定占16字節(jié)插龄。
64/16=4分區(qū)(主分區(qū)+擴(kuò)展分區(qū))
(5)磁盤分區(qū)實(shí)戰(zhàn)
磁盤分區(qū)關(guān)鍵就是修改64字節(jié)的的分區(qū)表而已愿棋。
磁盤分區(qū)常用命令fdisk,修改MBR分區(qū)表均牢,MBR格式糠雨。
缺陷,被修改的磁盤大小不能大于2T膨处。
磁盤分區(qū)其他命令parted,gpt分區(qū)格式见秤,既能修改小于2T也能修改大于2T的磁盤。
小于2T就用fdisk
大于2T就用parted
[root@oldboyedu ~]# fdisk -l
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000cd234
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sda1? *? ? ? ? 2048? ? ? 526335? ? ? 262144? 83? Linux
/dev/sda2? ? ? ? ? 526336? ? 2099199? ? ? 786432? 82? Linux swap / Solaris
/dev/sda3? ? ? ? 2099200? ? 41943039? ? 19921920? 83? Linux
[root@oldboyedu ~]# fdisk /dev/sdb? #<==開始給sdb磁盤分區(qū)
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xbc9b0906.
Command (m for help): m? #<==查看分區(qū)的幫助信息真椿。
Command action
? a? toggle a bootable flag
? b? edit bsd disklabel
? c? toggle the dos compatibility flag
? d? delete a partition? ? ? ? ? ? #<==刪除一個分區(qū)鹃答。
? g? create a new empty GPT partition table
? G? create an IRIX (SGI) partition table
? l? list known partition types
? m? print this menu? ? ? ? ? ? ? #<==查看分區(qū)的幫助信息。
? n? add a new partition? ? ? ? ? #<==添加一個新分區(qū)突硝。
? o? create a new empty DOS partition table
? p? print the partition table? ? #<==查看分區(qū)結(jié)果信息测摔。
? q? quit without saving changes? #<==退出不保存。
? s? create a new empty Sun disklabel
? t? change a partition's system id
? u? change display/entry units
? v? verify the partition table
? w? write table to disk and exit? #<==保存并退出解恰。
? x? extra functionality (experts only)
Command (m for help):
開始實(shí)踐:^
問題:如果給一個磁盤分6個分區(qū)有哪些方案锋八,同時(shí)寫出分區(qū)/dev/sda(數(shù)字)。
3P+1E(3L)? 1 2 3 5 6 7
2P+1E(4L)? 12 5678
1P+1E(5L)? 1 56789
用下面方案:
3P+1E(3L)? 1 2 3 5 6 7 每個150M
[root@oldboyedu ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x827687e9.
Command (m for help): ^C
[root@oldboyedu ~]#
[root@oldboyedu ~]#
[root@oldboyedu ~]#
[root@oldboyedu ~]#
[root@oldboyedu ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xbc9b0906.
Command (m for help): m
Command action
? a? toggle a bootable flag
? b? edit bsd disklabel
? c? toggle the dos compatibility flag
? d? delete a partition
? g? create a new empty GPT partition table
? G? create an IRIX (SGI) partition table
? l? list known partition types
? m? print this menu
? n? add a new partition
? o? create a new empty DOS partition table
? p? print the partition table
? q? quit without saving changes
? s? create a new empty Sun disklabel
? t? change a partition's system id
? u? change display/entry units
? v? verify the partition table
? w? write table to disk and exit
? x? extra functionality (experts only)
Command (m for help): n
Partition type:
? p? primary (0 primary, 0 extended, 4 free)
\
? e? extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +150M
Partition 1 of type Linux and of size 150 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
Command (m for help): n? ? ? ? ?
Partition type:
? p? primary (1 primary, 0 extended, 3 free)
? e? extended
Select (default p): p
Partition number (2-4, default 2):
First sector (309248-2097151, default 309248):
Using default value 309248
Last sector, +sectors or +size{K,M,G} (309248-2097151, default 2097151): +150m
Unsupported suffix: 'm'.
Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)
? ? ? ? ? ? 2^N: K? (KibiByte), M? (MebiByte), G? (GibiByte)
Last sector, +sectors or +size{K,M,G} (309248-2097151, default 2097151): +150M
Partition 2 of type Linux and of size 150 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
Command (m for help): n
Partition type:
? p? primary (2 primary, 0 extended, 2 free)
? e? extended
Select (default p): p
Partition number (3,4, default 3):
First sector (616448-2097151, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-2097151, default 2097151): +150M
Partition 3 of type Linux and of size 150 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
Command (m for help): n
Partition type:
? p? primary (3 primary, 0 extended, 1 free)
? e? extended
Select (default e): p
Selected partition 4
First sector (923648-2097151, default 923648):
Using default value 923648
Last sector, +sectors or +size{K,M,G} (923648-2097151, default 2097151): +150M
Partition 4 of type Linux and of size 150 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
/dev/sdb4? ? ? ? ? 923648? ? 1230847? ? ? 153600? 83? Linux
Command (m for help): n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.
Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
Command (m for help): n
Partition type:
? p? primary (3 primary, 0 extended, 1 free)
? e? extended
Select (default e): e
Selected partition 4
First sector (923648-2097151, default 923648):
Using default value 923648
Last sector, +sectors or +size{K,M,G} (923648-2097151, default 2097151):
Using default value 2097151
Partition 4 of type Extended and of size 573 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
/dev/sdb4? ? ? ? ? 923648? ? 2097151? ? ? 586752? ? 5? Extended
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (925696-2097151, default 925696):
Using default value 925696
Last sector, +sectors or +size{K,M,G} (925696-2097151, default 2097151): +150M
Partition 5 of type Linux and of size 150 MiB is set
Command (m for help): p?
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
/dev/sdb4? ? ? ? ? 923648? ? 2097151? ? ? 586752? ? 5? Extended
/dev/sdb5? ? ? ? ? 925696? ? 1232895? ? ? 153600? 83? Linux
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (1234944-2097151, default 1234944):
Using default value 1234944
Last sector, +sectors or +size{K,M,G} (1234944-2097151, default 2097151): +150M
Partition 6 of type Linux and of size 150 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
/dev/sdb4? ? ? ? ? 923648? ? 2097151? ? ? 586752? ? 5? Extended
/dev/sdb5? ? ? ? ? 925696? ? 1232895? ? ? 153600? 83? Linux
/dev/sdb6? ? ? ? 1234944? ? 1542143? ? ? 153600? 83? Linux
Command (m for help): n
All primary partitions are in use
Adding logical partition 7
First sector (1544192-2097151, default 1544192):
Using default value 1544192
Last sector, +sectors or +size{K,M,G} (1544192-2097151, default 2097151): +150M
Partition 7 of type Linux and of size 150 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbc9b0906
? Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System
/dev/sdb1? ? ? ? ? ? 2048? ? ? 309247? ? ? 153600? 83? Linux
/dev/sdb2? ? ? ? ? 309248? ? ? 616447? ? ? 153600? 83? Linux
/dev/sdb3? ? ? ? ? 616448? ? ? 923647? ? ? 153600? 83? Linux
/dev/sdb4? ? ? ? ? 923648? ? 2097151? ? ? 586752? ? 5? Extended
/dev/sdb5? ? ? ? ? 925696? ? 1232895? ? ? 153600? 83? Linux
/dev/sdb6? ? ? ? 1234944? ? 1542143? ? ? 153600? 83? Linux
/dev/sdb7? ? ? ? 1544192? ? 1851391? ? ? 153600? 83? Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncin