Lyndra's Blog

ubuntu创建新用户并分配VNC远程桌面

2024-06-08
系统维护 LinuxUbuntuServer
5分钟
936字
温馨提示:本文最后更新于 2025-04-25 ,部分信息可能因时间推移而不再适用,欢迎反馈。

使用命令useradd​。

Terminal window
1
$ useradd -h
2
Usage: useradd [options] LOGIN
3
useradd -D
4
useradd -D [options]
5
6
Options:
7
--badnames do not check for bad names
8
-b, --base-dir BASE_DIR base directory for the home directory of the
9
new account
10
--btrfs-subvolume-home use BTRFS subvolume for home directory
11
-c, --comment COMMENT GECOS field of the new account
12
-d, --home-dir HOME_DIR home directory of the new account
13
-D, --defaults print or change default useradd configuration
14
-e, --expiredate EXPIRE_DATE expiration date of the new account
15
-f, --inactive INACTIVE password inactivity period of the new account
24 collapsed lines
16
-g, --gid GROUP name or ID of the primary group of the new
17
account
18
-G, --groups GROUPS list of supplementary groups of the new
19
account 新用户需要添加到的其他组的组名的列表
20
-h, --help display this help message and exit
21
-k, --skel SKEL_DIR use this alternative skeleton directory
22
-K, --key KEY=VALUE override /etc/login.defs defaults
23
-l, --no-log-init do not add the user to the lastlog and
24
faillog databases
25
-m, --create-home create the user's home directory
26
-M, --no-create-home do not create the user's home directory
27
-N, --no-user-group do not create a group with the same name as
28
the user
29
-o, --non-unique allow to create users with duplicate
30
(non-unique) UID
31
-p, --password PASSWORD encrypted password of the new account
32
-r, --system create a system account
33
-R, --root CHROOT_DIR directory to chroot into
34
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
35
-s, --shell SHELL login shell of the new account
36
-u, --uid UID user ID of the new account
37
-U, --user-group create a group with the same name as the user
38
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
39
--extrausers Use the extra users database

  使用useradd创建新用户,创建家目录,指定用户id和组id,以及默认的shell,并将其添加到sudo组。需要保证组id在useradd之前已经创建。

Terminal window
1
groupadd -g 1000 dev && useradd -ms /bin/bash -u 1000 -g 1000 -G sudo ${username}

  ‍

  添加到sudo组:

  用 usermod 命令可以将现有用户添加到附加组,例如:

Terminal window
1
sudo usermod -aG sudo ${user}
2
-a 选项表示追加(append),即将用户添加到指定组而不从现有组中移除。
3
4
sudo usermod -aG docker $USER
5
将当前用户添加到docker组

  删除用户以及其用户目录

1
sudo userdel -r username

脚本

  使用方法:先按照下面的脚本建立 xstartup、vnc_run.sh、addUser.sh 并放在通用路径。然后运行 ./addUser.sh username

  脚本:/home/vnc_example/xstartup,避免使用ubuntu自带的gnome,目前的版本中出现锁屏无法输入密码的问题。

1
#!/bin/sh
2
export XKL_XMODMAP_DISABLE=1
3
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
4
export XDG_MENU_PREFIX="gnome-flashback-"
5
6
# 服务器物理显示器会默认使用显示端口 5901,需要确保 VNC端口以及配置 不与现有的 GNOME 会话发生冲突。
7
unset SESSION_MANAGER
8
unset DBUS_SESSION_BUS_ADDRESS
9
10
gnome-session --session=gnome-flashback-metacity --disable-acceleration-check

  脚本:/home/vnc_run.sh

1
#! /bin/bash
2
vncserver -geometry 1920x1080 :2 -localhost no # :1 reserved for local connection offline.

  脚本:addUser.sh​,创建新用户,并为其分配home空间、vnc设置(需要手动更改端口号)。创建过程中会使用上面提到的两个脚本。

1
#!/bin/bash
2
set -e # 若有命令出错,立即退出
3
# set -x # 调试模式
4
5
# 检查是否输入了用户名
6
if [ -z "$1" ]; then
7
echo -e "\033[31mError: Please provide a username.\033[0m"
8
exit 1
9
fi
10
11
# 获取用户名
12
username=$1
13
14
# 创建用户并将其添加到 docker 组
15
if sudo useradd -ms /bin/bash -G docker "$username"; then
50 collapsed lines
16
echo -e "\033[32mUser $username added successfully and added to docker group.\033[0m"
17
else
18
echo -e "\033[31mFailed to add user $username.\033[0m"
19
exit 1
20
fi
21
22
# 设置用户密码
23
echo -e "\033[33mPlease set the password for the new user: $username\033[0m"
24
sudo passwd "$username"
25
26
# 确保 .vnc 目录存在
27
sudo mkdir -p /home/"$username"/.vnc
28
sudo chown "$username":"$username" /home/"$username"/.vnc
29
30
# 复制 VNC 脚本
31
if sudo cp /home/vnc_run.sh /home/"$username"/; then
32
sudo chown "$username":"$username" /home/"$username"/vnc_run.sh
33
echo -e "\033[32mvnc.sh copied successfully to /home/$username.\033[0m"
34
else
35
echo -e "\033[31mFailed to copy vnc.sh to /home/$username.\033[0m"
36
exit 1
37
fi
38
39
# 复制 xstartup 配置文件
40
if sudo cp /home/vnc_example/xstartup /home/"$username"/.vnc/; then
41
sudo chown "$username":"$username" /home/"$username"/.vnc/xstartup
42
sudo chmod +x /home/"$username"/.vnc/xstartup
43
echo -e "\033[32mxstartup copied successfully to /home/$username/.vnc.\033[0m"
44
else
45
echo -e "\033[31mFailed to copy xstartup to /home/$username/.vnc.\033[0m"
46
exit 1
47
fi
48
49
# 在 ~/.profile 文件中添加自动启动 VNC 的命令
50
vnc_run_script="/home/$username/vnc_run.sh"
51
profile_file="/home/$username/.profile"
52
53
if ! sudo grep -q "$vnc_run_script" "$profile_file"; then
54
echo "$vnc_run_script" | sudo tee -a "$profile_file" > /dev/null
55
sudo chown "$username":"$username" "$profile_file"
56
echo -e "\033[32mAdded VNC auto-start to $username's .profile.\033[0m"
57
else
58
echo -e "\033[33mVNC auto-start script already exists in $username's .profile.\033[0m"
59
fi
60
61
# 输出提示修改 VNC 端口
62
echo -e "\033[31mPlease modify the VNC port manually for user $username in $vnc_run_script if needed.\033[0m"
63
64
# 添加成功创建用户的提示
65
echo -e "\033[32mAdd user $username in this server successfully.\033[0m"

  ‍

本文标题:ubuntu创建新用户并分配VNC远程桌面
文章作者:Lyndra
发布时间:2024-06-08
总访问量
总访客数人次
Copyright 2025
站点地图