linux_用户管理/查看本机的用户/完整删除用户/创建新用户(交互命令adduser来创建新用户/用户切换su/sudo

查看本机用户

具有家目录的用户(非root用户)

  • 这一点比较实用
  • ls /home
root@arch-virtual-machine:/home# ls
arch cxxu roo

查看所有用户(包括隐藏用户)

  • cat /etc/passwd | nl|tac|less
    • tac命令用于逆序输出(这一般是我们最先想看到的!)
  • 通常,列出的末尾若干行是我们创建给人登录的(具有通常具有家目录)
  • 在这里插入图片描述

添加用户

  • sudo adduser <userName>
➜ ~ sudo adduser cxxu_kali
Adding user `cxxu_kali' ...
Adding new group `cxxu_kali' (1000) ...
Adding new user `cxxu_kali' (1000) with group `cxxu_kali' ...
Creating home directory `/home/cxxu_kali' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for cxxu_kali
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
➜ ~ sudo adduser cxxu_kali sudo
Adding user `cxxu_kali' to group `sudo' ...
Adding user cxxu_kali to group sudo
Done.

中途不想填写的可以直接回车继续

  • 在比较新的linux发行版中已经可以使用友好的adduser来添加linux用户

sudo权限组问题

  • 某些版本运行adduser后,新建立的用户无法直接运行sudo命令

xxuser is not in the sudoers file. This incident will be reported.

cxxu@iZ2zef3tpqffm5ydsjqi4zsdsZ:/etc/apt$ su -
Password:
root@iZ2zef3tpqffm5ydjdfsfqi4zsZ:~# sudo addusr cxxu sudo
sudo: addusr: command not found
root@iZ2zef3tpqffm5ydjqi4zsZ:~# sudo adduser cxxu sudo
Adding user `cxxu' to group `sudo' ...
Adding user cxxu to group sudo
Done.

用户删除

deluser删除用户的所有内容

用户进程检查

ps -u <userName>

  • (debian系列发行版下),deluser是首先被推荐用来删除用户

以下操作建议再sudo权限下执行

  • 终止要被删除的用户的所有进程 sudo pkill -KILL -u <userName>
    • There are different ways to kill a user’s processes, but the command shown here is widely available and is a more modern implementation than some of the alternatives.
    • The pkill command will find and kill processes. We’re passing in the KILL signal, and using the -u (user) option.
  • 执行 sudo deluser --remove-home <UserName>
    • ┌──(cxxu_maintainer㉿CxxuWin11)-[/mnt/c/Users/cxxu]
      └─$ sudo deluser --remove-home cxxu_kali
      Looking for files to backup/remove ...
      Removing user `cxxu_kali' ...
      Warning: group `cxxu_kali' has no more members.
      Done.

userdel删除用户

  • 非debain系列发行版可用 userdel删除用户
  • sudo userdel --remove <userName>将一并删除用户家目录
    • 默认情况下(不带有–remove选项时),不会删除家目录

用户删除整合

具有deluser的发行版
  • deleteUser <userName>
deleteUser(){
sudo pkill -KILL -u $1
sudo deluser --remove-home $1
}
通用版本
deleteLinuxUser(){
sudo pkill -KILL -u $1
sudo userdel --remove $1
}

切换用户总结

  • 使用 su -l <userName> 切换到指定用户
    • -l 可以简写为 -
    • 不写用户名默认切换到 root
  • 还支持指定切换后的环境:(工作目录和shell)
    • -p 指定工作目录
    • -s 指定shell
  • In Linux, the su command (switch user) is used to run a command as a different user.

How the su Command Works

The su command is used to run a function as a different user. It is the easiest way to switch or change to the administrative account in the current logged in session.

Some versions of Linux, like Ubuntu, disable the root user account by default making the system more secure. But, this also restricts the user from running specific commands.

Using su to temporarily act as a root user allows you to bypass this restriction and perform different tasks with different users.

Note : A root account is a master administrator account with full access and permissions in the system. Because of the severity of changes this account can make, and because of the risk of it being compromised, most Linux versions use limited user accounts for normal use.

su Command Syntax

To use the su command, enter it into a command-line as follows:

su [options] [username [arguments]]

If a username is specified, su defaults to the superuser (root). Simply find the user you need and add it to the su command syntax.

su Command Options

To display a list of commands, enter the following:

su –h
cxxu@iZ2zef3tpqffm5ydjqi4zsZ:/etc/apt$ su -h
Usage: su [options] [LOGIN]
Options:
-c, --command COMMAND pass COMMAND to the invoked shell
-h, --help display this help message and exit
-, -l, --login make the shell a login shell
-m, -p,
--preserve-environment do not reset environment variables, and
keep the same shell
-s, --shell SHELL use SHELL instead of the default in passwd

su Command Examples

Switch to a Different User

To switch the logged-in user in this terminal window, enter the following:

su –l [other_user]

You’ll be asked for a password. Enter it, and the login will change to that user.

If you omit a username, it will default to the root account. Now, the logged-in user can run all system commands. This will also change the home directory and path to executable files.

Use the whoami command to verify you switched to a different user.

Note : If you are having issues with authentication, you can change the root or sudo password in a couple of simple steps.

Run Specific Command as a Different User

To run a specific command as a different user, use the –c option:

su –c [command] [other_user]

The system will respond by asking you for the user password.

When you enter this example, the system will use the specified account to run the ls (list directory contents) command.

Use a Different Shell

To use a different shell, or operating environment, enter the following:

su –s /usr/bin/zsh

This command opens a root user account in Z shell .

Use a Different User in the Same Environment

You can keep the environment of the current user account with the –p option:

su –p [other_user]

Replace [other_user] with the actual username you want to switch to.

The user account will switch, but you’ll keep the same home directory. This is useful if you need to run a command as a different user, but you need access to the current user’s data.

To verify you remained in the same home environment, use the echo $HOME command that will display the directory you are working in.

Command Comparison: su vs sudo

sudo Command

  • The sudo command grants a one-time or limited-time access to root functionality.
  • Typically, the sudo command is used to quickly run an administrative command, then return to the user account’s regular permissions.

To provide sudo access, the user has to be added to the sudo group.

Note : By default, some versions of Linux (such as Ubuntu) disable the root account. That means there’s no password assigned to the root user. However you can switch to root by running the following command and entering the currently logged-in user’s password:

sudo su -

su Command

  • The su command lets you switch the current user to any other user.
  • If you need to run a command as a different (non-root) user, use the –l [username] option to specify the user account.
  • Additionally,su can also be used to change to a different shell interpreter on the fly.
  • su is an older but more fully-featured command.
    • It can duplicate the functionality of sudo by use of the –c option to pass a single command to the shell.

reference adduser

reference link:Creating a new user and modifying its privileges in Linux

references su/sudo

reference delete user

reference check users

逆序输出结果

posted @   xuchaoxin1375  阅读(35)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示