一、用戶和組的基本概念? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1、用戶
用戶: 用于獲取計算機資源或服務(wù)的標(biāo)識符,比如用戶名。計算機處理的是UID,用戶名和UID的關(guān)系存儲在/etc/passwd文件中,每個用戶都有權(quán)限訪問這個文件。
影子口令: 用戶的密碼存儲在/etc/shadow中
用戶類別:
- 管理員。 管理員的UID為0
- 普通用戶(系統(tǒng)用戶和一般用戶)。 普 通用戶的UID 1-65535,一般用戶500-60000。
- 系統(tǒng)用戶。 系統(tǒng)用戶1-499 。 一登陸就運行的后臺進(jìn)程(服務(wù))以普通用戶的身份來運行,叫做系統(tǒng)用戶。系統(tǒng)用戶只是為了運行某一類進(jìn)程,這類用戶不需要登陸系統(tǒng)。
?2、組
容器( 用戶組) : 也是一個標(biāo)識符,用于關(guān)聯(lián)權(quán)限,方便地指派權(quán)限,每一個組都有一個GID。
影子口令: 組密碼存儲在/etc/gshadow
組類別:
- 普通組(系統(tǒng)組和一般組)
- 管理員組。
或者可以分為:
- 私有組:創(chuàng)建用戶時,如果沒有為其指定所屬的組,系統(tǒng)會自動為其創(chuàng)建一個與用戶名同名的組 ,該組即為私有組。
- 基本組:用戶的默認(rèn)組 ?
-
附加組,額外組:默認(rèn)組以外的其它組
?二、/etc/passwd ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
man 5 passwd 可以查看該文件詳細(xì)格式
[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:用戶默認(rèn)shell。當(dāng)前系統(tǒng)上所有的合法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可以查看文件詳細(xì)格式
字段解析:
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為加入的雜質(zhì),隨機生成的,因此密碼相同,雜質(zhì)也不一定相同。
3、date of last password change。上次密碼更改時間(從1970年1月1號開始算起經(jīng)過的天數(shù))。
4、?minimum password age。密碼最短使用期限
5、maximum password age 密碼最長使用期限。
6、password warning period:密碼警告期限
7、password inactivity period:密碼寬限期
8、account expiration date:密碼有效期
9、reserved field:保留字段
常用的加密方法:
對稱加密: 加密和解密使用同一個密碼。
公鑰加密: 每個密碼成對出現(xiàn),一個為私鑰(secret key),一個公鑰(public key)即公鑰加密,私鑰解密或私鑰加密,公鑰解密,即公鑰加密只能使用與之對應(yīng)的私鑰機密,反之亦然。公鑰加密的秘鑰長度比對稱加密要長的多,安全性也要比對稱加密安全,但是速度要慢,一般慢三個數(shù)量級,因此公鑰加密很少拿來加密數(shù)據(jù),用于做秘鑰交換。
單向加密: 或稱為散列加密。不能由密文取得明文。用于實現(xiàn)提取數(shù)據(jù)特征碼,每個數(shù)據(jù)的特征碼唯一,如指紋加密。單向加密可以用來做數(shù)據(jù)完整性校驗。其特性包括:
1、不可逆。也就是只能有明文獲得密文,不能由密文獲取明文
2、 雪崩效應(yīng) 。初始條件的微小改變可能引起結(jié)果的巨大改變,防暴力破解。對inittab文件進(jìn)行稍微可變,可以看出其特征碼改變很大
[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默認(rèn)為/etc/passwd下的最大用戶UID的下一個
[root@hao ~]#
useradd -u
1000
testuser
[root@hao
~]#
tail
-
1
/etc/
passwd
testuser:x:
1000
:
1000
::/home/testuser:/bin/bash
-g:指定基本組,如果不使用該選項則默認(rèn)添加一個和用戶同名的基本組,如果使用該選項則一定保證該組名存在。
[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:指定家目錄 默認(rèn)為在/home/下創(chuàng)建一個跟用戶同名的目錄為家目錄。
[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路徑。該路徑需要出現(xiàn)在/etc/shells。默認(rèn)為/bin/bash
-r:添加系統(tǒng)用戶。即UID在1-500之間,而且系統(tǒng)用戶不允許登陸系統(tǒng),且不存在家目錄,即使路徑存在。該選項通常單獨使用。
[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
添加用戶的默認(rèn)信息保存在/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等相關(guān)信息
命令格式:
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 修改用戶密碼
普通用戶只能使用不帶參數(shù)的該命令更改自己的密碼,管理員可以使用該命令更改其他用戶的密碼
命令格式:
passwd [option][username]
root@hao:~#
passwd
user1
Enter new UNIX password:
Retype new UNIX password:
passwd
: password updated successfully
命令選項:
--stdin(standard in ):從標(biāo)準(zhǔn)輸入讀取密碼。經(jīng)測試Ubuntu沒有該參數(shù),
可以使用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創(chuàng)建組
命令格式: groupadd [options] groupname
命令選項:
-g:指定GID
-r:添加系統(tǒng)組
[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?
用戶創(chuàng)建一個文件,該文件的屬主為該文件,屬組為該用戶所在的基本組
[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:將用戶的基本組臨時切換到其他組(登陸到其他組)
只有當(dāng)用戶跟所切換到的組沒有任何關(guān)系時(即要切換到的組也不是該用戶的附加組時),需要輸入密碼。可以用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
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

