码迷,mamicode.com
首页 > 其他好文 > 详细

2015-05-18

时间:2018-05-17 21:38:50      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:linux

第三章、用户和组管理

3.1用户配置文件和密码配置文件

3.1.1用户配置文件/etc/passwd

[root@localhost ~]# cat /etc/passwd |head -n5
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

管道命令符 “|”的作用是将前一个命令的标准输出 作为后一个命令的标准输入
‘/etc/passwd’ 文件由 ‘:’ 分割成7个字段,每个字段的具体含义是:

1)用户名(如第一行中的root就是用户名),代表用户账号的字符串。用户名字符可以是大小写字母、数字、减号(不能出现在首位)、点以及下划线,其他字符不合法。虽然用户名中可以出现点,但不建议使用,尤其是首位为点时,另外减号也不建议使用,因为容易造成混淆。

2)密码控位键,存放的就是该账号的口令,早期的unix系统口令确实是存放在这里,但基于安全因素,后来就将其存放到 ‘/etc/shadow’ 中了,在这里只用一个 ‘x’ 代替。

3)uid,这个数字代表用户标识号。系统识别用户身份就是通过这个数字来的,0就是root,如果把普通用户uid修改为0,那么系统会认为这个用户和root是同一个账户。通常uid的取值范围是0~65535(但实际上已经可以支持到4294967294),0是超级用户(root)的标识号,centos6之前,1~499由系统保留,作为管理账号创建的普通用户的标识号从500开始,centos7之后,1~999由系统保留,作为管理账号,创建的普通用户的标识号从1000开始

4)gid,这个数字表示组标识号。这个字段对应着/etc/group 中的一条记录。

5)用户描述信息,通常记录该用户的一些属性,例如姓名、电话、地址等等。

6)用户的家目录,当用户登录时就处在这个目录下。root的家目录是/root,普通用户的家目录则为/home/username,这个字段是可以自定义的,比如你建立一个普通用户test1,要想让test1的家目录在/data目录下,只要修改/etc/passwd文件中test1那行中的该字段为/data即可。

7)用户登陆的shell环境,用来将用户下达的指令传给内核,Linux的shell有很多种sh, csh, ksh, tcsh, bash等,而Redhat/CentOS的shell就是bash。查看/etc/passwd文件,该字段中除了/bin/bash外还有/sbin/nologin比较多,它表示不允许该账号登录。如果你想建立一个账号不让他登录,那么就可以把该字段改成/sbin/nologin,默认是/bin/bash

3.1.2密码配置文件/etc/shadow

[root@localhost ~]# cat /etc/shadow |head -n5
root:$6$yMt95fN5Hl/tPWJo$DXW2xJo5/RIlS/Aywruj.dY7K.TtjFaf.g7ZiW.ntt83pAbGjLcKqgDm93ZDOSnox8Kp0M4nHcecnCQmSRoGX1:17642:0:99999:7:::
bin:*:15980:0:99999:7:::
daemon:*:15980:0:99999:7:::
adm:*:15980:0:99999:7:::
lp:*:15980:0:99999:7:::

/etc/shadow文件是用户密码配置文件,实际的密码加密保存在这,用 ‘:’ 分割成9个字段。

1)用户名,跟/etc/passwd对应。

2)用户密码,这个才是该账号的真正的密码,不过这个密码已经加密过了。
3)上次更改密码的日期,这个数字是这样计算得来的,距离1970年1月1日到上次更改密码的日期,例如上次更改密码的日期为2012年1月1日,则这个值就是 ‘365 x (2012-1970) + (2012-1970)/4 + 1 = 15341’. 因为如果是闰年,则有366天。

4)要过多少天才可以更改密码,默认是0,即不限制。

5)密码多少天后到期。即在多少天内必须更改密码,例如这里设置成30,则30天内必须更改一次密码,否则将不能登录系统,默认是99999,可以理解为永远不需要改。

6)密码到期前的警告期限,若这个值设置成7,则表示当7天后密码过期时,系统就发出警告告诉用户,提醒用户他的密码将在7天后到期。

7)账号失效期限。你可以这样理解,如果设置这个值为3,则表示:密码已经到期,然而用户并没有在到期前修改密码,那么再过3天,则这个账号就失效了,即锁定了。

8)账号的生命周期,跟第三段一样,是按距离1970年1月1日多少天算的。它表示的含义是,账号在这个日期前可以使用,到期后账号作废。

9)作为保留用的,没有什么意义。

3.2用户组管理

配置文件/etc/group和/etc/gshadow,这个和用户配置文件有点类似,一个组可以对应多个用户。一个用户只能有一个主组和多个附属组。

3.2.1新增一个组

命令 : groupadd
语法 : groupadd [-g GID] groupname
“-g” 选项可以自定义gid

[root@localhost ~]# groupadd luo
[root@localhost ~]# tac /etc/group |head -n1
luo:x:1000:
[root@localhost ~]# groupadd -g 1005 luos     #新建一个 luos的组,指定gid为1005
[root@localhost ~]# tac /etc/group |head -n1
luos:x:1005:

3.2.2删除组

命令 : groupdel
语法 : groupdel groupname

[root@localhost ~]# groupdel luos
[root@localhost ~]# tail /etc/group -n2      #可以看到刚才建的组 luos已经被删除了
tcpdump:x:72:
luo:x:1000:

该命令没有特殊选项,但有一种情况不能删除组

[root@localhost ~]# groupdel user1
groupdel: cannot remove the primary group of user ‘user1‘

这是因为user1组中包含user1账户,只有删除user1账户后才可以删除该组。

3.3用户管理

3.3.1新增一个用户

命令 : useradd
语法 : useradd [-u UID] [-g GID] [-d HOME] [-M] [-s]
‘-u’ 自定义UID
‘-g’ 使其属于已经存在的某个组,后面可以跟组id, 也可以跟组名
‘-d’ 自定义用户的家目录
‘-M’ 不建立家目录
‘-s’ 自定义shell

[root@localhost ~]# useradd luo   #不加任何参数默认会创建一个和用户同名的组
[root@localhost ~]# tail -n1 /etc/passwd
luo:x:1000:1000::/home/luo:/bin/bash
[root@localhost ~]# tail -n1 /etc/group
luo:x:1000:

指定选项

[root@localhost ~]# useradd -u 1003 -g 1005 -s /sbin/nologin -M luo    #指定uid为1003,让其加入已经存在gid为1005的组,指定不能登陆,不创建家目录
[root@localhost ~]# tail -n1 /etc/passwd
luo:x:1003:1005::/home/luo:/sbin/nologin

注意:‘-g’ 选项后面跟一个不存在的gid会报错,提示该组不存在, ‘-M’ 选项加上后则不建立用户家目录,但是在/etc/passwd文件中仍然有这个字段。但是你使用 ls /home/luo 查看一下会提示该目录不存在。所以 ‘-M’ 选项的作用只是不创建那个目录。

[root@localhost ~]# ls /home/luo
ls: 无法访问/home/luo: 没有那个文件或目录

3.3.2用户删除用户

命令:userdel
语法: userdel [-r] username

[root@localhost ~]# ls -ld /home/luo/
drwx------ 2 1000 1000 62 5月  17 17:52 /home/luo/
[root@localhost ~]# userdel luo   #不加-r选项默认不删除用户家目录
[root@localhost ~]# ls -d /home/luo/
/home/luo/
[root@localhost ~]# userdel -r luo  #加-r选项和连同家目录一起删除
[root@localhost ~]# ls -ld /home/luo
ls: 无法访问/home/luo: 没有那个文件或目录

即:-r选项的作用时删除账户的时候连带账户的家目录一起删除

3.4usermod命令

命令 : usermod 更改用户属性
语法 : usermod [-u UID] [-g GID] [-d HOME] [-s]
‘-u’ 更改用户的UID
‘-g’ 更改用户的gid,可以是数字也可以是组名字
‘-d’ 更改用户的家目录
‘-G‘ 使用加入存在的附属组
‘-s’ 更改用户的登陆的shell

[root@localhost ~]# useradd luo
[root@localhost ~]# id luo
uid=1000(luo) gid=1000(luo) 组=1000(luo)
[root@localhost ~]# usermod -u 1001 luo  #修改uid为1001
[root@localhost ~]# id luo
uid=1001(luo) gid=1000(luo) 组=1000(luo)
[root@localhost ~]# usermod -g 1006 luo  #修改gid为1006, 用户组可以有多个,gid只能一个
[root@localhost ~]# id luo
uid=1001(luo) gid=1006(lll) 组=1006(lll)
[root@localhost ~]# usermod -G 1000,1007 luo  #使其加入两个扩展组1000和1007
[root@localhost ~]# id luo
uid=1001(luo) gid=1006(lll) 组=1006(lll),1000(luo),1007(uuu)
[root@localhost ~]# usermod -s /bin/nologin luo   #更改登陆的 shell为/bin/nologin
[root@localhost ~]# tail -n1 /etc/passwd
luo:x:1001:1006::/home/luo:/bin/nologin

3.5用户密码管理

命令:passwd 更改用户密码
语法: passwd [选项] [username]
选项:
无选项时表示设置某用户密码。
-l:锁定某用户密码(= usermod -L)
-u:解锁某用户密码(= usermod -U)
-d:删除密码,仅有系统管理者才有此权限
密码设置建议规则:
1)长度大于10个字符
2)密码中包含大小写字母、数字、特殊符号
3)不规则性
4)禁用明码(即不要带有自己的名字、公司名字、自己电话、自己生日等等)
说明: 只有root用户才可以更改其他用户密码,普通用户只能更改自己的密码。默认新建的用户不设置密码是不能登陆的,可以通过/etc/shadow第二列可以看到,如果是 * 或者 !!都是不能锁定不能登陆的状态。

[root@localhost ~]# tail -n3 /etc/shadow
rpcuser:!!:17653::::::
tcpdump:!!:17653::::::
luo:!!:17668:0:99999:7:::

命令:passwd --stdin [username] (通常用在shell脚本中)
只用输入一次密码就可修改(默认是要确认输入两次),且明文显示。

[root@localhost ~]# passwd --stdin luo
更改用户 luo 的密码 。
123456
passwd:所有的身份验证令牌已经成功更新

3.6mkpasswd命令

mkpasswd=make password生成密码,使用前需要安装一个包“expect”。

[root@localhost ~]# yum install -y expect

语法: mkpasswd [选项]
选项:
-l:指定密码长度

[root@localhost ~]# mkpasswd -l 10      #指定长度为10,默认不加选项长度为9
ygp>Tq51gZ
[root@localhost ~]# mkpasswd
1yh5KS@fs

-s:=special指定特殊字符个数

[root@localhost ~]# mkpasswd -l 10 -s 3  #指定长度为10 特殊字符为3个
"$wOpmK=58

-d:指定数字的个数

[root@localhost ~]# mkpasswd -l 10 -s 3 -d 3  #指定长度为10 特殊字符为3个数字为3个
y/T58..U3e

注: mkpasswd命令配合echo、passwd命令以及管道符“|”使用更加方便。
eg: 一条命令修改用户密码(两种方法)
1)echo -e "yourpasswd\nyourpasswd" |passwd user
2)echo "yourpasswd"|passwd --stdin user

说明:
第一种方法,echo -e可以使用换行符\n。
第二种,passwd --stdin直接设定密码。

3.7su命令

命令su
语法 : su [-] username
后面可以跟 ‘-‘ 也可以不跟,普通用户su不加username时就是切换到root用户,当然root用户同样可以su到普通用户。 ‘-‘ 这个字符的作用是,加上后会初始化当前用户的各种环境变量,注意:普通root用户su - 普通用户时不用输入密码

[root@localhost ~]# whoami
root
[root@localhost ~]# su - luo   #完全切换到普通用户luo,加载用户家目录下的各种环境变量
上一次登录:四 5月 17 20:40:48 CST 2018pts/0 上
[luo@localhost ~]$ pwd
/home/luo
[luo@localhost ~]$ 登出
[root@localhost ~]# su luo   #不加“-”,默认不加载用户家目录下的环境变量
[luo@localhost root]$ pwd
/root

加上-c选项,在root用户登录时,可以以某个普通用户的身份去执行一个命令,适用于脚本中

[root@localhost ~]# su - luo -c "touch /tmp/3.txt"  #用普通用户luo去创建一个文件
[root@localhost ~]# ll /tmp/3.txt
-rw-r--r-- 1 luo luo 0 5月  17 20:46 /tmp/3.txt         #可以看到这个文件是普通用户luo创建的

3.8sudo命令

命令:sudo

用来以其他身份来执行命令,预设的身份为root。在/etc/sudoers中设置了可执行sudo指令的用户。sudo执行一个root才能执行的命令是可以办到的,但是需要输入密码,这个密码并不是root的密码而是用户自己的密码。默认只有root用户能使用sudo命令,普通用户想要使用sudo,是需要root预先设定的,即,使用 visudo 命令去编辑相关的配置文件/etc/sudoers. 如果没有visudo这个命令,请使用 yum install -y sudo 安装。

在配置文件中添加一行test ALL=(ALL) ALL

[root@localhost ~]# visudo
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
test    ALL=(ALL)       ALL   #第一个是要制授权的用户,第一个ALL是在什么地方 ,(ALL)是授权什么用户的权限 ,最后一个ALL,是授权什么使用什么命令

默认root能够sudo是因为这个文件中有一行 “root ALL=(ALL) ALL” 在该行下面加入 “test ALL=(ALL) ALL” 就可以让test用户拥有了sudo的权利。使用 “visudo” 命令编辑/etc/sudoers配置文件。
验证下刚才加的用户test

[root@localhost ~]# su test
[test@localhost root]$ ls
ls: 无法打开目录.: 权限不够
[test@localhost root]$ sudo ls
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
[sudo] password for test:
anaconda-ks.cfg    install.log  install.log.syslog

由于切换到test账户后的当前目录依旧是在/root 下,test账户没有任何权限,所以 ‘ls’ 的时候提示说权限不够,然而使用 sudo ls 输入test账户自身的密码后就有权限了。初次使用sudo 时会有上面的一大段提示,而后再次使用sudo 命令则不再提示。

sudo配置文件样例

#
# Sample /etc/sudoers file.
#
# This file MUST be edited with the ‘visudo‘ command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

##
# User alias specification
##
User_Alias  FULLTIMERS = millert, mikef, dowdy
User_Alias  PARTTIMERS = bostley, jwfox, crawl
User_Alias  WEBMASTERS = will, wendy, wim

##
# Runas alias specification
##
Runas_Alias OP = root, operator
Runas_Alias DB = oracle, sybase

##
# Host alias specification
##
Host_Alias  SPARC = bigtime, eclipse, moet, anchor:        SGI = grolsch, dandelion, black:        ALPHA = widget, thalamus, foobar:        HPPA = boa, nag, python
Host_Alias  CUNETS = 128.138.0.0/255.255.0.0
Host_Alias  CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
Host_Alias  SERVERS = master, mail, www, ns
Host_Alias  CDROM = orion, perseus, hercules

##
# Cmnd alias specification
##
Cmnd_Alias  DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore,             /usr/sbin/rrestore, /usr/bin/mt
Cmnd_Alias  KILL = /usr/bin/kill
Cmnd_Alias  PRINTING = /usr/sbin/lpc, /usr/bin/lprm
Cmnd_Alias  SHUTDOWN = /usr/sbin/shutdown
Cmnd_Alias  HALT = /usr/sbin/halt
Cmnd_Alias  REBOOT = /usr/sbin/reboot
Cmnd_Alias  SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh,              /usr/local/bin/tcsh, /usr/bin/rsh,              /usr/local/bin/zsh
Cmnd_Alias  SU = /usr/bin/su
Cmnd_Alias  VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh,                /usr/bin/chfn

##
# Override built-in defaults
##
Defaults               syslog=auth
Defaults>root          !set_logname
Defaults:FULLTIMERS    !lecture
Defaults:millert       !authenticate
Defaults@SERVERS       log_year, logfile=/var/log/sudo.log

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root        ALL = (ALL) ALL
%wheel      ALL = (ALL) ALL

# full time sysadmins can run anything on any machine without a password
FULLTIMERS  ALL = NOPASSWD: ALL

# part time sysadmins may run anything but need a password
PARTTIMERS  ALL = ALL

# jack may run anything on machines in CSNETS
jack        CSNETS = ALL

# lisa may run any command on any host in CUNETS (a class B network)
lisa        CUNETS = ALL

# operator may run maintenance commands and anything in /usr/oper/bin/
operator    ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,        sudoedit /etc/printcap, /usr/oper/bin/

# joe may su only to operator
joe     ALL = /usr/bin/su operator

# pete may change passwords for anyone but root on the hp snakes
pete        HPPA = /usr/bin/passwd [A-z]*, !/usr/bin/passwd root

# bob may run anything on the sparc and sgi machines as any user
# listed in the Runas_Alias "OP" (ie: root and operator)
bob     SPARC = (OP) ALL : SGI = (OP) ALL

# jim may run anything on machines in the biglab netgroup
jim     +biglab = ALL

# users in the secretaries netgroup need to help manage the printers
# as well as add and remove users
+secretaries    ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser

# fred can run commands as oracle or sybase without a password
fred        ALL = (DB) NOPASSWD: ALL

# on the alphas, john may su to anyone but root and flags are not allowed
john        ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root*

# jen can run anything on all machines except the ones
# in the "SERVERS" Host_Alias
jen     ALL, !SERVERS = ALL

# jill can run any commands in the directory /usr/bin/, except for
# those in the SU and SHELLS aliases.
jill        SERVERS = /usr/bin/, !SU, !SHELLS

# steve can run any command in the directory /usr/local/op_commands/
# as user operator.
steve       CSNETS = (operator) /usr/local/op_commands/

# matt needs to be able to kill things on his workstation when
# they get hung.
matt        valkyrie = KILL

# users in the WEBMASTERS User_Alias (will, wendy, and wim)
# may run any command as user www (which owns the web pages)
# or simply su to www.
WEBMASTERS  www = (www) ALL, (root) /usr/bin/su www

# anyone can mount/unmount a cd-rom on the machines in the CDROM alias
ALL     CDROM = NOPASSWD: /sbin/umount /CDROM,        /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM

3.9限制root远程登陆

如果不想root远程直接登陆,可以修改配置文件vi /etc/ssh/sshd_config,修改“#PermitRootLogin yes”为“PermitRootLogin no”,即不允许root远程登录

[root@localhost ~]# vi /etc/ssh/sshd_config     #编辑这个配置文件
#LoginGraceTime 2m
#PermitRootLogin yes   #将这行修改为PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
[root@localhost ~]systemctl restart sshd.service   #重启sshd服务,使其生效!

2015-05-18

标签:linux

原文地址:http://blog.51cto.com/13736286/2117679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!