Ubuntu磁盘分区和挂载
1、加挂硬盘
sudo lshw -c disk
看看有多少个硬盘,一般会显示 disk:0(设备名为 /dev/vda) disk:1 (设备名为 /dev/vdb)
2、查看分区
ubuntu@VM-0-3-ubuntu:~$ sudo fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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 Disklabel type: gpt Disk identifier: 884D39AE-2030-4231-B486-520515A9ADD7 Device Start End Sectors Size Type /dev/vda1 2048 4095 2048 1M BIOS boot /dev/vda2 4096 104857566 104853471 50G Linux filesystem Disk /dev/vdb: 400 GiB, 429496729600 bytes, 838860800 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 Disklabel type: dos Disk identifier: 0x6c356be3 Device Boot Start End Sectors Size Id Type /dev/vdb1 2048 838860799 838858752 400G 83 Linux
3、分区
sudo fdisk /dev/vdb
Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-838860799, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-838860799, default 838860799): Created a new partition 1 of type 'Linux' and of size 400 GiB. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
4、格式化硬盘
sudo mkfs -t ext4 /dev/vdb1 把创建的新硬盘分区格式化成ext4
5、挂载新分区
创建目录/home/d sudo mkdir -p /home/d 挂载新分区到/home/d下 sudo mount /dev/vdb1 /home/d 例如: ubuntu@VM-0-3-ubuntu:~$ sudo mkdir -p /home/d ubuntu@VM-0-3-ubuntu:~$ sudo mount /dev/vdb1 /home/d ubuntu@VM-0-3-ubuntu:~$ sudo df -h Filesystem Size Used Avail Use% Mounted on udev 1.9G 0 1.9G 0% /dev tmpfs 394M 780K 393M 1% /run /dev/vda2 50G 4.3G 43G 10% / tmpfs 2.0G 24K 2.0G 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup tmpfs 394M 0 394M 0% /run/user/1000 /dev/vdb1 393G 73M 373G 1% /home/d
但是如果重启这种挂载关系就会消失了
6、设置自动挂载
编辑/etc/fstab文件 sudo vim /etc/fstab 在打开的文件中加入 /dev/vdb1 /home/d ext4 defaults 0 0 命令生效 sudo mount -a 查看挂载 sudo df -h 解除挂载 unmount 设备名 unmount /dev/vdb1 查询指定目录的磁盘占用情况,默认是当前目录 sudo du -ach --max-depth=1 /home/d - s:指定目录占用大小汇总 - h:带计量单位 - a:含文件 --max-depth=1 :子目录深度 - c:列出明细的同时,增加汇总值 以树状显示目录结构 sudo tree /home/d
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构