一、用户和组的基本概念
1、用户
用户: 用于获取计算机资源或服务的标识符,比如用户名。计算机处理的是UID,用户名和UID的关系存储在/etc/passwd文件中,每个用户都有权限访问这个文件。
影子口令: 用户的密码存储在/etc/shadow中
用户类别:
- 管理员。 管理员的UID为0
- 普通用户(系统用户和一般用户)。 普 通用户的UID 1-65535,一般用户500-60000。
- 系统用户。 系统用户1-499 。 一登陆就运行的后台进程(服务)以普通用户的身份来运行,叫做系统用户。系统用户只是为了运行某一类进程,这类用户不需要登陆系统。
2、组
容器( 用户组) : 也是一个标识符,用于关联权限,方便地指派权限,每一个组都有一个GID。
影子口令: 组密码存储在/etc/gshadow
组类别:
- 普通组(系统组和一般组)
- 管理员组。
或者可以分为:
- 私有组:创建用户时,如果没有为其指定所属的组,系统会自动为其创建一个与用户名同名的组 ,该组即为私有组。
- 基本组:用户的默认组
-
附加组,额外组:默认组以外的其它组
二、/etc/passwd
man 5 passwd 可以查看该文件详细格式
[root@hao ~]#
cat
/etc/
passwd
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
sync
:x:
5
:
0
:
sync
:/sbin:/bin/
sync
shutdown:x:
6
:
0
:shutdown:/sbin:/sbin/
shutdown
...
hao:x:
500
:
500
:haohao:/home/hao:/bin/bash
字段解析:
1、account: 登录名
2、password:密码。 如果是x则表示是密码占位符,真正的密码在/etc/shadow中
3、UID
4、GID:基本组ID
5、GECOS:用户注释信息
6、directory:家目录
7、shell:用户默认shell。当前系统上所有的合法shell存在/etc/shells。
[root@hao ~]#
cat
/etc/
shells
/bin/
sh
/bin/
bash
/sbin/
nologin
/bin/
dash
/bin/
tcsh
/bin/csh
三、/etc/shadow
[root@hao ~]#
cat
/etc/
shadow
root:$
6
$OPY00iMwaBSqsynd$uLgMkMKWSzVor5giiOkjk9tnN0pzIMEgqqudfTwnGu.
9
.Oxrw5DcXwffI7cVyw9nUquLGNa4VTTyl0To6gRWi.:
16277
:
0
:
99999
:
7
:::
bin:
*:
15980
:
0
:
99999
:
7
:::
...
hao:$
6
$Ogbf1lEV5ro4510u$
82
.pHdwem8YndttOjmImB/MxK/
2
/cLZ.8He0wF5pz/AS9m4R2S2pTCG.vnHqOPs9oCL1LA2E5oR9uhLRQnUih/:
16277
:
0
:
99999
:
7
:::
man 5 shadow可以查看文件详细格式
字段解析:
1、login name 登录名
2、encrypted password:加密后的密码,如下面。如果为*或!!,则代表该用户是锁定的,也就是不允许登陆的。
$
6
$
OPY00iMwaBSqsynd
$
uLgMkMKWSzVor5giiOkjk9tnN0pzIMEgqqudfTwnGu.
9
.Oxrw5DcXwffI7cVyw9nUquLGNa4VTTyl0To6gRWi.:
16277
:
0
:
99999
:
7
:::
密码的格式为$<ID>$<SALT>$<PWD>。 其中ID代表所使用的加密方法,具体如下
ID | Method
-------------------------------
1 | MD5 (Linux, BSD)
2a | Blowfish (OpenBSD)
md5 | Sun MD5
For the new SHA-256 and SHA-512 methods the following values are
selected:
ID | Method
-------------------------------
5 | SHA-256
6 | SHA-512
SALT为加入的杂质,随机生成的,因此密码相同,杂质也不一定相同。
3、date of last password change。上次密码更改时间(从1970年1月1号开始算起经过的天数)。
4、 minimum password age。密码最短使用期限
5、maximum password age 密码最长使用期限。
6、password warning period:密码警告期限
7、password inactivity period:密码宽限期
8、account expiration date:密码有效期
9、reserved field:保留字段
常用的加密方法:
对称加密: 加密和解密使用同一个密码。
公钥加密: 每个密码成对出现,一个为私钥(secret key),一个公钥(public key)即公钥加密,私钥解密或私钥加密,公钥解密,即公钥加密只能使用与之对应的私钥机密,反之亦然。公钥加密的秘钥长度比对称加密要长的多,安全性也要比对称加密安全,但是速度要慢,一般慢三个数量级,因此公钥加密很少拿来加密数据,用于做秘钥交换。
单向加密: 或称为散列加密。不能由密文取得明文。用于实现提取数据特征码,每个数据的特征码唯一,如指纹加密。单向加密可以用来做数据完整性校验。其特性包括:
1、不可逆。也就是只能有明文获得密文,不能由密文获取明文
2、 雪崩效应 。初始条件的微小改变可能引起结果的巨大改变,防暴力破解。对inittab文件进行稍微可变,可以看出其特征码改变很大
[root@hao ~]#
cp
/etc/inittab ./
[root@hao ~]#
ls
anaconda
-ks.cfg
install
.log ?????? ?????? ?????? ??????
inittab
install
.log.syslog ????????? ?????? ?????? ??????
[root@hao ~
]#
md5sum inittab
3f206e10339f61eaf8d378bf504db462 inittab
[root@hao ~
]#
nano inittab
[root@hao ~
]#
md5sum inittab
11bec7e54fd168e19ca5d8cff1b5dd1c inittab
3、定长输出 MD5:128位 SHA1:(Secure Hash Algorithm)安全的哈希算法 160位定长输出
三、/etc/group
第一个字段组名 第二个字段密码占位符 第三个字段GID 第四个字段以这个组为附加组的用户列表,多个用户用逗号隔开
[root@hao ~]#
cat /etc/
group root:x:
0
: bin:x:
1
:bin,daemon ...
hao:x:
500
: guang:x:
501:
四、用户管理
1、useradd(adduser)添加用户
[root@hao ~]#
which
useradd
/usr/sbin/
useradd
[root@hao
~]#
ls
-l $(
which
useradd)
-rwxr-x---.
1
root root
103096
Dec
8
2011
/usr/sbin/
useradd
[root@hao
~]#
which
adduser
/usr/sbin/
adduser
[root@hao
~]#
ls
-l $(
which
adduser)
lrwxrwxrwx.
1
root root
7
Jul
26
20
:
14
/usr/sbin/adduser -> useradd
命令格式: useradd [options] username
[root@hao ~
]#
useradd guang
[root@hao
~]#
tail
-
1
/etc/
passwd
guang:x:
501
:
501
::/home/guang:/bin/
bash
[root@hao
~]#
tail
-
1
/etc/
group
guang:x:
501
:
[root@hao
~]#
tail
-
1
/etc/
shadow
guang:
!!:
16377
:
0
:
99999
:
7
:::
[root@hao
~]#
passwd
guang
Changing password
for
user guang.
New password:
BAD PASSWORD: it does not contain enough DIFFERENT characters
BAD PASSWORD: is a palindrome
Retype new password:
passwd
: all authentication tokens updated successfully.
[root@hao
~]#
tail
-
1
/etc/
shadow
guang:$
6
$0ndd3rOB$OP74KA4BocxxtU87tDCUE/JGHlZbygHML9KnabirIXLJ8yQJAGdP7setenuZP1fVcOK2ZhhK05aUWYqdDBcjG/:
16377
:
0
:
99999
:
7
:::
命令选项:
-u:指定UID,须保证该UID未使用。如果未使用该选项,用户UID默认为/etc/passwd下的最大用户UID的下一个
[root@hao ~]#
useradd -u
1000
testuser
[root@hao
~]#
tail
-
1
/etc/
passwd
testuser:x:
1000
:
1000
::/home/testuser:/bin/bash
-g:指定基本组,如果不使用该选项则默认添加一个和用户同名的基本组,如果使用该选项则一定保证该组名存在。
[root@hao ~]#
useradd -g mygroup testusergroup
[root@hao ~]#
tail -1 /etc/passwd
testusergroup:x:1001:502::/home/testusergroup:/bin/bash
-G:指定额外组(附加组),可以有多个。
[root@hao ~]#
useradd -
G mygroup testuser1
[root@hao
~]#
tail
-
1
/etc/
passwd
testuser1:x:
1002
:
1002
::/home/testuser1:/bin/
bash
[root@hao
~]#
tail
/etc/
group
webalizer:x:
67
:
tomcat:x:
91
:
sshd:x:
74
:
tcpdump:x:
72
:
slocate:x:
21
:
hao:x:
500
:
guang:x:
501
:
mygroup:x:
502
:testuser1
testuser:x:
1000
:
testuser1:x:
1002
:
-c:指定注释信息
-d:指定家目录 默认为在/home/下创建一个跟用户同名的目录为家目录。
[root@hao ~]#
useradd -c
"
haohao
"
-d /home/
haohao testuser2
[root@hao
~]#
tail
-
1
/etc/
passwd
testuser2:x:
1003
:
1003
:haohao:/home/haohao:/bin/bash
-s:指定shell路径。该路径需要出现在/etc/shells。默认为/bin/bash
-r:添加系统用户。即UID在1-500之间,而且系统用户不允许登陆系统,且不存在家目录,即使路径存在。该选项通常单独使用。
[root@hao ~]#
useradd -
r testr
[root@hao
~]#
tail
-
1
/etc/
passwd
testr:x:
496
:
493
::/home/testr:/bin/
bash
[root@hao
~]#
ls
/
home
guang hao haohao testuser testuser1
添加用户的默认信息保存在/etc/default/useradd
[root@hao ~]#
file
/etc/default/
useradd
/etc/default/
useradd: ASCII text
[root@hao
~]#
cat
/etc/default/
useradd
# useradd defaults
file
GROUP
=
100
HOME
=/
home
INACTIVE
=-
1
EXPIRE
=
SHELL
=/bin/
bash
SKEL
=/etc/
skel
CREATE_MAIL_SPOOL
=yes
2、 userdel 删除用户
命令格式: userdel [option] username 删除用户时,若未指定选项,其家目录不会被删除
命令选项:
-r:删除用户同时删除用户的家目录
[root@hao ~]#
ls
/
home
guang hao haohao testuser testuser1 testusergroup
[root@hao
~
]#
userdel testuser
[root@hao
~]#
ls
/
home
guang hao haohao testuser testuser1 testusergroup
[root@hao
~]#
userdel -
r testusergroup
[root@hao
~]#
ls
/
home
guang hao haohao testuser testuser1
3、id 查看用户的UID等相关信息
命令格式:
id [option] [username]
命令选项:
-u:查看UID
[root@hao ~]#
id
-
u testuser1
1002
-g:查看基本组
[root@hao ~]#
id
-
g testuser1
1002
-G:查看其附加组和基本组
[root@hao ~]#
id
-
G testuser1
1002
502
-n:显示名字而不是ID
[root@hao ~]#
id
-u -
n testuser1
testuser1
[root@hao
~]#
id
-g -
n testuser1
testuser1
[root@hao
~]#
id
-G -
n testuser1
testuser1 mygroup
4、finger 查看用户账号信息
命令格式: finger username
5、usermod修改用户账号属性
命令格式: usermod [option] username。 用法同useradd,
命令选项:
-u:更改用户uid
-g:更改用户GID
-G:更改用户附加组,即覆盖原有的附加组。一般同-a选项一同使用,即添加用户到新的附加组,原来的附加组不被覆盖。
-c:更改注释信息
-d:更改家目录。如果原来家目录下已有该用户的文件夹,原家目录下的文件夹不会被迁移到新的家目录下。一般和-m选项一同使用,则可以使其原家目录下文件 迁移到新的家目录下。
-s:更改shell。
-l:更改用户名
-L:锁定账号
-U:解锁用户
6、chsh(change shell)修改用户shell
root@hao ~
]#
finger testuser1
Login: testuser1 Name:
Directory:
/home/testuser1 Shell: /bin/
bash
Never logged
in
.
No mail.
No Plan.
[root@hao
~
]#
chsh testuser1
Changing shell
for
testuser1.
New shell [
/bin/bash]: /bin/
tcsh
Shell changed.
[root@hao
~
]#
finger testuser1
Login: testuser1 Name:
Directory:
/home/testuser1 Shell: /bin/
tcsh
Never logged
in
.
No mail.
No Plan.
7、chfn(change finger)修改注释信息
[root@hao ~
]#
chfn testuser1
Changing finger information
for
testuser1.
Name []: test
Office []: test
Office Phone []:
110
Home Phone []:
110
Finger information changed.
[root@hao
~]#
tail
-
2
/etc/
passwd
testuser1:x:
1002
:
1002
:test,test,
110
,
110
:/home/testuser1:/bin/
tcsh
testuser2:x:
1003
:
1003
:haohao:/home/haohao:/bin/bash
8、 passwd 修改用户密码
普通用户只能使用不带参数的该命令更改自己的密码,管理员可以使用该命令更改其他用户的密码
命令格式:
passwd [option][username]
root@hao:~#
passwd
user1
Enter new UNIX password:
Retype new UNIX password:
passwd
: password updated successfully
命令选项:
--stdin(standard in ):从标准输入读取密码。经测试Ubuntu没有该参数,
可以使用chpasswd命令替换,chpasswd username password,均可以用echo输出指 定的信息,然后管道给这两个命令处理,如:
root@hao:~#
echo
"
newpassword
"
|
passwd
user1 --
stdin
root@hao:
~#
ehco
"
newpassword
"
|chpasswd user1
-l:锁定用户账号
-u:解锁用户
-d:删除密码
root@hao:~#
tail
-
1
/etc/
shadow
user1:$
6
$eKF3mI4G$O3pzjIQiRodxGRw9c4SVUAQ34oi8vOMcguGW4/7LBTYK2ADppH/sPVtqsjKP9ajZamXU8SmZgtm/uWKTtaxam/:
16362
:
0
:
99999
:
7
:::
root@hao:
~#
passwd
-
d user1
passwd
: password expiry information changed.
root@hao:
~#
tail
-
1
/etc/
shadow
user1::
16362
:
0
:
99999
:
7
:::
9、pwck(password check)检查用户完整性
[root@hao ~
]#
pwck
user
'
adm
'
: directory
'
/var/adm
'
does not exist
user
'
uucp
'
: directory
'
/var/spool/uucp
'
does not exist
user
'
gopher
'
: directory
'
/var/gopher
'
does not exist
user
'
ftp
'
: directory
'
/var/ftp
'
does not exist
user
'
avahi-autoipd
'
: directory
'
/var/lib/avahi-autoipd
'
does not exist
user
'
saslauth
'
: directory
'
/var/empty/saslauth
'
does not exist
user
'
pulse
'
: directory
'
/var/run/pulse
'
does not exist
pwck: no changes
五、组管理
1、groupadd创建组
命令格式: groupadd [options] groupname
命令选项:
-g:指定GID
-r:添加系统组
[root@hao ~]#
groupadd -
r nginx
[root@hao
~]#
tail
-
1
/etc/
group
nginx:x:
492
:
2、groupmod修改组属性
命令格式: groupmod [options] groupname
命令选项:
-g:修改GID
-n:修改groupname
3、groupdel删除一个组
4、gpasswd给组加密码
命令格式: gpasswd groupname
用户创建一个文件,该文件的属主为该文件,属组为该用户所在的基本组
[root@hao ~
]#
useradd hadoop
[root@hao
~]#
su
-
hadoop
[hadoop@hao
~]$
cd /
tmp
[hadoop@hao tmp]$
touch
a.hadoop
[hadoop@hao tmp]$
ll
total
40
-rw-rw-r--.
1
hadoop hadoop
0
Nov
4
12
:
09
a.hadoop
drwx
------.
2
hao hao
4096
Aug
6
15
:
58
keyring-
4BcUXT
drwx
------.
2
hao hao
4096
Jul
27
12
:
49
keyring-
DTEmAL
drwx
------.
2
hao hao
4096
Jul
27
13
:
01
keyring-
FD7mUX
drwx
------.
2
hao hao
4096
Aug
4
16
:
58
keyring-
YtvBn9
drwx
------.
2
hao hao
4096
Jul
26
13
:
05
keyring-
ZCIk4h
drwx
------.
2
root root
4096
Jul
26
12
:
48
keyring-
l3Td4R
drwx
------.
2
gdm gdm
4096
Nov
4
10
:
00
orbit-
gdm
drwx
------.
2
gdm gdm
4096
Nov
4
09
:
59
pulse-
0dTthpTUZIEl
drwx
------.
2
hao hao
4096
Aug
6
15
:
58
pulse-
1lfMnULTO8sm
drwx
------.
2
root root
4096
Jul
26
12
:
48
pulse-
u8BNu0nCsMTX
[hadoop@hao tmp]$
id
uid
=
1004
(hadoop) gid=
1004
(hadoop)
groups
=
1004
(hadoop) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
5、newgrp:将用户的基本组临时切换到其他组(登陆到其他组)
只有当用户跟所切换到的组没有任何关系时(即要切换到的组也不是该用户的附加组时),需要输入密码。可以用exit退出
[root@hao ~
]#
gpasswd mygroup
Changing the password
for
group mygroup
New Password:
Re
-
enter new password:
[root@hao
~]#
su
-
hadoop
[hadoop@hao
~]$
cd /
tmp
[hadoop@hao tmp]$
newgrp mygroup
Password:
[hadoop@hao tmp]$
id
uid
=
1004
(hadoop) gid=
502
(mygroup)
groups
=
1004
(hadoop),
502
(mygroup) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[hadoop@hao tmp]$
touch
b.hadoop
[hadoop@hao tmp]$
ll
total
40
-rw-rw-r--.
1
hadoop hadoop
0
Nov
4
12
:
09
a.hadoop
-rw-r--r--.
1
hadoop
mygroup
0
Nov
4
12
:
21
b.hadoop
drwx
------.
2
hao hao
4096
Aug
6
15
:
58
keyring-
4BcUXT
drwx
------.
2
hao hao
4096
Jul
27
12
:
49
keyring-
DTEmAL
drwx
------.
2
hao hao
4096
Jul
27
13
:
01
keyring-
FD7mUX
drwx
------.
2
hao hao
4096
Aug
4
16
:
58
keyring-
YtvBn9
drwx
------.
2
hao hao
4096
Jul
26
13
:
05
keyring-
ZCIk4h
drwx
------.
2
root root
4096
Jul
26
12
:
48
keyring-
l3Td4R
drwx
------.
2
gdm gdm
4096
Nov
4
10
:
00
orbit-
gdm
drwx
------.
2
gdm gdm
4096
Nov
4
09
:
59
pulse-
0dTthpTUZIEl
drwx
------.
2
hao hao
4096
Aug
6
15
:
58
pulse-
1lfMnULTO8sm
drwx
------.
2
root root
4096
Jul
26
12
:
48
pulse-u8BNu0nCsMTX

