Last active
June 2, 2025 03:48
-
-
Save pedoc/7c7065a09dcd36886677cdc31bcea1e8 to your computer and use it in GitHub Desktop.
install-vscode-server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
VER=${1:""} | |
if [ -z "$VER" ]; then | |
# shellcheck disable=SC2012 | |
readarray -t versions < <(ls vscode-server_*_*.tar.gz 2>/dev/null | \ | |
sed -n 's/^vscode-server_\([^_]*\)_[^_]*\.tar\.gz$/\1/p' | \ | |
sort -u) | |
if [ ${#versions[@]} -eq 0 ]; then | |
echo "❌ 未找到任何 vscode-server_{VER}_{ARCH}.tar.gz 文件。" | |
exit 1 | |
fi | |
select version in "${versions[@]}" "Quit"; | |
do | |
if [ "$version" == "Quit" ]; then | |
echo "❌ 退出安装" | |
exit 0 | |
fi | |
if [ -n "$version" ]; then | |
echo "✅ 已选择 版本: $version" | |
selected="$version" | |
break | |
else | |
echo "⚠️ 请输入正确的序号" | |
continue; | |
fi | |
done | |
VER="$selected" | |
fi | |
ARCH=$(uname -m) | |
REAL_ARCH="" | |
case "$ARCH" in | |
x86_64) | |
REAL_ARCH="x64" | |
;; | |
aarch64|arm64) | |
REAL_ARCH="arm64" | |
;; | |
*) | |
echo "❌ 未知的架构版本:$ARCH" | |
exit 1 | |
;; | |
esac | |
echo "ARCH=$ARCH REAL_ARCH=$REAL_ARCH VER=$VER" | |
VSCODE_TAR_FILE="vscode-server_${VER}_${REAL_ARCH}.tar.gz" | |
if [ ! -f "$VSCODE_TAR_FILE" ]; then | |
echo "❌ 未找到 $VSCODE_TAR_FILE,请先下载(https://github.com/MikeWang000000/vscode-server-centos7/releases)。" | |
exit 1 | |
fi | |
echo "ℹ️ file: $VSCODE_TAR_FILE,applying to ~/.vscode-server" | |
mkdir -p ~/.vscode-server | |
tar xzf "$VSCODE_TAR_FILE" -C ~/.vscode-server --strip-components 1 | |
~/.vscode-server/code-latest --patch-now | |
echo "✅ VSCode Server $VER 已安装" | |
check_and_enable_ssh_tcp_forwarding() { | |
local config="/etc/ssh/sshd_config" | |
local backup="/etc/ssh/sshd_config.bak.$(date +%Y%m%d%H%M%S)" | |
echo "🔍 检查 SSH TCP 转发配置..." | |
# 检查是否已设置为 yes | |
if grep -Ei '^AllowTcpForwarding[[:space:]]+yes' "$config" >/dev/null; then | |
echo "✅ TCP 转发已开启。" | |
return 0 | |
fi | |
# 备份配置文件 | |
echo "📝 备份配置到 $backup" | |
cp "$config" "$backup" | |
if grep -Ei '^AllowTcpForwarding' "$config" >/dev/null; then | |
# 修改现有配置为 yes | |
echo "✏️ 正在启用 TCP 转发..." | |
sed -i 's/^AllowTcpForwarding.*/AllowTcpForwarding yes/I' "$config" | |
else | |
# 添加配置到文件末尾 | |
echo "➕ 添加 AllowTcpForwarding yes 到配置文件末尾..." | |
echo "AllowTcpForwarding yes" >> "$config" | |
fi | |
# 重启 sshd 服务 | |
echo "🔁 重新加载 sshd 服务..." | |
if systemctl is-active sshd >/dev/null 2>&1; then | |
systemctl reload sshd | |
elif systemctl is-active ssh >/dev/null 2>&1; then | |
systemctl reload ssh | |
else | |
echo "⚠️ 无法识别 ssh 服务名,请手动重启 sshd" | |
return 1 | |
fi | |
echo "✅ TCP 转发已启用并应用。" | |
} | |
#!/bin/bash | |
ensure_inotify_watches_limit() { | |
local current_val | |
current_val=$(cat /proc/sys/fs/inotify/max_user_watches) | |
echo "🔍 当前 max_user_watches 值为: $current_val" | |
if [ "$current_val" -lt 10000 ]; then | |
echo "⚠️ 当前值小于 10000,准备修改为 524288..." | |
# 检查是否已存在该配置 | |
if grep -q '^fs.inotify.max_user_watches=' /etc/sysctl.conf; then | |
echo "🛠️ 已有配置,正在更新 fs.inotify.max_user_watches..." | |
sudo sed -i 's/^fs\.inotify\.max_user_watches=.*/fs.inotify.max_user_watches=524288/' /etc/sysctl.conf | |
else | |
echo "➕ 追加配置到 /etc/sysctl.conf..." | |
echo 'fs.inotify.max_user_watches=524288' | sudo tee -a /etc/sysctl.conf | |
fi | |
echo "🔄 正在使配置立即生效..." | |
sudo sysctl -p | |
echo "✅ 设置完成!" | |
else | |
echo "👌 当前值大于或等于 10000,无需修改。" | |
fi | |
} | |
check_and_enable_ssh_tcp_forwarding | |
ensure_inotify_watches_limit | |
echo "done." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment