1.磁盤分區(fdisk)
命令:
fdisk
常用參數:
-l:查看分區詳細信息
例:把/dev/sda進行分區,劃分出一個/dev/sda3
[root@freeit ~]# fdisk /dev/sda 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): //此處可以根據參數的不同,查看相應的信息。常用參數如下: ------------------------------------------常用參數----------------------------------------------------- m:幫助 p: 顯示分區表 n:添加新分區 t: 改變分區文件系統類型 d: 刪除分區 w: 保存分區並退出 q: 不保存退出 -------------------------------------------分區操作------------------------------------------------------------ Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000b2b59
Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 127502335 63545344 8e Linux LVM //查看/dev/sda現有的分區信息 Command (m for help): n //輸入n進行分區 Partition type: p primary (2 primary, 0 extended, 2 free) e extended //分區類型,p爲主分區,最多4個。e爲邏輯分區。 Select (default p): p Partition number (3,4, default 3): 3 //劃分第三個主分區 First sector (127502336-209715199, default 127502336): Using default value 127502336 Last sector, +sectors or +size{K,M,G} (127502336-209715199, default 209715199): +200M //大小爲200M Partition 3 of type Linux and of size 200 MiB is set
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000b2b59
Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 127502335 63545344 8e Linux LVM /dev/sda3 127502336 127911935 204800 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@freeit ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000b2b59
Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 127502335 63545344 8e Linux LVM /dev/sda3 127502336 127911935 204800 83 Linux |
格式化分區/dev/sda3
[root@freeit ~]# mkfs.ext4 /dev/sda3 mke2fs 1.42.9 (28-Dec-2013) Could not stat /dev/sda3 --- No such file or directory
The device apparently does not exist; did you specify it correctly? ----------------------------------------------查看分區是否成功------------------------------------------------- [root@freeit ~]# cat /proc/partitions major minor #blocks name
2 0 4 fd0 11 0 4139008 sr0 8 0 104857600 sda 8 1 204800 sda1 8 2 63545344 sda2 |
如上:進行格式化時,提示設備分區不存在。查看分區表,也沒有此分區信息。這是爲什麼呢?
這是因爲在Linux中,雖然分區已經劃分完畢,但是新分區信息還沒有寫進內核,所以內核不能識別,也就不能使用。要讓內核識別此分區,有兩種辦法:
重啓
使用partprobe 命令
以上兩種方法都能把分區信息寫進系統內核,讓系統識別。但是我們推薦使用第二種,不用重啓系統。更快、更便捷,同時也迎合Linux的一大優勢,做到基本不用重啓系統。
[root@freeit ~]# partprobe /dev/sda [root@freeit ~]# cat /proc/partitions major minor #blocks name
2 0 4 fd0 11 0 4139008 sr0 8 0 104857600 sda 8 1 204800 sda1 8 2 63545344 sda2 8 3 204800 sda3 //分區表中出現sda3 [root@freeit ~]# mkfs.ext4 /dev/sda3 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 51200 inodes, 204800 blocks 10240 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=33816576 25 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 |
分區完畢之後,如何使用呢?下個實驗,將介紹到如何掛載使用。
2.文件系統的掛載
Linux中的文件使用與windows不同,文件(或設備)的使用要先掛載。所以,本章我們詳細介紹文件的掛載。
2.1.mount直接掛載
這個是最簡單的掛載,也是直接掛載,僅需簡單的一條命令即可掛載使用。
例:把/dev/sda3掛載到/share
-----------------------------------------掛載前查看掛載信息---------------------------------------- [root@freeit ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/rhel-root 61410000 3019196 58390804 5% / devtmpfs 496084 0 496084 0% /dev tmpfs 505392 0 505392 0% /dev/shm tmpfs 505392 7136 498256 2% /run tmpfs 505392 0 505392 0% /sys/fs/cgroup /dev/sda1 201388 106044 95344 53% /boot // /dev/sda3沒有掛載 -------------------------------------------掛載/dev/sda3到/share------------------------------------ [root@freeit ~]# mkdir /share //創建掛載點 [root@freeit ~]# mount /dev/sda3 /share/ [root@freeit ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/rhel-root 61410000 3018988 58391012 5% / devtmpfs 496084 0 496084 0% /dev tmpfs 505392 0 505392 0% /dev/shm tmpfs 505392 7140 498252 2% /run tmpfs 505392 0 505392 0% /sys/fs/cgroup /dev/sda1 201388 106044 95344 53% /boot /dev/sda3 95054 1550 86336 2% /share |
注:
此掛載只是暫時的,系統重啓後失效
如要卸載掛載的設備,用umount以同樣的方法卸載。
2.2./etc/fstab自動掛載
鑑於mount命令的直接掛載爲臨時掛載,所以引進了/etc/fstab文件,/etc/fstab可以實現系統重啓後自動掛載。系統在重啓後,會檢查此文件中需要掛載的設備,在重啓過程中進行自動掛載,實現開機自動掛載功能。
此文件中默認有系統的幾個必須掛載項,如下:
[root@freeit ~]# cat /etc/fstab
# # /etc/fstab # Created by anaconda on Thu Dec 11 14:38:57 2014 # # 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 # /dev/mapper/rhel-root / xfs defaults 1 1 UUID=469e7785-f327-4d93-abd4-62d408e45b8a /boot xfs defaults 1 2 /dev/mapper/rhel-swap swap swap defaults 0 0 |
說明:
/dev/mapper/rhel-root:爲物理掛載設備
/ :掛載點
xfs :文件系統格式
defaults :缺省設置
1:是否檢測,1爲檢測,0爲不檢測.該選項被“dump”命令用來檢查一個文件系統應該以多塊頻率進行轉儲,若不需要轉儲就設置該字段爲0;
1:檢測順序,優先檢測數字大的(0/1/2)。該字段被fsck命令用來決定在啓動時需要被掃描的文件系統的順序,根文件系統“/”對應該字段的值應該爲1,其它文件系統應該爲2.若該文件系統無需在啓動時掃描,則設置該字段爲0.
現在,繼續1.1實驗的後續,設置開機自動掛載
# # /etc/fstab # Created by anaconda on Thu Dec 11 14:38:57 2014 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more in fo # /dev/mapper/rhel-root / xfs defaults 1 1 UUID=469e7785-f327-4d93-abd4-62d408e45b8a /boot xfs defaults 1 2 /dev/mapper/rhel-swap swap swap defaults 0 0 /dev/sda3/ /share ext4 defaults 0 0 |
如上,寫進文件之後就可以實現開機自動掛載了。
2.3.autofs掛載
Autofs與mount/Umount的不同之處在於,它是一種看守程序。如果它檢測到用戶正試圖訪問一個尚未掛載的文件系統,它就會自動檢測該文件系統,如果存在,那麼Autofs會自動將其掛載。另一方面,如果它檢測到某個已掛載的文件系統在一段時間內沒有被使用,那麼Autofs會自動將其卸載。因此一旦運行了Autofs後,用戶就不再需要手動完成文件系統的掛載和卸載。
2.3.1.autofs的安裝
RHEL7之前的系統,缺省已安裝autofs服務的,但是RHEL7缺省未安裝。
[root@freeit ~]# rpm -q autofs package autofs is not installed ---------------------------------------------yum安裝autofs---------------------------------------- <yum倉庫的配置見【軟件包的安裝與管理章節】> [root@freeit ~]# yum -y install autofs Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. base | 4.1 kB 00:00 (1/2): base/group_gz | 134 kB 00:00 (2/2): base/primary_db | 3.4 MB 00:00 Resolving Dependencies --> Running transaction check ---> Package autofs.x86_64 1:5.0.7-40.el7 will be installed --> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-40.el7.x86_64 --> Running transaction check ---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed --> Finished Dependency Resolution
Dependencies Resolved
========================================================================= Package Arch Version Repository Size ========================================================================= Installing: autofs x86_64 1:5.0.7-40.el7 base 550 k Installing for dependencies: hesiod x86_64 3.2.1-3.el7 base 30 k
Transaction Summary ========================================================================= Install 1 Package (+1 Dependent package)
Total download size: 579 k Installed size: 3.6 M Downloading packages: ------------------------------------------------------------------------- Total 7.7 MB/s | 579 kB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : hesiod-3.2.1-3.el7.x86_64 1/2 Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2 base/productid | 1.6 kB 00:00 Verifying : hesiod-3.2.1-3.el7.x86_64 1/2 Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2
Installed: autofs.x86_64 1:5.0.7-40.el7
Dependency Installed: hesiod.x86_64 0:3.2.1-3.el7
Complete! |
Autofs是一個服務,有兩個配置文件,都在/etc下,分別爲auto.master、auto.misc
[root@freeit ~]# ls /etc/auto autofs_ldap_auth.conf auto.master.d/ auto.net auto.master auto.misc auto.smb |
2.3.2.RHEL7之前系統的autofs掛載
首先查看第一個配置文件auto.master
# # Sample auto.master file # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # For details of the format look at autofs(5). # /misc /etc/auto.misc | | 掛載點主目錄 掛載點的配置文件 /share /etc/auto.share # # NOTE: mounts done from a hosts map will be mounted with the # "nosuid" and "nodev" options unless the "suid" and "dev" # options are explicitly given. # /net -hosts # +dir:/etc/auto.master.d # # Include central master map if it can be found using # nsswitch sources. # # Note that if there are entries for /net or /misc (as # above) in the included master map any keys that are the # same will not be seen as the first read key seen takes # precedence. # +auto.master |
如上:掛載點主目錄可以自己手動創建,也可以不創建,重啓autofs服務後會自動創建。掛載點配置文件可以與默認的一樣,也可以自己創建。如下,爲我們自己創建的第二個配置文件auto.share:
[root@freeit ~]# cp -p /etc/auto.misc /etc/auto.share //直接複製默認的配置文件即可,然後根據自己的需要更改 [root@freeit ~]# vim /etc/auto.share # # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # Details may be found in the autofs(5) manpage
# the following entries are samples to pique your imagination #linux -ro,soft,intr ftp.example.org:/pub/linux #boot -fstype=ext2 :/dev/hda1 #floppy -fstype=auto :/dev/fd0 #floppy -fstype=ext2 :/dev/fd0 #e2floppy -fstype=ext2 :/dev/fd0 #jaz -fstype=ext2 :/dev/sdc1 #removable -fstype=ext2 :/dev/hdd <下面一行爲自己添加的> server -fstype=ext4,rw :/dev/sda3 | | | | 真實掛載點 文件系統類型 權限 要掛載的物理設備 <真實掛載點不需要自己手動創建,當需要用到時系統會自動創建> ------------------------------------------重啓服務並驗證掛載------------------------------------------ [root@ns ~]# service autofs restart Stopping automount: [ OK ] Starting automount: [ OK ] [root@freeit ~]# ls /share/ [root@freeit ~]# //查看掛載主目錄,什麼也沒有,接下來查看掛載信息 [root@freeit ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/rhel-root 61410000 3049104 58360896 5% / devtmpfs 496084 0 496084 0% /dev tmpfs 505392 0 505392 0% /dev/shm tmpfs 505392 7148 498244 2% /run tmpfs 505392 0 505392 0% /sys/fs/cgroup /dev/sda1 201388 106044 95344 53% /boot /dev/sr0 4138442 4138442 0 100% /mnt //此時設備沒有掛載。接下來直接查看真實掛載點目錄 [root@freeit ~]# ls /share/server lost+found //查看真實掛載點,此時,有內容顯示,再查看掛載設備信息 [root@freeit ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/rhel-root 61410000 3049104 58360896 5% / devtmpfs 496084 0 496084 0% /dev tmpfs 505392 0 505392 0% /dev/shm tmpfs 505392 7148 498244 2% /run tmpfs 505392 0 505392 0% /sys/fs/cgroup /dev/sda1 201388 106044 95344 53% /boot /dev/sr0 4138442 4138442 0 100% /mnt /dev/sda3 95054 1550 86336 2% /share/server //設備已經掛載 |
注:autofs的掛載,當需要的時候纔會掛載,不需要時,默認300s自動卸載。
2.3.3.RHEL7系統的autofs掛載
RHEL7的autofs的掛載,方法可以與之前系統的掛載方法一樣,但是也有另一種方法,那就是第一個配置文件的不同,如下:
------------------------------------------------查看auto.master文件-------------------------------------- [root@freeit ~]# vim /etc/auto.master # Sample auto.master file # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # For details of the format look at autofs(5). # /misc /etc/auto.misc # # NOTE: mounts done from a hosts map will be mounted with the # "nosuid" and "nodev" options unless the "suid" and "dev" # options are explicitly given. # /net -hosts # # Include /etc/auto.master.d/*.autofs # +dir:/etc/auto.master.d # # Include central master map if it can be found using # nsswitch sources. # # Note that if there are entries for /net or /misc (as # above) in the included master map any keys that are the # same will not be seen as the first read key seen takes # precedence. # +auto.master |
注意到上面的加粗字體。第一個配置文件的設置也可以是/etc/ auto.master.d下以.autofs結尾的文件。那麼/etc/ auto.master.d到底有什麼呢?
[root@freeit ~]# cd /etc/auto.master.d/ [root@freeit auto.master.d]# ls [root@freeit auto.master.d]# |
如上,此文件下什麼也沒有,也就是說,如果要使用這種方式,需自己創建。
創建所需配置文件
[root@freeit auto.master.d]# pwd /etc/auto.master.d [root@freeit auto.master.d]# vim share.autofs /share /etc/auto.share | | 掛載主目錄 掛載點配置文件 ~ ~ ~ //wq!保存退出 |
接下來創建另一個配置文件/etc/auto.share
[root@freeit etc]# cp -p auto.misc auto.share [root@freeit etc]# vim auto.share # # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # Details may be found in the autofs(5) manpage
# the following entries are samples to pique your imagination #linux -ro,soft,intr ftp.example.org:/pub/linux #boot -fstype=ext2 :/dev/hda1 #floppy -fstype=auto :/dev/fd0 #floppy -fstype=ext2 :/dev/fd0 #e2floppy -fstype=ext2 :/dev/fd0 #jaz -fstype=ext2 :/dev/sdc1 #removable -fstype=ext2 :/dev/hdd server -fstype=ext4,rw :/dev/sda3 |
接下來,重啓服務,驗證掛載
[root@freeit etc]# systemctl restart autofs.service [root@freeit etc]# [root@freeit etc]# ls /share/server lost+found [root@freeit etc]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/rhel-root 61410000 3049376 58360624 5% / devtmpfs 496084 0 496084 0% /dev tmpfs 505392 0 505392 0% /dev/shm tmpfs 505392 7148 498244 2% /run tmpfs 505392 0 505392 0% /sys/fs/cgroup /dev/sda1 201388 106044 95344 53% /boot /dev/sr0 4138442 4138442 0 100% /mnt /dev/sda3 95054 1550 86336 2% /share/server //掛載成功。 |
3.文件系統的acl設置
Linux系統中,不僅文件可以設置acl權限,而且分區也可以acl權限。但是,缺省情況下,分區的acl功能是沒有開啓的(RHEL7之前的系統),如果要設置acl權限,需要爲分區開啓acl功能。開啓方式如下:
[root@ns ~]# vim /etc/fstab /dev/sda3 /mnt iso9660 defaults,acl 0 0 //在defaults後加上”,”+”acl“ [root@ns ~]# mount -o remount /dev/cdrom //重新掛載文件系統 |
這樣,文件系統的acl權限就打開了,可以通過以下命令查看
[root@freeit etc]# tune2fs -l /dev/sda3 //查看分區詳細信息 tune2fs 1.42.9 (28-Dec-2013) Filesystem volume name: <none> Last mounted on: <not available> Filesystem UUID: 3fa9fa6b-e967-4748-9229-206a3ae590f2 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: user_xattr acl //acl功能打開 Filesystem state: clean ………………….. |
Acl權限打開之後,即可以通過setacl命令設置(設置方式見權限管理--ACL)。
4.用戶遷移
現實生產環境中,可能遇到這樣的情況:一臺Linux上存儲着公司的員工賬戶(有可能上萬個甚至更多)。現在,由於發展需要,要升級硬件。此時問題就來了,上萬個用戶在新的機器上重新創建,顯然是個體力活,不太可能,那麼有什麼方式來實現簡單快捷的遷移呢?
下面就來介紹一個命令:newusers,通過此命令來實現用戶遷移功能
假設現在有兩臺機子,192.168.10.10&192.168.10.250,前者爲升級的硬件(新設備),後者爲需要遷移的設備(待升級設備,即舊設備),如何實現呢?如下:
-------------------------------------先在250設備上創建幾個實驗用戶--------------------------------- [root@ns ~]# useradd user1 [root@ns ~]# useradd user2 [root@ns ~]# useradd user3 [root@ns ~]# id user1 uid=501(user1) gid=501(user1) groups=501(user1) [root@ns ~]# id user2 uid=502(user2) gid=502(user2) groups=502(user2) [root@ns ~]# id user3 uid=503(user3) gid=503(user3) groups=503(user3) -----------------------------------從250設備複製用戶配置文件到10設備------------------------------ [root@freeit ~]# scp [email protected]:/etc/passwd /tmp/ //使用scp命令,在10設備上把250上的用戶配置文件“拉”過來 The authenticity of host '192.168.10.250 (192.168.10.250)' can't be established. RSA key fingerprint is af:63:d5:cf:76:af:71:02:22:3f:0d:7c:7e:eb:73:5e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.10.250' (RSA) to the list of known hosts. [email protected]'s password: passwd 100% 2029 2.0KB/s 00:00 [root@freeit ~]# scp [email protected]:/etc/shadow /tmp/ [email protected]'s password: shadow 100% 1131 1.1KB/s 00:00 [root@freeit ~]# scp [email protected]:/etc/gshadow /tmp/ [email protected]'s password: gshadow 100% 769 0.8KB/s 00:00 [root@freeit ~]# scp [email protected]:/etc/group /tmp/ [email protected]'s password: group 100% 941 0.9KB/s 00:00 [root@freeit ~]# ls /tmp/ group gshadow passwd shadow ---------------------------------------驗證此時能夠使用遷移過來的用戶----------------------------- [root@freeit ~]# id user1 id: user1: no such user [root@freeit ~]# id user2 id: user2: no such user [root@freeit ~]# id user3 id: user3: no such user //很明顯,不能使用 --------------------------------------------使用newusers命令--------------------------------------- [root@freeit ~]# cd /tmp/ [root@freeit tmp]# newusers passwd [root@freeit tmp]# id user1 uid=501(user1) gid=501(user1) groups=501(user1) [root@freeit tmp]# id user2 uid=502(user2) gid=502(user2) groups=502(user2) [root@freeit tmp]# id user3 uid=503(user3) gid=503(user3) groups=503(user3) //如上,現在用戶可以使用了 -----------------------------------------登錄用戶驗證-------------------------------------------------- [root@freeit tmp]# su - user1 -bash-4.2$ //雖然可以使用,但是很明顯,此時的用戶沒有shell環境 --------------------------------------爲用戶提供本地shell環境------------------------------------ -bash-4.2$ cp -p /etc/skel/.* . cp: omitting directory ‘/etc/skel/.’ cp: omitting directory ‘/etc/skel/..’ cp: omitting directory ‘/etc/skel/.mozilla’ //複製本地的用戶shell環境到新用戶的家目錄 -------------------------------------退出,重登驗證---------------------------------------------------------- [root@freeit tmp]# su - user1 Last login: Tue May 5 19:16:34 CST 2015 on pts/0 [user1@freeit ~]$ //shell環境調用成功。 |