Red Hat Linux System Administrator Unit 5 - User Administration

學習目標

Adding a New User Account

新增帳號,密碼預設用MD5加密存在 /etc/shadow

# adduser  smallken

增加帳號系統自動做了以下幾件事情

1.編輯 /etc/passwd, /etc/shadow, /etc/group, /etc/gshadow

2.產生家目錄,預設家目錄內容從 /etc/skel/ COPY 而來

3.設定給使用者家目錄的權限

增加帳號後,接著要幫使用者設定密碼

# passwd smallken

newusers,大量新增使用者指令

User Private Groups

新增使用者至少要加入一個群組

private group,使用者的群組名稱與自己的使用者名稱相同,如 RedHat Linux

public group,使用者的群組名稱共同一個群組,如SuSe Linux,使用者共用 users 群組

Modifying/Deleting User Accounts

修改# usermod [option] username

-c,修改 comment

-d,修改家目錄

-e,修改到期日

-g,修改主要 group

-G,修改次要 group

-l,修改使用者名稱

-s,修改 sheel

-u,修改uid,系統只認uid,小心系統不認得

-p,修改密碼

-L,鎖住帳號

-U,解除帳號

刪除# userdel -r username,-r 包含目錄也一併刪除

Group Administration

相關檔案 /etc/group and /etc/gshadow

# groupadd group1,增加群組

# groupdel group1,刪除群組

# groupmod -n newname oldname,更改群組名稱

Password Aging Policies

預設密碼期限無限長,設定檔 /etc/login.defs

chage,設定使用者的密碼使用期限

# chage [option] username

-m,最少天之內不能再改密碼

-M,密碼最多能用多少天

-l,列出設定值

Switching Account

# su [-],登入成 root

# su [-] [user],- shell 環境重新設定

# su [-] [user] -c command

一般使用者之間切換需要輸入密碼,但 root 可以不需要密碼切換成任何人

sudo

# visudo,控管誰在從哪一台機器可以做什麼事? 設定檔 /etc/sudoers

# user    MACHINE=COMMANDS

#%users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

   root    ALL=(ALL)       ALL

Network Users

使用帳號控管的Server登入,NIS,LDAP,SMB...

GUI 設定 system-config-authentication

TUI(文字介面)設定 authconfig-tui

NIS Configuration: 需要的套件 ypbind and portmap RPMs,給 NIS Server、domain name

LDAP Configuration: 需要的套件 nss-ldap and openldap RPMs,給 LDAP Server、DN、TLS

SUID , SGID and Sticky Bit

SUID,# chmod u+s filename,執行該檔案時可以暫時擁有該檔案擁有者的權限,ex. -rwsr-xr-x 1 root root 22984  1月  7  2007 /usr/bin/passwd

SGID,# chmod g+s filename,執行該檔案時可以暫時擁有該檔案群組擁有者的權限

SGID,# chmod g+s directory,目錄設定了SGID時,任何user新建檔案的group會比照目錄的group擁有者

The Sticky Bit,# chmod o+t /home/share,該目錄內的檔案只有自己的才能刪,ex.drwxrwxrw 3 root root  4096  2月  3 03:21 tmp

修改檔案,看檔案的w權限

刪除檔案,需要有該目錄的w跟x權限

該目錄是Sticky Bit 時,刪除檔案,需要有該目錄的w跟x跟file的owner

Default File Permissions

原來產生檔的權限666,原來產生目錄的權限777

umask,遮掉權限,產生預設檔案的權限,ex.# umask 022,產生預設檔案的權限644,產生預設目錄的權限755

Access Control Lists(ACLs)

舊有rwx權限控管有其限制,ACLs是為了增加更大的權限控管的彈性而設計

# mount -o acl /directory,mount 檔案系統時要支援

# getfacl file/directory,得到權限內容

# setfacl -m u:gandolf:rwx file|directory,用 -m 設定user gandolf 有 rwx 的權限

# setfacl -m g:nazgul:rw file|directory,用 -m 設j定group nazgul 有rw的權限

# setfacl -m d:u:frodo:rw directory,d 為 default,子目錄自動繼承

# setfacl -x u:samwise file|directory,刪除權限

# setfacl -b file/directory,清空ACLs

# getfacl file

# file: file

# owner: root

# group: root

user::rw-

user:smallken:rw-

group::r--

mask::rw- ← 權限不能超過 mask 這個值

other::r--

SELinux

透過 kernel 強行去限制什麼人對什麼樣的檔案有什麼權限?

# ls -Z,查看檔案SELinux的資訊

# ps -Z,查看行程SELinux的資訊

user:role:type:sensitivity:category(可有可無)

system_u:object_r:user_home_t:s0

# chcon -t tmp_t /etc/hosts,設定type為tmp_t

# restorecon /etc/hosts,回復預設值

SELinux: Management

# vi /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#       enforcing - SELinux security policy is enforced.(運作模式)

#       permissive - SELinux prints warnings instead of enforcing.(監看模式,必要可以修改)

#       disabled - SELinux is fully disabled.

SELINUX=disabled

# SELINUXTYPE= type of policy in use. Possible values are:

#       targeted - Only targeted network daemons are protected.

#       strict - Full SELinux protection.

SELINUXTYPE=targeted

# getenforce,取得設定 Enforcing | Permissive

# setenforce 0|1,設定 0 or 1

usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]

Lab 5 重點

# echo 'umask 002' >> /etc/skel/.bashrc

for USER in joshua alex dax bryan zak ed manager

do

   useradd $USER

   echo passwd | passwd --stdin $USER

done

# groupadd -g 200 sales

# groupadd -g 201 hr

# groupadd -g 202 web

# usermod -G sales joshua

# usermod -G sales alex

# usermod -G hr dax

# usermod -G hr bryan

# usermod -G web zak

# usermod -G web ed

# usermod -G sales,hr,web manager

# id alex,查看使用者uid,gid,groups

# mkdir -p /depts/{sales,hr,web}

for GROUP in sales hr web

do

chgrp $GROUP /depts/$GROUP

done

# chmod 770 /depts/*

# chmod g+s /depts/*

目前joshua,alex,dax,bryan,zak,ed都可以在各自的群組目錄裡增加檔案,manager則三個目錄都可以增加檔案

# chown root:hr /depts/tech/,更改owner

# chmod  2770 /depts/tech/,設SGID

# setfacl -m g:web:rwx /depts/tech/,用ACL把web group設在tech目錄

# setfacl -m u:alex:rx /depts/tech/

# setfacl -m d:u:alex:rw /depts/tech/,給預設的ACL

# su - joshua

# touch /depts/tech/test

touch: cannot touch ‘/depts/tech/test’: 拒絕不符權限的操作,joshua 沒有權限在目錄裡產生檔案

# su - manager

$ touch /depts/tech/manager 因為manager是web這個群組的人,所以可以新增檔案

$ getfacl /depts/tech/manager 查看新增檔案的權限內容, group 被 mask 限制在 rw-

getfacl: Removing leading '/' from absolute path names

# file: depts/tech/manager

# owner: manager

# group: hr

user::rw-

user:alex:rw-

group::rwx                      #effective:rw-

mask::rw-

other::---

# yum -y install httpd

# service httpd start

# elinks -dump http://localhost,瀏覽網頁

# setenforce 1,啟用SELinux

# getenforce

Enforcing

# echo "home directory" > /root/home.html,產生一個內容為"home directory" 的網頁在家目錄中

# echo "tmp directory" >  /tmp/tmp.html,產生一個內容為"tmp directory"的網頁在/tmp目錄中

# ls -Z /root/home.html

-rw-r--r--  root root root:object_r:user_home_t:s0     /root/home.html

# ls -Z /tmp/tmp.html

-rw-r--r--  root root root:object_r:tmp_t:s0           /tmp/tmp.html

# elinks -dump http://localhost/home/html,沒有權限可以瀏覽網頁(home.html的文件type不對)

Forbidden

   You don't have permission to access /home.html on this server.

# setenforce 0,取消 SELinux

# elinks -dump http://localhost/home.html,關掉SELinux之後,出現網頁了

   home directory

# setenforce 1

# cp /tmp/tmp.html /var/www/html/

# ls -Z /var/www/html/

-rw-r--r--  root root root:object_r:user_home_t:s0     home.html

-rw-r--r--  root root root:object_r:httpd_sys_content_t:s0 tmp.html

# elinks -dump http://localhost/tmp.html,雖然SELinux打開,但是因為文件的type對了,所以可以看見網頁

   tmp directory

# restorecon /var/www/html/home.html,使用restorecon指令重新設定好type

-rw-r--r--  root root system_u:object_r:httpd_sys_content_t:s0 home.html

-rw-r--r--  root root root:object_r:httpd_sys_content_t:s0 tmp.html

# elinks -dump http://localhost/home.html,type設對了之後網頁就可以正常打開了

   home directory

設定 NIS authentication

必要套件: portmap,ypbind,yp-tools,authconfig,authconfig-gtk

1. # authconfig-tui

2. enable NIS

3. 設domain

4. 設Server

By SmallKen