使用命令useradd
。
1$ useradd -h2Usage: useradd [options] LOGIN3 useradd -D4 useradd -D [options]5
6Options:7 --badnames do not check for bad names8 -b, --base-dir BASE_DIR base directory for the home directory of the9 new account10 --btrfs-subvolume-home use BTRFS subvolume for home directory11 -c, --comment COMMENT GECOS field of the new account12 -d, --home-dir HOME_DIR home directory of the new account13 -D, --defaults print or change default useradd configuration14 -e, --expiredate EXPIRE_DATE expiration date of the new account15 -f, --inactive INACTIVE password inactivity period of the new account24 collapsed lines
16 -g, --gid GROUP name or ID of the primary group of the new17 account18 -G, --groups GROUPS list of supplementary groups of the new19 account 新用户需要添加到的其他组的组名的列表20 -h, --help display this help message and exit21 -k, --skel SKEL_DIR use this alternative skeleton directory22 -K, --key KEY=VALUE override /etc/login.defs defaults23 -l, --no-log-init do not add the user to the lastlog and24 faillog databases25 -m, --create-home create the user's home directory26 -M, --no-create-home do not create the user's home directory27 -N, --no-user-group do not create a group with the same name as28 the user29 -o, --non-unique allow to create users with duplicate30 (non-unique) UID31 -p, --password PASSWORD encrypted password of the new account32 -r, --system create a system account33 -R, --root CHROOT_DIR directory to chroot into34 -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files35 -s, --shell SHELL login shell of the new account36 -u, --uid UID user ID of the new account37 -U, --user-group create a group with the same name as the user38 -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping39 --extrausers Use the extra users database
使用useradd创建新用户,创建家目录,指定用户id和组id,以及默认的shell,并将其添加到sudo组。需要保证组id在useradd之前已经创建。
1groupadd -g 1000 dev && useradd -ms /bin/bash -u 1000 -g 1000 -G sudo ${username}
添加到sudo组:
用 usermod 命令可以将现有用户添加到附加组,例如:
1sudo usermod -aG sudo ${user}2-a 选项表示追加(append),即将用户添加到指定组而不从现有组中移除。3
4sudo usermod -aG docker $USER5将当前用户添加到docker组
删除用户以及其用户目录
1sudo userdel -r username
脚本
使用方法:先按照下面的脚本建立 xstartup、vnc_run.sh、addUser.sh 并放在通用路径。然后运行 ./addUser.sh username
脚本:/home/vnc_example/xstartup,避免使用ubuntu自带的gnome,目前的版本中出现锁屏无法输入密码的问题。
1#!/bin/sh2export XKL_XMODMAP_DISABLE=13export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"4export XDG_MENU_PREFIX="gnome-flashback-"5
6# 服务器物理显示器会默认使用显示端口 5901,需要确保 VNC端口以及配置 不与现有的 GNOME 会话发生冲突。7unset SESSION_MANAGER8unset DBUS_SESSION_BUS_ADDRESS9
10gnome-session --session=gnome-flashback-metacity --disable-acceleration-check
脚本:/home/vnc_run.sh
1#! /bin/bash2vncserver -geometry 1920x1080 :2 -localhost no # :1 reserved for local connection offline.
脚本:addUser.sh
,创建新用户,并为其分配home空间、vnc设置(需要手动更改端口号)。创建过程中会使用上面提到的两个脚本。
1#!/bin/bash2set -e # 若有命令出错,立即退出3# set -x # 调试模式4
5# 检查是否输入了用户名6if [ -z "$1" ]; then7 echo -e "\033[31mError: Please provide a username.\033[0m"8 exit 19fi10
11# 获取用户名12username=$113
14# 创建用户并将其添加到 docker 组15if sudo useradd -ms /bin/bash -G docker "$username"; then50 collapsed lines
16 echo -e "\033[32mUser $username added successfully and added to docker group.\033[0m"17else18 echo -e "\033[31mFailed to add user $username.\033[0m"19 exit 120fi21
22# 设置用户密码23echo -e "\033[33mPlease set the password for the new user: $username\033[0m"24sudo passwd "$username"25
26# 确保 .vnc 目录存在27sudo mkdir -p /home/"$username"/.vnc28sudo chown "$username":"$username" /home/"$username"/.vnc29
30# 复制 VNC 脚本31if sudo cp /home/vnc_run.sh /home/"$username"/; then32 sudo chown "$username":"$username" /home/"$username"/vnc_run.sh33 echo -e "\033[32mvnc.sh copied successfully to /home/$username.\033[0m"34else35 echo -e "\033[31mFailed to copy vnc.sh to /home/$username.\033[0m"36 exit 137fi38
39# 复制 xstartup 配置文件40if sudo cp /home/vnc_example/xstartup /home/"$username"/.vnc/; then41 sudo chown "$username":"$username" /home/"$username"/.vnc/xstartup42 sudo chmod +x /home/"$username"/.vnc/xstartup43 echo -e "\033[32mxstartup copied successfully to /home/$username/.vnc.\033[0m"44else45 echo -e "\033[31mFailed to copy xstartup to /home/$username/.vnc.\033[0m"46 exit 147fi48
49# 在 ~/.profile 文件中添加自动启动 VNC 的命令50vnc_run_script="/home/$username/vnc_run.sh"51profile_file="/home/$username/.profile"52
53if ! sudo grep -q "$vnc_run_script" "$profile_file"; then54 echo "$vnc_run_script" | sudo tee -a "$profile_file" > /dev/null55 sudo chown "$username":"$username" "$profile_file"56 echo -e "\033[32mAdded VNC auto-start to $username's .profile.\033[0m"57else58 echo -e "\033[33mVNC auto-start script already exists in $username's .profile.\033[0m"59fi60
61# 输出提示修改 VNC 端口62echo -e "\033[31mPlease modify the VNC port manually for user $username in $vnc_run_script if needed.\033[0m"63
64# 添加成功创建用户的提示65echo -e "\033[32mAdd user $username in this server successfully.\033[0m"