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---------------------------------------- [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: Last mounted on: 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环境调用成功。 |