User and group

本文围绕Linux系统中用户与组管理展开。介绍了Linux安全上下文,说明进程资源访问与用户的关系。阐述了用户组类型,列举了用户和组相关的配置文件,还详细介绍了用户和组的添加、修改、删除等命令,如useradd、groupadd等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. linux security context
    Which resources a process can access depends on the user running the process.
    For example,the root user own the following file.
        -rw-------. 1 root root 1920 Aug 25 21:05 anaconda-ks.cfg
        But we want to view the file with the cat command in other users
            The owner of the file is root, so we can not access the file with the cat command by its permission of owner
            The group of the file is root, so we can not access the file with the cat command by its permission of group.
            So we can access the file by other permission.
            --------------This is linux security context
2. The type of linux group:
    Basic group of users(the main group):
        The basic group has the same name as user:private group
    Additional group of users:
3. The configuration files related to users and the group of users:
    /etc/passwd:users and their attributes infomations(name、UID、GID);
        name:password:UID:GID:GECOS:directory:shell
        user name:passwd:UID:GID:GECOS:home directory:default shell
    /etc/group:groups and their attributes infomations;
        group_name:password:GID:user_list
        group name:gpasswd:GID:the user list in which users use current group as their additional group (seperated by comma).
    /etc/shadow:用户密码及其相关属性;
        用户名:加密了的密码:最近一次更改密码的日期:密码的最小使用期限:最大密码使用期限:密码警告时间段:密码禁用期:账户过期日期:保留字段
    /etc/gshadow:组密码及其相关属性;
4. 用户相关命令
    4.1 useradd : 添加用户
        [root@promote ~]# useradd user1
            上面这句命令执行后,/etc/passwd /etc/group /etc/shadow /etc/gshadow这四个文件中都添加了user1相应的内容
        -u 指明UID: 范围在[UID_MIN, UID_MAX], 而这两个值定义在/etc/login.defs
        -g GID:指明用户所属基本组,可为组名,也可以GID;
        -c "COMMENT":用户的注释信息(也就是GECOS);
        -d /PATH/TO/HOME_DIR: 以指定的路径为家目录;
        -s SHELL: 指明用户的默认shell程序,可用列表在/etc/shells文件中;
        -G GROUP1[,GROUP2,...[,GROUPN]]]:为用户指明附加组;组必须事先存在;
        -r: 创建系统用户
            CentOS 6: ID<500
            CentOS 7: ID<1000
        默认值设定:/etc/default/useradd文件中
            useradd -D
                -s SHELL
5. 组相关命令
    5.1 groupadd : 添加组
        -g GID: 指明GID号;[GID_MIN, GID_MAX] 而这两个值定义在/etc/login.defs
        -r: 创建系统组;
            CentOS 6: ID<500
            CentOS 7: ID<1000

6. id : print real and effective user and group IDs
7. su : 切换用户或以其他用户身份执行命令
    切换用户的方式:
        su UserName:非登录式切换,即不会读取目标用户的配置文件;
        su - UserName:登录式切换,会读取目标用户的配置文件;完全切换;
        Note:root su至其他用户无须密码;非root用户切换时需要密码;
        换个身份执行命令:
            su [-] UserName -c 'COMMAND'
        选项:
            -l:“su -l UserName”相当于“su - UserName”
8. usermod : 用户属性修改
    -u UID: 新UID
    -g GID: 新基本组
    -G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使用-a选项,表示append;
    -s SHELL:新的默认SHELL;
    -c 'COMMENT':新的注释信息;
    -d HOME: 新的家目录;原有家目录中的文件不会同时移动至新的家目录;若要移动,则同时使用-m选项;
    -l login_name: 新的名字;
    -L: lock指定用户
    -U: unlock指定用户
    -e YYYY-MM-DD: 指明用户账号过期日期;
    -f INACTIVE: 设定非活动期限;
9. passwd : 给用户添加密码  /etc/shadow文件
    passwd [OPTIONS] UserName: 修改指定用户的密码,仅root用户权限
    passwd: 修改自己的密码;
    常用选项:
        -l: 锁定指定用户
        -u: 解锁指定用户
        -n mindays: 指定最短使用期限
        -x maxdays:最大使用期限
        -w warndays:提前多少天开始警告
        -i inactivedays:非活动期限;
        --stdin:从标准输入接收用户密码;
            echo "PASSWORD" | passwd --stdin USERNAME
10. userdel : 删除用户 userdel [OPTION]... login
    -r: 删除用户家目录;
11. groupmod : 组属性修改
    groupmod [OPTION]... group
        -n group_name: 新名字
        -g GID: 新的GID;
12. groupdel : 组删除
    groupdel GROUP
13. gpasswd : 组密码
    gpasswd [OPTION] GROUP
        -a user: 将user添加至指定组中;
        -d user: 删除用户user的以当前组为组名的附加组
        -A user1,user2,...: 设置有管理权限的用户列表
14. newgrp命令:临时切换基本组;
    如果用户本不属于此组,则需要组密码;
15. chage : change user password expiry information
    chage [OPTION]... LOGIN
        -d LAST_DAY
        -E, --expiredate EXPIRE_DATE
        -I, --inactive INACTIVE
        -m, --mindays MIN_DAYS
        -M, --maxdays MAX_DAYS
        -W, --warndays WARN_DAYS
    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d 这是MySQL数据库的配置文件,以下是每条配置的翻译: [mysqld] - datadir=/var/lib/mysql:指定MySQL数据库存储的目录。 - socket=/var/lib/mysql/mysql.sock:指定MySQL数据库与客户端通信的套接字文件。 - symbolic-links=0:禁用符号链接以预防各种安全风险。 - 在使用systemd时,usergroup设置会被忽略。如果需要以不同的用户或组运行mysqld,请根据mariadb的systemd unit文件自定义配置,具体操作方式请参考http://fedoraproject.org/wiki/Systemd。 [mysqld_safe] - log-error=/var/log/mariadb/mariadb.log:指定MariaDB的错误日志文件路径。 - pid-file=/var/run/mariadb/mariadb.pid:指定MariaDB的进程ID文件路径。 !includedir /etc/my.cnf.d - 包含配置目录中的所有文件。 这些是MySQL/MariaDB数据库的配置项,用于指定数据库存储、通信、安全等方面的设置。具体的配置和参数设置可以根据需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值