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

文章目录

    • 查看本机用户
      • 具有家目录的用户(非root用户)
      • 查看所有用户(包括隐藏用户)
    • 添加用户
      • sudo权限组问题
    • 用户删除
      • deluser删除用户的所有内容
        • 用户进程检查
      • userdel删除用户
      • 用户删除整合
        • 具有deluser的发行版
        • 通用版本
    • 切换用户总结
    • How the su Command Works
    • su Command Syntax
      • su Command Options
    • su Command Examples
      • Switch to a Different User
      • Run Specific Command as a Different User
      • Use a Different Shell
      • Use a Different User in the Same Environment
    • Command Comparison: su vs sudo
      • sudo Command
      • su Command
    • reference adduser
    • references su/sudo
    • reference delete user
    • reference check users
    • 逆序输出结果

查看本机用户

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

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

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

  • cat /etc/passwd | nl|tac|less
    • tac命令用于逆序输出(这一般是我们最先想看到的!)
  • 通常,列出的末尾若干行是我们创建给人登录的(具有通常具有家目录)
  • linux_用户管理/查看本机的用户/完整删除用户/创建新用户(交互命令adduser来创建新用户/用户切换su/sudo_第1张图片

添加用户

  • sudo adduser

➜  ~ 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.

  • Creating a new user and modifying its privileges in Linux | Average Linux User
  • 执行sudo adduser sudo赋予该用户sudo的使用权
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

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

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

  • 终止要被删除的用户的所有进程 sudo pkill -KILL -u
    • 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
    • ┌──(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 将一并删除用户家目录
    • 默认情况下(不带有–remove选项时),不会删除家目录

用户删除整合

具有deluser的发行版

  • deleteUser
deleteUser(){
    sudo pkill -KILL -u $1
    sudo deluser --remove-home $1
}

通用版本

deleteLinuxUser(){
    sudo pkill -KILL -u $1
    sudo userdel --remove $1
}

切换用户总结

  • 使用 su -l 切换到指定用户
    • -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

  • su Command

reference delete user

  • How to Delete a User on Linux (and Remove Every Trace) (howtogeek.com)

    • 删除用之前,请确报备份相关数据;或者该用户的数据不需要被保留!

    How to Delete a User on Linux (and Remove Every Trace)1. User Accounts on Linux

    1. Our Scenario
    2. Check the Login
    3. Reviewing The User’s Processes
    4. Locking the Account
    5. Killing the Processes
    6. Archiving the User’s home Directory
    7. Removing cron Jobs
    8. Removing Print Jobs
    9. Deleting the User Account
    10. It’s a Wrap

reference check users

  • How To List Users and Groups on Linux – devconnected

    1. List Users on Linux
    2. List Usernames using the /etc/passwd file
      1. a – List Usernames using cut
      2. b – List Usernames using awk
    3. List Users on Linux using getent
      1. a – List Usernames with getent
    4. List Connected Users on your Linux host
    5. List Groups on Linux using the /etc/group file
    6. List Groupnames using the /etc/group
    7. List Groups using getent
    8. List Groups for the current user
    9. Conclusion

逆序输出结果

  • How to Reverse the Order of Lines in a File in Linux | Baeldung on Linux

你可能感兴趣的:(linux)