Linux5用户和文件权限管理.docx_第1页
Linux5用户和文件权限管理.docx_第2页
Linux5用户和文件权限管理.docx_第3页
Linux5用户和文件权限管理.docx_第4页
Linux5用户和文件权限管理.docx_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、 用户管理【系统用户文件】#系统用户文件/etc/passwd,主要保存用户及用户设置 # tr -s : tt passwd | grep rootroot x00root /root /bin/bash. .第一列:用户名第二列:用户登录密码,x表示密码被保存在/etc/shadow中第三列:用户ID(即UID);UID从0499都是系统保留给系统用户使用的;因此,自建用户的UID都是从500开始第四列:用户组ID(即GID)第五列:可选字段,一般为空,通常用来保存一些用户全名之类的附加信息第六列:该用户的家目录第七列:保存用户当前使用的Shell种类#用户密码文件/etc/shadow,保存密码、登录相关配置信息(分隔符为:)# cat shadowat:*:16564:0:99999:7:bin:*:15870:daemon:*:15870:ftp:*:15870:root:$1$FOc$mPk/6NkY5FlHDeZ7tq6F/0:15870:. .第一列:用户名第二列:加密后的用户密码,如果以$1$开头,表示采用MD5不可逆加密第三列:最后一次修改密码的距今天的天数(从未修改过的话、则默认从1970年1月1日算起)第四列:两次修改密码的间隔天数,在此期间、用户不能随意修改密码(0则表示可以随时修改)第五列:密码最长保留天数,1表示不能修改密码第六列:密码过期前的警告天数,-1表示没有警告第七列:密码过期后、禁止用户登录的天数,1表示不会被禁用,以自1970年1月1日以来的天数表示第八列:保留字段,一般都为空*注:上述两个文件不要直接使用编辑命令直接修改【用户管理】用户组基本概念及相关配置文件Linux和Windows类似,也可以通过新增用户组实现权限集中管理。但稍有不同,linux在创建每个用户时,会自动创建和新用户同名的一个用户组(称为私有组)、并自动将这个用户加入到这个私有组里,同时,如果不指定新建用户的非私有组,则linux会把它加入到默认的users用户组下面,如下:# lldrwx- 25 xk users 4096 Dec 5 06:21 xk系统用户组文件/etc/group,内容如下# cat /etc/grouproot:x:0:bin:x:1:daemon daemon:x:2:sys:x:3:dba:!:1003:oracleoinstall:!:1004:第一列:用户组名称第二列:组密码,为空表示不需要密码;x表示密码存在/etc/gshadow*在我搭建的虚拟机上没有找到/etc/gshadow这个文件,有待确认第三列:组ID,即GID第四列:该组包含的用户列表,多个用户之间用逗号分隔用户组密码文件/etc/gshadow传统存放编码后的群组密码,但这个文件目前已经不被系统支持,所以如果建立群组密码,还是会被直接放在/etc/group 的密码列。*在我搭建的虚拟机上没有找到/etc/gshadow这个文件,有待确认添加用户组groupadd【命令格式】groupadd option groupname【OPTION】-g, -gid gidForce the new group ID to be the given number. This value must be positive and unique. The default is to use the first free ID after the greatest used one. The range from which the group ID is chosen can be specified in /etc/login.defs.-preferred-gid gidSet the new group ID to the specified value if possible. If that value is already in use the first free ID will be chosen as described above.-o, -non-uniqueAllow duplicate (non-unique) group IDs.-p, -password passwordEncrypted password as returned by crypt(3) for the new account. The default is to disable the account.-r, -systemCreate a system group. A system group is an entry with an GID between SYSTEM_GID_MIN and SYSTEM_GID_MAX as defined in /etc/login.defs, if no GID is specified.-service serviceAdd the group to a special directory. The default is files, but ldap is also valid.-D, -binddn binddnUse the Distinguished Name binddn to bind to the LDAP directory. The user will be prompted for a password for simple authentication.-P, -path pathThe group file is located below the specified directory path. groupadd will use this files, not /etc/group.-help Print a list of valid options with a short description.-usagePrint a short list of valid options.-v, -versionPrint the version number and exit.【涉及的系统文件】/etc/group - group account information删除用户组groupdel【命令格式】groupadd -D binddn -g gid -o -r -P path -p password groupname【OPTION】-service serviceAdd the account to a special directory. The default is files, but ldap is also valid.-D, -binddn binddnUse the Distinguished Name binddn to bind to the LDAP directory. The user will be prompted for a password for simple authentication.-P, -path pathThe passwd and shadow files are located below the specified directory path. chpasswd will use this files, not /etc/passwd and /etc/shadow.-help Print a list of valid options with a short description.-u, -usagePrint a short list of valid options.-v, -versionPrint the version number and exit.【涉及的系统文件】/etc/group - group account information添加用户useradd该命令位于/usr/sbin/下面,有些OS会给useradd加一个软链接命名为adduser,因为老些的操作系统这么命名。【命令格式】useradd option username【OPTION】-c, -comment comment用户的一些描述信息,存放在/etc/passwd的第五列-d, -home homedir指定新用户的家目录,如果不指定,则根据/etc/default/useradd中的HOME变量进行分配,一般和下面的m参数配合使用-m, -create-home为新用户创建家目录,和上面的参数d配合使用.-e, -expire expireWith this option the date when the account will be expired can be changed. expiredate has to be specified as number of days since January 1st, 1970. The date may also be expressed in the format YYYY-MM-DD. If not specified, the default from /etc/default/useradd is used.-f, -inactive inactiveThis option is used to set the number of days of inactivity after a password has expired before the account is locked. A user whose account is locked must contact the system administrator before being able to use the account again. A value of -1 disables thisfeature. If not specified, the default from /etc/default/useradd is used.-G, -groups group,.指定新用户的用户组(supplementary groups),可以属于多个组,每个组名用逗号隔开. 如果不指定,默认根据/etc/default/useradd中的GROUP(值为组ID)变量进行分配-g, -gid gid新用户所属的主用户组(main group)。如果不指定,默认根据/etc/default/useradd中的GROUP(值为组ID)变量进行分配。*这里注意下-g和-G的区别:如果用id命令查看某个用户的话:# id oracleuid=1003(oracle) gid=1004(oinstall) groups=1003(dba),1004(oinstall)-g对应的是里面的gid,-G对应的是里面的groups值-k, -skel skeldir指定从哪个目录向新用户家目录复制模板文件,默认从/etc/default/useradd中读取.-o, -non-uniqueAllow duplicate (non-unique) User IDs.-p, -password passwordEncrypted password as returned by crypt(3) for the new account. The default is to disable the account.-U, -umask umaskThe permission mask is initialized to this value. It is used by useradd for creating new home directories. The default is taken from /etc/default/useradd.-u, -uid uidForce the new userid to be the given number. This value must be positive and unique. The default is to use the first free ID after the greatest used one. The range from which the user ID is chosen can be specified in /etc/login.defs.-preferred-uid uidSet the new userid to the specified value if possible. If that value is already in use the first free ID will be chosen as described above.-r, -systemCreate a system account. A system account is an user with an UID between SYSTEM_UID_MIN and SYSTEM_UID_MAX as defined in /etc/login.defs, if no UID is specified. The GROUPS entry in /etc/default/useradd is ignored, too.-s, -shell shell指定用户登录后使用的默认shell. 默认是从/etc/default/useradd中的SHELL取值, 如果设为/bin/false则表示不允许该用户通过终端登录.-service serviceAdd the account to a special directory. The default is files, but ldap is also valid.-D, -binddn binddnUse the Distinguished Name binddn to bind to the LDAP directory. The user will be prompted for a password for simple authentication.-P, -path path自定义新用户passwd和shadow文件的存放位置,而不把该用户的基本信息存放到/etc/passwd and /etc/shadow.-help Print a list of valid options with a short description.-usagePrint a short list of valid options.-v, -versionPrint the version number and exit.【涉及的系统文件】1. /etc/group - group information2. /etc/default/useradd 为useradd命令提供默认值GROUP=100 注:gid=100,表示可创建普通组HOME=/home 注:把用户的家目录建在/home中;用户家目录的默认创建地INACTIVE=-1 注:是否启用帐号过期停权,-1表示不启用;宽限天数,0及以下数皆为无效数字EXPIRE= 注:帐号终止日期,不设置表示不启用;帐号失效日期(如:20081212)SHELL=/bin/bash 注:所用SHELL的类型;登录后执行的程序SKEL=/etc/skel 注: 用户家目录中的环境文件,默认添加用户的目录默认文件存放位置;也就是说,当我们用adduser添加用户时,用户家目录下的文件,都是从这个目录中复制过去的;CREATE_MAIL_SPOOL=yes注:是否创建用户邮件缓冲,yes表示创建GROUPS:为useradd命令中的-G参数提供默认值,3. /etc/skel 新建用户时,会将该目录下的所有文件复制到新用户的家目录设置用户密码passwd(略)删除用户userdel只有一个参数-r,表示删除用户同时删除该用户家目录下的所有文件。如果不加、则只删除用户而保留家目录。管理用户(组)usermod【命令格式】usermod option username【OPTION】-A, -add-to-group group,.将用户以追加的形式添加到若干个group中(即允许该用户拥有多个gid),注意与-G的区别-c, -comment commentThis option specifies the new users finger information. It is normally modified using the chfn(1) utility.-d, -home homedir为用户设定新的家目录.-m, -move_home更换用户家目录的同时、将原家目录下的文件拷贝过来. 但如果新的家目录已经存在, 命令会报错,并终止操作。-e, -expire expire指定用户过期的日志,格式为YYYY-MM-DD.也可以指定天数(自1970-1-1开始数起)-f, -inactive inactive用户过期后仍可登录的缓冲天数,如果设置为-1表示禁用该功能-G, -groups group,.可以为用户添加多个附加组(指supplementary groups,注意与-A选项的区别),使用逗号隔开.-g, -gid gid改变指定用户所属的用户组(gid). The group name must exist and a group number must refer to an already existing group.-l, -login nameSpecify the new account name for the user. The account name must begin with an alphabetic character and the rest of the string should be from the POSIX portable character class. Nothing else is changed.-o, -non-uniqueAllow duplicate (non-unique) User IDs.-u, -uid uidChange the userid to be the given number. This value must be positive and unique (unless the -o option is used). Any file with the old UID of the user and which is located in the directory tree rooted at the users home directory will be changed to be owned by the new UID automatically.-p, -password passwordEncrypted password as returned by crypt(3) as the new password.-R, -remove-from-group group,.将用户从一个或多个组中移除,组名用逗号分隔。-s, -shell shell修改用户登录后的默认shell-D, -binddn binddnUse the Distinguished Name binddn to bind to the LDAP directory. The user will be prompted for a password for simple authentication.-L, -lock锁定指定的用户、禁止其登录.-U, -unlock解锁用户,但如果该用户密码为空,则解锁失败。-P, -path pathThe passwd and shadow files are located below the specified directory path. usermod will use this files, not /etc/passwd and /etc/shadow.-service serviceModify the account from a special directory. The default is files, but ldap is also valid.-help Print a list of valid options with a short description.-usagePrint a short list of valid options.-v, -versionPrint the version number and exit.【涉及的系统文件】/etc/group - group information/etc/passwd - user account information/etc/shadow - shadow user account information二、权限管理属主和属组当用户创建一个文件时:# touch own# ll own-rw-r-r- 1 root root 0 Dec 5 10:23 own系统会自动为这个文件分配属主(即上面的root用户本身)、属组(即root用户对应的私有组root)及其他组用户对这个文件的权限。上述属组/属主可以通过下面的命令进行更改chown【命令格式】chown option user:group filename最常用的option就是R,如果加上的话,会把目标目录及其下所有文件的属组和属主都改掉,不加的话则只更改指定的文件(夹)。【OPTION】-c, -changeslike verbose but report only when a change is made-dereferenceaffect the referent of each symbolic link (this is the default), rather than the symbolic link itself-h, -no-dereferenceaffect each symbolic link instead of any referenced file (useful only on systems that can change the ownership of a symlink)-from=CURRENT_OWNER:CURRENT_GROUPchange the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute-no-preserve-rootdo not treat / specially (the default)-preserve-rootfail to operate recursively on /-f, -silent, -quietsuppress most error messages-reference=RFILEuse RFILEs owner and group rather than specifying OWNER:GROUP values-R, -recursiveoperate on files and directories recursively-v, -verboseoutput a diagnostic for every file processedThe following options modify how a hierarchy is traversed when the -R option is also specified. If more than one is specified, only thefinal one takes effect.-H if a command line argument is a symbolic link to a directory, traverse it-L traverse every symbolic link to a directory encountered*普通用户是无法将自己文件的属组和属主传递给其他用户的。会提示Operation not permittedchgrp这个命令只能修改文件的属组,而上面的chown命令已经完全包含了这个命令的功能,所以很少使用这个命令。可以通过man chgrp文件权限当用户创建一个文件时:# touch own# ll own-rw-r-r- 1 root root 0 Dec 5 10:23 own系统会自动为这个文件分配属主(即上面的root用户本身)、属组(即root用户对应的私有组root)及其他组用户对这个文件的权限,上面的例子-rw-r-r表示:第一位-:表示这个一个文件,文件夹则用d表示第二位r(4):表示该用户自己对这个文件具有读权限,-表示没有第三位w(2):表示该用户自己对这个文件具有写权限,-表示没有第四位-(1):表示该用户自己对这个文件没有执行权限,x表示有第五位r(4):表示该用户所在组下的其他用户对这个文件具有读权限,-表示没有第六位-(2):表示该用户所在组下的其他用户对这个文件没有写权限,w表示有第七位-(1):表示该用户所在组下的其他用户对这个文件没有执行权限,x表示有第八位r(4):表示其他组(即不包含该用户的组)下的用户对这个文件具有读权限,-表示没有第九位-(2):表示其他组下的用户对这个文件没有写权限,w表示有第十位-(1):表示其他组下的用户对这个文件没有执行权限,x表示有通常也把第2,3,4位权限记为u,第5,6,7位记为g,第8,9,10三位的权限记为o(other),第210位权限记为a(all),如果记不住下面的“绝对权限”表示法,则可以通过这种表示进行权限变更操作。*读、写、执行权限对于文件和文件夹来说,包含的操作有所不同:一旦文件具备了执行权限(即x),该文件就可以通过./或定时任务执行;读和写就不解释了。而文件的执行权限表示是否允许通过cd命令进入该目录;读权限则允许查看目录中的文件列表(比如ls命令);写权限则允许用户在该目录中创建文件。*上文每一位后面的数字,表示这个权限对应的8进制数值,除第一位、后面每三位相加(-为0,不计算在内)会得出得出的三位8进制数,就是这个文件(夹)的8进制权限表示,上例中文件的权限也可以表示为644上述权限都可以通过下面的命令进行更改chmod【命令格式】chmod option file【OPTION】-c, -changeslike verbose but report only when a change is made-no-preserve-rootdo not treat / specially (the default)-preserve-rootfail to operate recursively on /-f, -silent, -quietsuppress most error messages-v, -verboseoutput a diagnostic for every file processed-reference=RFILEuse RFILEs mode instead of MODE values-R, -recursivechange files and directories recursively-help display this help and exit-versionoutput version information and exit【MODE】Each MODE is of the form ugoa*(-+=(rwxXst*|ugo)+.默认权限值umask这个值决定了用户创建文件(夹)时,文件(夹)的默认权限。每个用户可以自定义自己的umask值。*有时用户的umask值会是4位数,千位、也就是第一位表示的是下面说道的suid/sgid/sticky权限。详见下文【计算方法】文件夹-用777,减去umask值(如果不够三位、在前面补零);文件-用666减去umask值;得到的就是新文件(夹)创建后的默认权限。【修改方法】echo umask 033 /.bash_profilesource /.bash_profilesuid、sgid、sticky权限概述【概念解释】SUID属性只能运用在可执行文件上,当用户执行该执行文件时,会临时拥有该执行文件所有者的权限。passwd命令启用了SUID功能,所以一般用户在使用passwd命令修改密码时,会临时拥有了passwd命令所有者root用户的权限,这样一般用户才可以将自己的密码写入/etc/shadow文件。在使用“ls -l”或“ll”命令浏览文件时,如果可执行文件所有者权限的第三位是一个小写的“s”就表明该执行文件拥有SUID属性。rootsrv # ll /usr/bin/passwd-rwsr-xr-x 1 root root 22960 Jul 17 2006 /usr/bin/passwd如果在浏览文件时,发现所有者权限的第三位是一个大写的“S”则表明该文件的SUID属性无效,比如将SUID属性给一个没有执行权限的文件。SGID于SUID不同,SGID属性可以应用在目录或可执行文件上。当SGID属性应用在目录上时,该目录中所有建立的文件或子目录的拥有组都会是该目录的拥有组。比如“/charles”目录的拥有组是charles,当“/charles”目录拥有SGID属性时,任何用户在该目录中建立的文件或子目录的拥有组都会时charles;当SGID属性应用在可执行文件上时,其他用户在使用该执行文件时就会临时拥有该执行文件拥有组的权限。比如/sbin/apachectl文件的拥有组是httpd,当/sbin/apachectl文件有SGID属性时,任何用户在执行该文件时都会临时拥有用户组httpd的权限。在使用“ls -l”或“ll”命令浏览文件或目录时,如果拥有组权限的第三位是一个小写的“s”就表明该执行文件或目录拥有SGID属性。rootsrv tmp# lltotal 4drwxr-sr-x 2 root root 4096 Feb 15 22:25 test4-r-xr-sr-x 1 root root 6144 Feb 15 22:25 test6如果在浏览文件时,发现拥有组权限的第三位是一个大写的“S”则表明该文件的SGID属性无效,比如将SGID属性给一个没有执行权限的文件。Sticky属性只能应用在目录,当目录拥有Sticky属性所有在该目录中的文件或子目录无论是什么权限只有文件或子目录所有者和root用户能删除。比如当用户test8在“/charles”目录中建立一个文件并将该文件权限配置为777,当/charles目录拥有Sticky属性时,只有root和test8用户可以将该文件删除。在使用“ls -l”或“ll”命令浏览目录时,如果其他用户权限的第三位是一个小写的“t”就表明该执行文件或目录拥有Sticky属性。rootsrv tmp# lltotal 4drwxrwxrwt 2 root root 4096 Feb 15 22:34 test4【配置SUID/SGID/Sticky属性】配置普通权限时可以使用字符或数字,SUID、SGID、Sticky也是一样。使用字符时s表示SUID和SGID、t表示Sticky;4表示SUID、2表示SGID、1表示Sticky。在配置这些属性时还是使用chmod命令。举例:#为文件test8增加SUID属性。在使用数字表示时,原来的三位不变,只是增加了一个千位专门用于SUID、SGID、Sticky属性。下面千位的4就是SUID属性,2是SGID属性,1表示sticky属性rootsrv tmp# chmod 4555 test8 #为目录test2增加SGID属性rootsrv tmp# chmod g+s test2#为目录test3增加SGID属性rootsrv tmp# chmod 2755 test3/#为文件test7增加SUID和SGID属性,6=4(SUID)+2(SGID)rootsrv tmp# chmod 6555 test7#为目录test4增加Sticky属性rootsrv tmp# chmod o+t test4/#为目录test5增加Sticky属性rootsrv tmp# chmod 1755 test5/ #为目录test1增加SGID和Sticky属性,3=2(SGID)+1(Sticky)。rootsrv tmp# chmod 3755 test1/#执行上述更改命令后当前目录的情况rootsrv tmp# lldrwxr-sr-x 2 root root 4096 Feb 15 22:47 test2drwxr-sr-x 2 root root 4096 Feb 15 22:47 test3drwxr-xr-t 2 root root 4096 Feb 15 22:46 test4drwxr-xr-t 2 root root 4096 Feb 15 22:46 test5-r-sr-xr-x 1 root root 5120 Feb 15 22:46 test6-r-sr-sr-x 1 root root 3072 Feb 15 22:50 test7drwxr-sr-t 2 root root 4096 Feb 15 22:56 test1-r-sr-xr-x 1 root root 6144 Feb 15 22:48 test8-r-xr-xr-x 1 root root 9216 Feb 15 22:51 test9#取消目录test1的SGID属性。rootsrv tmp# chmod g-s test1/*在使用umask命令显示当前的权限掩码时,千位的“0”就是表示SUID、SGID、Sticky属性。提示:在有些资料上SUID、SGID被翻译为“强制位”,Sticky被翻译为“冒险位”。三、POSIX(Portable Operation System Interface) ACL权限系统首先要查看Linux的内核版本:cat /proc/version或者uname -a因为POSIX在2.6版上才被正式支持其次,POSIX还需要文件系统支持,比如ext2,ext3,JFS等,如果因为文件系统不支持ACL,可以尝试重新挂载的方式:mount -o remount,acl /dev/sr1其中,/dev/sr1表示磁盘名由于Linux原有的权限管理系统不够灵活(比如Windows,可以就某个具体的文件设置对某(些)用户(组)的权限,但从Unix继承来的权限系统很难做到这一点),因此推出了POSIX ACL。【查看ACL权限命令-getfacl】【命令格式】getfacl option filename对某个文件执行该命令后,会显示如下内容:# file: ifconfig.sh# owner: root# group: root# flags: ss-user:rwxgroup:r-xother:-user:两个:中间为空,表示这个文件的属主对该文件具备的权限group:两个:中间为空,表示这个文件的属组对该文件具备的权限other:两个:中间为空,表示这个所有其他用户(组)对该文件具备的权限前四行(#所在行)则说明了这个文件的属组、属主、文件名以及suid/sgid/sticky权限。【OPTION】-a , -access:显示文件或目录的访问控制列表。-d , -default:显示文件或目录的默认(缺省)的访问控制列表。-c , -omit-header:不显示默认的访问控制列表。-R , -recursive:操作递归到子目录。【ACL权限管理命令】【命令格式】setfacl option filename【!ACL权限上线值mask!】系统会为ACL设定一个默认的权限上限,当你要设置的ACL权限超过这个上限时,超出的权限会自动被系统屏蔽。【OPTION】-m为某文件(夹)修改、新增ACL权限举例:#将上述root用户的ifconfig.sh文件的读、执行权只赋给cusersetfacl -m u:cuser:rx ifconfig.sh-b, -remove-all删除所有扩展ACL权限举例:setfacl b filename-d, -default为文件设置默认的ACL权限,可以对某个目录设置一个默认的ACL权限,以后在这个目录下创建的文件(夹)就都默认有了这个ACL权限。需要配合m参数使用*这里要注意,为文件(夹)赋ACL权限时,不能超过上面说的mask值,否则需要先更改该文件的默认mask值,如下:setfacl -m mask:rwx file_name举例:#指定用户xk和oinstall组下的用户,对root文件夹下的所有新建文件(夹)拥有rwx权限setfacl -dm u:xk:rwx,g:oinstall:rwx root/getfacl -omit-header root/user:rwxuser:xingkai:rwxgroup:r-xgroup:oinstall:rwxmask:rwxother:r

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论