掛載命令
? ? 在Windows系統(tǒng)中如果插入了U盤、移動(dòng)硬盤、光驅(qū)等,只要能被Windows系統(tǒng)識(shí)別出來,則系統(tǒng)會(huì)進(jìn)行自動(dòng)掛載并添加盤符,然后我們就可以訪問,而這一切均由系統(tǒng)完成,用戶并不需要做任何操作即可使用。那么在Linux系統(tǒng)中,雖然一些Linux系統(tǒng)已經(jīng)做了一部分自動(dòng)掛載功能,但大部分情況還是需要手動(dòng)掛載完成。而這一功能則是由命令mount和umount命令完成。
mount命令
? ? mount命令主要用于加載文件系統(tǒng)到指定的掛載點(diǎn)。其基本語法如下所示:
mount [選項(xiàng)] [設(shè)備名稱] [掛載點(diǎn)]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -a, --all | 掛載/etc/fstab文件中所有的文件系統(tǒng) |
| -F, --fork | 常與-a一起使用,在系統(tǒng)掛載大量NFS系統(tǒng)時(shí),可以提高掛載速度 |
| -f, --fake | 常用于排錯(cuò),并不執(zhí)行真正的掛載操作,僅模擬掛載過程,常與-v一起使用 |
| -n, --no-mtab | 不把加載記錄寫入/etc/mtab中,通常在/etc目錄僅為只讀時(shí)使用 |
| -r, --read-only | 將文件掛載為只讀模式,同-o ro |
| -w, --rw, --read-write | 將文件掛載為可讀可寫模式,同-o rw,系統(tǒng)默認(rèn)方式 |
| -t, --types vfstype | 指定掛載的文件系統(tǒng)類型,如果不指定該選項(xiàng),則默認(rèn)為auto |
| -o, --options opts | 指定掛載的掛載方式 |
| -V, --version | 顯示版本信息 |
| -v, --verbose | 顯示詳細(xì)信息 |
在使用mount命令掛載,需要確認(rèn)掛載點(diǎn)必須是一個(gè)已經(jīng)存在的目錄。
其中-t , --types常用的文件系統(tǒng)格式如下所示:
| 文件系統(tǒng)格式 | 說明 |
|---|---|
| ext2 | Linux系統(tǒng)常用的文件系統(tǒng)格式 |
| ext3 | Linux系統(tǒng)常用的文件系統(tǒng)格式 |
| ext4 | Linux系統(tǒng)常用的文件系統(tǒng)格式 |
| iso9660 | CD-ROW光盤標(biāo)準(zhǔn)文件系統(tǒng)格式 |
| vfat | Windows FAT32文件系統(tǒng)格式 |
| xfs | Linux系統(tǒng)常用的文件系統(tǒng)格式,CentOS7中支持 |
| nfs | 網(wǎng)絡(luò)文件系統(tǒng)格式 |
| ntfs | Windows NTFS文件系統(tǒng)格式,需要單獨(dú)安裝包 |
| auto | 自動(dòng)檢測(cè)文件系統(tǒng)格式 |
其中 -o, --options 常用的選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| atuo | 允許和-a一起使用 |
| noauto | 不允許和-a一起使用 |
| defaults | 使用默認(rèn)選項(xiàng):rw, suid, dev, exec, auto, nouser, and async |
| ro | 以只讀模式進(jìn)行掛載 |
| rw | 以讀寫模式進(jìn)行掛載 |
| nouser | 禁止某一個(gè)用戶(root)掛載文件系統(tǒng) |
| users | 允許所有用戶掛載和卸載文件系統(tǒng) |
| owner | 允許非root進(jìn)行掛載,但用戶必須擁有該設(shè)備的所有權(quán)限 |
| loop | 以loop形式進(jìn)行掛載 |
在Linux中,有一種特殊的設(shè)備叫l(wèi)oop device,這種設(shè)備是通過映射操作系統(tǒng)上的正常文件而形成的虛擬塊設(shè)備。因此我們可以基于該設(shè)備,創(chuàng)建一種存在于其他文件中的虛擬文件系統(tǒng)的機(jī)制。
umount命令
? ? umount功能與mount相反,主要用于卸載已經(jīng)掛載的文件系統(tǒng),類似于Windows中的刪除U盤、移動(dòng)硬盤等。其基本語法如下所示
umount [選項(xiàng)] [掛載點(diǎn)]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -a | 卸載/etc/mtab中記錄的所有文件系統(tǒng) |
| -r | 如果無法卸載,則以只讀方式重新掛載 |
| -d | 如果卸載的設(shè)備為loop設(shè)備,卸載的同時(shí)釋放該loop設(shè)備 |
| -t vfstype | 僅卸載指定類型的文件系統(tǒng) |
| -f | 強(qiáng)行卸載 |
硬盤分區(qū)命令
? ? 在Windows中,如果系統(tǒng)增加了硬盤,能被系統(tǒng)識(shí)別,通常情況只需要在計(jì)算機(jī)管理中選擇對(duì)應(yīng)的硬盤右鍵選擇格式化即可。而在Linux如何操作了?我們引入兩個(gè)格式化命令fdisk和parted命令。
在實(shí)際應(yīng)用中,如何選擇這兩個(gè)命令則由硬盤的容量決定
- 硬盤容量小于2TB,則優(yōu)先選擇MBR,則使用 fdisk 命令
- 硬盤容量大于或等于2TB,則必須選擇GPT,則使用 parted 命令
fdisk命令
基本語法如下所示:
fdisk [選項(xiàng)] [設(shè)備]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -h | 顯示幫助信息 |
| -l | 顯示指定設(shè)備的分區(qū)表信息,如未指定設(shè)備,則默認(rèn)顯示/proc/partitions中的記錄 |
| -v | 顯示版本信息 |
| -s partition | 顯示指定設(shè)備分區(qū)大小 |
fdisk 菜單常用說明如下:
| 菜單項(xiàng) | 說明 |
|---|---|
| d | 刪除分區(qū)表 |
| l | 顯示支持的分區(qū)表類型 |
| m | 顯示菜單和幫助信息 |
| n | 新建分區(qū)表 |
| p | 顯示分區(qū)信息 |
| q | 退出不保存 |
| v | 分區(qū)表檢查 |
| w | 保存分區(qū)表信息并退出 |
parted命令
常用語法如下所示:
parted [選項(xiàng)] [設(shè)備] [命令]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -h, --help | 顯示幫助信息 |
| -l, --list | 顯示所有設(shè)備的分區(qū)表信息 |
| -v, --version | 顯示版本信息 |
| -s, --script | 自動(dòng)模式,無須人工干預(yù) |
| -a alignment-type | 分區(qū)對(duì)齊方式 |
常用的對(duì)齊方式如下所示:
- none:根據(jù)硬盤類型選擇最低限度的對(duì)齊
- cylinder:根據(jù)硬盤柱面進(jìn)行對(duì)齊
- optimal:根據(jù)硬盤的拓?fù)湫畔⑦x擇最優(yōu)對(duì)齊方式
常用的命令如下所示:
| 命令 | 說明 |
|---|---|
| help [command] | 顯示指定命令的幫助信息 |
| mklabel label-type | 指定分區(qū)表類型 |
| mkpart part-type [fs-type] start end | 創(chuàng)建分區(qū),start/end為開始位置和結(jié)束位置,默認(rèn)為MB |
| rm partition | 刪除分區(qū) |
| select device | 指定要編輯的設(shè)備 |
| set partition flag state | 設(shè)置分區(qū)類型和狀態(tài) |
| 顯示分區(qū)表信息 | |
| quit | 退出 |
常見的 label-type 如下所示:
- bsd,dvh,gpt,loop,mac,msdos,pc98,sun
常見的 part-type 如下所示(在創(chuàng)建分區(qū)的時(shí)候必須指定一種類型):
- primary,logical,extended
常見的 fs-type 如下所示:
- ext2,ext3,ext4,fat32,ntfs,xfs
常見的 flag 如下所示:
- boot,root,swap,hidden,raid(軟件RAID),lvm(邏輯卷)
常見的 state 如下所示:
- on,off
分區(qū)中起始和結(jié)束位置表示方法:
| 起始位置 | 結(jié)束位置 | 說明 |
|---|---|---|
| 1 | -1 | 使用全部容量 |
| 0% | 100% | 使用全部容量 |
| 0 | 30G | 使用前面30G的空間容量 |
| 30G | 100% | 使用剩余空間容量 |
格式化分區(qū)
? ? mkfs命令全稱為make filesystem,主要作用是用來創(chuàng)建Linux文件系統(tǒng)。
mkfs命令
其常用語法格式如下所示:
mkfs [-t fstype] [fs-options] [設(shè)備]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -t fstype | 可查看parted中fs-type,如不指定則默認(rèn)為ext2 |
| -c | 在創(chuàng)建文件系統(tǒng)前,檢查分區(qū)是否有壞道 |
| -l filename | 從文件中讀取壞道信息 |
| -v | 顯示詳細(xì)信息 |
mke2fs命令
? ? 用于創(chuàng)建ext2,ext3,ext4文件系統(tǒng)。常用語法如下所示:
mke2fs [選項(xiàng)]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -b block-size | 設(shè)置每個(gè)數(shù)據(jù)區(qū)塊占用的空間大小,可支持1024B、2048B、4096B |
| -c | 在創(chuàng)建文件系統(tǒng)前,檢查分區(qū)是否有壞道 |
| -F | 強(qiáng)行創(chuàng)建文件系統(tǒng) |
| -j | 創(chuàng)建ext3類型的文件系統(tǒng) |
| -q | 靜默模式 |
| -t fs-type | 指定創(chuàng)建的文件系統(tǒng)類型 |
| -v | 顯示詳細(xì)過程 |
mke2fs與mke2fs.ext2/mke2fs.ext3/mke2fs.ext4功能等同。
mkfs.xfs命令
? ? 用于創(chuàng)建xfs文件系統(tǒng)(CentOS 7默認(rèn)的文件系統(tǒng))。常用語法如下所示:
mkfs.xfs [選項(xiàng)] [設(shè)備]
常用選項(xiàng)如下所示:
| 選項(xiàng) | 說明 |
|---|---|
| -b block-size | 設(shè)置每個(gè)數(shù)據(jù)區(qū)塊占用的空間大小,默認(rèn)為4KiB,最小為512,最大為64KiB |
| -f | 強(qiáng)行覆蓋已經(jīng)存在的文件系統(tǒng) |
| -q | 靜默模式 |
示例
1、掛載ISO鏡像文件
[root@localhost ~]# ll *.iso
-rw-rw-r--. 1 root root 4470079488 6月 20 21:04 CentOS-7.5-x86_64-DVD-1804.iso
# 創(chuàng)建掛載點(diǎn)
[root@localhost ~]# mkdir -p /tmp/CDROM
[root@localhost ~]# mount -t iso9660 -o loop ~/CentOS-7.5-x86_64-DVD-1804.iso /tmp/CDROM/
mount: /dev/loop2 寫保護(hù),將以只讀方式掛載
[root@localhost ~]# ll /tmp/CDROM/
總用量 678
-rw-rw-r-- 1 root root 14 5月 2 19:28 CentOS_BuildTag
drwxr-xr-x 3 root root 2048 5月 4 04:34 EFI
-rw-rw-r-- 1 root root 227 8月 30 2017 EULA
-rw-rw-r-- 1 root root 18009 12月 10 2015 GPL
drwxr-xr-x 3 root root 2048 5月 4 04:44 images
drwxr-xr-x 2 root root 2048 5月 4 04:34 isolinux
drwxr-xr-x 2 root root 2048 5月 4 04:34 LiveOS
drwxrwxr-x 2 root root 655360 5月 4 04:52 Packages
drwxrwxr-x 2 root root 4096 5月 4 04:54 repodata
-rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root 2883 5月 4 04:55 TRANS.TBL
# 查看掛載點(diǎn)
[root@localhost ~]# df -h
文件系統(tǒng) 容量 已用 可用 已用% 掛載點(diǎn)
/dev/sda2 275G 20G 255G 8% /
/dev/sda1 500M 9.8M 490M 2% /boot/efi
/dev/sdb1 15T 4.2G 15T 1% /home/data
/dev/loop2 4.2G 4.2G 0 100% /tmp/CDROM
2、卸載掛載點(diǎn)
[root@localhost ~]# umount /dev/loop2
[root@localhost ~]# ll /tmp/CDROM/
總用量 0
3、fdisk創(chuàng)建分區(qū)、格式化和掛載
[root@localhost home]# fdisk -l /dev/sdb
磁盤 /dev/sdb:4294 MB, 4294967296 字節(jié),8388608 個(gè)扇區(qū)
Units = 扇區(qū) of 1 * 512 = 512 bytes
扇區(qū)大小(邏輯/物理):512 字節(jié) / 512 字節(jié)
I/O 大小(最小/最佳):512 字節(jié) / 512 字節(jié)
[root@localhost home]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。
更改將停留在內(nèi)存中,直到您決定將更改寫入磁盤。
使用寫入命令前請(qǐng)三思。
Device does not contain a recognized partition table
使用磁盤標(biāo)識(shí)符 0xa8afbc5f 創(chuàng)建新的 DOS 磁盤標(biāo)簽。
命令(輸入 m 獲取幫助):m
命令操作
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)
命令(輸入 m 獲取幫助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分區(qū)號(hào) (1-4,默認(rèn) 1):2
起始 扇區(qū) (2048-8388607,默認(rèn)為 2048):
將使用默認(rèn)值 2048
Last 扇區(qū), +扇區(qū) or +size{K,M,G} (2048-8388607,默認(rèn)為 8388607):
將使用默認(rèn)值 8388607
分區(qū) 2 已設(shè)置為 Linux 類型,大小設(shè)為 4 GiB
命令(輸入 m 獲取幫助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盤。
[root@localhost home]# fdisk -l /dev/sdb
磁盤 /dev/sdb:4294 MB, 4294967296 字節(jié),8388608 個(gè)扇區(qū)
Units = 扇區(qū) of 1 * 512 = 512 bytes
扇區(qū)大小(邏輯/物理):512 字節(jié) / 512 字節(jié)
I/O 大小(最小/最佳):512 字節(jié) / 512 字節(jié)
磁盤標(biāo)簽類型:dos
磁盤標(biāo)識(shí)符:0xe0dcbc1d
設(shè)備 Boot Start End Blocks Id System
/dev/sdb2 2048 8388607 4193280 83 Linux
# 格式化所創(chuàng)建的分區(qū)
[root@localhost home]# mkfs -v -t ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
fs_types for mke2fs.conf resolution: 'ext4'
文件系統(tǒng)標(biāo)簽=
OS type: Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
262144 inodes, 1048320 blocks
52416 blocks (5.00%) reserved for the super user
第一個(gè)數(shù)據(jù)塊=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: 完成
正在寫入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
# 創(chuàng)建掛載點(diǎn)
[root@localhost home]# mkdir -p /home/MountDisk
# 向/etc/fstab添加掛載信息
[root@localhost ~]# echo "/dev/sdb2 /home/MountDisk ext4 defaults 0 0" >> /etc/fstab
# 掛載硬盤并查看掛載點(diǎn)
[root@localhost ~]# mount -a && df -h
文件系統(tǒng) 容量 已用 可用 已用% 掛載點(diǎn)
/dev/sda6 19G 4.8G 15G 25% /
/dev/sda1 1014M 281M 734M 28% /boot
/dev/sda2 8.0G 76M 8.0G 1% /home
/dev/sda3 8.0G 1.2G 6.9G 15% /var
/dev/sdb2 3.9G 16M 3.7G 1% /home/MountDisk
4、parted創(chuàng)建分區(qū)、格式化和掛載
[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 標(biāo)志
# 設(shè)置分區(qū)類型
(parted) mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes
# 設(shè)置分區(qū)大小
(parted) mkpart primary 1 -1
# 保存并退出
(parted) quit
信息: You may need to update /etc/fstab.
[root@localhost ~]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 標(biāo)志
1 1049kB 1075MB 1074MB primary xfs 啟動(dòng)
2 1075MB 9665MB 8590MB primary xfs
3 9665MB 18.3GB 8590MB primary xfs
4 18.3GB 42.9GB 24.7GB extended
5 18.3GB 22.6GB 4295MB logical linux-swap(v1)
6 22.6GB 42.9GB 20.4GB logical xfs
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 標(biāo)志
1 1049kB 4294MB 4293MB ext4 primary
# 格式化所創(chuàng)建的分區(qū)
[root@localhost ~]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=262016 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1048064, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# 創(chuàng)建掛載點(diǎn)
[root@localhost home]# mkdir -p /home/MountDisk
# 向/etc/fstab添加掛載信息
[root@localhost ~]# echo "/dev/sdb1 /home/MountDisk xfs defaults 0 0" >> /etc/fstab
# 掛載硬盤并查看掛載點(diǎn)
[root@localhost ~]# mount -a ; df -h -T
文件系統(tǒng) 類型 容量 已用 可用 已用% 掛載點(diǎn)
/dev/sda6 xfs 19G 4.8G 15G 25% /
/dev/sda1 xfs 1014M 281M 734M 28% /boot
/dev/sda2 xfs 8.0G 76M 8.0G 1% /home
/dev/sda3 xfs 8.0G 1.2G 6.9G 15% /var
/dev/sdb1 xfs 4.0G 33M 4.0G 1% /home/MountDisk