1. 程式人生 > >4、linux建組

4、linux建組

用戶添加 nbsp ssh 令牌 height groupadd -1 mozilla logout

一 手動創建用戶流程

1) 新建用戶natasha,uid為1000,gid為555,備註信息為“master”

vim /etc/passwd

natasha:x:1000:555:master:/home/Natasha:/bin/bash


2) 修改natasha用戶的家目錄為/Natasha

[[email protected] ~]# vim /etc/passwd
[[email protected] ~]# vim /etc/shadow
[[email protected] ~]# vim /etc/group
[[email protected]

/* */ ~]# vim /etc/gshadow
[[email protected] ~]# mkdir /home/Natasha
[[email protected] ~]# ls -a /home/Natasha/
. ..

[[email protected] ~]# cp -r /etc/skel/.[!.]* /Natasha/

[[email protected] ~]# touch /var/spool/mail/Ntasha

[[email protected] ~]# id natasha
uid=1000(natasha) gid=555(natasha) 組=555(natasha)

3) 查看用戶信息配置文件的最後一行

[[email protected] ~]# tail -1 /etc/passwd
natasha:x:1001:555:master:/home/Natasha:/bin/bash


4) 為natasha用戶設置密碼“123”

[[email protected] ~]# passwd natasha
更改用戶 natasha 的密碼 。
新的 密碼:
無效的密碼: 密碼少於 8 個字符
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。


5) 查看用戶密碼配置文件的最後一行

[[email protected]

/* */ ~]# tail -1 /etc/shadow
natasha:$6$J9U/RNhi$YKp4XbjYxqhZo1QdFbS62hRzN5wGXxfUcGx80iJBIFuNcJSXeSZQ.Q2m8X/FeEECzK5EEQ0ooo3ivKymyL2Az0:17310:0:99999:7:::


6) 將natasha用戶賬戶鎖定

[[email protected] ~]# usermod -L natasha


7) 將natasha用戶賬戶解鎖

[[email protected] ~]# usermod -U natasha


8) 新建組police,gid為999

[[email protected] ~]# groupadd -g 999 police

police:x:999:


9) 查看組配置文件的最後一行

[[email protected] ~]# tail -1 /etc/group
police:x:999:


10) 將natasha用戶加入police組

[[email protected] ~]# usermod natasha -G police
[[email protected] ~]# id natasha
uid=1000(natasha) gid=555(natasha) 組=555(natasha),999(police)


11) 修改police組的組名為jingcha

[[email protected] ~]# groupmod -n jingcha police
[[email protected] ~]# tail -1 /etc/group
jingcha:x:999:natasha


12) 刪除natasha用戶,連家目錄和郵箱一起刪除

userdel -r natasha
13) 刪除jingcha組

[[email protected] ~]# groupdel jingcha
id[[email protected] ~]# tail /etc/group
sshd:x:74:
avahi:x:70:
slocate:x:21:
postdrop:x:90:
postfix:x:89:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
love:x:1000:love

二 創建目錄test

1) 在用戶的主目錄下創建目錄test,進入test創建空文件file1

[[email protected] ~]# mkdir test
[[email protected] ~]# cd test
[[email protected] test]# touch file1

2) 以長格式形式顯示文件信息,註意文件的權限和所屬用戶和組

[[email protected] test]# ll
總用量 0
-rw-r--r--. 1 root root 0 5月 25 04:07 file1
3) 為文件file1設置權限,使其他用戶可以對此文件進行寫操作。

[[email protected] test]# chmod o+w file1


4) 查看設置結果,

[[email protected] test]# ll
總用量 0
-rw-r--rw-. 1 root root 0 5月 25 04:07 file1
5) 取消同組用戶對文件file1的讀取權限,並查看設置結果。

[[email protected] test]# chmod g=- file1
[[email protected] test]# ll
總用量 0
-rw----rw-. 1 root root 0 5月 25 04:07 file1


6) 用數字表示法為文件file設置權限,所有者可讀、可寫、可執行,所屬組用戶和其他用戶只具有讀和執行的權限。設置完成後查看設置結果。

[[email protected] test]# chmod 733 file1
[[email protected] test]# ll
總用量 0
-rwx-wx-wx. 1 root root 0 5月 25 04:07 file1


7) 用數字形式更改文件file1的權限,使所有者只能讀取此文件。其他任何用戶都沒有權限。查看設置結果。

[[email protected] test]# chmod 400 file1
[[email protected] test]# ll
總用量 0
-r--------. 1 root root 0 5月 25 04:07 file1


8) 回到上層目錄,查看test的權限

[[email protected] test]# cd ..
[[email protected] ~]# ll d test
ls: 無法訪問d: 沒有那個文件或目錄
test:
總用量 0
-r--------. 1 root root 0 5月 25 04:07 file1


9) 為其他用戶添加對此目錄的寫權限

[[email protected] ~]# chmod o+w test/

[[email protected] ~]# ll -d test
drwxr-xrwx. 2 root root 19 5月 25 04:07 test

三 以操作文件的方式,新建一個用戶alex

1、[[email protected] ~]# vim /etc/passwd ----->1,2 ---->編輯用戶文件2、[[email protected] ~]# tail -1 /etc/passwd 添加新用戶,查看操作 alex:1111:1111::/home/alex:/bin/bash

3、[[email protected] ~]# vim /etc/shadow ----->3,4 ---->編輯用戶密碼

4、[[email protected] ~]# tail -1 /etc/shadow 文件,查看操作
alex:!!:17310:0:99999:7:::

5、[[email protected] ~]# vim /etc/group ------->5,6 ----->編輯組文件

6、[[email protected] ~]# tail -1 /etc/group 查看操作

alex:x:1111:

7、[[email protected] ~]# vim /etc/gshadow ---->7,8 --->編輯組密碼文件

8、[[email protected] ~]# tail -1 /etc/gshadow 查看操作

alex:!::

9,10,11------>常見文件,把用戶模板復制給alex用戶

9、[[email protected] ~]# mkdir /home/alex

10、[[email protected] ~]# cp -r /etc/skel/.[!.]* /home/alex

11、[[email protected] ~]# ls -a /home/alex
. .. .bash_logout .bash_profile .bashrc .mozilla

12,13,14---->由於用戶alex是‘root’用戶創建,更改用戶屬主與屬組,

改為alex

12、[[email protected] ~]# ll -d /home/alex

drwxr-xr-x. 3 alex alex 78 5月 25 06:47 /home/alex

13、[[email protected] ~]# chown -R alex:alex /home/alex

14、[[email protected] ~]# touch /var/spool/mail/alex

15,16,17----->創建用戶郵箱,由於用戶alex是‘root‘用戶創建,更改屬主與屬組分別為alex、mail

15、[[email protected] ~]# chown -R alex:mail /var/spool/mail/alex

16、[[email protected] ~]# ll -d /var/spool/mail/alex
-rw-r--r--. 1 alex mail 0 5月 25 06:56 /var/spool/mail/alex

17、[[email protected] ~]# id alex
uid=1111(alex) gid=1111(alex) 組=1111(alex)

四 驗證權限

1) 新建目錄/test/dir,屬主為tom,數組為group1,/test目錄的權限為777

[[email protected] ~]# mkdir -p /test/dir
[[email protected] ~]# ll -d /test/dir
drwxr-xr-x. 2 root root 6 5月 25 22:26 /test/dir

[[email protected] ~]# useradd tom

[[email protected] ~]# groupadd group1

[[email protected] ~]# chown tom:group1 /test/dir/
[[email protected] ~]# ll -d /test/dir
drwxr-xr-x. 2 tom group1 6 5月 25 22:26 /test/dir

[[email protected] ~]# chmod 777 /test
[[email protected] ~]# ll -d /test
drwxrwxrwx. 3 root root 17 5月 25 22:26 /test


2) [[email protected] tmp]# mkdir test

[[email protected] ~]# cd /tmp
[[email protected] tmp]# mkdir test
3) [[email protected] tmp]# ll -d test

[[email protected] tmp]# ll -d test

4) drwxr-xr-x. 2 root root 6 5月 25 08:32 test

drwxr-xr-x. 2 root root 6 5月 25 22:40 test
2) 新建用戶jack,切換到jack用戶下,驗證jack用戶對dir目錄的rwx權限(開啟另外一個終端,依次修改dir目錄的others權限)

[[email protected] tmp]# useradd jack

[[email protected] tmp]# id jack
uid=1009(jack) gid=1011(jack) 組=1011(jack)

[[email protected] tmp]# su - jack
[[email protected] ~]$ ll /test/dir/
總用量 0

[[email protected] ~]# chmod o+w /test/dir/

[[email protected] ~]$ touch /test/dir/a.txt
[[email protected] ~]$ ll -d /test/dir/a.txt
-rw-rw-r--. 1 jack jack 0 5月 25 22:52 /test/dir/a.txt

[[email protected] ~]$ cd /test/dir/
[[email protected] dir]$ cd -
/home/jack

[[email protected] ~]# echo "date" > /test/dir/a.txt

[[email protected] ~]# chmod o-x /test/dir/
[[email protected] ~]# chmod o-r /test/dir/

3)將jack加入group1組,驗證jack用戶對dir目錄的rwx權限(開啟另外一個終端,依次修改dir目錄的group權限)技術分享

4)切換到tom用戶,驗證tom用戶對dir目錄的rwx權限(開啟另外一個終端,依次修改dir目錄的user權限)技術分享

5)在dir目錄內新建文件tom.txt,屬主為tom,屬組為group1,/test目錄的權限為777技術分享

6)新建用戶rose,切換到rose用戶下,驗證rose用戶對tom.txt的rwx權限(開啟另外一個終端,依次修改tom.txt的others權限來配合驗證過程)技術分享

7)將rose加入group1組,在rose用戶下,驗證rose用戶對tom.txt的rwx權限(開啟另外一個終端,依次修改tom.txt的group1權限來配合驗證過程)技術分享

8)切換到tom用戶,驗證tom用戶對tom.txt的rwx權限(開啟另外一個終端,依次修改tom.txt的user權限來配合驗證過程)技術分享

4、linux建組