国外服务器:
curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O ${_##*/} $_国内服务器:
curl -O https://cnb.cool/bin456789/reinstall/-/git/raw/main/reinstall.sh || wget -O ${_##*/} $_直接DD ubuntu 26.04
bash reinstall.sh ubuntu 26.04脚本来源:https://github.com/bin456789/reinstall
安装过后执行以下脚本
#复制下方脚本
nano setup-gnome-rdp.sh
#!/usr/bin/env bash
set -Eeuo pipefail
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
NC="\033[0m"
LOG_FILE="/root/setup-gnome-rdp-$(date +%Y%m%d_%H%M%S).log"
log() {
echo -e "${GREEN}[OK]${NC} $*"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $*"
}
err() {
echo -e "${RED}[ERROR]${NC} $*" >&2
}
need_root() {
if [ "$(id -u)" -ne 0 ]; then
err "请用 root 执行,或者使用 sudo bash setup-gnome-rdp.sh"
exit 1
fi
}
backup_file() {
local file="$1"
if [ -f "$file" ]; then
cp -a "$file" "${file}.bak.$(date +%Y%m%d_%H%M%S)"
fi
}
pkg_installed() {
dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -q "install ok installed"
}
ask_password_twice() {
local title="$1"
local var_name="$2"
local p1=""
local p2=""
while true; do
read -rsp "$title: " p1
echo
read -rsp "再次输入$title: " p2
echo
if [ -z "$p1" ]; then
echo "密码不能为空,请重新输入。"
continue
fi
if [ "$p1" != "$p2" ]; then
echo "两次密码不一致,请重新输入。"
continue
fi
printf -v "$var_name" '%s' "$p1"
break
done
}
ask_port() {
local default_port="$1"
local input_port=""
while true; do
read -rp "RDP 监听端口 [${default_port}]: " input_port
input_port="${input_port:-$default_port}"
if [[ "$input_port" =~ ^[0-9]+$ ]] && [ "$input_port" -ge 1 ] && [ "$input_port" -le 65535 ]; then
if [ "$input_port" = "22" ]; then
warn "不建议使用 22,这是 SSH 端口。"
read -rp "仍然使用 22?[y/N]: " use22
case "$use22" in
y|Y|yes|YES)
printf '%s' "$input_port"
return
;;
*)
continue
;;
esac
fi
printf '%s' "$input_port"
return
fi
echo "端口必须是 1-65535 的数字。"
done
}
is_ipv4() {
local ip="$1"
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || return 1
local a b c d
IFS=. read -r a b c d <<< "$ip"
[ "$a" -le 255 ] && [ "$b" -le 255 ] && [ "$c" -le 255 ] && [ "$d" -le 255 ]
}
is_private_ipv4() {
local ip="$1"
[[ "$ip" =~ ^10\. ]] && return 0
[[ "$ip" =~ ^127\. ]] && return 0
[[ "$ip" =~ ^169\.254\. ]] && return 0
[[ "$ip" =~ ^192\.168\. ]] && return 0
[[ "$ip" =~ ^172\.(1[6-9]|2[0-9]|3[0-1])\. ]] && return 0
[[ "$ip" =~ ^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\. ]] && return 0
return 1
}
get_local_ipv4() {
local ip_addr=""
ip_addr="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++){if($i=="src"){print $(i+1); exit}}}')"
if [ -z "$ip_addr" ]; then
ip_addr="$(hostname -I 2>/dev/null | awk '{print $1}')"
fi
echo "$ip_addr"
}
get_public_ipv4() {
local ip_addr=""
if command -v curl >/dev/null 2>&1; then
for api in \
"https://api.ipify.org" \
"https://ifconfig.me/ip" \
"https://icanhazip.com" \
"https://checkip.amazonaws.com"
do
ip_addr="$(curl -4fsS --max-time 6 "$api" 2>/dev/null | tr -d '[:space:]')"
if is_ipv4 "$ip_addr"; then
echo "$ip_addr"
return 0
fi
done
fi
if command -v wget >/dev/null 2>&1; then
for api in \
"https://api.ipify.org" \
"https://icanhazip.com" \
"https://checkip.amazonaws.com"
do
ip_addr="$(wget -qO- --timeout=6 "$api" 2>/dev/null | tr -d '[:space:]')"
if is_ipv4 "$ip_addr"; then
echo "$ip_addr"
return 0
fi
done
fi
return 1
}
detect_connect_ip() {
local local_ip=""
local public_ip=""
local_ip="$(get_local_ipv4)"
if is_ipv4 "$local_ip"; then
if is_private_ipv4 "$local_ip"; then
public_ip="$(get_public_ipv4 || true)"
if is_ipv4 "$public_ip"; then
echo "$public_ip"
return 0
fi
echo "$local_ip"
return 0
else
echo "$local_ip"
return 0
fi
fi
public_ip="$(get_public_ipv4 || true)"
if is_ipv4 "$public_ip"; then
echo "$public_ip"
return 0
fi
echo ""
}
get_grd_port() {
grdctl --system status 2>/dev/null | awk '/^[[:space:]]*Port:/ {print $2; exit}'
}
get_listen_port_from_ss() {
ss -lntp 2>/dev/null | awk '/gnome-remote-de/ {
n=split($4,a,":");
print a[n];
exit
}'
}
set_grd_credentials() {
local user="$1"
local pass="$2"
grdctl --system rdp set-credentials "$user" "$pass"
}
set_grd_port() {
local port="$1"
if grdctl --system rdp set-port "$port" >/tmp/grd-set-port.out 2>&1; then
rm -f /tmp/grd-set-port.out
log "已设置 GNOME Remote Desktop RDP 端口为:$port"
return 0
fi
warn "当前系统的 grdctl 可能不支持 rdp set-port,端口设置未生效。"
warn "将继续使用 GNOME Remote Desktop 的实际监听端口。"
echo "grdctl set-port 输出:"
cat /tmp/grd-set-port.out || true
rm -f /tmp/grd-set-port.out
return 1
}
need_root
clear
echo
echo "============================================================"
echo " Ubuntu 26.04 原版 GNOME + GNOME Remote Desktop / RDP"
echo " 一键安装 / 修改配置脚本"
echo
echo "支持:"
echo " - 首次安装 ubuntu-desktop + GNOME Remote Desktop"
echo " - 第二次运行时修改第一层 RDP 账号密码"
echo " - 第二次运行时修改第二层 GNOME/Linux 用户密码"
echo " - 尝试自定义 RDP 端口"
echo " - 自动生成 Windows .rdp 文件"
echo
echo "不安装:"
echo " - VNC"
echo " - x11vnc"
echo " - LightDM"
echo " - XRDP"
echo "============================================================"
echo
read -rp "第一层 RDP 用户名 [administrator]: " RDP_USER
RDP_USER="${RDP_USER:-administrator}"
ask_password_twice "第一层 RDP 密码" RDP_PASS
echo
read -rp "第二层 GNOME/Linux 登录用户名 [ubuntu]: " DESKTOP_USER
DESKTOP_USER="${DESKTOP_USER:-ubuntu}"
ask_password_twice "第二层 GNOME/Linux 用户 ${DESKTOP_USER} 的密码" DESKTOP_PASS
echo
DEFAULT_PORT="$(get_grd_port || true)"
DEFAULT_PORT="${DEFAULT_PORT:-3389}"
RDP_PORT="$(ask_port "$DEFAULT_PORT")"
echo
AUTO_IP="$(detect_connect_ip)"
if [ -z "$AUTO_IP" ]; then
warn "未能自动检测公网 IP,请手动输入服务器公网 IP。"
fi
read -rp "生成 .rdp 文件使用的连接地址/IP [${AUTO_IP:-服务器公网IP}]: " CONNECT_HOST
CONNECT_HOST="${CONNECT_HOST:-$AUTO_IP}"
CONNECT_HOST="${CONNECT_HOST:-服务器公网IP}"
echo
echo "正在检测当前系统状态..."
echo "------------------------------------------------------------"
if pkg_installed ubuntu-desktop; then
DESKTOP_INSTALLED="yes"
echo "GNOME 桌面:已安装 ubuntu-desktop"
else
DESKTOP_INSTALLED="no"
echo "GNOME 桌面:未安装 ubuntu-desktop"
fi
if pkg_installed gnome-remote-desktop; then
GRD_INSTALLED="yes"
echo "GNOME Remote Desktop:已安装"
else
GRD_INSTALLED="no"
echo "GNOME Remote Desktop:未安装"
fi
echo "当前 RDP 端口:${DEFAULT_PORT}"
echo "计划 RDP 端口:${RDP_PORT}"
echo "RDP 连接地址:${CONNECT_HOST}"
echo "------------------------------------------------------------"
echo
INSTALL_COMPONENTS="yes"
if [ "$DESKTOP_INSTALLED" = "yes" ] && [ "$GRD_INSTALLED" = "yes" ]; then
echo "检测到 GNOME 桌面和 GNOME Remote Desktop 已经安装。"
read -rp "是否重新安装/修复相关软件包?默认跳过 [y/N]: " REINSTALL
case "$REINSTALL" in
y|Y|yes|YES)
INSTALL_COMPONENTS="yes"
;;
*)
INSTALL_COMPONENTS="no"
;;
esac
else
echo "检测到必要组件不完整,需要安装。"
read -rp "是否继续安装缺失组件?[Y/n]: " DO_INSTALL
case "$DO_INSTALL" in
n|N|no|NO)
echo "已取消。"
exit 0
;;
*)
INSTALL_COMPONENTS="yes"
;;
esac
fi
echo
echo "即将执行:"
echo "------------------------------------------------------------"
echo "第一层 RDP 用户名:${RDP_USER}"
echo "第一层 RDP 密码:已输入,不显示"
echo
echo "第二层 GNOME/Linux 用户名:${DESKTOP_USER}"
echo "第二层 GNOME/Linux 密码:已输入,不显示"
echo
echo "RDP 计划端口:${RDP_PORT}"
echo "RDP 文件连接地址:${CONNECT_HOST}:${RDP_PORT}"
echo "重新安装/修复软件包:${INSTALL_COMPONENTS}"
echo "日志文件:${LOG_FILE}"
echo "------------------------------------------------------------"
echo
read -rp "确认继续?[y/N]: " CONFIRM
case "$CONFIRM" in
y|Y|yes|YES) ;;
*)
echo "已取消。"
exit 0
;;
esac
exec > >(tee -a "$LOG_FILE") 2>&1
echo
log "日志文件:$LOG_FILE"
log "开始自动化配置。"
echo
echo "==================== 1. 更新软件源 ===================="
apt update
if [ "$INSTALL_COMPONENTS" = "yes" ]; then
echo
echo "==================== 2. 安装/修复 GNOME 桌面和远程桌面组件 ===================="
DEBIAN_FRONTEND=noninteractive apt install -y \
ubuntu-desktop \
gnome-remote-desktop \
openssl \
dbus-x11 \
curl
else
echo
echo "==================== 2. 跳过软件包安装 ===================="
log "已按选择跳过 ubuntu-desktop / gnome-remote-desktop 重新安装。"
fi
echo
echo "==================== 3. 停用可能冲突的 XRDP ===================="
systemctl disable --now xrdp 2>/dev/null || true
systemctl disable --now xrdp-sesman 2>/dev/null || true
echo
echo "==================== 4. 启用 GDM3 图形登录管理器 ===================="
systemctl disable lightdm 2>/dev/null || true
systemctl set-default graphical.target
systemctl enable --now gdm3
echo
echo "==================== 5. 恢复 GDM 默认 Wayland/GNOME 配置 ===================="
GDM_CONF="/etc/gdm3/custom.conf"
backup_file "$GDM_CONF"
if [ ! -f "$GDM_CONF" ]; then
mkdir -p /etc/gdm3
cat > "$GDM_CONF" <<'EOF'
[daemon]
EOF
fi
sed -i 's/^[[:space:]]*WaylandEnable=false/#WaylandEnable=false/g' "$GDM_CONF" || true
sed -i 's/^[[:space:]]*DefaultSession=ubuntu-xorg.desktop/#DefaultSession=ubuntu-xorg.desktop/g' "$GDM_CONF" || true
if [ -f /etc/X11/xorg.conf.d/10-dummy.conf ]; then
log "检测到旧 dummy 显示器配置,已备份并移除。"
mkdir -p /root/remote-desktop-backup
mv /etc/X11/xorg.conf.d/10-dummy.conf \
"/root/remote-desktop-backup/10-dummy.conf.bak.$(date +%Y%m%d_%H%M%S)"
fi
echo
echo "==================== 6. 创建或更新第二层 GNOME/Linux 用户 ===================="
if id "$DESKTOP_USER" >/dev/null 2>&1; then
log "用户 ${DESKTOP_USER} 已存在,将更新密码并加入 sudo 组。"
else
log "用户 ${DESKTOP_USER} 不存在,正在创建。"
adduser --disabled-password --gecos "" "$DESKTOP_USER"
fi
printf '%s:%s\n' "$DESKTOP_USER" "$DESKTOP_PASS" | chpasswd
passwd -u "$DESKTOP_USER" 2>/dev/null || true
usermod -aG sudo "$DESKTOP_USER"
unset DESKTOP_PASS
log "第二层 GNOME/Linux 用户 ${DESKTOP_USER} 已配置完成。"
echo
echo "==================== 7. 准备 GNOME Remote Desktop TLS 证书 ===================="
GRD_USER="gnome-remote-desktop"
GRD_HOME="$(getent passwd "$GRD_USER" | cut -d: -f6 || true)"
if [ -z "$GRD_HOME" ]; then
err "找不到 gnome-remote-desktop 系统用户,请确认 gnome-remote-desktop 是否安装成功。"
exit 1
fi
CERT_DIR="$GRD_HOME/.local/share/gnome-remote-desktop"
mkdir -p "$CERT_DIR"
REGEN_CERT="no"
if [ ! -f "$CERT_DIR/tls.key" ] || [ ! -f "$CERT_DIR/tls.crt" ]; then
REGEN_CERT="yes"
else
echo
read -rp "检测到已有 RDP TLS 证书,是否重新生成以匹配当前连接地址?默认不重新生成 [y/N]: " CERT_CONFIRM
case "$CERT_CONFIRM" in
y|Y|yes|YES)
REGEN_CERT="yes"
;;
*)
REGEN_CERT="no"
;;
esac
fi
if [ "$REGEN_CERT" = "yes" ]; then
log "生成新的 RDP TLS 证书。"
OPENSSL_CNF="/root/grd-openssl-$(date +%Y%m%d_%H%M%S).cnf"
cat > "$OPENSSL_CNF" <<EOF
[req]
default_bits = 4096
prompt = no
default_md = sha256
x509_extensions = v3_req
distinguished_name = dn
[dn]
CN = ${CONNECT_HOST}
[v3_req]
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${CONNECT_HOST}
DNS.2 = $(hostname)
EOF
if [[ "$CONNECT_HOST" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
cat >> "$OPENSSL_CNF" <<EOF
IP.1 = ${CONNECT_HOST}
EOF
fi
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 \
-config "$OPENSSL_CNF" \
-out "$CERT_DIR/tls.crt" \
-keyout "$CERT_DIR/tls.key"
else
log "继续使用已有 TLS 证书。"
fi
chown -R gnome-remote-desktop:gnome-remote-desktop "$GRD_HOME/.local"
chmod 700 "$CERT_DIR"
chmod 600 "$CERT_DIR/tls.key"
chmod 644 "$CERT_DIR/tls.crt"
echo
echo "==================== 8. 配置系统级 RDP 远程登录 ===================="
grdctl --system rdp set-tls-key "$CERT_DIR/tls.key"
grdctl --system rdp set-tls-cert "$CERT_DIR/tls.crt"
set_grd_credentials "$RDP_USER" "$RDP_PASS"
unset RDP_PASS
grdctl --system rdp enable
log "第一层 RDP 账号 ${RDP_USER} 已配置完成。"
echo
echo "==================== 9. 尝试配置 RDP 自定义端口 ===================="
PORT_SET_OK="no"
if set_grd_port "$RDP_PORT"; then
PORT_SET_OK="yes"
else
PORT_SET_OK="no"
fi
echo
echo "==================== 10. 启动 GNOME Remote Desktop 服务 ===================="
systemctl enable --now gnome-remote-desktop.service
systemctl restart gnome-remote-desktop.service
systemctl restart gdm3 || true
sleep 2
ACTUAL_PORT="$(get_grd_port || true)"
if [ -z "$ACTUAL_PORT" ]; then
ACTUAL_PORT="$(get_listen_port_from_ss || true)"
fi
if [ -z "$ACTUAL_PORT" ]; then
ACTUAL_PORT="3389"
fi
if [ "$PORT_SET_OK" != "yes" ] && [ "$ACTUAL_PORT" != "$RDP_PORT" ]; then
warn "你输入的端口 ${RDP_PORT} 没有生效。"
warn "当前 GNOME Remote Desktop 实际端口是:${ACTUAL_PORT}"
warn "Windows 连接请使用:${CONNECT_HOST}:${ACTUAL_PORT}"
fi
echo
echo "==================== 11. 放行 RDP 端口 ===================="
if command -v ufw >/dev/null 2>&1; then
ufw allow "${ACTUAL_PORT}/tcp" >/dev/null 2>&1 || true
ufw reload >/dev/null 2>&1 || true
log "已尝试通过 ufw 放行 ${ACTUAL_PORT}/tcp。"
else
warn "未检测到 ufw,跳过系统防火墙配置。"
fi
echo
echo "==================== 12. 生成 Windows RDP 示例文件 ===================="
RDP_FILE="/root/ubuntu-gnome.rdp"
cat > "$RDP_FILE" <<EOF
full address:s:${CONNECT_HOST}:${ACTUAL_PORT}
username:s:${RDP_USER}
prompt for credentials:i:1
authentication level:i:0
enablecredsspsupport:i:1
use redirection server name:i:1
redirectclipboard:i:1
audiomode:i:0
screen mode id:i:2
desktopwidth:i:1920
desktopheight:i:1080
session bpp:i:32
EOF
log "已生成 RDP 示例文件:$RDP_FILE"
echo
echo "==================== 13. 最终状态检查 ===================="
echo
echo "---- gnome-remote-desktop.service ----"
systemctl --no-pager --full status gnome-remote-desktop.service | sed -n '1,16p' || true
echo
echo "---- grdctl --system status ----"
grdctl --system status || true
echo
echo "---- RDP 监听状态 ----"
ss -lntp | grep ":${ACTUAL_PORT}" || ss -lntp | grep 3389 || true
echo
echo "---- GDM3 状态 ----"
systemctl --no-pager --full status gdm3 | sed -n '1,12p' || true
echo
echo "============================================================"
echo " 配置完成"
echo "============================================================"
echo
echo "流程日志:"
echo " $LOG_FILE"
echo
echo "RDP 示例文件:"
echo " $RDP_FILE"
echo
echo "Windows 连接:"
echo " 地址:${CONNECT_HOST}:${ACTUAL_PORT}"
echo
echo "第一层 RDP 登录:"
echo " 用户名:${RDP_USER}"
echo " 密码:脚本开始时设置的第一层 RDP 密码"
echo
echo "第二层 GNOME/Linux 登录:"
echo " 用户名:${DESKTOP_USER}"
echo " 密码:脚本开始时设置的第二层 GNOME/Linux 密码"
echo
echo "如果是云服务器,还需要在云厂商安全组放行:"
echo " TCP ${ACTUAL_PORT}"
echo
echo "Windows 清理旧凭据命令示例:"
echo " cmdkey /delete:TERMSRV/${CONNECT_HOST}"
echo " cmdkey /delete:TERMSRV/${CONNECT_HOST}:${ACTUAL_PORT}"
echo
echo "建议现在重启一次:"
echo " reboot"
echochmod +x setup-gnome-rdp.sh
./setup-gnome-rdp.sh