目录
[dd 命令详解](#dd 命令详解)
[mkfs 命令详解](#mkfs 命令详解)
[df 命令详解](#df 命令详解)
[mount 命令详解](#mount 命令详解)
[unshare 命令详解](#unshare 命令详解)
dd 命令详解
Linux dd 命令用于读取、转换并输出数据。
dd 可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设 备或标准输出。
语法
dd OPTION
参数
-
if=文件名:输入文件名,默认为标准输入。即指定源文件。
-
of=文件名:输出文件名,默认为标准输出。即指定目的文件。
-
ibs=bytes:一次读入 bytes 个字节,即指定一个块大小为 bytes 个字节。
obs=bytes:一次输出 bytes 个字节,即指定一个块大小为 bytes 个字节。
bs=bytes:同时设置读入/输出的块大小为 bytes 个字节。 -
cbs=bytes:一次转换 bytes 个字节,即指定转换缓冲区大小。
-
skip=blocks:从输入文件开头跳过 blocks 个块后再开始复制。
-
seek=blocks:从输出文件开头跳过 blocks 个块后再开始复制。
-
count=blocks:仅拷贝 blocks 个块,块大小等于 ibs 指定的字节数。
-
conv=,关键字可以有以下 11 种:
conversion:用指定的参数转换文件。- ascii:转换 ebcdic 为 ascii +
- ebcdic:转换 ascii 为 ebcdic
- ibm:转换 ascii 为 alternate ebcdic
- block:把每一行转换为长度为 cbs,不足部分用空格填充
- unblock:使每一行的长度都为 cbs,不足部分用空格填充
- lcase:把大写字符转换为小写字符
- ucase:把小写字符转换为大写字符
- swap:交换输入的每对字节
- noerror:出错时不停止
- notrunc:不截短输出文件
- sync:将每个输入块填充到 ibs 个字节,不足部分用空(NUL)字符补齐。
-
--help:显示帮助信息
-
--version:显示版本信息
[user1@iZ5waahoxw3q2bZ 26-5-15] clear [user1@iZ5waahoxw3q2bZ 26-5-15] dd if=/dev/zero of=test.img bs=8k count=1024
1024+0 records in
1024+0 records out
8388608 bytes (8.4 MB) copied, 0.00566241 s, 1.5 GB/s
[user1@iZ5waahoxw3q2bZ 26-5-15] ll total 8192 -rw-rw-r-- 1 user1 user1 8388608 May 15 18:44 test.img [user1@iZ5waahoxw3q2bZ 26-5-15] vim test.img
[user1@iZ5waahoxw3q2bZ 26-5-15] dd if=/dev/zero of=test2.img bs=8k count=10240 10240+0 records in 10240+0 records out 83886080 bytes (84 MB) copied, 0.053167 s, 1.6 GB/s [user1@iZ5waahoxw3q2bZ 26-5-15] ll
total 90112
-rw-rw-r-- 1 user1 user1 83886080 May 15 18:45 test2.img
-rw-rw-r-- 1 user1 user1 8388608 May 15 18:44 test.img
小写转大写
[user1@iZ5waahoxw3q2bZ 26-5-16]$ vim in.txt
[user1@iZ5waahoxw3q2bZ 26-5-16]$ cat in.txt
hello world!
[user1@iZ5waahoxw3q2bZ 26-5-16]$ dd if=in.txt of=out.txt conv=ucase
0+1 records in
0+1 records out
13 bytes (13 B) copied, 0.00011954 s, 109 kB/s
[user1@iZ5waahoxw3q2bZ 26-5-16]$ cat out.txt
HELLO WORLD!
mkfs 命令详解
用于在设备上创建 Linux 文件系统,俗称格式化,比如我们使用 U 盘的时候可以格式化。
语法
mkfs [-V] [-t fstype] [fs-options] filesys [blocks]
参数
-t fstype:指定要建立何种文件系统;如 ext3,ext4
filesys :指定要创建的文件系统对应的设备文件名;
blocks:指定文件系统的磁盘块数。
-V : 详细显示模式
fs-options:传递给具体的文件系统的参数
格式化成ext4的一种文件类型
[user1@iZ5waahoxw3q2bZ 26-5-15]$ mkfs -t ext4 ./test.img
mke2fs 1.42.9 (28-Dec-2013)
./test.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
2048 inodes, 8192 blocks
409 blocks (4.99%) reserved for the super user
First data block=1
Maximum filesystem blocks=8388608
1 block group
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
执行了 mkfs -t ext4 ./test.img,在一个普通文件 (而非块设备)上成功创建了一个 ext4 文件系统。这很常见,通常用于创建虚拟磁盘镜像 ,之后可以通过 loop 设备挂载使用。
df 命令详解
Linux df(英文全拼:disk free) 命令用于显示目前在 Linux 系统上的文件系统磁盘使 用情况统计。
语法
df [OPTION]... [FILE]...
常见参数
- -a, --all 包含所有的具有 0 Blocks 的文件系统
- -h, --human-readable 使用人类可读的格式(预设值是不加这个选项的...)
- -H, --si 很像 -h, 但是用 1000 为单位而不是用 1024
- -t, --type=TYPE 限制列出文件系统的 TYPE
- -T, --print-type 显示文件系统的形式
先直接输入df先
[user1@iZ5waahoxw3q2bZ 26-5-16]$ ll
total 8
-rw-rw-r-- 1 user1 user1 13 May 16 10:48 in.txt
-rw-rw-r-- 1 user1 user1 13 May 16 10:48 out.txt
[user1@iZ5waahoxw3q2bZ 26-5-16]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 41147472 2899824 36344156 8% /
devtmpfs 888812 0 888812 0% /dev
tmpfs 899468 0 899468 0% /dev/shm
tmpfs 899468 388 899080 1% /run
tmpfs 899468 0 899468 0% /sys/fs/cgroup
tmpfs 179896 0 179896 0% /run/user/1001
-h
[user1@iZ5waahoxw3q2bZ 26-5-16]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 2.8G 35G 8% /
devtmpfs 868M 0 868M 0% /dev
tmpfs 879M 0 879M 0% /dev/shm
tmpfs 879M 388K 879M 1% /run
tmpfs 879M 0 879M 0% /sys/fs/cgroup
tmpfs 176M 0 176M 0% /run/user/1001
-t
[user1@iZ5waahoxw3q2bZ 26-5-16]$ df -t ext4
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 41147472 2899828 36344152 8% /
-T
[user1@iZ5waahoxw3q2bZ 26-5-16]$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/vda1 ext4 41147472 2899828 36344152 8% /
devtmpfs devtmpfs 888812 0 888812 0% /dev
tmpfs tmpfs 899468 0 899468 0% /dev/shm
tmpfs tmpfs 899468 388 899080 1% /run
tmpfs tmpfs 899468 0 899468 0% /sys/fs/cgroup
tmpfs tmpfs 179896 0 179896 0% /run/user/1001
mount 命令详解
mount 命令用于加载文件系统到指定的加载点。此命令的也常用于挂载光盘,使我们可以访问光盘中的数据,因为你将光盘插入光驱中,Linux 并不会自动挂载,必须使用 Linux mount 命令来手动完成挂载。
Linux 系统下不同目录可以挂载不同分区和磁盘设备,它的目录和磁盘分区是分离的, 可以自由组合(通过挂载)
不同的目录数据可以跨越不同的磁盘分区或者不同的磁盘设备。
挂载的实质是为磁盘添加入口(挂载点)。
mount 常见用法
mount [-l]
mount [-t vfstype] [-o options] device dir
常见参数
-
-l:显示已加载的文件系统列表;
-
-t: 加载文件系统类型支持常见系统类型的 ext3,ext4,iso9660,tmpfs,xfs 等,大部分情况 可以不指定,mount 可以自己识别
-
-o options 主要用来描述设备或档案的挂接方式。
- loop:用来把一个文件当成硬盘分区挂接上系统
- ro:采用只读方式挂接设备
- rw:采用读写方式挂接设备
-
device: 要挂接(mount)的设备。
-
dir: 挂载点的目录
[user1@iZ5waahoxw3q2bZ 26-5-15] sudo mkdir -p /data/testmymount [sudo] password for user1: [user1@iZ5waahoxw3q2bZ 26-5-15] ll -la /data/testmymount
total 8
drwxr-xr-x 2 root root 4096 May 16 11:09 .
drwxr-xr-x 3 root root 4096 May 16 11:09 ..
[user1@iZ5waahoxw3q2bZ 26-5-15] sudo mount ./test.img /data/testmymount [sudo] password for user1: [user1@iZ5waahoxw3q2bZ 26-5-15] df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 2.8G 35G 8% /
devtmpfs 868M 0 868M 0% /dev
tmpfs 879M 0 879M 0% /dev/shm
tmpfs 879M 392K 879M 1% /run
tmpfs 879M 0 879M 0% /sys/fs/cgroup
tmpfs 176M 0 176M 0% /run/user/1001
/dev/loop0 6.8M 77K 6.2M 2% /data/testmymount[user1@iZ5waahoxw3q2bZ testmymount] echo "123" | sudo tee test.txt 123 [user1@iZ5waahoxw3q2bZ testmymount] ll -h
total 13K
drwx------ 2 root root 12K May 16 11:07 lost+found
-rw-r--r-- 1 root root 4 May 16 11:39 test.txt
[user1@iZ5waahoxw3q2bZ testmymount]$ cat test.txt
123
mount把磁盘或者说格式化好的文件系统给它指定一个入口
unshare 命令详解
unshare 主要能力是使用与父程序不共享的名称空间运行程序。
语法
unshare [options] program [arguments]
常用参数
| 参数 | 含义 |
|---|---|
-i, --ipc |
不共享 IPC 空间 |
-m, --mount |
不共享 Mount 空间 |
-n, --net |
不共享 Net 空间 |
-p, --pid |
不共享 PID 空间 |
-u, --uts |
不共享 UTS 空间。不共享主机名 |
-U, --user |
不共享用户 |
-V, --version |
版本查看 |
--fork |
执行 unshare 的进程 fork 一个新的子进程,在子进程里执行 unshare 传入的参数 |
--mount-proc |
执行子进程前,将 proc 优先挂载过去 |
[user1@iZ5waahoxw3q2bZ testmymount]$ sudo unshare -u /bin/bash
[root@iZ5waahoxw3q2bZ testmymount]# hostname test1
[root@iZ5waahoxw3q2bZ testmymount]# hostname
test1
[root@iZ5waahoxw3q2bZ testmymount]# exit
exit
[user1@iZ5waahoxw3q2bZ testmymount]$ hostname
iZ5waahoxw3q2bZ
PID隔离
[user1@iZ5waahoxw3q2bZ testmymount]$ sudo unshare -p /bin/bash
bash: fork: Cannot allocate memory
[root@iZ5waahoxw3q2bZ testmymount]# exit
exit
[user1@iZ5waahoxw3q2bZ testmymount]$ sudo unshare -p --fork /bin/bash
unshare -p创建一个新的 PID namespace,但unshare进程本身并不会自动成为新 namespace 中的 PID 1 。实际上,unshare进程仍位于原 namespace,只是它的子进程会进入新 namespace 并获得 PID 1。当直接运行
sudo unshare -p /bin/bash时,unshare会调用exec直接替换当前进程为/bin/bash,没有产生新的子进程 。于是/bin/bash继承了unshare的 PID(比如 12345),而新 namespace 中不存在 PID 1 。缺少 PID 1 的情况下,bash 尝试fork创建子进程(例如执行命令)时会失败,内核返回Cannot allocate memory(实际上是无法分配 PID,因为新 namespace 中没有 init 进程来处理信号等)加上
--fork后,unshare先fork出一个子进程,该子进程进入新 PID namespace 并成为 PID 1 ,然后这个子进程再执行/bin/bash。此时 bash 作为 PID 1 运行,可以正常fork子进程。
然后我们再打开一个相同的终端输入ps -ef。发现两边相同,可以知道PID没有发生隔离
之后再进行如下操作就可以进行PID的一个隔离
[root@iZ5waahoxw3q2bZ testmymount]# exit
exit
[user1@iZ5waahoxw3q2bZ testmymount]$ sudo unshare -p --fork --mount-proc /bin/bash
[root@iZ5waahoxw3q2bZ testmymount]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:53 pts/0 00:00:00 /bin/bash
root 13 1 0 11:55 pts/0 00:00:00 ps -ef
Mount隔离
[user1@iZ5waahoxw3q2bZ 26-5-16]$ sudo unshare --mount --fork /bin/bash
[root@iZ5waahoxw3q2bZ 26-5-16]# ll -h
total 8.0K
-rw-rw-r-- 1 user1 user1 13 May 16 10:48 in.txt
-rw-rw-r-- 1 user1 user1 13 May 16 10:48 out.txt
[root@iZ5waahoxw3q2bZ 26-5-16]# dd if=/dev/zero of=data2.img bs=8k count=10240
10240+0 records in
10240+0 records out
83886080 bytes (84 MB) copied, 0.0554101 s, 1.5 GB/s
[root@iZ5waahoxw3q2bZ 26-5-16]# ls -l
total 81928
-rw-r--r-- 1 root root 83886080 May 16 12:00 data2.img
-rw-rw-r-- 1 user1 user1 13 May 16 10:48 in.txt
-rw-rw-r-- 1 user1 user1 13 May 16 10:48 out.txt
[root@iZ5waahoxw3q2bZ 26-5-16]# mkfs -t ext4 ./data2.img
mke2fs 1.42.9 (28-Dec-2013)
./data2.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
20480 inodes, 81920 blocks
4096 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
10 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@iZ5waahoxw3q2bZ 26-5-16]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 2.8G 35G 8% /
devtmpfs 868M 0 868M 0% /dev
tmpfs 879M 0 879M 0% /dev/shm
tmpfs 879M 0 879M 0% /sys/fs/cgroup
tmpfs 879M 424K 878M 1% /run
tmpfs 176M 0 176M 0% /run/user/1001
/dev/loop0 6.8M 78K 6.2M 2% /data/testmymount
[root@iZ5waahoxw3q2bZ 26-5-16]# mkdir -p /data/maxhou/data2mount
[root@iZ5waahoxw3q2bZ 26-5-16]# mount -t ext4 ./data2.img /data/maxhou/data2mount
[root@iZ5waahoxw3q2bZ 26-5-16]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 2.8G 35G 8% /
devtmpfs 868M 0 868M 0% /dev
tmpfs 879M 0 879M 0% /dev/shm
tmpfs 879M 0 879M 0% /sys/fs/cgroup
tmpfs 879M 428K 878M 1% /run
tmpfs 176M 0 176M 0% /run/user/1001
/dev/loop0 6.8M 78K 6.2M 2% /data/testmymount
/dev/loop1 74M 1.6M 67M 3% /data/maxhou/data2mount
[user1@iZ5waahoxw3q2bZ ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 2.8G 35G 8% /
devtmpfs 868M 0 868M 0% /dev
tmpfs 879M 0 879M 0% /dev/shm
tmpfs 879M 428K 878M 1% /run
tmpfs 879M 0 879M 0% /sys/fs/cgroup
tmpfs 176M 0 176M 0% /run/user/1001
/dev/loop0 6.8M 78K 6.2M 2% /data/testmymount
可以发现在另外一个终端上是没有这个挂载点的