对于长期使用ubuntu的用户来说,磁盘挂载已是日常最频繁的操作之一了,我们要想访问磁盘上的某一个分区上的文件,首先必须挂载该分区。当然老是在用的时候再进行挂载显得十分的麻烦。于是linux系统提供了一个很人性化的机制:那就是我们可以通过一些设置使得在开机是自动挂载我们平时频繁使用的分区。
本文就Ubuntu系统开机自动挂载指定磁盘分区进行简单的讨论。
我们知道ubuntu有关开机自动挂载磁盘的设置文件为/etc/fstab。该文件的所有者为root,默认权限为644,所以对于我们普通用户来说是不可以修改该文件的,下面的所有操作都是以root身份进行的。
1. 首先我们打开/etc/fstab
root@hp:~# vim /etc/fstab
内容如下
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda10 during installation
UUID=8f9fe40b-3909-482c-9b30-d315e1249af2 / ext4 errors=remount-ro0 1
因为(某些原因)我在安装系统的时候只挂载了根目录,没有单独挂载/boot, /home等,并且没有设置交换分区,所以这里只有 / 这个分区的挂载信息。
关于挂载信息的解释(每行只能包含一个磁盘的挂载信息,每行包含6个域,用空格隔开)
FIRST_FIELD(fs_spec)
设备名,或者是UUID,LABEL(推荐使用后两者,正如MAN手册里所说,当系统增添或者移除一个SCSI磁盘的时候,只改变设备名,而不改变卷标)
SECOND_FIELD(fs_file)
挂载点,对于SWAP分区,必须指定为none。如果挂载点包含空格,用'\040'代替
THIRD_FIELD(fs_vfstype)
文件系统类型,如ext2, hpfs, ntfs, vfat,iso9660。swap表示为交换分区,ignore表示该行被忽略,还可以为none
FOURTH_FIELD(fs_mntops)
mount选项,可以为mount基本选项和用于挂载指定文件系统类型的选项,每个选项之间用逗号隔开,有关详细选项可以查看man mount。
FIFTH_FIELD(fs_freq)
文件系统备份操作,如果没有该选项或者为0时,表示不需要进行该操作
SIXTH_FIELD(fs_passno)
重启时fsck程序检测磁盘的序列号,根目录为1,其他文件系统为2,没有或者为0时表示不需要被检测
2. 查看需要自动挂载的分区的信息
(一)如果使用UUID挂载,可以使用以下命令查看
root@hp:~# blkid
结果如下:
/dev/sda1:LABEL="WIN7(1)" UUID="0EB0FB28B0FB1545" TYPE="ntfs"
/dev/sda5: LABEL="WIN7(2)" UUID="BE601AFC601ABADB" TYPE="ntfs"
/dev/sda6: LABEL="SOFTWARE" UUID="4ECCEFBCCCEF9D09" TYPE="ntfs"
/dev/sda7: LABEL="MEDIA" UUID="384C301C4C2FD402" TYPE="ntfs"
/dev/sda8: LABEL="STUDY" UUID="1230CCB430CCA05A" TYPE="ntfs"
/dev/sda9: LABEL="PACKAGE" UUID="305878F85878BE63" TYPE="ntfs"
/dev/sda10: UUID="8f9fe40b-3909-482c-9b30-d315e1249af2" TYPE="ext4"
或者
root@hp:~# ls -all /dev/disk/by-uuid
结果如下
total 0
drwxr-xr-x 2 root root 180 6月 14 2013 .
drwxr-xr-x 6 root root 120 6月 14 2013 ..
lrwxrwxrwx 1 root root 10 6月 13 20:41 0EB0FB28B0FB1545 -> ../../sda1
lrwxrwxrwx 1 root root 10 6月 13 20:41 1230CCB430CCA05A -> ../../sda8
lrwxrwxrwx 1 root root 10 6月 13 20:41 305878F85878BE63 -> ../../sda9
lrwxrwxrwx 1 root root 10 6月 13 20:41 384C301C4C2FD402 -> ../../sda7
lrwxrwxrwx 1 root root 10 6月 13 20:41 4ECCEFBCCCEF9D09 -> ../../sda6
lrwxrwxrwx 1 root root 11 6月 13 20:41 8f9fe40b-3909-482c-9b30-d315e1249af2 -> ../../sda10
lrwxrwxrwx 1 root root 10 6月 13 20:41 BE601AFC601ABADB -> ../../sda5
当然第一个命令的结果可能更明了一点。
这里我们想要自动挂载STUDY分区到/media/study/,其UUID=1230CCB430CCA05A,可以在/etc/fstab中添加如下一行即可
UUID=1230CCB430CCA05A /media/study/ ntfs defaults 0 0
其中defaults包含一组默认参数,具体如下
rw 以可读写模式挂载
suid 开启用户ID和群组ID设置位
dev 可解读文件系统上的字符或区块设备
exec 可执行二进制文件
auto 自动挂载
nouser 使一般用户无法挂载
async 以非同步方式执行文件系统的输入输出操作
(二)使用设备文件名挂载分区,也就是将上面的UUID替换为设备文件名,这里STUDY分区文件为/dev/sda8
3.关于如上挂载产生的权限问题
细心的朋友们可能发现,通过上面的方法进行挂载,不管是文件还是目录,它的权限都为777,而且他们的owner和group都为root, 这意味着所有人都具有对文件读写和执行的权限,并且对于普通文件来说,本不该出现的X权限在这里也出现了,如下(当然这里的权限也可能不为777,因为默认为当前系统进程的UMASK)
root@hp:~# ls /media/study/ -l
total 8978717
-rwxrwxrwx 1 root root 0 6月 9 17:40 a.out
drwxrwxrwx 1 root root 4096 1月 31 19:28 Books
drwxrwxrwx 1 root root 4096 4月 8 21:20 C_Language
drwxrwxrwx 1 root root 0 6月 13 19:28 Others
drwxrwxrwx 1 root root 4096 6月 13 20:22 Programs
-rwxrwxrwx 1 root root 443 6月 2 18:36 src.c
...
于是我们需要进一步设置,我们查看man mount,在FAT文件系统类型那块(下面附有该节内容)有一UMASK选项,表示挂载后文件或目录不出现(not present)的权限,所以对于文件的权限问题,我们可以如下设置,去掉文件的可执行权限
UUID=1230CCB430CCA05A /media/study/ ntfs defaults,umask=111 0 0
这样一来,却又导致了普通用户不可进入目录的问题
zyj@hp:~$ cd /media/study/
bash: cd: /media/study/: Permission denied
还好mount选项里几个选项可以解决这个问题:dmask,fmask,uid,gid。我们可以通过设置这几个选项,使得文件和目录为我们所有,并且实现对其权限的控制
UID 和GID可以通过以下命令查看
zyj@hp:~$ id zyj
当然这里的zyj需要改为自己的用户名,结果如下
uid=1000(zyj) gid=1000(zyj) groups=1000(zyj) ....
修改/etc/fatab文件
UUID=1230CCB430CCA05A /media/study/ ntfs defaults,uid=100,gid=100,dmask=022,fmask=133 0 0
结果如下
zyj@hp:~$ ls /media/study/ -l
total 8978717
-rw-r--r-- 1 zyj zyj 0 6月 9 17:40 a.out
drwxr-xr-x 1 zyj zyj 4096 1月 31 19:28 Books
drwxr-xr-x 1 zyj zyj 4096 4月 8 21:20 C_Language
drwxr-xr-x 1 zyj zyj 0 6月 13 19:28 Others
drwxr-xr-x 1 zyj zyj 4096 6月 13 20:22 Programs
-rw-r--r-- 1 zyj zyj 443 6月 2 18:36 src.c
...
到这里基本上差不多了,如果出现中文无法正常显示的情况,在选项里添加utf8或者codepage=936,iocharset=cp936(或gb2312)即可。
附:
Mount options for fat
(Note: fat is not a separate filesystem, but a common part of the msdos, umsdos and vfat filesystems.)blocksize={512|1024|2048}
Set blocksize (default 512). This option is obsolete.
uid=value and gid=value
Set the owner and group of all files. (Default: the uid and gid of the current process.)
umask=value
Set the umask (the bitmask of the permissions that are not present). The default is the umask of the current process.
The value is given in octal.
dmask=value
Set the umask applied to directories only. The default is the umask of the current process. The value is given in
octal.
fmask=value
Set the umask applied to regular files only. The default is the umask of the current process. The value is given in
octal.
allow_utime=value
This option controls the permission check of mtime/atime.
20 If current process is in group of file's group ID, you can change timestamp.
2 Other users can change timestamp.
The default is set from `dmask' option. (If the directory is writable, utime(2) is also allowed. I.e. ~dmask & 022)
Normally utime(2) checks current process is owner of the file, or it has CAP_FOWNER capability. But FAT filesystem
doesn't have uid/gid on disk, so normal check is too unflexible. With this option you can relax it.
check=value
Three different levels of pickyness can be chosen:
r[elaxed]
Upper and lower case are accepted and equivalent, long name parts are truncated (e.g. verylongname.foobar
becomes verylong.foo), leading and embedded spaces are accepted in each name part (name and extension).
n[ormal]
Like "relaxed", but many special characters (*, ?, <, spaces, etc.) are rejected. This is the default.
s[trict]
Like "normal", but names may not contain long parts and special characters that are sometimes used on Linux, but are not accepted by MS-DOS are rejected. (+, =, spaces, etc.)
codepage=value
Sets the codepage for converting to shortname characters on FAT and VFAT filesystems. By default, codepage 437 is used.
conv={b[inary]|t[ext]|a[uto]}
The fat filesystem can perform CRLF<-->NL (MS-DOS text format to UNIX text format) conversion in the kernel. The following conversion modes are available:
binary no translation is performed. This is the default.
text CRLF<-->NL translation is performed on all files.
auto CRLF<-->NL translation is performed on all files that don't have a "well-known binary" extension. The list of known extensions can be found at the beginning of fs/fat/misc.c (as of 2.0, the list is: exe, com, bin, app, sys, drv, ovl, ovr, obj, lib, dll, pif, arc, zip, lha, lzh, zoo, tar, z, arj, tz, taz, tzp, tpz, gz, tgz, deb, gif, bmp, tif, gl, jpg, pcx, tfm, vf, gf, pk, pxl, dvi).
Programs that do computed lseeks won't like in-kernel text conversion. Several people have had their data ruined by this translation. Beware!
For filesystems mounted in binary mode, a conversion tool (fromdos/todos) is available. This option is obsolete.
cvf_format=module
Forces the driver to use the CVF (Compressed Volume File) module cvf_module instead of auto-detection. If the kernel
supports kmod, the cvf_format=xxx option also controls on-demand CVF module loading. This option is obsolete.
cvf_option=option
Option passed to the CVF module. This option is obsolete.
debug Turn on the debug flag. A version string and a list of filesystem parameters will be printed (these data are also
printed if the parameters appear to be inconsistent).
fat={12|16|32}
Specify a 12, 16 or 32 bit fat. This overrides the automatic FAT type detection routine. Use with caution!
iocharset=value
Character set to use for converting between 8 bit characters and 16 bit Unicode characters. The default is iso8859-1.
Long filenames are stored on disk in Unicode format.
tz=UTC This option disables the conversion of timestamps between local time (as used by Windows on FAT) and UTC (which Linux uses internally). This is particularly useful when mounting devices (like digital cameras) that are set to UTC in order to avoid the pitfalls of local time.
quiet Turn on the quiet flag. Attempts to chown or chmod files do not return errors, although they fail. Use with caution!
showexec
If set, the execute permission bits of the file will be allowed only if the extension part of the name is .EXE, .COM,
or .BAT. Not set by default.
sys_immutable
If set, ATTR_SYS attribute on FAT is handled as IMMUTABLE flag on Linux. Not set by default.
flush If set, the filesystem will try to flush to disk more early than normal. Not set by default.
usefree
Use the "free clusters" value stored on FSINFO. It'll be used to determine number of free clusters without scanning
disk. But it's not used by default, because recent Windows don't update it correctly in some case. If you are sure the
"free clusters" on FSINFO is correct, by this option you can avoid scanning disk.
dots, nodots, dotsOK=[yes|no]
Various misguided attempts to force Unix or DOS conventions onto a FAT filesystem.
Mount options forhpfs
uid=value and gid=value
Set the owner and group of all files. (Default: the uid and gid of the current process.)
umask=value
Set the umask (the bitmask of the permissions that are not present). The default is the umask of the current process.
The value is given in octal.
case={lower|asis}
Convert all files names to lower case, or leave them. (Default: case=lower.)
conv={binary|text|auto}
For conv=text, delete some random CRs (in particular, all followed by NL) when reading a file. For conv=auto, choose
more or less at random between conv=binary and conv=text. For conv=binary, just read what is in the file. This is the
default.
nocheck
Do not abort mounting when certain consistency checks fail.
Mount options forntfs
iocharset=name
Character set to use when returning file names. Unlike VFAT, NTFS suppresses names that contain nonconvertible char‐
acters. Deprecated.
nls=name
New name for the option earlier called iocharset.
utf8 Use UTF-8 for converting file names.
uni_xlate={0|1|2}
For 0 (or `no' or `false'), do not use escape sequences for unknown Unicode characters. For 1 (or `yes' or `true') or
2, use vfat-style 4-byte escape sequences starting with ":". Here 2 give a little-endian encoding and 1 a byteswapped
bigendian encoding.
posix=[0|1]
If enabled (posix=1), the filesystem distinguishes between upper and lower case. The 8.3 alias names are presented as
hard links instead of being suppressed. This option is obsolete.
uid=value, gid=value and umask=value
Set the file permission on the filesystem. The umask value is given in octal. By default, the files are owned by
root and not readable by somebody else.