理論知識有點多,加油!node
相關目錄做用linux
目錄名稱 | 應放置文件的內容 |
---|---|
/boot | 開機所需文件—內核、開機菜單以及所需配置文件等 |
/dev | 以文件形式存聽任何設備與接口 |
/etc | 配置文件 |
/home | 用戶主目錄 |
/bin | 存放單用戶模式下還能夠操做的命令 |
/lib | 開機時用到的函數庫,以及/bin與/sbin下面的命令要調用的函數 |
/sbin | 開機過程當中須要的命令 |
/media | 用於掛載設備文件的目錄 |
/opt | 放置第三方的軟件 |
/root | 系統管理員的家目錄 |
/srv | 一些網絡服務的數據文件目錄 |
/tmp | 任何人都可使用的「共享」臨時目錄 |
/proc | 虛擬文件系統,例如系統內核、進程、外部設備及網絡狀態等 |
/usr/local | 用戶自行安裝的軟件 |
/usr/sbin | Linux系統開機時不會使用到的軟件/命令/腳本 |
/usr/share | 幫助與說明文件,也可放置共享文件 |
/var | 主要存放常常變化的文件,如日誌 |
/lost+found | 當文件系統發生錯誤時,將一些丟失的文件片斷存放在這裏 |
路徑指的是如何定位到某個文件,分爲絕對路徑與相對路徑web
硬件設備 | 文件名稱 |
---|---|
IDE設備 | /dev/hd[a-d] |
SCSI/SATA/U盤 | /dev/sd[a-z] |
virtio設備 | /dev/vd[a-z] |
軟驅 | /dev/fd[0-1] |
打印機 | /dev/lp[0-15] |
光驅 | /dev/cdrom |
鼠標 | /dev/mouse |
磁帶機 | /dev/st0或/dev/ht0 |
主分區和邏輯分區shell
主分區或擴展分區的編號從1開始,到4結束;vim
邏輯分區從編號5開始。服務器
主要有Ext 二、Ext 三、Ext 四、XFSmarkdown
Ext3的改進版本,做爲RHEL 6系統中的默認文件管理系統,它支持的存儲容量高達1EB(1EB=1,073,741,824GB),且可以有無限多的子目錄。另外,Ext4文件系統可以批量分配block塊,從而極大地提升了讀寫效率。如今不少主流服務器也會使用。 網絡
是一種高性能的日誌文件系統,並且是RHEL 7/8中默認的文件管理系統,它的優點在發生意外宕機後尤爲明顯,便可以快速地恢復可能被破壞的文件,並且強大的日誌功能只用花費極低的計算和存儲性能。而且它最大可支持的存儲容量爲18EB,這幾乎知足了全部需求。 app
該文件的訪問權限(read、write、execute);socket
該文件的全部者與所屬組(owner、group);
該文件的大小(size);
該文件的建立或內容修改時間(ctime);
該文件的最後一次訪問時間(atime);
該文件的修改時間(mtime);
文件的特殊權限(SUID、SGID、SBIT);
該文件的真實數據地址(point)
一 個inode的默認大小僅爲128字節,記錄一個block則消耗4字節。當文件的inode被寫滿後,Linux系統會自動分配出一個block塊,專門用於像inode那樣記錄其餘block塊的信息,這樣把各個block塊的內容串到一塊兒,就可以讓用戶讀到完整的文件內容了。
兩種常見狀況
狀況1:文件很小(1KB),但依然會佔用一個block,所以會潛在地浪費3KB。
狀況2:文件很大(5KB),那麼會佔用兩個block(5KB-4KB後剩下的1KB也要佔用一個block)。
用於掛載文件系統
格式爲「mount 文件系統 掛載目錄」
參數 | 做用 |
---|---|
-a | 掛載全部在/etc/fstab中定義的文件系統 |
-t | 指定文件系統的類型 |
# 掛載到目錄 [root@linuxprobe ~]# mount /dev/sdb2 /backup # 掛載到網絡 # 查看UUID值 [root@localhost ~]# blkid /dev/sda3: UUID="1253ac5b-eaed-4c4c-808d-09fb4828358f" TYPE="xfs" PARTUUID="0afc9026-03" /dev/sda1: UUID="bcc55e4d-0854-44a8-9449-dad12374a6d3" TYPE="xfs" PARTUUID="0afc9026-01" /dev/sda2: UUID="45a308e8-b622-4f24-9ea2-d4f473388981" TYPE="swap" PARTUUID="0afc9026-02" # 根據UUID掛載 [root@linuxprobe ~]# mount UUID=bcc55e4d-0854-44a8-9449-dad12374a6d3 /backup
umount命令用於卸載設備或文件系統
格式爲:「umount [設備文件/掛載目錄]」
[root@linuxprobe ~]# umount /dev/sdb2
若是想將文件系統爲ext4的硬件設備/dev/sdb2在開機後自動掛載到/backup目錄上,並保持默認權限且無需開機自檢,就須要在/etc/fstab文件中寫入下面的信息,這樣在系統重啓後也會成功掛載。
字段 | 意義 |
---|---|
設備文件 | 通常爲設備的路徑+設備名稱,也能夠寫惟一識別碼(UUID,Universally Unique Identifier) |
掛載目錄 | 指定要掛載到的目錄,需在掛載前建立好 |
格式類型 | 指定文件系統的格式,好比Ext三、Ext四、XFS、SWAP、iso9660(此爲光盤設備)等 |
權限選項 | 若設置爲defaults,則默認權限爲:rw, suid, dev, exec, auto, nouser, async |
是否備份 | 若爲1則開機後使用dump進行磁盤備份,爲0則不備份 |
是否自檢 | 若爲1則開機後自動進行磁盤自檢,爲0則不自檢 |
[root@linuxprobe ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 21 05:03:40 2020 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # /dev/mapper/rhel-root / xfs defaults 0 0 UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 0 0 /dev/mapper/rhel-swap swap swap defaults 0 0 /dev/sdb2 /backup ext4 defaults 0 0
寫入到/etc/fstab文件中的設備信息並不會當即生效,須要使用mount -a參數進行自動掛載:
[root@linuxprobe ~]# mount -a
用於已掛載的磁盤空間使用狀況
語法格式爲:「df -h」
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 890M 0 890M 0% /dev tmpfs 904M 0 904M 0% /dev/shm tmpfs 904M 9.4M 894M 2% /run tmpfs 904M 0 904M 0% /sys/fs/cgroup /dev/sda3 18G 4.4G 14G 25% / /dev/sda1 295M 144M 152M 49% /boot tmpfs 181M 16K 181M 1% /run/user/42 tmpfs 181M 4.0K 181M 1% /run/user/0
查看已掛載的磁盤空間使用狀況
[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 300M 0 part /boot ├─sda2 8:2 0 2G 0 part [SWAP] └─sda3 8:3 0 17.7G 0 part / sdb 8:16 0 20G 0 disk sr0 11:0 1 1024M 0 rom sr1 11:1 1 1024M 0 rom
查看分區或目錄所佔用的磁盤容量大小
語法格式爲:「du -sh 目錄名稱」
[root@localhost ~]# du -sh /* 0 /bin 126M /boot 0 /dev 28M /etc 12K /home 0 /lib 0 /lib64 0 /media 0 /MMX 0 /mnt 0 /opt 0 /proc 84M /root 9.4M /run 0 /sbin 0 /srv 0 /sys 52K /tmp 3.9G /usr 139M /var
步驟:
一、安裝硬盤
二、在/dev/sd*找到新安裝硬盤,通常沒有分區,好比/dev/sdb,不存在/dev/sdb【n】等
三、fdisk命令新建分區
四、mkfs命令格式化分區
五、新建目錄【分區好後掛載上面】mkdir /XXX
六、使用mount命令,mount /dev/sd【x】 步驟5目錄
七、編輯註冊文件/etc/fstab
八、mount -a自動掛載
九、df -h查看掛載結果
十、reboot重啓,再次使用df -h查看是否開機自動掛載
[root@localhost ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb [root@localhost ~]# mkfs mkfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs [root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). 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. Created a new DOS disklabel with disk identifier 0x2c7d0518. Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu u change display/entry units x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table 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): 1 First sector (2048-41943039, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +5G Created a new partition 1 of type 'Linux' and of size 5 GiB. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1 [root@localhost ~]# mkdir /MMX [root@localhost ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327680 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=1310720, 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 [root@localhost ~]# mount /dev/sdb1 /MMX [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 890M 0 890M 0% /dev tmpfs 904M 0 904M 0% /dev/shm tmpfs 904M 9.4M 894M 2% /run tmpfs 904M 0 904M 0% /sys/fs/cgroup /dev/sda3 18G 4.4G 14G 25% / /dev/sda1 295M 144M 152M 49% /boot tmpfs 181M 16K 181M 1% /run/user/42 tmpfs 181M 4.0K 181M 1% /run/user/0 /dev/sdb1 5.0G 68M 5.0G 2% /MMXsks.
重啓以後:
[root@localhost ~]# tail -n 5 /etc/fstab # UUID=1253ac5b-eaed-4c4c-808d-09fb4828358f / xfs defaults 0 0 UUID=bcc55e4d-0854-44a8-9449-dad12374a6d3 /boot xfs defaults 0 0 UUID=45a308e8-b622-4f24-9ea2-d4f473388981 swap swap defaults 0 0 /dev/sdb1 /MMX xfs defaults 0 0 #順利自動掛載上去! [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 890M 0 890M 0% /dev tmpfs 904M 0 904M 0% /dev/shm tmpfs 904M 9.4M 894M 2% /run tmpfs 904M 0 904M 0% /sys/fs/cgroup /dev/sda3 18G 4.4G 14G 25% / /dev/sdb1 5.0G 68M 5.0G 2% /MMX /dev/sda1 295M 144M 152M 49% /boot tmpfs 181M 16K 181M 1% /run/user/42 tmpfs 181M 4.0K 181M 1% /run/user/0
編輯/etc/fstab錯誤,致使SSH沒法鏈接
一、編輯的/etc/fstab文件以下,xfs格式錯誤,寫成了ext4
# # /etc/fstab # Created by anaconda on Sat Jul 3 11:06:41 2021 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # UUID=1253ac5b-eaed-4c4c-808d-09fb4828358f / xfs defaults 0 0 UUID=bcc55e4d-0854-44a8-9449-dad12374a6d3 /boot xfs defaults 0 0 UUID=45a308e8-b622-4f24-9ea2-d4f473388981 swap swap defaults 0 0 /dev/sdb1 /MMX ext4 defaults 0 0
二、此時已經沒法鏈接SSH
[C:\~]$ Connecting to 192.168.180.128:22... Could not connect to '192.168.180.128' (port 22): Connection failed. Type `help' to learn how to use Xshell prompt.
三、虛擬機界面以下
四、輸入密碼以後,查看IP地址,發現有問題
五、刪除/etc/fstab最後編輯的哪一行,重啓reboot
六、恢復正常,SSH已經能夠鏈接
七、從新編輯/etc/fstab目錄
# # /etc/fstab # Created by anaconda on Sat Jul 3 11:06:41 2021 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # UUID=1253ac5b-eaed-4c4c-808d-09fb4828358f / xfs defaults 0 0 UUID=bcc55e4d-0854-44a8-9449-dad12374a6d3 /boot xfs defaults 0 0 UUID=45a308e8-b622-4f24-9ea2-d4f473388981 swap swap defaults 0 0 /dev/sdb1 /MMX xfs defaults 0 0 ~
八、重啓看效果
[root@localhost ~]# reboot Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(紅帽8模擬器) at 20:13:30. Type `help' to learn how to use Xshell prompt. [C:\~]$ Connecting to 192.168.180.128:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Activate the web console with: systemctl enable --now cockpit.socket Last login: Sun Jul 11 05:11:49 2021 from 192.168.180.1 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 890M 0 890M 0% /dev tmpfs 904M 0 904M 0% /dev/shm tmpfs 904M 9.4M 894M 2% /run tmpfs 904M 0 904M 0% /sys/fs/cgroup /dev/sda3 18G 4.4G 14G 25% / /dev/sdb1 5.0G 68M 5.0G 2% /MMX /dev/sda1 295M 144M 152M 49% /boot tmpfs 181M 16K 181M 1% /run/user/42 tmpfs 181M 4.0K 181M 1% /run/user/0
SWAP交換分區是一種經過在硬盤中預先劃分必定的空間,而後將把內存中暫時不經常使用的數據臨時存放到硬盤中,以便騰出物理內存空間讓更活躍的程序服務來使用的技術,其設計目的是爲了解決真實物理內存不足的問題。
[root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 881Mi 113Mi 9.0Mi 810Mi 732Mi Swap: 2.0Gi 0B 2.0Gi
[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 300M 0 part /boot ├─sda2 8:2 0 2G 0 part [SWAP] └─sda3 8:3 0 17.7G 0 part / sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 5G 0 part /MMX └─sdb2 8:18 0 5G 0 part sr0 11:0 1 1024M 0 rom sr1 11:1 1 1024M 0 rom
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu u change display/entry units x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table Command (m for help): n Partition type p primary (2 primary, 0 extended, 2 free) e extended (container for logical partitions) Select (default p): p Partition number (3,4, default 3): First sector (20973568-41943039, default 20973568): Last sector, +sectors or +size{K,M,G,T,P} (20973568-41943039, default 41943039): +5G Created a new partition 3 of type 'Linux' and of size 5 GiB. Command (m for help): t Partition number (1-3, default 3): 3 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris'. Command (m for help): p Disk /dev/sdb: 20 GiB, 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 Disklabel type: dos Disk identifier: 0x2c7d0518 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 10487807 10485760 5G 83 Linux /dev/sdb2 10487808 20973567 10485760 5G 83 Linux /dev/sdb3 20973568 31459327 10485760 5G 82 Linux swap / Solaris Command (m for help): w The partition table has been altered. Syncing disks.
mkswap 掛載交換分區路徑
swapon 交換分區路徑
[root@localhost ~]# mkswap /dev/sdb3 Setting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=3fbdf0fd-e425-448f-a9a1-c8a6ef5962de # 激活前 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 884Mi 108Mi 9.0Mi 813Mi 730Mi Swap: 2.0Gi 0B 2.0Gi [root@localhost ~]# swapon /dev/sdb2 # 激活後 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 888Mi 104Mi 9.0Mi 813Mi 726Mi Swap: 7.0Gi 0B 7.0Gi
[root@localhost ~]# vim /etc/fstab [root@localhost ~]# tail -n 5 /etc/fstab UUID=1253ac5b-eaed-4c4c-808d-09fb4828358f / xfs defaults 0 0 UUID=bcc55e4d-0854-44a8-9449-dad12374a6d3 /boot xfs defaults 0 0 UUID=45a308e8-b622-4f24-9ea2-d4f473388981 swap swap defaults 0 0 /dev/sdb1 /MMX xfs defaults 0 0 /dev/sdb3 swap swap defaults 0 0
[root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 768Mi 546Mi 9.0Mi 490Mi 848Mi Swap: 2.0Gi 0B 2.0Gi # 發現這個,好像把sdb2掛載上去了……【mkswap /dev/sdb3】 # 從新執行一次命令 [root@localhost ~]# mkswap /dev/sdb3 [root@localhost ~]# swapon /dev/sdb3 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 767Mi 544Mi 9.0Mi 494Mi 849Mi Swap: 7.0Gi 0B 7.0Gi # 果真…… [root@localhost ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Sat Jul 3 11:06:41 2021 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # UUID=1253ac5b-eaed-4c4c-808d-09fb4828358f / xfs defaults 0 0 UUID=bcc55e4d-0854-44a8-9449-dad12374a6d3 /boot xfs defaults 0 0 UUID=45a308e8-b622-4f24-9ea2-d4f473388981 swap swap defaults 0 0 /dev/sdb1 /MMX xfs defaults 0 0 /dev/sdb3 swap swap defaults 0 0 # 加上去把,一塊兒12G /dev/sdb2 swap swap defaults 0 0
[C:\~]$ Connecting to 192.168.180.128:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Activate the web console with: systemctl enable --now cockpit.socket Last login: Sun Jul 11 06:25:17 2021 from 192.168.180.1 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 1.8Gi 769Mi 564Mi 9.0Mi 472Mi 839Mi Swap: 11Gi 0B 11Gi