linux建立使用者的本質介紹,linux建立本質
ref: useradd命令的參數 /etc/default/useradd設定檔 /etc/skel目錄下的檔案 手動建立一個使用者
useradd命令的參數
[vincent@localhost ~]$ useradd --help-bash: /usr/sbin/useradd: Permission denied[vincent@localhost ~]$ sudo useradd --help[sudo] password for vincent:Usage: useradd [options] LOGIN useradd -D useradd -D [options]Options: -b, --base-dir BASE_DIR base directory for the home directory of the new account -c, --comment COMMENT GECOS field of the new account -d, --home-dir HOME_DIR home directory of the new account # 可以看出,這個參數可以列印添加使用者時預設的設定檔 -D, --defaults print or change default useradd configuration -e, --expiredate EXPIRE_DATE expiration date of the new account -f, --inactive INACTIVE password inactivity period of the new account -g, --gid GROUP name or ID of the primary group of the new account -G, --groups GROUPS list of supplementary groups of the new account -h, --help display this help message and exit -k, --skel SKEL_DIR use this alternative skeleton directory -K, --key KEY=VALUE override /etc/login.defs defaults -l, --no-log-init do not add the user to the lastlog and faillog databases -m, --create-home create the user's home directory -M, --no-create-home do not create the user's home directory -N, --no-user-group do not create a group with the same name as the user -o, --non-unique allow to create users with duplicate (non-unique) UID -p, --password PASSWORD encrypted password of the new account -r, --system create a system account -R, --root CHROOT_DIR directory to chroot into -s, --shell SHELL login shell of the new account -u, --uid UID user ID of the new account -U, --user-group create a group with the same name as the user -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping# 查看添加使用者時的預設配置[vincent@localhost ~]$ sudo useradd -DGROUP=100HOME=/homeINACTIVE=-1EXPIRE=SHELL=/bin/bashSKEL=/etc/skelCREATE_MAIL_SPOOL=yes[vincent@localhost ~]$ sudo useradd -D -s /sbin/nologin# 其實上面輸出的結果就是/etc/default/useradd檔案裡面的內容]$ sudo cat /etc/default/useradd# useradd defaults fileGROUP=100HOME=/homeINACTIVE=-1EXPIRE=SHELL=/sbin/nologinSKEL=/etc/skelCREATE_MAIL_SPOOL=yes
/etc/default/useradd設定檔
即useradd -D列印的輸出結果的來源
/etc/skel目錄下的檔案
此目錄下的檔案會在建立使用者時,將此目錄下的檔案複製至新建立使用者的家目錄下,前提是這個使用者有家目錄
分類樹如下
[vincent@localhost ~]$ sudo ls -la /etc/skel/total 24drwxr-xr-x. 2 root root 59 Mar 22 11:14 .drwxr-xr-x. 74 root root 8192 Mar 22 13:47 ..-rw-r--r--. 1 root root 18 Nov 20 2015 .bash_logout-rw-r--r--. 1 root root 193 Nov 20 2015 .bash_profile-rw-r--r--. 1 root root 231 Nov 20 2015 .bashr
手動建立一個使用者
1、建立使用者的家目錄
$ sudo mkdir /home/annie[sudo] password for vincent:
2、拷貝登入、登出使用的檔案於新使用者目錄
[vincent@localhost ~]$ sudo ls /etc/skel/ -a. .. .bash_logout .bash_profile .bashrc[vincent@localhost ~]$ sudo cp /etc/skel/.bash_logout /etc/skel/.bashrc /etc/skel/.bash_profile /home/annie/
3、修改相關的設定檔
/etc/passwd
$ grep annie /etc/passwdannie:x:1001:1001:vincent:/home/annie:/bin/bash
/etc/shadow
$ sudo grep annie /etc/shadow annie:$6$PiTiKh9B$Da4VaaR0qlk55r1VFyX3OfQfgj944xExTIdAsyVVTqjmf/u.On8MAfkV0B9RQg5E/OzWj2nYEHGnX.M8feXE3/:17612:0:99999:7:::
/etc/group
$ sudo grep annie /etc/group annie:x:1001:annie
4、修改密碼,登入測試
[vincent@localhost ~]$ sudo passwd annie Changing password for user annie.New password: BAD PASSWORD: The password is shorter than 8 charactersRetype new password: passwd: all authentication tokens updated successfully.[vincent@localhost ~]$ [vincent@localhost ~]$ [vincent@localhost ~]$ su - annie Password: Last failed login: Thu Mar 22 17:26:18 EDT 2018 on pts/1There was 1 failed login attempt since the last successful login.[annie@localhost ~]$ [annie@localhost ~]$ id uid=1001(annie) gid=1001(annie) groups=1001(annie)[annie@localhost ~]$ who am i vincent pts/1 2018-03-22 17:23 (172.16.81.1)[annie@localhost ~]$ id annie uid=1001(annie) gid=1001(annie) groups=1001(annie)[annie@localhost ~]$ logout