day2用户及用户组管理
1.useradd
2.userdel
3.usermod
4.groupadd
5.passwd
6.id
7.su
创建系统用户且不可登陆系统
useradd -r -s /sbin/nologin test
给test用户免交互添加密码
echo “123” | passwd –stdin test
删除test用户且把家目录也删掉
userdel -r test
更改test用户shell类型不可登陆
usermod -s /sbin/nologin test
更改test用户登录名为test1
usermod -l test test1
权限管理
给文件test添加所有人可读
chmod +r test
chmod a+r test
给文件test添加所有人可执行
chmod +x test
chmod a+x test
给文件test添加所有人可写
chmod a+w test
-rw-rw—- 1 root root 4 Mar 12 11:09 test2
思考:现在想让ken用户读写test2文件,你能想到几种方案?
方法一:
chmod 777 test
方法二:
chmod 0+rw test
方法三:
chmod +r test
chmod a+w test
方法四:
chown :ken test
方法五:
chown ken test
方法六:
chmod o=rw test
方法七:
usermod -G root ken
方法八:
usermod -g root ken
方法九:
chmod u+s `which cat`
chmod u+s `which echo`
方法10:
setacl -m u:ken:rw test
visudo
作用:是以特定的用户身份去执行某个命令
root ALL=(ALL) ALL
root 表示那个用户
ALL 表示可以从那台主机进行登录
(ALL) 表示以谁的身份运行命令,如果是ALL表示的是root身份
ALL 表示可以执行什么样的命令 #注意:命令这里要使用绝对路径
例子1:ken用户以root身份来执行useradd和userdel命令
ken ALL=(ALL) /usr/sbin/useradd,/usr/sbin/userdel
验证:
[ken@ken ~]$ useradd kenken
-bash: /usr/sbin/useradd: Permission denied
[ken@ken ~]$ sudo useradd kenken
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 ken: #注意:这里输入的是ken用户的密码而不是root管理员的密码
[ken@ken ~]$ id kenken
uid=1001(kenken) gid=1001(kenken) groups=1001(kenken)
[ken@ken ~]$ sudo userdel -r kenken #注意:五分钟之内不需要输入密码
[ken@ken ~]$ id kenken
id: kenken: no such user
1. vim编辑器中怎么替换全文中的root为oscar
方法一:
1,$ s/root/oscar/g
方法二:
% s/root/oscar/g
2. vim中怎么直接跳转到末行
G
vim中跳转到行尾
end
3.创建级联目录
mkdir -p /2/3/4
4.创建一个普通文件
touch test
5.删除目录
rm -rf test
find
作用:根据一定的条件去查找指定的文件
使用格式:
find path 条件
-type 根据文件类型查找f d
-name 根据文件名查找
-perm 根据权限查找
-user 根据属主查找
-group 根据主组查找
-nouser 根据无属主查找
-nogroup 根据五属组查找
-size 根据文件大小查找
-atime 根据访问时间查找
-mtime 根据修改时间查找 #内容内容
-ctime 根据属性修改查找 #大小,权限,属主,属组
-exec
例子1:查找以test开头的文件
[root@ken tmp]# find / -name “test*” #
例子2:查找以test结尾的文件
[root@ken tmp]# find / -name “*test”
例子3:查找以t开头t结尾的文件
[root@ken tmp]# find / -name “t*t”
例子4:查找777权限的文件都有哪些
[root@ken tmp]# find / -perm 777
[root@ken tmp]# ls -ld /bin
lrwxrwxrwx. 1 root root 7 Feb 26 22:21 /bin -> usr/bin
例子5:查找包含权限111的文件
[root@ken tmp]# find / -perm -111
[root@ken tmp]# ls -ld /srv
drwxr-xr-x. 2 root root 6 Apr 11 2018 /srv
例子6:找到文件大小大于100M的文件
[root@ken tmp]# find / -size +100M
[root@ken tmp]# ls -ldh /usr/lib/locale/locale-archive
-rw-r–r–. 1 root root 102M Feb 26 22:21 /usr/lib/locale/locale-archive
例子7: 找到所有属主是ken的文件
[root@ken tmp]# find / -user ken
[root@ken tmp]# ls -ld /home/ken
drwx—— 2 ken ken 83 Mar 11 18:16 /home/ken
例子8: 找到所有属组是ken的文件
[root@ken tmp]# find / -group ken
例子9:找到无属主的文件
[root@ken tmp]# find / -nouser
例子10:找到所有的普通文件并且文件权限包含111
[root@ken tmp]# find / -type f -a -perm -111 #是和的意思,两个条件需要同时满足
[root@ken tmp]# ls -ld /usr/libexec/generate-rndc-key.sh
-rwxr-xr-x 1 root root 546 Apr 13 2018 /usr/libexec/generate-rndc-key.sh
例子11:找到所有的普通文件或者文件权限包含111
[root@ken tmp]# find / -type f -o -perm -111
[root@ken tmp]# ls -ld /mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 /mnt
[root@ken tmp]# ls -ld /home/kenken/.bashrc
-rw-r–r– 1 1001 1001 231 Apr 11 2018 /home/kenken/.bashrc
例子12.找到所有无属主的文件并且删掉
[root@ken tmp]# find /home -nouser -exec rm -rf {} \;
例子13:找到/home下的包含111权限的文件,并且把所有的执行权限去掉
[root@ken ~]# find /home -perm -111 -exec chmod -x {} \;
[root@ken ~]# ls /home
ken test
[root@ken ~]# ls /home -l
total 0
drwx—— 2 ken ken 83 Mar 11 18:16 ken
-rw-r–r– 1 root root 0 Jun 23 10:01 test
例子14.找到/home下所有的普通文件,并且删除
方法一:
[root@ken ~]# find /home -type f -exec rm -rf {} \;
[root@ken ~]# ls /home -l
total 0
drwx—— 2 ken ken 6 Jun 23 10:03 ken
[root@ken ~]#
方法二:
[root@ken ~]# find /home -type f | xargs rm -rf
[root@ken ~]# ll /home
total 0
drwx—— 2 ken ken 6 Jun 23 10:03 ken
计划任务
at 一次性计划任务
第一步:下载at服务程序
[root@ken ~]# yum install at -y
第二步:启动atd服务
[root@ken ~]# systemctl restart atd
第三步:开启自启
[root@ken ~]# systemctl enable atd
第四步:查看atd是否正常启动
方法一:
[root@ken ~]# systemctl status atd
方法二:
[root@ken ~]# ps aux | grep atd
root 5299 0.0 0.0 25904 944 ? Ss 10:30 0:00 /usr/sbin/atd -f
使用atd
注意:确保你的系统时间是正确的!
[root@ken ~]# date
Sun Jun 23 10:32:22 CST 2019
[root@ken ~]# date -s “2019-6-23 10:35:00”
使用ntp时间服务器:
[root@ken ~]# yum install ntp -y #下载ntp服务
[root@ken ~]# ntpdate time1.aliyun.com #使用ntpdate进行阿里云时间同步
使用at一次性计划任务
例子1 10:38分的时候备份test2文件到/tmp目录下
[root@ken ~]# at 10:38
at> cp /root/test2 /tmp
at> <EOT> #ctrl+d进行保存
job 1 at Sun Jun 23 10:38:00 2019
查看at计划任务
[root@ken ~]# at -l
1 Sun Jun 23 10:38:00 2019 a root
[root@ken ~]# atq
1 Sun Jun 23 10:38:00 2019 a root
验证计划任务是否正确执行:
[root@ken ~]# atq #如果at计划任务正确执行,这里就不会再显示计划任务了
[root@ken ~]# ls /tmp
test2
删除at计划任务
[root@ken ~]# at 10:50
at> rm -rf /*
at> <EOT>
job 2 at Sun Jun 23 10:50:00 2019
[root@ken ~]# atq #使用atq命令获取到计划任务的编号
2 Sun Jun 23 10:50:00 2019 a root
[root@ken ~]# atrm 2 #使用atrm加上计划任务编号删除计划任务
[root@ken ~]# atq
使用周期性加护任务crond。一定要确保crond服务正在运行!
[root@ken ~]# ps aux | grep crond
root 631 0.0 0.1 126280 1592 ? Ss 08:17 0:00 /usr/sbin/crond -n
crond使用方法:
vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root #/var/spool/mail/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
注意:在使用计划任务的时候,命令一定要写绝对路径!
常用选项:
-l 查看计划任务
-r 移除计划任务
-e 编辑计划任务
-u 指定给谁创建计划任务
例子1:每分钟输出123到test文件中
[root@ken ~]# crontab -e
* * * * * /usr/bin/echo “123” >> /root/test
查看计划任务:
[root@ken ~]# crontab -l
* * * * * /usr/bin/echo “123” >> /root/test
验证:
[root@ken ~]# tail -f test
123
删除计划任务:
方法一:
[root@ken ~]# crontab -r
[root@ken ~]# crontab -l
no crontab for root
方法二:注释
* 表示任意时间
– 表示7-10表示7,8,9,10
,表示间隔 时7,9,12
/ 表示每的意思 分 */10 表示每10分钟执行一次
例子1:7,9,10执行一次重启网卡的操作
0 7,9,10 * * * /usr/bin/systemctl restart network
问题1:每月1、10、22日的4:45重启network服务
45 4 1,10,22 * * /usr/bin/systemctl restart network
问题2:每周六、周日的1:10重启network服务
10 1 * * 6,7 /usr/bin/systemctl restart network
问题3:每天18:00至23:00之间每隔30分钟重启network服务
0,30 18-23 * * * /usr/bin/systemctl restart network
*/30 18-23 * * * /usr/bin/systemctl restart network
问题4:每隔两天的上午8点到11点的第3和第15分钟执行一次重启
3,15 8-11 */2 * * /usr/sbin/reboot
注意:计划任务的运行间隔最小单位是分钟
压缩归档
作用:把文件进行打包操作,方便传输
tar
czvf #进行打包,f参数必须放在最后
xzvf #解压包
例子1:对/ken目录下的文件进行打包操作
[root@ken ken1]# tar czvf ken.tar.gz ./*
[root@ken ken1]# tar xzvf ken.tar.gz
例子2:打包的文件名以时间进行命名
[root@ken ken]# tar czvf `date “+%F”`.tar.gz ./*
例子3:解压到特定目录之下
[root@ken ken]# tar xf 2019-06-24.tar.gz -C /ken1
crontab练习题
1.每周一到周六的凌晨3点20分,运行tar命令对/etc/目录进行存档另存,存储位置为/backups/etc-YYYY-MM-DD.tar.gz
20 3 * * 1-6 /usr/bin/tar czvf /backups/etc-`date “+\%F”`.tar.gz /etc
2.每周日凌晨2点30分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss;
30 2 * * 7 /usr/bin/cp /etc/fstab /backup/fstab-`date “+\%F-\%T”`
3.每天晚上12点,取得/proc/meminfo文件中所有以S或M开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线;
0 0 * * * /usr/bin/echo -e `=========\ncat /proc/meminfo | grep -E “(^S)|(^M)”` >> /statistics/meminfo.txt
4.每月每天凌晨3点30分和中午12点20分执行test.sh脚本
30 3 * * * test.sh #这样写的话一定要确保test.sh具有执行权限!
20 12 * * * test.sh
5.每月每天每隔6小时的每30分钟执行test.sh脚本
*/30 */6 * * * test.sh
6. 每月每天早上8点到下午18点每隔2小时的每30分钟执行test.sh脚本
*/30 8-18/2 * * * test.sh
7. 每月每天晚上21点30分执行test.sh脚本
30 21 * * * test.sh
8. 每月1号、10号、22号凌晨4点45分执行test.sh脚本
45 4 1,10,22 * * test.sh
9.8月份周一、周日凌晨1点10分执行test.sh脚本
10 1 * 8 1,7 test.sh
10.每月每天每小时整点执行test.sh脚本
0 * * * * test.sh
11. 每天晚上 12 点,打包站点目录/var/www/html 备份到/data 目录下。每次备份按时间生成不同的备份包
0 0 * * * /usr/bin/tar czf /data/web.tar.gz /var/www/html/
12.一月一号的4点重启smb
0 4 1 1 * /usr/bin/systemctl restart smb
13.每小时执行/etc/cron.hourly目录内的脚本
0 * * * * /etc/cron.hourly/*
14. 每月的4号与每周一到周三的11点重启smb
0 11 4 * 1-3 /usr/bin/systemctl restart smb
15 晚上11点到早上7点之间,每隔一小时重启smb
0 23,0-7 * * * /usr/bin/systemctl restart smb
16.每一小时重启smb
0 */1 * * * /usr/bin/systemctl restart smb
17 每星期六的晚上11 : 00 pm重启smb
0 23 * * 6 /usr/bin/systemctl restart smb
18每天18 : 00至23 : 00之间每隔30分钟重启smb
*/30 18-23 * * * /usr/bin/systemctl restart smb
19 每周六、周日的1 : 10重启smb
10 1 * * 6-7 /usr/bin/systemctl restart smb
20 每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /usr/bin/systemctl restart smb
MBR
512字节
前446字节bootloader
64字节磁盘分区表
2字节魔数55AA
主分区+扩展分区 最多只能有4个
扩展分区可以是0个,最多是1个
扩展分区不能直接使用,扩展分区必须首先创建成逻辑分区才能使用
逻辑分区可以是0个 1个 多个
fdisk管理磁盘
n 创建一个新的分区
p 显示分区信息
q 不保存退出
w 保存退出
实战:创建一个主分组并使用
第一步:fdisk分区
[root@ken ~]# 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 0x241555e6.
Command (m for help): m #输入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 #选择p创建主分区
Partition number (1-4, default 1): #选择默认的分区编号,回车默认是1
First sector (2048-41943039, default 2048): #选择磁盘扇区起始位置,直接回车默认即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G #选择创建磁盘的大小
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): P #查看分区信息
Disk /dev/sdb: 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: 0x241555e6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
Command (m for help): W #w进行保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
You have new mail in /var/spool/mail/root
[root@ken ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1
第二步:格式化 #安装文件系统
[root@ken ~]# mkfs.xfs /dev/sdb1
第三步:挂载使用
[root@ken ~]# mount /dev/sdb1 /ken
第四步:查看磁盘使用量及挂载信息
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 5.0G 33M 5.0G 1% /ken
第五步:写入数据
[root@ken ken]# touch {1..100}.txt
[root@ken ken]# ls
100.txt 20.txt 31.txt 42.txt 53.txt 64.txt 75.txt 86.txt 97.txt
10.txt 21.txt 32.txt 43.txt 54.txt 65.txt 76.txt 87.txt 98.txt
11.txt 22.txt 33.txt 44.txt 55.txt 66.txt 77.txt 88.txt 99.txt
第六步:卸载磁盘
[root@ken ~]# umount /ken
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
第七步:检查数据
[root@ken ~]# ls /ken
[root@ken ~]#
思考:数据那?
[root@ken ~]# mount /dev/sdb1 /ken2
[root@ken ~]# ls /ken2
100.txt 20.txt 31.txt 42.txt 53.txt 64.txt 75.txt 86.txt 97.txt
10.txt 21.txt 32.txt 43.txt 54.txt 65.txt 76.txt 87.txt 98.txt
11.txt 22.txt 33.txt 44.txt 55.txt 66.txt 77.txt 88.txt 99.txt
例子2:创建扩展分区建立逻辑分区
第一步:创建拓展分区
[root@ken ~]# 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.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e #选择e建立扩展分区
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +10G
Partition 2 of type Extended and of size 10 GiB is set
Command (m for help): P
Disk /dev/sdb: 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: 0x241555e6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 31459327 10485760 5 Extended
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
第二步:查看磁盘分区
[root@ken ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1
[root@ken ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@ken ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2
第三步:创建逻辑分区
[root@ken ~]# 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.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l #选择l创建逻辑分区
Adding logical partition 5
First sector (10489856-31459327, default 10489856):
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-31459327, default 31459327): +11G
Value out of range.
Last sector, +sectors or +size{K,M,G} (10489856-31459327, default 31459327): +2G
Partition 5 of type Linux and of size 2 GiB is set
Command (m for help): P
Disk /dev/sdb: 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: 0x241555e6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 31459327 10485760 5 Extended
/dev/sdb5 10489856 14684159 2097152 83 Linux
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
第四步:格式化
[root@ken ~]# mkfs.xfs /dev/sdb5
第五步:进行挂载
[root@ken ~]# mount /dev/sdb5 /ken
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 5.0G 33M 5.0G 1% /ken2
/dev/sdb5 2.0G 33M 2.0G 2% /ken
练习题:创建一个5G大小的主分区和一个2G逻辑分区
1.fdisk创建一个分区
2.格式化
3.挂载使用
4. 查看挂载信息
注意:扩展分区是不能被直接使用的!
只在在扩展分区之上创建逻辑分区!
开机自动挂载
方法一:
[root@ken ~]# vim /etc/fstab
/dev/sdb5 /ken xfs defaults 0 0
[root@ken ~]# mount -a #使用该命令会自动挂载/etc/fstab文件里面未挂载的磁盘
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb5 2.0G 33M 2.0G 2% /ken
注意:此方法容易使得系统启动不起来。
方法二:写到/etc/rc.local文件 #推荐此方法
[root@ken ~]# vim /etc/rc.local
mount /dev/sdb5 /ken
[root@ken ~]# chmod +x /etc/rc.d/rc.local
交换分区
划分磁盘里面的一块空间临时作为内存使用
怎么查看交换分区:
[root@ken ~]# free -h
total used free shared buff/cache available
Mem: 974M 86M 771M 7.6M 116M 745M
Swap: 2.0G 0B 2.0G
制作交换分区:
方法一:谁用磁盘的一个分区
第一步:创建主分区
[root@ken ~]# 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.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3):
First sector (31459328-41943039, default 31459328):
Using default value 31459328
Last sector, +sectors or +size{K,M,G} (31459328-41943039, default 41943039): +2G
Partition 3 of type Linux and of size 2 GiB is set
Command (m for help): P
Disk /dev/sdb: 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: 0x241555e6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 31459327 10485760 5 Extended
/dev/sdb3 31459328 35653631 2097152 83 Linux
/dev/sdb5 10489856 14684159 2097152 83 Linux
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@ken ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@ken ~]# ls /dev/sd*
/dev/sda /dev/sda2 /dev/sdb1 /dev/sdb3
/dev/sda1 /dev/sdb /dev/sdb2 /dev/sdb5
第二步:swap格式化
[root@ken ~]# mkswap /dev/sdb4
第三步:查看内存
[root@ken ~]# free -h
total used free shared buff/cache available
Mem: 974M 86M 759M 7.6M 128M 739M
Swap: 2.0G 0B 2.0G
第四步:启动交换分区
[root@ken ~]# swapon /dev/sdb4
第五步:再次查看内存信息
[root@ken ~]# free -h
total used free shared buff/cache available
Mem: 974M 87M 758M 7.6M 128M 737M
Swap: 4.0G 0B 4.0G
第六步:开机自启
/dev/sdb4 swap swap defaults 0 0
方法二:使用本地一个文件作为交换分区
第一步:创建/swap目录
[root@ken ~]# mkdir /swap
第二步:制作本地文件
[root@ken ~]# dd if=/dev/zero of=/swap/swap bs=1M count=2048
第三步:制作swap文件
[root@ken ~]# mkswap /swap/swap
第四步:写入fstab文件
[root@ken ~]# vim /etc/fstab
/swap/swap swap swap defaults 0 0
第五步:启动swap
[root@ken ~]# swapon -a
第六步:改变权限
[root@ken ~]# chmod 0600 /swap/swap
第七步:查看交换分区
[root@ken ~]# free -h
total used free shared buff/cache available
Mem: 974M 88M 63M 7.6M 822M 712M
Swap: 6.0G 0B 6.0G
系统启动不起来的处理方式:
1,进入救援模式
2,进入系统
3,删除/etc/fstab里面的挂载信息
4,退出重启
实例1;演示如何创建LVM逻辑卷
环境:
物理卷:/dev/sdb /dev/sdc
卷组: vg_ken
逻辑卷:lv_ken
第一步:创建物理卷
[root@ken ~]# pvcreate /dev/sd{b,c}
Physical volume “/dev/sdb” successfully created.
Physical volume “/dev/sdc” successfully created.
[root@ken ~]# pvdisplay
第二步:创建卷组
[root@ken ~]# vgcreate vg_ken /dev/sd{b,c}
Volume group “vg_ken” successfully created
[root@ken ~]# vgdisplay
第三步:创建逻辑卷
[root@ken ~]# lvcreate -n lv_ken -L 2G vg_ken
Logical volume “lv_ken” created.
[root@ken ~]# lvdisplay
第四步:格式化
[root@ken ~]# mkfs.xfs /dev/vg_ken/lv_ken
第五步:创建挂载点
[root@ken ~]# mkdir /ken
第六步:进行挂载
[root@ken ~]# mount /dev/vg_ken/lv_ken /ken
第七步:进行查看
[root@ken ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.6M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg_ken-lv_ken 2.0G 33M 2.0G 2% /ken
xfs格式的逻辑卷在线扩容
第一步:扩容lv_ken2个G
[root@ken ~]# lvextend -L +2G /dev/vg_ken/lv_ken
Size of logical volume vg_ken/lv_ken changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
Logical volume vg_ken/lv_ken successfully resized.
第二步:扩展内层
[root@ken ~]# xfs_growfs /dev/vg_ken/lv_ken
在线扩容根磁盘
第一步:添加sdc磁盘