北在北方

太白枝头看,花开不计年,杯中浮日月,楼外是青天。

导航

Linux文件系统管理

Posted on 2013-01-03 15:43  CN.programmer.Luxh  阅读(781)  评论(0编辑  收藏  举报

1、文件系统构成

  /usr/bin、/bin:存放所有用户可以执行的命令

  /usr/sbin、/sbin:存放只有root可以执行的命令

  /home:用户缺省宿主目录

  /proc:虚拟文件系统,存放当前内存镜像

  /dev:存放设备文件

  /lib:存放系统程序运行所需的共享库

  /lost+found:存放一些系统出错的检查结果

  /tmp:存放临时文件

  /etc:系统配置文件

  /var:包含经常发生变动的文件,如邮件、日志文化、计划任务等

  /usr:存放所有命令、库、手册页等

  /mnt:临时文件系统的安装点

  /boot:内核文件及自举程序文件保存位置

  1)常用命令

    查看分区情况:df

[root@localhost ~]# df
文件系统                 1K-块      已用      可用 已用% 挂载点
/dev/sda1              5039616    623020   4160596  14% /
tmpfs                   515396         0    515396   0% /dev/shm
/dev/sda5              1007896     44760    911936   5% /home
/dev/sda2              5039616   1737788   3045828  37% /usr
[root@localhost ~]# df -h
文件系统              容量  已用  可用 已用%% 挂载点
/dev/sda1             4.9G  609M  4.0G  14% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda5             985M   44M  891M   5% /home
/dev/sda2             4.9G  1.7G  3.0G  37% /usr
[root@localhost ~]# df -m
文件系统                 1M-块      已用      可用 已用% 挂载点
/dev/sda1                 4922       609      4064  14% /
tmpfs                      504         0       504   0% /dev/shm
/dev/sda5                  985        44       891   5% /home
/dev/sda2                 4922      1698      2975  37% /usr
[root@localhost ~]# 

    查看文件、目录大小:du

[root@localhost ~]# du -h /etc/services
628K    /etc/services
[root@localhost ~]# du -sh /etc
27M     /etc
[root@localhost ~]# 

    检测修复文件系统:fsck、e2fsck(单用户模式执行)

    判断文件类型:file

  2)使用光驱

    挂载光驱

[root@localhost ~]# mount /dev/cdrom /mnt/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]#

    如果没有/mnt/cdrom目录,先手工创建该目录。

    查看挂载信息

[root@localhost ~]# df -h
文件系统              容量  已用  可用 已用%% 挂载点
/dev/sda1             4.9G  609M  4.0G  14% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda5             985M   44M  891M   5% /home
/dev/sda2             4.9G  1.7G  3.0G  37% /usr
/dev/sr0              3.5G  3.5G     0 100% /mnt/cdrom
[root@localhost ~]#

    卸载光驱

[root@localhost ~]# umount /mnt/cdrom

    或者

[root@localhost ~]# eject

  3)添加磁盘或分区

    现在用虚拟机添加了一块硬盘sdb,重启系统后查看新添加的硬盘是否被识别:

[root@localhost ~]# dmesg | grep sdb
sd 2:0:1:0: [sdb] 10485760 512-byte logical blocks: (5.36 GB/5.00 GiB)
sd 2:0:1:0: [sdb] Write Protect is off
sd 2:0:1:0: [sdb] Mode Sense: 61 00 00 00
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
 sdb:
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
sd 2:0:1:0: [sdb] Attached SCSI disk
[root@localhost ~]#

    3.1)分区:fdisk

      查看新添加的硬盘信息

[root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@localhost ~]# 

      对硬盘进行分区

      fdisk  /dev/sdb

      常用命令:

      m  帮助

      p  显示分区表

      n  添加新分区

      t  改变分区文件系统类型

      d  删除分区

      w  保存退出

      q  不保存退出

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x118c05b0.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p

    3.2)创建文件系统(格式化):mkfs

[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
160960 inodes, 642592 blocks
32129 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=658505728
20 block groups
32768 blocks per group, 32768 fragments per group
8048 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# 

    3.3)尝试挂载:mount

      把/dev/sdb1挂载到/web空目录

      先创建/web目录

[root@localhost ~]# mkdir /web

      挂载

[root@localhost ~]# mount /dev/sdb1 /web

      查看是否挂载成功

[root@localhost ~]# df -h
文件系统              容量  已用  可用 已用%% 挂载点
/dev/sda1             4.9G  609M  4.0G  14% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda5             985M   44M  891M   5% /home
/dev/sda2             4.9G  1.7G  3.0G  37% /usr
/dev/sdb1             2.5G   68M  2.3G   3% /web
[root@localhost ~]# 

     3.4)写入配置文件:/etc/fstab

[root@localhost ~]# more /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Dec  5 04:36:40 2012
#
# 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
#
UUID=1782302e-7804-4b32-9e59-04aac3651342 /                       ext4    defaul
ts        1 1
UUID=e5ec7966-8a82-438a-b929-962d375bb1e5 /home                   ext4    defaul
ts        1 2
UUID=926c0281-29b0-4ab0-9483-c1066462d508 /usr                    ext4    defaul
ts        1 2
UUID=ac208436-48b7-4bfa-b25d-758af1ea9019 swap                    swap    defaul
ts        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@localhost ~]#

    由以下部分组成

    物理分区名/卷标  挂载点  文件系统  缺省设置  是否检测  检测顺序

      proc      /proc   proc    defaults  1/0    0/1/2

    把我们刚新加的分区配置进去,让系统引导的时候自动加载 

[root@localhost ~]# vi /etc/fstab


#
# /etc/fstab
# Created by anaconda on Wed Dec  5 04:36:40 2012
#
# 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
#
UUID=1782302e-7804-4b32-9e59-04aac3651342 /                       ext4    defaults        1 1
UUID=e5ec7966-8a82-438a-b929-962d375bb1e5 /home                   ext4    defaults        1 2
UUID=926c0281-29b0-4ab0-9483-c1066462d508 /usr                    ext4    defaults        1 2
UUID=ac208436-48b7-4bfa-b25d-758af1ea9019 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb1               /web                    ext4    defaults        1 2
~
~
"/etc/fstab" 17L, 936C written
[root@localhost ~]#


 2、磁盘配额

  1)开启分区配额功能

    1.1)编辑/etc/fstab文件,在挂载属性上加上标志:usrquota(用户配额)或grpquota(用户组配额)

    假如/home这个分区,限制每个用户只能使用50M的空间:

    加载用户配额设置

[root@localhost ~]# vi /etc/fstab


#
# /etc/fstab
# Created by anaconda on Wed Dec  5 04:36:40 2012
#
# 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
#
UUID=1782302e-7804-4b32-9e59-04aac3651342 /                       ext4    defaults        1 1
UUID=e5ec7966-8a82-438a-b929-962d375bb1e5 /home                   ext4    defaults        1 2
UUID=926c0281-29b0-4ab0-9483-c1066462d508 /usr                    ext4    defaults        1 2
UUID=ac208436-48b7-4bfa-b25d-758af1ea9019 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb1               /web                    ext4    defaults,usrquota       1 2
~
"/etc/fstab" 17L, 945C written
[root@localhost ~]#

    临时开启分区配额功能

[root@localhost ~]# mount -o remount,usrquota /home

    1.2)创建配额数据库:quotacheck -cvu /home

[root@localhost ~]# quotacheck -cvu /web
quotacheck: Mountpoint (or device) /web not found or has no quota enabled.
quotacheck: Cannot find filesystem to check or filesystem not mounted with quota option.
[root@localhost ~]# quotacheck -cvu /home
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/sda5 [/home] done
quotacheck: Cannot stat old user quota file: 没有那个文件或目录
quotacheck: Old group file not found. Usage will not be substracted.
quotacheck: Checked 387 directories and 1626 files
quotacheck: Old file not found.
[root@localhost ~]# 

    /home下已经有了aquota.user这个文件

[root@localhost ~]# ls /home
aquota.user  lixunxuan   luixaofeng  luxh01  luxh03  luxh05      yekai
cnblogs001   lost+found  luxh        luxh02  luxh04  luxiaofeng
[root@localhost ~]# 

    1.3)启动配额功能

[root@localhost ~]# quotaon /home
[root@localhost ~]#

    如果要关闭配额功能

[root@localhost ~]# quotaoff /home
[root@localhost ~]#

    1.4)编辑用户配额

      edquota 用户名

[root@localhost ~]# edquota luxh01

Disk quotas for user luxh01 (uid 1001):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda5                         8          0       51200          2        0        0
~

    blocks soft  hard 限制用户能使用的空间大小

    inodes  soft  hard  限制用户能创建多少个文件

    查看luxh01用户的配额信息

[root@localhost ~]# quota luxh01
Disk quotas for user luxh01 (uid 1001): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sda5       8       0   51200               2       0       0        
[root@localhost ~]# 

   查看所有用户的配额信息

[root@localhost ~]# repquota /home
*** Report for user quotas on device /dev/sda5
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --   26936       0       0           1964     0     0       
luxh      --       8       0       0              2     0     0       
lihuai    --      36       0       0              9     0     0       
luixaofeng --      32       0       0              8     0     0       
lixunxuan --      32       0       0              8     0     0       
luxiaofeng --      32       0       0              8     0     0       
luxh01    --       8       0   51200              2     0     0       
luxh02    --       4       0       0              1     0     0       
luxh03    --       4       0       0              1     0     0       
luxh04    --       4       0       0              1     0     0       
luxh05    --       4       0       0              1     0     0       
cnblogs001 --      32       0       0              8     0     0       


[root@localhost ~]# 

 

    复制用户配额信息:

    edquota -p 模板用户 复制用户1 复制用户2

[root@localhost ~]# edquota -p luxh01 luxh02 luxh03

 

3、备份

  1)cp命令备份举例

    备份目录

    cp -Rpu 备份目录 目标目录

      -p  保持备份目录及文件属性

      -u  增量备份   

[root@localhost ~]# cp -p /etc/inittab /backup/inittab_20130103.bak

    远程备份可以用scp

  2)tar命令打包备份

    备份/etc目录,可以同时备份多个目录

[root@localhost ~]# tar -zcf /backup/etc_20130103.tar.gz /etc

    还原/etc目录,默认还原到打包文件源目录

[root@localhost ~]# tar -zxf /backup/etc_20130103.tar.gz

    只还原指定的文件,如/etc/passwd,注意,在命令中要写成etc/passwd,解压到当前目录。

[root@localhost ~]# tar -zvf /backup/etc_20130103.tar.gz etc/passwd