Linux系统账号管理

一、用户账号管理
1、用户账号概述
(1)用户分类
*超级用户、普通用户、程序用户
(2)UID:用户标示
(3)用户账号文件
1> /etc/passwd
*作用:保存用户名称、宿主目录、登录Shell等基本信息,每一行对应一个用户的账号
*共七各字段,个字段代表含义 :
[root@localhost ~]# tail -1 /etc/passwd
zhangwuji❌500:500::/home/zhangwuji:/bin/bash
2> /etc/shadow
*作用:保存用户的密码、账号有效期等信息,每一行对应一个用户的密码记录
*共九字段,目前只启用前7字段,个字段代表的含义:
root@localhost ~]# tail -1 /etc/shadow
zhangwuji:!!:17962:0:99999:7:::
2、添加用户账号useradd命令
(1)格式:useradd[选项] 用户名
(2)选项:-u -d -e -M -s -g -G -c
例:
[root@localhost ~]# groupadd groupl
[root@localhost ~]# mkdir -p /testgroupl
[root@localhost ~]# groupadd jiaoxue
[root@localhost ~]# useradd -d /testgroupl/tom/ -g groupl -G jiaoxue -s /bin/bash -e 2016-01-01 tom
[root@localhost ~]# passwd tom
更改用户 tom 的密码 。
新的 密码:
无效的密码: 过于简单化/系统化
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@localhost ~]# tail -1 /etc/passwd
tom❌501:501::/testgroupl/tom/:/bin/bash
[root@localhost ~]# tail -1 /etc/shadow
tom:$6$07yoRcjy$6O.yVk9jdJDvL4TY9am2g5Vlzw5rgDpoRWHZMuk7I2dSn3sBg2ZVOjZOvhen/xyzFfn15l9OGisHmnzAaBFhX0:17968:0:99999:7::16801:

3、用户账号的初始配置文件
(1)文件来源:新建用户账号时,从/etc/skel/目录复制而来
(2)主要的用户初始配置文件:
1>~/.bash_profile: 每次登录时执行
2>~/.bashrc: 每次进入新bash环境时执行
3>~/.bash_logout: 每次退出登录时执行
(3)进行修改后查看具体作用:
[root@localhost ~]# vi ~tom/.bash_profile
# .bash_profile
# Get the aliases and functions

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH= P A T H : PATH: PATH:HOME/bin
export PATH
echo “welcome tom!” 【增加此内容】
保存并退出

[root@localhost ~]# vi ~tom/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
echo “hi!this is a new bash!” 【增加此项】
保存并退出
[root@localhost ~]# vi ~tom/.bash_logout
~/.bash_logout
echo “byebye tom!” 【增加此项】
sleep 3
保存并退出
在虚拟机中新打开tyy2端口 以tom身份登录

4、设置/更改用户口令passwd命令
(1)格式:passwd[选项] 用户名
(2)常用选项
-d -l -S -u -x -n -w -i

[root@localhost ~]# passwd -l tom
锁定用户 tom 的密码 。
passwd: 操作成功
[root@localhost ~]# passwd -S tom
tom LK 2019-03-13 0 99999 7 -1 (密码已被锁定。)
[root@localhost ~]# passwd -u tom
解锁用户 tom 的密码 。
passwd: 操作成功
[root@localhost ~]# passwd -S tom
tom PS 2019-03-13 0 99999 7 -1 (密码已设置,使用 SHA512 加密。)
[root@localhost ~]# passwd -d tom
清除用户的密码 tom。
passwd: 操作成功
[root@localhost ~]# passwd -x 120 tom
调整用户密码老化数据tom。
passwd: 操作成功
[root@localhost ~]# grep tom /etc/shadow
tom::17968:0:120:7:::
[root@localhost ~]# passwd -n 1 tom
调整用户密码老化数据tom。
passwd: 操作成功
[root@localhost ~]# grep tom /tec/shadow
grep: /tec/shadow: 没有那个文件或目录
[root@localhost ~]# grep tom /etc/shadow
tom::17968:1:120:7:::
[root@localhost ~]# passwd -w 5 tom
调整用户密码老化数据tom。
passwd: 操作成功
[root@localhost ~]# grep tom /etc/shadow
tom::17968:1:120:5:::
[root@localhost ~]# passwd -i 7 tom
调整用户密码老化数据tom。
passwd: 操作成功
[root@localhost ~]# grep tom /etc/shadow
tom::17968:1:120:5:7::
5、修改用户属性usermod命令
(1)格式:usermod [选项] 用户名
(2)常用选项
-l -c -L -U
以下选项与useradd命令中的含义相同:-u -d -e -s -c

[root@localhost ~]# usermod -l tom1 tom
[root@localhost ~]# tail -1 /etc/passwd
tom1❌501:503::/home/tom:/bin/bash
[root@localhost ~]# usermod -c jiaoxue tom1
[root@localhost ~]# tail -1 /etc/passwd
tom1❌501:503:jiaoxue:/home/tom:/bin/bash
6、删除用户账号userdel -r tom1
(1)格式:userdel 用户名
(2)常用选项: -r

[root@localhost ~]# userdel -r tom1
[root@localhost ~]# ls /testgroupl

二、组账号管理
1、组长号概述
(1)组账号分类
基本组(私有)
附加组(公共)
(2)GID:组标识号
(3)组账号文件
1> /etc/group
2> /etc/gshadow
2、添加组账号groupadd命令
(1)格式:groupadd [-g GID] 组账号名
(2)选项
-g GID

[root@localhost ~]# tail -3 /etc/group
groupl❌501:
jiaoxue❌502:
tom❌503:
[root@localhost ~]# groupadd -g 888 market
[root@localhost ~]# tail -4 /etc/group
groupl❌501:
jiaoxue❌502:
tom❌503:
market❌888:
3、设置组账号密码,添加、删除组成员gpasswd命令
(1)格式:gpasswd[选项] 组账号
(2)选项
-a -d -M

[root@localhost ~]# useradd test1
[root@localhost ~]# useradd test2
[root@localhost ~]# useradd test3
[root@localhost ~]# gpasswd -a test1 market
Adding user test1 to group market
[root@localhost ~]# tail -4 /etc/group
market❌888:test1
test1❌889:
test2❌890:
test3❌891:
[root@localhost ~]# gpasswd -d test1 market
Removing user test1 from group market
[root@localhost ~]# tail -4 /etc/group
market❌888:
test1❌889:
test2❌890:
test3❌891:
[root@localhost ~]# gpasswd -M test1,test2,test3 market
[root@localhost ~]# tail -4 /etc/group
market❌888:test1,test2,test3
test1❌889:
test2❌890:
test3❌891:
[root@localhost ~]# gpasswd -M test1 market
[root@localhost ~]# tail -4 /etc/group
market❌888:test1
test1❌889:
test2❌890:
test3❌891:
(4)增加或删除组成员,也可以用vi编辑器对 /etc/gruop文件直接编译修改
4、删除组账号命令groupdel命令
(1)格式:groupdel组账号名

[root@localhost ~]# groupdel market
[root@localhost ~]# tail -5 /etc/group
jiaoxue❌502:
tom❌503:
test1❌889:
test2❌890:
test3❌891:

三、查询命令——id、groups、finger、w、whoami、who
1、id命令
(1)作用:查询用户身份标示
(2)格式:id [用户名]

[root@localhost ~]# id zhangwuji
uid=500(zhangwuji) gid=500(zhangwuji) 组=500(zhangwuji)
[root@localhost ~]# id
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
2、groups命令
(1)作用:查询客户所属的组
(2)格式: groups [用户名]

[root@localhost ~]# groups zhangwuji
zhangwuji : zhangwuji
[root@localhost ~]# groups
root
3、finger命令(需安装,)
(1)作用:查询账号的详细信息
(2)格式;finger [用户名]

安装yum后再安装finger
[root@localhost yum.repos.d]# yum -y install finger
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile

  • c6-media:
    Setting up Install Process
    Resolving Dependencies
    –> Running transaction check
    —> Package finger.x86_64 0:0.17-39.el6 will be installed
    –> Finished Dependency Resolution
    Dependencies Resolved
    =======================================================================
    Package Arch Version Repository Size
    =======================================================================
    Installing:
    finger x86_64 0.17-39.el6 c6-media 21 k
    Transaction Summary
    =======================================================================
    Install 1 Package(s)
    Total download size: 21 k
    Installed size: 27 k
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
    Installing : finger-0.17-39.el6.x86_64 1/1
    Verifying : finger-0.17-39.el6.x86_64 1/1
    Installed:
    finger.x86_64 0:0.17-39.el6
    Complete!
    [root@localhost yum.repos.d]# finger zhangwuji
    Login: zhangwuji Name:
    Directory: /home/zhangwuji Shell: /bin/bash
    Never logged in.
    No mail.
    No Plan.
    [root@localhost yum.repos.d]# finger
    Login Name Tty Idle Login Time Office Office Phone
    root root tty1 3:42 Mar 13 08:25 (:0)
    root root pts/0 3:40 Mar 13 08:26 (:0.0)
    root root pts/1 Mar 13 08:28 (172.16.23.11)
    4、w 命令
    (1)作用:查询已登录到主机的用户信息

    [root@localhost ~]# w
    11:42:54 up 3:18, 3 users, load average: 0.00, 0.00, 0.00
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    root tty1 :0 08:25 3:18m 5.39s 5.39s /usr/b
    root pts/0 :0.0 08:26 3:16m 0.02s 0.02s /bin/b
    root pts/1 172.16.23.11 08:28 0.00s 0.36s 0.26s w
    5、whoami
    (1)作用:查询当前登录的账号名

    [root@localhost ~]# whoami
    root
    6、who
    (1)作用:与w命令类似,查询已登录到主机的用户

    [root@localhost ~]# who
    root tty1 2019-03-13 08:25 (:0)
    root pts/0 2019-03-13 08:26 (:0.0)
    root pts/1 2019-03-13 08:28 (172.16.23.11)

实验补充:
1、命令的输写注意事项:字母、符号 之间的间隔;数字1与小写字母l的区分
2、在虚拟机黑色界面用户登录失败的原因:
(1)用户或者密码错误;
(2) 用户失效,需要重新创建账户和修改密码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值