Linux用戶只有兩個等級:root及非root。Linux中還有一部分用戶,如:apache、mysql、nobody、ftp等,這些也都是非root用戶,即普通用戶。Linux的權限實際是上不同用戶所能訪問的文件的不同產生的假象。而這些假象的造成,還要涉及到另外一個概念:用戶組
· 一個用戶至少要屬於一個用戶組
· 一個用戶可以屬於多個用戶組
用戶組存在的原因主要還是方便分配權限。而用戶本身和權限的差別不是很大,各個用戶之間主要的不同是:
· 是否擁有密碼
· home目錄(普通用戶可以有一個以自己用戶名命名的home目錄,存放的地址是/home/username,root用戶的home目錄是:/root)
· shell
像nobody這樣用來執行Nginx的工作進程的用戶,一般不分配密碼和shell,甚至連home目錄都沒有。
爲什麼不分配密碼?如果設置了密碼,程序無法自動使用。由於不會有人使用這個用戶登錄系統,所以就沒有必要分配shell。(備註:其實嚴格上說是有分配shell,只是分配的shell是/sbin/nologin這個特殊的shell,沒有任何其他功能,主要功能是防止你登陸。)
所有用戶都可以通過查看/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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:AvahiIPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemdBus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemdNetwork Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:997:995:Userfor polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsddaemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
qw:x:1000:1000:qw:/home/qw:/bin/bash
文件的每一行代表着一個用戶,每一行由冒號“:”分割成7個字段,其結構如下:
用戶名:密碼:UID:GID:用戶全名:home目錄:shell
UID:
· UID 0 root用戶
· UID 1~999 是佔坑用戶,即一寫無法登錄的用戶(以前是系統是1~499,最近剛改)
· UID 1000 以上是正常的可登錄用戶
GID:前面說了一個用戶可以屬於多個用戶組,但這裏只有一個,表示的是專職用戶組,即一個用戶只有一個專職用戶組,其屬於其他用戶組的關聯關係存儲在/etc/group 文件中。
其中比較特殊的是密碼字段,統一由x代替了,看/etc/passwd就知道一開始Linux是將密碼存在這個文件裏的,由於考慮到/etc/passwd可以被所有人查看,所以將統一存儲到/etc/shadow文件(只有root權限可以訪問)中,具體數據如下:
root:$6$yp9k7rTFRX76aFP/$RpQ0Q6SIFpKn5jA3vEmpzsMdMDqDjmKQY3qCcVyuVNa1dTOOwJqC0SWCxqsNrZgYiBf5Og94cDR97hGCfd0OX1::0:99999:7:::
bin:*:16659:0:99999:7:::
daemon:*:16659:0:99999:7:::
adm:*:16659:0:99999:7:::
lp:*:16659:0:99999:7:::
sync:*:16659:0:99999:7:::
shutdown:*:16659:0:99999:7:::
halt:*:16659:0:99999:7:::
mail:*:16659:0:99999:7:::
operator:*:16659:0:99999:7:::
games:*:16659:0:99999:7:::
ftp:*:16659:0:99999:7:::
nobody:*:16659:0:99999:7:::
avahi-autoipd:!!:17031::::::
systemd-bus-proxy:!!:17031::::::
systemd-network:!!:17031::::::
dbus:!!:17031::::::
polkitd:!!:17031::::::
tss:!!:17031::::::
postfix:!!:17031::::::
sshd:!!:17031::::::
qw:$6$W1QYF2/.vMB7BPm/$n7GV1D07lcVDhsyGS0xUe68loRTfmFj2vT4TkELQgQGfqDxNn2fGzBYUYj4CgZMhaiNSvvmhW4gubpQxBhr3H0::0:99999:7:::
其結構如下:
登錄名:加密過的密碼(*代表此用戶不能用來登錄):密碼最近更改日期(linux時間戳):最少密碼天數(0代表隨時可更改):最多密碼天數:過期前幾天提醒用戶:密碼不可用期限:賬戶過期日期:保留位
再來看看/etc/group文件:
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
utmp:x:22:
utempter:x:35:
ssh_keys:x:999:
avahi-autoipd:x:170:
input:x:998:
systemd-journal:x:190:
systemd-bus-proxy:x:997:
systemd-network:x:996:
dbus:x:81:
polkitd:x:995:
dip:x:40:
tss:x:59:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
qw:x:1000:qw
其結構如下:
組名:用戶組密碼:GID:用戶組內的用戶名
正常的使用中很少會用到用戶組密碼,其存儲在/etc/gshadow中。
用戶組文件比較特特殊的是“”用戶組內的用戶名”,其實就是這個組下的用戶列表,每個用戶之間用逗號“,”分割;本字段可以爲空;如果字段爲空表示用戶組爲GID的用戶名
普通用戶的權限非常的低,就連在系統裏安裝軟件的權限都沒有,很多時候可以臨時給普通用戶以特權,就是sudo(在命令前添加sudo)。比如:
sudo cat /etc/shadow
完成後需要您輸入root的密碼,這樣就可以假借root身份了,centos默認普通用戶是沒有sudo權限的,這與主要以桌面版爲主的Ubuntu和Fedora不同,如需給予用戶root特權,就需要更改/etc/sudoers文件,修改內容。
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections ## ofrelated commands, which can then be delegated out to particular## users orgroups.
##
## This file must be edited with the 'visudo' command.
## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias FILESERVERS = fs1, fs2
# Host_Alias MAILSERVERS = smtp, smtp2
## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem
## Command Aliases
## These are groups of related commands...
## Networking
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping,/sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial,/sbin/iwconfig, /sbin/mii-tool
## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
## Services
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctlstart, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctlrestart, /usr/bin/systemctl status, /usr/bin/systemctl enable,/usr/bin/systemctl disable
## Updating the locate database
# Cmnd_Alias LOCATE = /usr/bin/updatedb
## Storage
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted,/sbin/partprobe, /bin/mount, /bin/umount
## Delegating permissions
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp
## Processes
# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
## Drivers
# Cmnd_Alias DRIVERS = /sbin/modprobe
# Defaults specification
#
# Disable "ssh hostname sudo <cmd>", because it will show thepassword in clear.
# You have to run "ssh -t hostname sudo<cmd>".
#
Defaults requiretty
#
# Refuse to run if unable to disable echo on the tty. This setting should alsobe # changed in order to be able to use sudo without a tty. Seerequiretty above.
#
Defaults !visiblepw
#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults always_set_home
Defaults env_reset
Defaults env_keep = "COLORSDISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIRUSERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATELC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARYLC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALLLANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME" Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
## Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere root ALL=(ALL) ALL
## Allows members of the 'sys' group to run networking, software, ## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES,LOCATE, DRIVERS ## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
給沒有用戶添加sudo特權,只需參照
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
添加如下:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
qw ALL=(ALL) ALL
如果要給某個用戶組添加sudo特權則爲:(與給用戶不同的是多了一個%)
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
另外一種方式是添加不需要輸入root密碼即有root權限的用戶,添加方法如下:
qw ALL=(ALL) NOPASSWD:ALL
另外還可以設定到底有哪些執行權限,具體的規則如下:(具體可看sudoers配置文件詳解)
授權用戶主機=[(切換到哪些用戶或用戶組)] [是否需要密碼驗證] 命令1,[(切換到哪些用戶或用戶組)] [是否需要密碼驗證] [命令2],[(切換到哪些用戶或用戶組)] [是否需要密碼驗證] [命令3]......
另外默認情況下第一次使用sudo時,需要輸入root密碼,如果5分鐘內再次執行sudo則無需再輸入密碼,超過5分鐘則要重新輸入。這個時間也是可以進行配置的,在sudoers中添加如下內容即可:
ts:用戶名timestamp_timeout=20
其中單位是分鐘,如果設爲0,則表示每次都要輸入密碼。
講解了這麼多,接下來學習下常用的shell命令:
useradd命令用於Linux中創建的新的系統用戶。useradd可用來建立用戶帳號。帳號建好之後,再用passwd設定帳號的密碼.而可用userdel刪除帳號。使用useradd指令所建立的帳號,實際上是保存在/etc/passwd文本文件中。
語法: useradd(選項)(參數)
選項:
-c<備註>:加上備註文字。備註文字會保存在passwd的備註欄位中
-d<登入目錄>:指定用戶登入時的啓始目錄
-D:變更預設值
-e<有效期限>:指定帳號的有效期限
-f<緩衝天數>:指定在密碼過期後多少天即關閉該帳號
-g<羣組>:指定用戶所屬的羣組
-G<羣組>:指定用戶所屬的附加羣組
-m:自動建立用戶的登入目錄
-M:不要自動建立用戶的登入目錄
-n:取消建立以用戶名稱爲名的羣組
-r:建立系統帳號
-s:指定用戶登入後所使用的shell
-u:指定用戶id
案例:
sudo useradd username -m -s /sbin/nologin -d /home/username -g groupname
其中:
· -s /sbin/nologin 設置不能登陸
· -d 設置用戶主目錄
· -g 用戶組
· -m 創建用戶目錄