Linux(2):用戶(2)

Linux ben-u 5.13.0-35-generic #40~20.04.1-Ubuntu SMP

--

 

序章

前文提到,用戶關心信息位於以下文件中:

  • /etc/passwd
  • /etc/shadow
  • /etc/group
  • /etc/gshadow

接下來對用戶的操作會影響到這些文件的內容。

 

命令合集

useradd  adduser

usermod 

passwd

userdel deluser

groupadd

groupmod

groupdel

 

用戶(user)操作

添加用戶:useradd、adduser

useradd命令

useradd 是一個 底層的添加用戶的命令,而 adduser 是 Debian系統特有的。

$ man useradd
useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead.

 

使用 useradd 添加 用戶 user12

$ useradd -s /bin/bash -d /home/user12 user12
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.

# 沒有權限,添加 sudo 後執行

$ sudo useradd -s /bin/bash -d /home/user12 user12
[sudo] password for ben:
ben@ben-u:~$
ben@ben-u:~$ ls /home/
ben  git  stservice

# 指定了 home目錄爲 /home/user12 ,但 沒有這個目錄
# 可以使用 -m 選項在創建時添加:
-m, --create-home             create the user's home directory

 

useradd --help:

更多詳情 還得查看 man頁面。

 

上面的 user12 添加後,用戶相關文件的變化:本發佈於博客園

# 多了 user12行
ben@ben-u:~$ cat /etc/passwd | grep user12
user12:x:1002:1002::/home/user12:/bin/bash

# 創建時 沒有設置密碼,此時,第二個字段爲 感嘆號(!)
ben@ben-u:~$ sudo cat /etc/shadow | grep user12
[sudo] password for ben:
user12:!:19354:0:99999:7:::

# 創建時沒有指定用戶組,這裏多了一個 名爲 user12 的組
ben@ben-u:~$ cat /etc/group | grep user12
user12:x:1002:

 

由於user12 沒有密碼,此時,無法使用改賬號登錄。

注意,也沒有自動創建 home目錄。

 

測試使用 useradd 給已存在用戶添加 密碼、創建 home目錄:失敗

注意,這裏的 -p 設置的密碼 需要密文,不同的加密方式有不同的密文。像上面的明文 222 是不可以成功的。

 

adduser命令

使用 adduser --help 查看命令的使用信息。本發佈於博客園

 

下面添加一個 普通用戶 user14:

交互式創建用戶;

創建了用戶、組;

創建了 home目錄;

拷貝了 /etc/skel

交互過程中,需要輸入密碼、用戶full name等信息;

ben@ben-u:~$ sudo adduser user14
[sudo] password for ben:
Adding user `user14' ...
Adding new group `user14' (1003) ...
Adding new user `user14' (1003) with group `user14' ...
Creating home directory `/home/user14' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
No password supplied
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for user14
Enter the new value, or press ENTER for the default
        Full Name []: ben 123
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
ben@ben-u:~$
ben@ben-u:~$ cat /etc/passwd | grep user14
user14:x:1003:1003:ben 123,,,:/home/user14:/bin/bash
ben@ben-u:~$
ben@ben-u:~$ cat /etc/shadow | grep user14
cat: /etc/shadow: Permission denied
ben@ben-u:~$ sudo cat /etc/shadow | grep user14
user14:$6$vZB5YSmWe16ckKyp$PrCsFf8tNGNOaFtR1NhqkcTJ7DjJuoXHXEuCtiFEMo1X/gPCwljnecEAyrsH4ry52Uut5AtW8K37Wn5qQDNNt1:19354:0:99999:7:::
ben@ben-u:~$
ben@ben-u:~$ sudo cat /etc/shadow | grep user1
user12:$6$uR/enewcZhL1opO.$9B/YHvMYzc74ZiOMW2oZUYypBE/fvR6smkpFhyPPioQ/ItzYwAn5vU76xqzjdMIkg6Rf1te6OWwfK/tLMQMdh0:19354:7:14:2:::
user14:$6$vZB5YSmWe16ckKyp$PrCsFf8tNGNOaFtR1NhqkcTJ7DjJuoXHXEuCtiFEMo1X/gPCwljnecEAyrsH4ry52Uut5AtW8K37Wn5qQDNNt1:19354:0:99999:7:::
ben@ben-u:~$
ben@ben-u:~$ sudo cat /etc/group | grep user14
user14:x:1003:
ben@ben-u:~$
ben@ben-u:~$ sudo cat /etc/group | grep user1
user12:x:1002:
user14:x:1003:
ben@ben-u:~$

 

/etc/skel 是什麼?

原來,home目錄 的初始文件在 這裏。本發佈於博客園

 

修改用戶:usermod、passwd

修改密碼:usermod

可以修改密碼,但是,密碼需要加密後輸入。

usermod --help:

 

疑問:本發佈於博客園

Linux支持哪些加密算法呢?密文怎麼造呢?TODO

 

修改密碼:passwd

passwd 位於 /usr/bin 目錄。

passwd --help:

 

man passwd:本發佈於博客園

PASSWD(1) 
NAME
       passwd - change user password
SYNOPSIS
       passwd [options] [LOGIN]
DESCRIPTION
       The passwd command changes passwords for user accounts. 
       A normal user may only change the password for their own account, 
       while the superuser may change the password for any account.  
       passwd also changes the account or associated password validity period.
省略更多

 

執行修改密碼:用戶 ben 修改用戶 user12 的密碼

 

需要使用sudo,輸入兩次新密碼(明文)即可。

 

現在,user12 擁有密碼了,使用它來登錄:

ben@ben-u:~$ su user12
Password:
user12@ben-u:/home/ben$ pwd
/home/ben

登錄成功。

 

給用戶建立home目錄

非常規方式:使用 mkdir、chown 創建 /home/user12 目錄(空白目錄),並 更改擁有者爲 user12(用戶和組)。

# ben 用戶
cd /home/
sudo mkdir user12
sudo chown user12:user12 user12/

不過,此時的 home目錄 user12 裏面沒有任何內容。本發佈於博客園

登錄後,連 ll 命令等都無法執行。

查看 PATH 並和 ben用戶 的進行對比:

ben@ben-u:/home/user13$ echo $PATH
/home/ben/bin:/usr/lib/scala/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

ben@ben-u:/home/user13$ su user12
Password:
user12@ben-u:/home/user13$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

ben用戶多了 開頭的兩個 “/home/ben/bin:/usr/lib/scala/bin” 路徑——都是後來添加的。本發佈於博客園

 

使用 useradd 命令創建:-m參數

賬號 user13

此時建立的 home目錄 多了 3個文件:.bash_logout、.bashrc、.profile

此時,切換到 user13 執行 ll命令 沒有問題。

 

拷貝上面3個文件到 user12 的 home目錄,再看看效果:可以執行 ll 命令了

 

用戶的其它修改項

除了 密碼,用戶的其它項也可以修改(usermod、passwd)。

過期日期、密碼過期後無效。

主group、附屬groups。

鎖定、解鎖。

MIN_DAYS、WARN_DAYS、MAX_DAYS 等設置(passwd,參考資料#1)。本發佈於博客園

 

MIN_DAYS、WARN_DAYS、MAX_DAYS 示例:

 

刪除用戶:userdel

userdel --help:

操作比較簡單。

 

刪除上面創建的 user13:包括其 home目錄 和 spool郵件(-r)本發佈於博客園

/home/user13 也已經被刪除,用戶組(user13)也被刪除。

 

userdel 的 -f 選項會導致一些異常,儘量不使用它。下面的 man頁的信息:

-f, --force
This option forces the removal of the user account, even if the user is still logged in. 
It also forces userdel to remove the user's home directory and mail spool, even if another 
user uses the same home directory or if the mail spool is not owned by the specified user. 
If USERGROUPS_ENAB is defined to yes in /etc/login.defs and if a group exists with the same
name as the deleted user, then this group will be removed, even if it is still the primary
group of another user.

Note: This option is dangerous and may leave your system in an inconsistent state.

 

注,userdel 是底層的命令,在 Debian 系統中,提供了一個 deluser 的命令。

 

小結(1)

以上命令,除了 passwd外,都在 /usr/sbin 目錄下:本發佈於博客園

而 passwd 位於 /usr/bin 目錄下:

 

用戶組(group)操作

添加用戶組:groupadd

groupadd --help:

Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
      --extrausers              Use the extra users database

添加時可以:設置gid,設置 /etc/login.defs 中的 鍵的值,設置密碼,建立系統賬號等。本發佈於博客園

 

示例:新建組 g1

$ sudo groupadd g1
[sudo] password for ben:

# 創建成功
ben@ben-u:~$ cat /etc/group | grep g1
g1:x:1004:

 

修改用戶組:groupmod

groupmod - -help:本發佈於博客園

Usage: groupmod [options] GROUP

Options:
  -g, --gid GID                 change the group ID to GID
  -h, --help                    display this help message and exit
  -n, --new-name NEW_GROUP      change the name to NEW_GROUP
  -o, --non-unique              allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD       change the password to this (encrypted)
                                PASSWORD
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files

 

修改組名:g1 改爲 g1x

ben@ben-u:~$ sudo groupmod -n g1x g1
ben@ben-u:~$
ben@ben-u:~$ cat /etc/group | grep g1
g1x:x:1004:

 

刪除用戶組:groupdel

groupdel --help:

Usage: groupdel [options] GROUP

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -f, --force                   delete group even if it is the primary group of a user
      --extrausers              Use the extra users database

 

示例:刪除上面的 組 g1x

ben@ben-u:~$ sudo groupdel g1x
ben@ben-u:~$  cat /etc/group | grep g1
ben@ben-u:~$

 

示例:刪除 存在用戶 的 作爲 主用戶組的 user12。刪除失敗。本發佈於博客園

$ sudo groupdel user12
groupdel: cannot remove the primary group of user 'user12'

當然,加了 -f 參數 是可以刪除的,但會造成系統混亂。

 

小結(2)

疑問:

用戶組也可以有密碼?密碼用來做什麼呢?newgrp(log in to a new group)有什麼用呢?(參考資料#3 此功能因爲 sudo等工具的開發,已過時)

用戶的 主組、附件組 分別有什麼用呢?

Linux中的資源(文件)是怎麼和用戶組建立關係的呢?本發佈

於博客園

 

除了group開頭的3個命令,還可以使用 addgroup、delgroup 操作用戶組,兩者是符號鏈接,分別指向 adduser、deluser。

 

本文鏈接:

https://www.cnblogs.com/luo630/p/16997577.html

 

參考資料

1、passwd (用於更改用戶帳戶的密碼)

https://www.uc23.net/command/363.html

2、Linux 用戶和用戶組管理

https://www.runoob.com/linux/linux-user-manage.html

3、linux中什麼是組密碼

https://www.php.cn/linux-493486.html

4、

 

本發佈於博客園

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章