Day18
定时任务要领:
* * * * * /bin/sh /server/scripts/bak.sh &>/dev/null
如何调试定时任务?
1、查看错误日志*****
/var/log/cron
学会最小化排除:
先清空,>/var/log/cron
然后在测试和观察。
2、增加执行任务频率调试任务 *****
把频率调快。
3、把定时任务执行时间比当前时间拖后5分钟
00 03执行,当前5:00,咱们就把时间调整 05 05,有个5分钟提前量。
4、调整系统时间(测试环境还凑合)
00 03执行,把系统时间调整到2:55。
5、通过脚本日志输出调试定时任务
定时任务输出
* * * * * /bin/sh /server/scripts/bak.sh &>/tmp/bak.log
脚本输出:
tar zcvf oldboy.tar.gz ./data &>/tmp/tar.log
注意点:
有时候程序只能执行一次。一定要在测试环境测试好,然后直接复制到正式。
代码发布流程:
办公室测试环境===>IDC机房测试环境===>IDC机房正式环境
防止在正式环境发生错误,从而影响用户体验,业务稳定。
企业故障案例:No space left on device常见企业故障案例
磁盘满的报错,原因往往是inode被占满了。
df -i
因为定时任务没有加&>/dev/null,定时任务执行的时候把输出给系统root发邮件。
邮件服务器postfix服务默认不开,这些给系统发的邮件就会堆在临时目录(大量小文件存在)
/var/spool/clientmqueue/ 6
/var/spool/postfix/maildrop/ 7
企业故障案例:程序通过手动可以执行,放定时任务里不执行。
export变量生产案例
定时任务在执行脚本的时候,多数情况没办法加载环境变量,特别是/etc/profile。
登录后怎么操作都对,但是就是放到定时任务不行。
命令行:bash登录方式
1.手工登录后,加载所有环境变量(~/.bash_profile,~/.bash_rc,/etc/profile,/etc/bashrc)
2.定时任务执行脚本属于非登录状态(/etc/bashrc)。
实践检验,定时任务能否自动加载/etc/bashrc和/etc/profile文件。
所有运维都会把变量放到此文件/etc/profile,把这个文件里的变量定义
在执行的脚本中重新定义。
具体为crond执行Shell时只能识别为数不多的系统环境变量,普通环境变量一般是无法识别的,如果在编写的脚本中需要使用变量,最好使用export重新声明下该变量,以确保脚本正确执行。以后要将其作为一个开发基本规范写上。
1)在每周6的凌晨3:15执行/home/shell/collect.pl,并将标准输出和标准错误输出到/dev/null设备,请写出crontab中的语句。
######oldboy...
15 03 * * 6 /bin/perl /home/shell/collect.pl >/dev/null 2>&1
2)crontab在11月份内,每天的早上6点到12点中,每隔2小时执行一次/usr/bin/httpd.sh,
怎么实现?
00 6-12/2 * 11 * /bin/sh /usr/bin/httpd.sh &>/dev/null
老男孩Linux58期day18
用户管理知识:
用户相关文件:
/etc/passwd用户所在文件
/etc/shadow密码所在文件
useradd oldboy添加用户实际上就是修改上述两个文件
passwd oldboy改密码实际上就是修改密码所在文件
用户组相关文件:
/etc/group用户组所在文件
/etc/gshadow用户组密码所在文件(废弃状态)
useradd oldboy添加用户实际上也会修改上述两个文件(因为要创建同名的用户组)
groupadd sa添加用户组就是修改上述两个文件
md5sum给文件设置指纹(计算和检查MD5数字信息)
[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
文件发生变化
[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是命令行管理,平时输入ls,cp,谁识别,帮我们把我们想要的输出呢?
这个工具就是bash(命令行解释器)。 * ? [abc]
用户登录:输入命令,希望哪个解释器解释(结尾列决定)
/etc/passwd:bingbing:x:1004:1006::/home/bingbing:/bin/bash
CentOS7默认解释器是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的扩展,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最近更改密码的时间
:0禁止修改密码的天数
:99999用户必须更改口令的天数
:7警告更改密码的期限
: 在用户密码过期之后到禁用账户的天数
: 从1970年1月1日起,到用户被禁用的天数
: 保留
/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
给开发等人员添加用户,尽量给截止时间。
[root@oldboyedu ~]# useradd gongli2 -g incahome
[root@oldboyedu ~]# id gongli2
uid=1006(gongli2) gid=1004(incahome) groups=1004(incahome)
/etc/default/useradd文件 useradd命令的配置文件
默认shell就是/bin/bash
为什么默认的家目录在home下
为什么默认的家目录/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相当于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目录,创建用户默认就会把此目录下的文件拷贝到/home/用户名下。
/etc/skel为每个用户提供用户环境变量的目录。
[root@oldboyedu ~]# ls /etc/skel -A
.bash_logout .bash_profile .bashrc
===================================================
.bash_logout .bash_profile .bashrc用户环境变量
登录系统后,命令行出现如下提示:为什么?
[root@oldboyedu /home/chenglong1]# su - chenglong1
-sh-4.2$
-sh-4.2$
解答:用户的环境变量缺失导致的。
执行如下命令解决
-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
不交互设置密码:
方法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
作业:批量创建10个用户,oldboy01-oldboy10,请设置和用户名相同的密码。
下节命令:
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:创建新用户range,要求该用户7天内不能更改密码,
60天以后必须修改密码,过期前10天通知用户,过期后30天后禁止用户登录。
chage -m7 -M60 -W10 -I30 oldboy
联系英文:
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。
只有执行的任务需要root权限的时候才允许你登录root。
系统维护通道。
用su管理,必须知道root密码,安全隐患。
10个运维,都得知道root密码,安全隐患。
需求:
1、不用知道root密码还能管理服务器。
2、最小化管理服务器,想关机,就只给你halt权限。
sudo命令:
可以以最小化的权限(单个命令),执行命令时拥有root用户的权限
SUID 针对命令,任何用户执行命令都有root身份。 任何用户执行某个命令:模糊
SUDO 针对用户,给某个用户以root身份执行某个命令。指定用户执行某个命令:具体。
如何编辑配置sudo?
sudo是一个提权的命令(对应权限通过读取/etc/sudoers(严格语法)文件实现的)
配置/etc/sudoers可以使用visudo命令,或vim /etc/sudoers(不推荐)
[oldboy@oldboyedu ~]$ ls /root
ls: cannot open directory /root: Permission denied
完成上面的动作。
给oldboy用户,针对ls设置权限。
visudo进入编辑状态,100G
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
oldboy ALL=(ALL) /usr/bin/ls,/bin/cp #<===增加一行
用户 主机=(角色) 命令
注意:
1、路径要全路径:
which cp查
2、不要vim /etc/sudoers编辑,如果非要用编辑完visudo -c检查语法
oldboy是管理员,并且不要密码:
oldboy ALL=(ALL) NOPASSWD: ALL
登录后切到root运行。
[oldboy@oldboyedu ~]$ sudo su - root
Last login: Tue Mar 26 10:32:57 CST 2019 on pts/3
[root@oldboyedu ~]#
为了安全禁止root远程连接linux。
工作中如何登录?使用普通用户登录然后利用sudo提权到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 ~]$
练习:给普通用户oldgirl增加添加用户和删除用户权限。
root用户找回密码
https://baijiahao.baidu.com/s?id=1614000108255096283&wfr=spider&for=pc
Day20
企业生产环境主流磁盘的相关信息对比:
企业生产场景普及程度SAS>SSD>SATA
单位容量对比性能和价格:SSD>SAS>SATA(一块SSD和一块SATA)
单位价格购买磁盘容量:SATA>SAS>SSD
512字节*每磁道扇区数=磁道大小
磁道大小*磁道数=盘面大小
盘面大小*磁头数=磁盘容量
磁盘容量=盘面大小*磁头数
磁盘容量=磁道大小*磁道数*磁头数
磁盘容量=512字节*每磁道扇区数*磁道数*磁头数
磁盘容量=柱面大小*柱面数(磁道数)
柱面大小=磁道大小*磁头数
磁道大小=512字节*每磁道扇区数
磁盘容量=512字节*每磁道扇区数*柱面数(磁道数)*磁头数
磁盘容量=512字节*每磁道扇区数*磁道数*磁头数
磁盘容量=512字节*每磁道扇区数*柱面数(磁道数)
*磁头数磁盘容量=512*63*2610*255字节
day21
Raid是廉价冗余磁盘阵列(Redundant Array of Inexpensive Disk)的简称, 有时也简称磁盘阵列(Disk Array)。
RAID分为两类:
软RAID,系统层面实现的,性能差。
硬RAID,硬件层面实现的,性能好。
主板板载RAID:功能弱,0,1
独立RAID卡:功能强,0,1,5,10 *****工作选择。
LVM全称(Logic Volume Management (Manager))逻辑卷管理,它的最大用途是可以【灵活的管理磁盘的容量】,让磁盘分区可以随意放大或缩小,便于更好的应用磁盘的剩余空间,如果过于强调性能与备份,那么还是应该使用RAID功能,而不是LVM。
LVM是软件层面实现的,性能太低。性能降低5-10%。
买服务器插满磁盘,分区规划好,永远都不需要LVM。
RAID1又称为Mirror或Mirroring(镜像),
它的宗旨是最大限度的保证用户数据的可用性和可修复性。
RAID1的操作方式是把用户写入一个磁盘的数据百分之百地自动复制到另外
一个磁盘上,从而实现存储双份的数据。
RAID5描述 中庸
RAID5是一种存储性能、数据安全和存储成本兼顾的存储解决方案。
RAID5需要三块或以上的物理磁盘,可以提供热备盘实现故障的恢复;
采用【奇偶校验】,可靠性强,且只有同时损坏两块硬盘时数据才会完全损坏,
只损坏一块硬盘时,系统会根据存储的奇偶校验位重建数据,临时提供服务;此时如果有热备盘,系统还会自动在热备盘上重建故障磁盘上的数据;
分区知识:
回忆:
(1)什么是分区?
磁盘分区就相当于给磁盘打隔断。
(2)磁盘和分区在Linux里的命名。
IDE /dev/hda hdb
SCSI sda sdb
分区数字表示:sda1 sda2 sda3
(3)磁盘分区类型和特点:
1、主分区(primary)P
1)系统中必须要存在的分区,系统盘选择主分区安装。
2)数字编号只能是1-4.sda1、sda2、sda3、sda4。
3)主分区最多四个,最少一个。
2、扩展分区(extend)E
1)相当于一个独立的小磁盘。独立的分区表,不能独立存在。
2)有独立的分区表。
3)不能独立存在,即不能直接存放数据。
4)必须在扩展分区上建立逻辑分区才能存放数据。
5)占用主分区的编号(主分区+扩展分区)之和最多4个。
6)扩展分区可以没有,最多只能有一个。
3、逻辑分区(logic)L
2)数字编号只能是从5开始。
3)存放于扩展分区之上。
4)存放任意普通数据。
磁盘分区注意事项要点
一块硬盘的分区方式只能为如下组合之一:
(1)任意多个主分区,但要求1≤主分区数量≤4。
例如:一个硬盘可以分为4个主分区3个主分区2个主分区或1个主分区。
(2)扩展分区可以和主分区组合,但要求2≤(主分区+扩展分区)数量≤4)。
例如:3个主分区+1个扩展分区或2个主分区+1个扩展分区或1个主分区+1个扩展分区。
当总分区的数量大于4个的时候,必须提前分一个扩展分区,扩展分区最多只能有一个。
(3)如果要分成四个磁盘分区的话,那么最多就是可以:
P + P + P + P
P + P + P + E
问题:如果给一个磁盘分6个分区有哪些方案,同时写出分区/dev/sda(数字)。
3P+1E(3L) 1 2 3 5 6 7
2P+1E(4L) 12 5678
1P+1E(5L) 1 56789
分区分完了,空间还有剩余浪费掉空间。
P + P + P + P
(4)磁盘分区工作原理:
磁盘是按柱面分区的。
磁盘分区登记的地点,磁盘分区表。
磁盘分区表存放分区结果信息的。
磁盘分区表位置,0磁道0磁头1扇区(512字节)
占用1扇区的前446字节(系统引导信息的)后面的64字节(分区表),
剩下2个字节分区结束标志。
磁盘分区表的容量是有限的,64字节,一个分区固定占16字节。
64/16=4分区(主分区+扩展分区)
(5)磁盘分区实战
磁盘分区关键就是修改64字节的的分区表而已。
磁盘分区常用命令fdisk,修改MBR分区表,MBR格式。
缺陷,被修改的磁盘大小不能大于2T。
磁盘分区其他命令parted,gpt分区格式,既能修改小于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磁盘分区
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):
开始实践:^
问题:如果给一个磁盘分6个分区有哪些方案,同时写出分区/dev/sda(数字)。
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