Linux 账户管理和ACL
1. /etc/passwd /etc/shadow的档案结构
在/etc/passwd里,每一行都代表一个帐号,有几行就代表有几个帐号在你的系统中!不过需要特别留意的是,里头很多帐号本来就是系统正常运作所必须要的,我们可以简称他为系统帐号,例如bin, daemon, adm, nobody等等。
每一行使用『:』分隔开,各7列代表:
帐号名称:密码:UID:
GID:使用者信息:Home目录:
Shell
UID 为0 的时候,就是root; 1~999 为系统账户;1000~60000 为可登入账号。
GID:组ID,这个与/etc/group有关
【NOTE】
密码是存储在另一个文档中:/etc/shadow
当然也不可能是明文。(加密方式可以通过命令 authconfig –test | grep hashing 查询)
文档也是9列,各列代表:
账户名称:密码:最近改密时间:
密码不可被更动的天数:密码需要重新变更的天数:密码需要变更期限前的警告天数:
密码过期后的帐号宽限时间(密码失效日):帐号失效日期:保留
如果忘记root密码,有以下途径修改:
- 重新开机进入单人维护模式后,系统会主动的给予root权限的bash介面,此时再以passwd修改密码即可
- 或以Live CD开机后挂载根目录去修改/etc/shadow,将里面的root的密码栏位清空,再重新开机后root将不用密码即可登入!登入后再赶快以passwd指令去设定root密码即可。
2. 一些命令
- groups : 能查看当前用户属于哪些组
- id: 查询某人或自己的相关UID/GID 等等的资讯
- passwd: 改密码
- chage: 能让使用者在第一次登录的时候,强制他们更改密码。chage -d 0 newUser
- useradd: 加用户
- userdel: 删用户,加-r表示将目录也删掉
- usermod: 帐号相关资料的微调
- id: 查看相关UID/GID
- finger: 可以查阅很多使用者相关的信息
- chfn: change finger
- chsh: change shell. -l 列出选项; -s 设定内容
- w : 查询目前的使用者
- who : 同上
- lastlog: 查询每个帐号的最近登入的时间
3. Access Control List
ACL 主要的目的是在提供传统的 owner,group,others 的read,write,execute 权限之外的细部权限设定.
- getfacl:取得某个档案/目录的ACL 设定项目;
- setfacl:设定某个目录/档案的ACL 规范。
4. 使用者对谈: write, mesg, wall
可以跟系统上面的使用者谈天说地。。。
举例来说,我们的Linux 目前有vbird1 与root 两个人在线上, 我的root 要跟vbird1 讲话,可以这样做:
[root@study ~]# write使用者帐号[使用者所在终端介面]
[root@study ~]# who
vbird1 tty3 2015-07-22 01:55 <==有看到vbird1在线上
root tty4 2015-07-22 01:56
[root@study ~]# write vbird1 pts/2
Hello, there:
Please don't do anything wrong... <==这两行是root写的资讯!
#结束时,请按下[ctrl]-d来结束输入。此时在vbird1的画面中,会出现:
Message from root@study.centos.vbird on tty4 at 01:57 ...
Hello, there:
Please don't do anything wrong...
EOF
不过……当时vbird1 正在查资料,哇!这些讯息会立刻打断vbird1 原本的工作喔!所以,如果vbird1 这个人不想要接受任何讯息,直接下达这个动作:
[vbird1@study ~]$ mesg n # message get? no!
[vbird1@study ~]$ mesg
is n
不过,这个mesg 的功能对root 传送来的讯息没有抵挡的能力!所以如果是root 传送讯息, vbird1 还是得要收下。但是如果root 的mesg 是n 的,那么vbird1 写给root 的资讯会变这样:
[vbird1@study ~]$ write root
write: root has messages disabled
如果想要解开的话,再次下达『 mesg y 』就好啦!
想『对所有系统上面的使用者传送简讯(广播)』如何下达?用wall 即可!他的语法也是很简单的喔!
[root@study ~]# wall "I will shutdown my linux server..."
5. 批量建立账户的脚本:
#!/bin/bash
# This shell script will create amount of linux login accounts for you.
# 1. check the "accountadd.txt" file exist? you must create that file manually.
# one account name one line in the "accountadd.txt" file.
# 2. use openssl to create users password.
# 3. User must change his password in his first login.
# 4. more options check the following url:
# http://linux.vbird.org/linux_basic/0410accountmanager.php#manual_amount
# 2015/07/22 VBird
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# 0. userinput
usergroup="" # if your account need secondary group, add here.
pwmech="openssl" # "openssl" or "account" is needed.
homeperm="no" # if "yes" then I will modify home dir permission to 711
# 1. check the accountadd.txt file
action="${1}" # "create" is useradd and "delete" is userdel.
if [ ! -f accountadd.txt ]; then
echo "There is no accountadd.txt file, stop here."
exit 1
fi
[ "${usergroup}" != "" ] && groupadd -r ${usergroup}
rm -f outputpw.txt
usernames=$(cat accountadd.txt)
for username in ${usernames}
do
case ${action} in
"create")
[ "${usergroup}" != "" ] && usegrp=" -G ${usergroup} " || usegrp=""
useradd ${usegrp} ${username} #新增帐号
[ "${pwmech}" == "openssl" ] && usepw=$(openssl rand -base64 6) || usepw=${username}
echo ${usepw} | passwd --stdin ${username} #建立密码
chage -d 0 ${username} #强制登入修改密码
[ "${homeperm}" == "yes" ] && chmod 711 /home/${username}
echo "username=${username}, password=${usepw}" >> outputpw.txt
;;
"delete")
echo "deleting ${username}"
userdel -r ${username}
;;
*)
echo "Usage: $0 [create|delete]"
;;
esac
done
保存为 accountadd.sh, 在同目录下创建accountadd.txt文档,每个账户一行。
创建指令:
sh accountadd.sh create
账户和密码会保存在同目录下的outputpw.txt里。