Linux增加/删除普通用户,修改普通用户获取root权限

该博客主要围绕Linux系统展开,介绍了普通用户的创建与删除操作,包括创建用户的命令及输入密码步骤,还说明了删除用户时相关参数的含义。同时,详细阐述了赋予用户root权限的三种方法,通过修改特定文件来实现权限的提升。

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

创建普通用户

就是直接输入命令便可以创建

创建用户

useradd mshing
passwd mshing

然后输入两遍密码就创建好一个mshing的用户了

删除普通用户

userdel mshing # 如果加参加-r的话就是连/home/mshing目录一起删除,不加就只是单纯删除用户,不删除数据。

参数说明

useradd创建的账户的默认值

useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
  • 新用户添加到GID为100的公共组
  • 新用户的HOME目录将会位于/home/username
  • 新用户账户密码在过期后不会被禁用
  • 新用户账户未被设置为某个日期后就过期
  • 新用户账户将bash shell作为默认shell
  • 系统会将/etc/skel目录下的内容复制到用户的HOME目录下
  • 系统为该用户账户在mail目录下创建一个用于接收邮件的文件

useradd具体参数

useradd -h
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]
 
Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

passwd具体参数

passwd --help
Usage: passwd [OPTION...] <accountName>
  -k, --keep-tokens       keep non-expired authentication tokens
  -d, --delete            delete the password for the named account (root only)
  -l, --lock              lock the password for the named account (root only)
  -u, --unlock            unlock the password for the named account (root only)
  -e, --expire            expire the password for the named account (root only)
  -f, --force             force operation
  -x, --maximum=DAYS      maximum password lifetime (root only)
  -n, --minimum=DAYS      minimum password lifetime (root only)
  -w, --warning=DAYS      number of days warning users receives before
                          password expiration (root only)
  -i, --inactive=DAYS     number of days after password expiration when an
                          account becomes disabled (root only)
  -S, --status            report password status on the named account (root
                          only)
  --stdin                 read new tokens from stdin (root only)
 
Help options:
  -?, --help              Show this help message
  --usage                 Display brief usage message

userdel具体参数

userdel -h
Usage: userdel [options] LOGIN
 
Options:
  -f, --force                   force some actions that would fail otherwise
                                e.g. removal of user still logged in
                                or files, even if not owned by the user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -R, --root CHROOT_DIR         directory to chroot into
  -Z, --selinux-user            remove any SELinux user mapping for the user

赋予root权限

方法一

修改 /etc/sudoers 文件,找到下面一行,把前面的注释(#)去掉

## Allows people in group wheel to run all commands
%wheel    ALL=(ALL)    ALL

然后修改用户,使其属于root组(wheel),命令如下:

usermod -g root mshing

修改完毕,现在可以用mshing帐号登录,然后用命令 su - ,即可获得root权限进行操作。

方法二

修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:

## Allow root to run any commands anywhere
root    ALL=(ALL)     ALL
mshing   ALL=(ALL)     ALL

修改完毕,现在可以用mshing帐号登录,然后用命令 su - ,即可获得root权限进行操作。

方法三

修改 /etc/passwd 文件,找到如下行,把用户ID修改为0 ,如下所示:

修改前

mshing:x:500:500:mshing:/home/mshing:/bin/bash

修改后

mshing:x:0:500:mshing:/home/mshing:/bin/bash

保存,用mshing账户登录后,直接获取的就是root帐号的权限。

<think>嗯,用户想知道Linuxroot用户和普通用户权限区别。我需要先回忆一下相关的知识,然后结合提供的引用内容来组织答案。 首先,root用户是超级管理员,拥有系统的完全控制权,而普通用户权限受限,只能操作自己的文件和被授权的部分。这点在引用[1]提到sudo su是临时获取root权限,环境还是当前用户,说明普通用户需要授权才能执行特权命令。 然后,权限范围方面,root可以访问所有文件和目录,修改系统配置,安装软件,管理用户和组,以及进行系统级别的操作。普通用户只能在自己的家目录和授权区域操作,无法修改系统设置。引用[3]和[4]提到通过修改sudoers文件赋予普通用户sudo权限,说明普通用户默认没有这些权限,需要手动配置。 安全风险方面,root用户操作不当可能导致系统崩溃或安全漏洞,所以建议使用sudo来临时提权,如引用[1]和[4]所建议的,使用sudo su而不是直接切换到root,以减少风险。 切换方式的话,引用[2]提到普通用户切换到root可以使用su或sudo su,而root切换回普通用户可以用su - username。这也说明了两种用户之间的切换方法,以及使用环境变量的差异,比如su -会加载root的环境,而sudo su可能保留当前用户的部分设置。 总结下来,应该从权限级别、权限范围、安全影响和切换方式几个方面来回答。同时需要引用提供的资料来支持每个点,比如引用[1]和[4]关于sudo的使用,引用[3]关于修改sudoers文件,引用[2]关于切换方法。 可能用户还想知道如何安全地管理权限,或者如何正确配置sudoers文件,所以生成相关问题的时候可以包括这些方面。比如如何赋予普通用户sudo权限,或者为什么推荐使用sudo而不是直接登录root。 需要确保回答结构清晰,分点说明,每个部分都自然引用相关的引用内容。同时,语言要口语化,避免使用专业术语过多,让用户容易理解。</think>### Linuxroot用户与普通用户权限差异 #### 1. **权限级别** - **root用户**:拥有系统的最高权限,可执行所有操作,包括修改系统配置、访问任意文件、安装/卸载软件、管理用户权限等[^1][^4]。 - **普通用户**:权限受限制,默认只能操作自己的家目录(`/home/用户名`)及部分被授权的文件,无法修改系统级配置或访问其他用户的私有文件[^2][^3]。 #### 2. **权限范围** - **文件操作**: - root用户:可读写、删除所有文件(包括系统关键文件如`/etc/passwd`)。 - 普通用户:仅能操作自身文件或通过`chmod`授权的文件。 - **系统管理**: - root用户:可管理进程(如终止任意进程)、修改网络配置、安装全局软件。 - 普通用户:需通过`sudo`临时提权才能执行特权命令(如`apt install`)[^4]。 #### 3. **安全影响** - root用户操作不当可能导致系统崩溃或安全漏洞,因此推荐日常使用普通用户,仅在必要时通过`sudo`或`su -`临时提权[^4]。 - 普通用户通过`sudo`执行命令时,系统会记录操作日志,便于审计和追溯。 #### 4. **切换方式** - **普通用户root**: - `sudo su`:临时获取root权限,保留当前用户的环境变量(更安全)[^1]。 - `su - root`:完全切换到root用户,加载root的环境变量(需输入root密码)[^2]。 - **root普通用户**: - `su - 用户名`:切换回普通用户并加载其环境。 #### 5. **权限赋予** - 普通用户可通过修改`/etc/sudoers`文件获得sudo权限,例如添加: ```bash username ALL=(ALL) ALL # 允许用户username执行所有命令[^4] ``` --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

之芫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值