Linux基礎之用戶管理

  • 2019 年 10 月 6 日
  • 筆記

今天給大家介紹的是linux基本的用戶(usr)管理和組(group)管理。

在linux中,任何一個要使用系統資源的用戶,都必須首先向系統管理員(root)申請一個帳號,然後以這個帳號的身份進入系統。

用戶的帳號一方面可以幫助系統管理員對使用系統的用戶進行跟蹤,並控制他們對系統資源的訪問;另一方面也可以幫助用戶組織文件,並為用戶提供安全性保護。

在linux中擁有最高層理員許可權的賬戶為root,root帳號是其他所有帳號的基礎,首先,在有了root根帳號的情況下,我們想要創建我們自己的個人帳號就需要用到以下指令:

useradd 參數 用戶名

關於參數,我們這邊介紹一下,有哪些常用參數:

-c comment 指定一段注釋性描述。

-d 目錄 指定用戶主目錄。

-g 用戶組 指定用戶所屬的用戶組。

-u 用戶號 指定用戶的用戶號。

useradd -d /usr/john -m john

創建了一個用戶john,其中-d和-m選項用來為登錄名john產生一個主目錄/usr/john

這是添加帳號,下面我們來看看刪除帳號,指令:

userdel 參數 用戶名

我們一般會用到的參數是-r ,遞歸刪除,把它所在的目錄也刪除掉。

試試手:

userdel -r john

此命令刪除用戶sam在系統文件中(主要是/etc/passwd, /etc/shadow, /etc/group等)的記錄,同時刪除用戶的主目錄

下面講一下修改帳號

修改用戶帳號就是根據實際情況更改用戶的有關屬性,如用戶號、主目錄、用戶組、登錄Shell等。

修改已有用戶的資訊使用usermod命令:

usermod 參數 用戶名

常用的選項包括-c, -d, -m, -g, -G, -s, -u以及-o等,這些選項的意義與useradd命令中的選項一樣,可以為用戶指定新的資源值。

另外,有些系統可以使用選項:-l 新用戶名

(有興趣的都讀一下,所有的參數都在這裡了

這個選項指定一個新的帳號,即將原來的用戶名改為新的用戶名。

-c, –comment COMMENT new value of the GECOS field

-d, –home HOME_DIR new home directory for the user account

-e, –expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE

-f, –inactive INACTIVE set password inactive after expiration

to INACTIVE

-g, –gid GROUP force use GROUP as new primary group

-G, –groups GROUPS new list of supplementary GROUPS

-a, –append append the user to the supplemental GROUPS

mentioned by the -G option without removing

him/her from other groups

-h, –help display this help message and exit

-l, –login NEW_LOGIN new value of the login name

-L, –lock lock the user account

-m, –move-home move contents of the home directory to the

new location (use only with -d)

-o, –non-unique allow using duplicate (non-unique) UID

-p, –password PASSWORD use encrypted password for the new password

-R, –root CHROOT_DIR directory to chroot into

-s, –shell SHELL new login shell for the user account

-u, –uid UID new UID for the user account

-U, –unlock unlock the user account

-v, –add-subuids FIRST-LAST add range of subordinate uids

-V, –del-subuids FIRST-LAST remove range of subordinate uids

-w, –add-subgids FIRST-LAST add range of subordinate gids

-W, –del-subgids FIRST-LAST remove range of subordinate gids

-Z, –selinux-user SEUSER new SELinux user mapping for the user account

試試手:

usermod -d /home/z yuqing

把用戶所在的主目錄改為/home/z

usermod -g yuqingwu yuqing

把用戶所在的組改成yuqingwu

對賬戶的增刪改我們已經介紹完畢了,現在我們來看看,口令管理(這裡的口令就是我們所指的帳號密碼,這邊我們把它叫做口令)

我們剛剛創建用戶的時候沒有口令,會被系統鎖定,無法使用,必須為其指定口令後才可以使用,即使是指定空口令。

指定和修改用戶口令的Shell命令是passwd。超級用戶可以為自己和其他用戶指定口令,普通用戶只能用它修改自己的口令。命令的格式為:

passwd 參數 用戶名

可使用的參數:

-l 鎖定口令,即禁用帳號。

-u 口令解鎖。

-d 使帳號無口令。

-f 強迫用戶下次登錄時修改口令。

我們看看系統還提供了那些命令參數

(自行翻譯,經常用的我已經列出來了,下面是系統提供的所有的參數,感興趣的都了解一下)

Usage: passwd [options] [LOGIN]

Options:

-a, –all report password status on all accounts

-d, –delete delete the password for the named account

-e, –expire force expire the password for the named account

-h, –help display this help message and exit

-k, –keep-tokens change password only if expired

-i, –inactive INACTIVE set password inactive after expiration

to INACTIVE

-l, –lock lock the password of the named account

-n, –mindays MIN_DAYS set minimum number of days before password

change to MIN_DAYS

-q, –quiet quiet mode

-r, –repository REPOSITORY change password in REPOSITORY repository

-R, –root CHROOT_DIR directory to chroot into

-S, –status report password status on the named account

-u, –unlock unlock the password of the named account

-w, –warndays WARN_DAYS set expiration warning days to WARN_DAYS

-x, –maxdays MAX_DAYS set maximum number of days before password change to MAX_DAYS

現在我們來改一下yuqing這個用戶的口令(密碼):

passwd yuqing

為yuqing這個用戶設置口令(密碼)

TIPS:在linux中,所有的輸入密碼都是看不到的,所以別人看不到你輸入的密碼有多少位,是多少,安全性比較強。

我現在是處在root用戶下面,所以可以直接進行設置修改,如果是普通用戶,

他們在修改自己的口令時,passwd命令會先詢問原口令,驗證後再要求用戶輸入兩遍新口令,如果兩次輸入的口令一致,則將這個口令指定給用戶;而超級用戶(root)為用戶指定口令時,就不需要知道原口令。

為用戶指定空口令時,執行下列形式的命令:

passwd -d yuqing

其實嚴格來說,為了系統安全起見,用戶應該選擇比較複雜的口令,例如最好使用8位長的口令,口令中包含有大寫、小寫字母和數字。

下面,我們可以禁用yuqing這個帳號

passwd -l yuqing

這樣yuqing這個帳號就被鎖住的,我們就不可以對其進行訪問了。

這樣關於用戶的介紹就結束了,有興趣的可以登陸網站http://man.linuxde.net/了解一下。

下面我們來看看組(group)的管理:

每個用戶都有一個用戶組,系統可以對一個用戶組中的所有用戶進行集中管理。不同Linux 系統對用戶組的規定有所不同,如Linux下的用戶屬於與它同名的用戶組,這個用戶組在創建用戶時同時創建。

用戶組的管理涉及用戶組的添加、刪除和修改。組的增加、刪除和修改實際上就是對/etc/group文件的更新。

先來添加組吧,需要用到的指令是:

groupadd 參數 組名

可以使用的參數有:

-g GID 指定新用戶組的組標識號(GID)。

-o 一般與-g選項同時使用,表示新用戶組的GID可以與系統已有用戶組的GID相同。

我們來看看系統裡面還有什麼其他的參數:

(自行翻譯,如果真的英語不行,請告訴我,我給你有翻譯的網址,謝謝,不客氣!)

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

–extrausers Use the extra users database

試試:

groupadd yuqing2

向系統中增加了一個新組yuqing2

group -g 201 yuqing3

此命令向系統中增加了一個新組yuqing3,同時指定新組的組標識號是201。

下面我們再來看看刪除一個組:

刪除組其實很簡單:

groupdel 組名

都不用加參數的,我們來試試:

這樣就刪除我們剛剛新建的group yuqing2和yuqing3了。

接下來我們看看修改組屬性,這個時候我們用到的指令是:

groupmod 參數 用戶組

常用的參數有:

-g GID 為用戶組指定新的組標識號。

-o 與-g選項同時使用,用戶組的新GID可以與系統已有用戶組的GID相同。

-n新用戶組 將用戶組的名字改為新名字

下面我們來看看系統里還提供了什麼參數:

(自行翻譯)

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

現在我們要把group yuqing的組標識改成404:

groupmod -g 404 yuqing

同時修改組標識為202和重命名成yuqingjohn

groupmod -g 202 -n yuqingjohn yuqing

最後還有個切換問題:

如果一個用戶同時屬於多個用戶組,那麼用戶可以在用戶組之間切換,以便具有其他用戶組的許可權。

用戶可以在登錄後,使用命令newgrp切換到其他用戶組,這個命令的參數就是目的用戶組。例如:

newgrp root

這條命令將當前用戶切換到root用戶組,前提條件是root用戶組確實是該用戶的主組或附加組。

今天就講到這裡了。我們下次再見吧!