迁移/home目录示例
注:来自Linux系统管理_磁盘分区和格式化的扩展
Linux系统管理_磁盘分区和格式化:http://murongqingqqq.blog.51cto.com/2902694/1361918
迁移/home目录的两种方式:
第一种方法具体步骤:
第一步:创建用户user1和user2
第二步:磁盘分区,划分一个20G的磁盘
第三步:更新磁盘分区:partprobe
第四步:格式化新划分的磁盘:mkfs.ext3/dev/sda5
第五步:将用户user1和user2的家目录copy到/tmp目录下(使用拷贝的时候注意cp的参数必须有p,因为为目录,所以使用r参数,也就是cp -rp /home/user1 /home/user2 /tmp)
第六步:删除用户在/home目录下的家目录user1和user2
第七步:将新划分的磁盘挂载到/home目录下:mount /dev/sda5 /home
第八步:将user1和user2的家目录从/tmp下拷贝过来:cp-rp /tmp/user* /home
第九步:切换到user1用户查看是否有宿主目录:su - user1
第十步:实现开机自动挂载
具体操作如下:
第一步:创建用户user1和user2
[root@localhosthome]# ls //当前是没有普通用户的
[root@localhosthome]# useradduser1
[root@localhosthome]# useradduser2
第二步:磁盘分区,划分一个20G的磁盘
[root@localhosthome]# fdisk /dev/sda //对/dev/sda磁盘进行分区
The number ofcylinders for this disk is set to 10443.
There is nothingwrong with that, but this is larger than 1024,
and could in certainsetups cause problems with:
1) software thatruns at boot time (e.g., old versions of LILO)
2) booting andpartitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m forhelp): p //p为查看当前分区状态
Disk /dev/sda: 85.8GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2563 20482875 83 Linux
/dev/sda3 2564 2824 2096482+ 82 Linux swap / Solaris
Command (m forhelp): n
Command action
e extended
p primary partition (1-4)
e
Selected partition 4
First cylinder(2825-10443, default 2825):
Using default value2825
Last cylinder or+size or +sizeM or +sizeK (2825-10443, default 10443):
Using default value10443
Command (m forhelp): n
First cylinder(2825-10443, default 2825):
Using default value2825
Last cylinder or+size or +sizeM or +sizeK (2825-10443, default 10443):
Using default value10443
Command (m forhelp): d
Partition number(1-5): 4
Command (m forhelp): p
Disk /dev/sda: 85.8GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2563 20482875 83 Linux
/dev/sda3 2564 2824 2096482+ 82 Linux swap / Solaris
Command (m forhelp): n
First cylinder(2825-10443, default 2825):
Using default value2825
Last cylinder or+size or +sizeM or +sizeK (2825-10443, default 10443): +20G
Command (m forhelp): p
Disk /dev/sda: 85.8GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2563 20482875 83 Linux
/dev/sda3 2564 2824 2096482+ 82 Linux swap / Solaris
/dev/sda4 2825 10443 61199617+ 5 Extended
/dev/sda5 2825 5257 19543041 83 Linux
Command (m forhelp): w //保存并退出
The partition tablehas been altered!
Calling ioctl() tore-read partition table.
WARNING: Re-readingthe partition table failed with error 16: 设备或资源忙.
The kernel stilluses the old table.
The new table willbe used at the next reboot.
Syncing disks.
第三步:更新磁盘分区:partprobe
[root@localhosthome]# partprobe //重新加载所有磁盘的分区
[root@localhosthome]# partprobe /dev/sda //重新加载/dev/sda的分区
第四步:格式化新划分的磁盘:mkfs.ext3/dev/sda5
[root@localhosthome]# mkfs.ext3 /dev/sda5 //格式化/dev/sda5分区为ext3格式
mke2fs 1.39(29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096(log=2)
Fragment size=4096(log=2)
2443200 inodes,4885760 blocks
244288 blocks(5.00%) reserved for the super user
First data block=0
Maximum filesystemblocks=4294967296
150 block groups
32768 blocks pergroup, 32768 fragments per group
16288 inodes pergroup
Superblock backupsstored on blocks:
32768, 98304, 163840, 229376, 294912,819200, 884736, 1605632, 2654208,
4096000
Writing inodetables: done
Creating journal(32768 blocks): done
Writing superblocksand filesystem accounting information: done
This filesystem willbe automatically checked every 39 mounts or
180 days, whichevercomes first. Use tune2fs -c or -i tooverride.
第五步:将用户user1和user2的家目录copy到/tmp目录下
注:使用拷贝的时候注意cp的参数必须有p,权限都必须不能改变;
因为为目录,所以使用r参数,也就是cp -rp /home/user1 /home/user2 /tmp
[root@localhosthome]# cp -rp /home/* /tmp
[root@localhosthome]# ls
user1 user2
第六步:删除用户在/home目录下的家目录user1和user2
[root@localhosthome]# rm -rf *
第七步:将新划分的磁盘挂载到/home目录下:mount /dev/sda5 /home
[root@localhosthome]# mount /dev/sda5 /home/
[root@localhosthome]# df -h
文件系统容量已用 可用 已用% 挂载点
/dev/sda2 19G 2.7G 16G 15% /
/dev/sda1 99M 12M 82M 13% /boot
tmpfs 1005M 0 1005M 0% /dev/shm
/dev/sdb1 19G 173M 18G 1% /data/tools
/dev/sdb2 19G 173M 18G 1% /data/soft
/dev/sdb6 9.2G 150M 8.6G 2% /data/game
/dev/sda5 19G 173M 18G 1% /home
[root@localhosthome]# su - user1
su: warning: cannotchange directory to /home/user1: 没有那个文件或目录
-bash-3.2$ exit
logout
第八步:将user1和user2的家目录从/tmp下拷贝过来:cp-rp /tmp/user* /home
[root@localhosthome]# cp -rf /tmp/user
user1/ user2/
[root@localhosthome]# cp -rf /tmp/user
user1/ user2/
[root@localhosthome]# cp -rp /tmp/user* /home/
第九步:切换到user1用户查看是否有宿主目录:su - user1
[root@localhosthome]# su - user1 //成功迁移
[user1@localhost ~]$
第十步:开机自动挂载请看:
Linux系统管理_磁盘分区和格式化:http://murongqingqqq.blog.51cto.com/2902694/1361918中的第五项:开机自动挂载
第二种方法:
第一步:新建一个磁盘分区,将其挂载到/data目录下
第二步:将/home目录下的所有用户宿主目录移动到/data目录下
第三步:修改/etc/passwd文件,将该文件内普通用户的宿主目录进行修改,即将/home修改为/data