Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15406
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-26 16:04
文章分类

全部博文(6)

文章存档

2013年(6)

我的朋友

分类: 系统运维

2013-10-08 11:07:08

用户账户和组的概念?

用户账户:

超级账户:(root)对本机有至高无上的权限

程序账户: 维持系统中的某个程序正常运行

普通账户:一般在自己的宿主目录有绝对权限

组账户:

基本组(私有组)

附加组(公共组)

 

UIDGID

Linux 登录主机是我们输入的账户,但系统里不会认识账户名的,

而是通过,/etc/passwd 文件 UID和GID识别的。

 

UID :每个账户对应一个UID号,UID在本机的操作系统里是唯一的。

GID :每个组账户会对应一个GID号,GID在本机的操作里是唯一的。

 

UID的限制

0

uid0时,代表这个账户是管理员,当你想把某个账户设置成管理员时,值直接修改uid 就可以了。也就是说,系统中管理员账户不一定是root

1~499

系统默认保留账户,和其他账户没什么不一样的,只是一些服务默认账户

500~65535

手动创建的一般账户


实例说明

[root@maxiaole ~]# usermod -u 0 test

usermod: uid 0 is not unique             #报错uid 0不是唯一的

[root@maxiaole ~]# usermod -u 501 root  #rootuid 改成501

[root@maxiaole ~]# usermod -u 0 test     #修改test uid 0

[root@maxiaole ~]# cat /etc/passwd |grep test

test:x:0:500::/home/test:/bin/bash

[root@maxiaole ~]# su test  #切换账户为test

[test@maxiaole root]# w

 19:16:11 up 6 min,  2 users,  load average: 0.14, 0.26, 0.12

USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT

root     tty1     -                19:11    4:24   0.06s  0.20s login -- root

root     pts/0    192.168.1.6      19:12    0.00s  0.16s  0.08s –bash

注释:这里登陆账户信息还是root,因为是系统默认的超级账户是root的原因

[test@maxiaole /]# chmod 755 hehe

[test@maxiaole /]# chmod 644 /hehe/test

[test@maxiaole /]# cd /hehe

[test@maxiaole hehe]# ll

total 4

-rw-r--r-- 1 test root 0 Jul 20 22:23 test

[test@maxiaole hehe]# su root  #将账户切换为root

[root@maxiaole hehe]$ rm test  #很重要的信息,root的标示符变成了$普通账户

rm: remove write-protected regular empty file `test'? y

rm: cannot remove `test': Permission denied  #这是在以root账户删除文件的提示权限不够

[root@maxiaole hehe]$ su test  #切换test账户

Password:

[test@maxiaole hehe]# rm test

[test@maxiaole hehe]# ll

total 0               #删除成功


Passwd文件实例图

[root@maxiaole home]# less /etc/passwd

root:x:0:0:root:/root:/bin/bash

省略部分……..

xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin

abel:x:500:500::/home/abel:/bin/bash


/etc/passwd配置文件信息,(共七个字段,字段之间冒号隔开)

第一字段(abel):账户名称

第二字段(x):加密后账户密码

第三字段(500):账户的UID

第四字段(500):组账户GID

第五字段(普通账户一般为空)账户的全名

第六字段:(/home/abel)账户的宿主目录

第七字段:(/bin/bash)账户的shell


Shadow密码文件

由于系统每个程序都需要,取得UID和GID来判断权限问题,所以/etc.passwd

文件的权被设置成-rw-r--r—644 ,这样一来所有人都可以看见用户密码了,即

使密码是加密的,也会有被人破解的可能,就是因为这个原因密码信息被移到

了/etc/shadow文件中,并对这个文件的权限,及密码,增加了很多限制。

增加的限制

 

[root@maxiaole hehe]# ll /etc/shadow

-r-------- 1 root root 979 Jul 25 23:11 /etc/shadow

注释:这个文件对root,也只有只读的权限

[root@maxiaole hehe]# vi /etc/shadow

test:$1$UZCqBbbY$dxJ2rOsuehRE2d9vmYrVj.:15546:0:99999:3:::

E45: 'readonly' option is set (add ! to override)

将账户过期提醒日期改写成三天,提示不能添加

 

文件内容部分剪贴

[root@maxiaole ~]# cat /etc/shadow

root:$1$mwhh8iuE$SRy5YXfyQDMAoTIS1/BYm0:15539:0:99999:7:::

bin:*:15539:0:99999:7:::

abel:$1$ZQTVecEq$OGJPD/ohPKHczANBcFHb0.:15540:0:99999:7:::


/etc/shadow密码信息:(有9个字段、含义如下)

和/etc/passwd 文件差不多,每行对应一个账户;以“:”隔开分为九个字段;

简单概括九个字段的含义(了解即可)

1:帐号名称
2:加密的密码信息
3:上次修改密码的时间从1970年7月1日算起
4:密码的最短有效天数,默认值为0
5:密码的最长有效天数,默认值为99999
6:提前多少天警告用户口令将过期,默认值为7
7:在密码过期之后多少天禁用此用户
8:帐号失效时间,默认值为空
9:保留字段(未使用) 


账户管理命令

useradd 添加账户命令

语法:useradd  [选项]  [参数]  账户

选项:(*为常用选项)

*-u  创建账户时定义的uid

*-g  定义账户的giu (该组账户必须存在)

*-d  指定账户家目录的路径

*-s  指定账户登陆的shell   (/sbin/nologin为禁止账户登录)

*-M 创建账户但不创建账户宿主目录

-G  定义账户附加组 (除属组之外的组)

     -e  指定账户失效时间

实例操作

1创建账户abel

[root@maxiaole ~]# useradd abel

useradd: user abel exists

[root@maxiaole ~]# cat /etc/passwd

省略部分……..

abel:x:500:500::/home/abel:/bin/bash

注释:useradd如不添加任何参数,默认的会uidgid会从500开始以此类推,账户

的宿主目录创建/home以账户名生成宿主目录。目前linux使用的默认shell

类型为bash

 

2创建账户test,设置账户uid 800,不创建账户宿主目录,并且禁止账户登录。

[root@maxiaole ~]# useradd -u 800 -s /sbin/nologin -M test

[root@maxiaole ~]# cat /etc/passwd

省略部分…….

abel:x:500:500::/home/abel:/bin/bash

test:x:800:800::/home/test:/sbin/nologin

[root@maxiaole ~]# ll /home/

total 8

drwx------ 3 abel abel 4096 Jul 20 02:15 abel

注释:查看passwd文件时我们会看见test的账户信息里账户的宿主目录会有/home/test

      这样的信息,但我们查看home目录里查看时,test是不存在宿主目录的。这

      说明了-M是生效了,passwd文件里标记的宿主目录,只是该文件定义的格式。

 

passwd 账户密码修改命令

语法:passwd [选项] 账户名

选项:

     -d 清空账户密码

     -l 锁定账户

     -s 查看状态(是否锁定)

     -u 解除锁定

 

实例

设置账户abel的密码为:123456

[root@maxiaole ~]# passwd abel

Changing password for user abel.

New UNIX password:#输入的密码

BAD PASSWORD: it is too simplistic/systematic

Retype new UNIX password: #再一次输入密码

passwd: all authentication tokens updated successfully. #提示修改成功

 

清除账户abel的密码

[root@maxiaole ~]# passwd -d abel

Removing password for user abel.  #清除账户密码

passwd: Success #成功

 

 

usermod修改账户属性(该命令生产环境中并不常用)

语法:usermod  [选项]  [参数]  账户

选项:

       u 指定uid
        -d
指定宿主目录
        -e
指定失效时间
        -g
指定基本组名
        -G
指定附加组名
        -M
不创建主目录
        -s
指定使用shell 
        -l
更改用户帐号的登录名称
        -L
锁定
        -U
解锁    

 

userdel 账户删除命令

语法:userdel  [选项]  账户

选项:

     -r 删除账户的同时删除账户的宿主目录

[root@maxiaole ~]# userdel -r abel

[root@maxiaole ~]# cat /etc/passwd |grep abel

 

组账户管理命令

组账户管理文件

/etc/group

 [root@maxiaole ~]# cat /etc/group

root:x:0:root

省略部分…….

test:x:800:

第一列:主账户名

第二列:加密后的密码

第三列:组账户GID

第四列:组账户的名称(手动创建的组账户第四列默认是没有的)

组账户管理账户

 

/etc/gshdow

[root@maxiaole ~]# cat /etc/gshadow

root:::root

test:!::

 

groupadd 组账户添加命令

语法:groupadd  [选项]  组账户明

选项:

     -g

实例

[root@maxiaole ~]# groupadd -g 900  xiaoma

[root@maxiaole ~]# cat /etc/group |grep xiaoma

xiaoma:x:900:

 

gpasswd 设置组密码
    -a
向组内添加用户  gpasswd -a user group
    -d
从组内删除用户
    -M
同时添加多个用户,以逗号分隔

groupdel 删除组

 

用户和组账户查询命令
id 查询用户身份
groups
查询用户所属的组
finger
查询用户帐号的详细信息
users
w who(信息较为详细) 查询已登录到主机的用户信息

 

权限概括

 

文件拥有者

Linux是多用户,多任务的系统,常常在同一时间有很多人在同时主机工作,

考虑到个人隐私,和公共工作环境,文件拥有者就显得特别重要。(很多服

务也对拥有者的权限有一定得要求)

 

工作组

举个简单的例子;账户 ABC 属于一个工作组test。账户a1a2a3属于

工作组test1。账户A 创建了一个目录xiaoma ,属主是账户A ,属组test组。

并且对目录的属组设置了写入权限,那么该组的其他成员BC对该目录

同样拥有写入权限。当账户a1需要xiaoma有写入权限的时候,可以把账户

a1加入到test组这样也也就有了写入权限。

 

其他人权限

账户 ABC 属于一个工作组test。账户A 创建了一个目录xiaoma ,属主是

账户A ,属组test组并且对目录的属组test设置了绝对权限。但是希望改组以外

的账户能看到,不能修改。这是需要设置的权限就是其他人权限

 

linux对普通文件的权限说明

[r] 可读;对文件有阅读的权限

[w]可写;对文件有修改内容的权限(删除文件自身受父目录权限的影响)

[x] 可执行;对文件表示具有执行文件的权限

 

Linux对目录的权限说明

[r] 可读;表示具有浏览目录的权限

[w]可写;表示具有修改目录内文件的权限

[x] 可执行;表示具有进入目录的执行权限

 

文件和目录的权限对比

[r] 可读

对文件具有查看文件内容的权限

对目录具有浏览目录的权限

[w] 可写

对文件有修改删除文件内容的权限(但父目录权限影响)

对目录有修改目录内文件的权限

[r] 可执行

对文件有执行文件的权限

对目录有进入目录的权限

 

对于删除的提示

对目录而言,删除目录与上层目录权限有关,与要删除的目录本身属性无关

对文件而言,写目录是修改目录,和删除目录不同,因此写目录是和文件本身属性有关

 

例题

1root创建/hehe/test文件,默认权限是644。测试普通账户abel 内否浏览、修改,该文件?

[root@maxiaole hehe]# su abel

[abel@maxiaole hehe]$ ll

total 8

-rw-r--r-- 1 root root 4 Jul 20 02:16 test

[abel@maxiaole hehe]$ cat test

123

[abel@maxiaole hehe]$ echo "abc" > /hehe/test

bash: /hehe/test: Permission denied                            权限不够

解答:账户abel对该文件有只读权限,可以浏览该test文件里的内容,但不能修改或删除内容。

 

2)设置赋予test文件646权限(让其他人有写入权限)测试,能否修改test文件内容,删除文

件本身

[root@maxiaole hehe]# chmod 646 test

[root@maxiaole hehe]# su abel

[abel@maxiaole hehe]$ echo "a b c" >/hehe/test

[abel@maxiaole hehe]$ cat test            可以修改文件内容

a b c

[abel@maxiaole hehe]$ rm test  

rm: cannot remove `test': Permission denied       删除文件本身时,权限不不够

drw-r--rwx  2 root root  4096 Jul 20 02:14 hehe         赋予了hehe目录其他人执行权限

[abel@maxiaole /]$ rm /hehe/test

rm: remove write-protected regular empty file `/hehe/test'? y

[abel@maxiaole hehe]$ ll

total 0          删除完成

解答:对于文件来说写入权限可以修改删除文件内容。对于删除自身,和该文件自身的权限没有

      关系,受控于父目录的执行权限控制。

 

(3)root创建目录/hehe/xiaom并赋予777的绝对权限,账户abel能否删除xiaoma

[root@maxiaole /]# mkdir /hehe/xiaoma

[root@maxiaole /]# chmod 755 hehe

[root@maxiaole /]# cd hehe/

[root@maxiaole hehe]# ll

total 12

-rw-r--r-- 1 root root    0 Jul 20 22:23 test

drwxr-xr-x 2 root root 4096 Jul 20 22:28 xiaoma

[root@maxiaole hehe]# su abel

[abel@maxiaole hehe]$ ll

total 12

-rw-r--r-- 1 root root    0 Jul 20 22:23 test

drwxr-xr-x 2 root root 4096 Jul 20 22:28 xiaoma

[abel@maxiaole hehe]$ exit

[root@maxiaole hehe]# chmod  777 xiaoma

[root@maxiaole hehe]# ll

total 12

-rw-r--r-- 1 root root    0 Jul 20 22:23 test

drwxrwxrwx 2 root root 4096 Jul 20 22:28 xiaoma

[root@maxiaole hehe]# su abel

[abel@maxiaole hehe]$ rm -rf xiaoma

rm: cannot remove directory `xiaoma': Permission denied

[abel@maxiaole hehe]$ exit

exit

[root@maxiaole hehe]# chmod 757 /hehe

[root@maxiaole hehe]# ll /hehe

total 12

-rw-r--r-- 1 root root    0 Jul 20 22:23 test

drwxrwxrwx 2 root root 4096 Jul 20 22:28 xiaoma

[root@maxiaole hehe]# su abel

[abel@maxiaole hehe]$ rm -rf xiaoma

[abel@maxiaole hehe]$ ll

total

-rw-r--r-- 1 root root 0 Jul 20 22:23 test

解答:对于删除目录本身来说,与自身权限无关,和上层目录权限有关。

 

权限管理命令

chmod 修改文件权限命令

语法: chmod  [选项]  [参数]  文件或目录

选项:

     -R对目录以递归的设置目录

 

数字方式修改权限

rw- r-- r-- 1 root root  3619 Jul 19 00:50 install.log.syslog

文件属性共9个权限位,每9位为一组,共3

r(可读)=4

w(可写)=2

x(执行)=1

同一组(owner/group/others) 三个属性(rwx)累加

owner = rw = 4+2=6

group = r = 4

others = r = 4

 

操作

设置abel目录权限是rw

root@maxiaole /]# ll

total 154

drwxr-xr-x  2 root root  4096 Jul 19 23:30 abel

[root@maxiaole /]# chmod 600 abel

drw-------  2 root root  4096 Jul 19 23:30 abel

 

递归的设置abel目录和其子目录子文件权限为755

[root@maxiaole abel]# mkdir xiaoma

[root@maxiaole abel]# touch xiaole

[root@maxiaole abel]# ll

total 12

-rw-r--r-- 1 root root    0 Jul 25 20:55 xiaole

drwxr-xr-x 2 root root 4096 Jul 25 20:55 xiaoma

操作

[root@maxiaole abel]# chmod -R  755 /abel

[root@maxiaole abel]# ll /

total 154

drwxr-xr-x  3 root root  4096 Jul 25 20:55 abel

[root@maxiaole abel]# ll /abel/

total 12

-rwxr-xr-x 1 root root    0 Jul 25 20:55 xiaole

drwxr-xr-x 2 root root 4096 Jul 25 20:55 xiaoma

 

 

字符类型改变权限

 

Chmod

 u

 g

 o

 a

    

      +  (加法)

-(减法)

      =(绝对值)

r

w

x

 

 

文件或目录

 

字符类型概括

u=属主

g=属组

o=其他人

a=all (所有)

操作

oldboy/文件修改成555权限

[root@xiaole /]# chmod  u=rx,g=rx,o=rx oldboy/

[root@xiaole /]# ls -dl /oldboy/

dr-xr-xr-x 3 root root 4096 Nov 20 08:53 /oldboy/

注释:u=rx,g=rx,o=rx 每组属性之间“,” 逗号开;等号两边没有空格

 

a+w 为目录三个属主,属组,其他人权限

[root@xiaole /]# ls -ld oldboy/

drwxrwxrwx 3 root root 4096 Nov 20 08:53 oldboy/

 

chown 修改文件属主属组命令

操作

 

修改文件属主

文件oldboy目录属主修改成xiaole2

[root@xiaole /]# chown xiaole2 oldboy/

[root@xiaole /]# ls -dl oldboy/

drwxrwxrwx 3 xiaole2 root 4096 Nov 20 08:53 oldboy/

 

-R递归修改oldboy 目录文件属主和属组

[root@xiaole /]# chown -R xiaole2.xiaole2 oldboy/

[root@xiaole /]# ls -dl oldboy/

drwxrwxrwx 3 xiaole2 xiaole2 4096 Nov 20 08:53 oldboy/

注释:修改属主和属组“:”冒号隔开

[root@xiaole /]# ls -l oldboy/

total 40

drwxrwxrwx 2 xiaole2 xiaole2 4096 Nov 20 08:53 hehe

-rwxrwxrwx 1 xiaole2 xiaole2   24 Nov 20 08:08 oldboy

-rwxrwxrwx 1 xiaole2 xiaole2   24 Nov 20 07:54 xiaole

-rwxrwxrwx 1 xiaole2 xiaole2   16 Nov 20 05:04 xiaoma

-rwxrwxrwx 1 xiaole2 xiaole2  135 Nov 20 07:28 xiaoma.2011-11-20.tar.gz

 

特殊的权限

SUID对属主加SET位权限 只能针对文件 
SGID
对属组加SET位权限 文件和目录       2

SUIDSGID属于一种特殊的权限,举个例子当有一个执行程序文件,这个文件的属主

root,加上了SUID权限,就可以暂时取得root权限,来执行该程序文件。SGID亦是

如此。SUID是在属主为加权限,SGID是在属组为加权限。

设置方法为:

chmod  u+s    filename  (suid)

chmod  g+s    filename  (sgid)

chmod  4777   (suid)针对属主

粘滞位

对于带粘滞位文件或目录来讲,只有root或者文件所有者才可以对文件有进行修改删除操作

其他账户即使是对该文件或目录有绝对权限也是无法对其进行修改和删除操作的。

设置方法:

          chmod  1777 namefile

          chmod  +t   namefile

实例说明

[root@maxiaole /]# chmod -R +t abel                增加粘滞位

[root@maxiaole /]# ll

total 154

drwxrwxrwt  3 root root  4096 Jul 25 20:55 abel

[root@maxiaole /]# su test                切换账户test

[test@maxiaole /]$ cd /abel/

[test@maxiaole abel]$ ll

total 12

-rwxrwxrwt 1 root root    0 Jul 25 20:55 xiaole

drwxrwxrwt 2 root root 4096 Jul 25 20:55 xiaoma

[test@maxiaole abel]$ rm -f xiaole

rm: cannot remove `xiaole': Operation not permitte #提示权限不够

注意下面的步骤

[root@maxiaole abel]# chmod 777 xiaole 去掉了xiaole文件的粘滞位

[root@maxiaole abel]# ll

total 12

-rwxrwxrwx 1 root root    0 Jul 25 20:55 xiaole

drwxrwxrwt 2 root root 4096 Jul 25 20:55 xiaoma

[root@maxiaole abel]# su test   切换账户

[test@maxiaole abel]$ rm xiaole

rm: cannot remove `xiaole': Operation not permitted   仍提示权限问题,这说明文件受父目录影响,该文件父目录有粘滞位权限。

 

SUID 实例

[root@maxiaole /]# chmod 4705 wangchao

[root@maxiaole /]# ll

-rws---r--   1 root root    17 Jul 28 20:44 wangchao

[root@maxiaole /]# su test

[test@maxiaole /]$ sh wangchao

Sat Jul 28 20:45:27 CST 2012

 

 

文件的隐藏属性

chattr 设置文件的隐藏属性

chattr [+-=] [ai] 文件或目录

[参数]

+

增加某个特殊的参数,原本的参数不动只是增加。

-

删除某个特殊的参数,其他原本的参数不动

=

定义仅有的参数,原有参数被更改

 

这里值介绍个人认为常用的参数

-a 这个文件只能增加数据,不能更改和删除删除数据;只有root才能设置这个属性

-i  添加这个参数后的文件,不能添加,删除,修改数据。一句话就是哪也不能动只

root才能设置这个属性

 

设置xiaoma.cc添加指定的隐藏属性添加-a属性,xiaoma.cc文件只能添加数据,不能

删除修改数据

[root@xiaole /]# touch xiaoma.cc  创建测试文件

[root@xiaole /]# cat >xiaoma.cc <

> SHISHIKAN

> EOF   写测试信息

[root@xiaole /]# chattr +a xiaoma.cc 增加特殊的隐藏属性

[root@xiaole /]# echo 'xinzengshujv' >>xiaoma.cc 测试增加文件信息

[root@xiaole /]# cat xiaoma.cc

SHISHIKAN

Xinzengshujv 新增的信息

 

[root@xiaole /]# rm -f xiaoma.cc 删除测试

rm: cannot remove `xiaoma.cc': Operation not permitted 报错不允许操作:

注意:这里只做了,删除文件本身的测试,删除出文件或修改文件内容是也是同样报错,不允许操作。

 

 

测试增加i属性

[root@xiaole /]# chattr +i xiaoma.cc

[root@xiaole /]# vi xiaoma.cc

SHISHIKA1

试图添加内容或修改内容

"xiaoma.cc"

"xiaoma.cc" E212: Can't open file for writing

Press ENTER or type command to continue

报错无法写入,并且退出后会生成一下文件。

-rw-r--r--   1 root root    23 Nov 17 12:42 xiaoma.cc~该文件的内容和原文件的内容一致,

并且,每使用vi命令编辑文件一次,就会生成一个结尾为~的文件。如想删除此文件只

能更改去掉源文件的隐藏属性------------- xiaoma.cc~但该文件并没有隐藏属性。

[root@xiaole /]# cat >xiaoma.cc <

> SHISHIKAN

> EOF   使用这样的方式写入同样报错':Operation not permitted

 

lsattr 查看文件隐藏的属性

lsattr  [-adR] 文件或目录

-a 列出包括的隐藏文件的隐藏属性

-d 针对目录,仅列出目录本身的属性,不包括子目录

-R连同子目录的属性一并列出

[root@xiaole /]# lsattr xiaoma.cc

----ia------- xiaoma.cc

[root@xiaole /]# lsattr -d xiaole/

-----a------- xiaole/

[root@xiaole /]# lsattr -ad xiaole/

-----a------- xiaole/





阅读(4517) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~