基本配置:1.
配置文件(~/.config/code-server/config.yaml):bind-addr: 0.0.0.0:8080
auth: password
password: secure_password_here
cert: false
proxy-domain: code.yourdomain.com
2.
身份验证方式:# Password authentication (default)
auth: password
password: your_password
# Disable authentication (dangerous, use with reverse proxy auth)
auth: none
# Use hashed password
hashed-password: "$argon2i$v=19$m=4096,t=3,p=1$..."
3.
环境变量:# Set password via env
export PASSWORD="your_password"
# Disable telemetry
export TELEMETRY_LEVEL="off"
# Custom extensions directory
export EXTENSIONS_GALLERY='{"serviceUrl": "https://open-vsx.org/vscode/gallery"}'
4.
端口转发配置:# Forward local dev server to public URL
# In code-server terminal:
npm run dev # Starts on localhost:3000
# code-server automatically detects and offers to forward
# Access at: https://code.yourdomain.com/proxy/3000/
5.
扩展配置:# Install from marketplace
code-server --install-extension publisher.extension-name
# Install from VSIX
code-server --install-extension /path/to/extension.vsix
# List installed extensions
code-server --list-extensions
# Uninstall extension
code-server --uninstall-extension publisher.extension-name
6.
设置同步:// settings.json location: ~/.local/share/code-server/User/settings.json
{
"workbench.colorTheme": "Default Dark+",
"editor.fontSize": 14,
"editor.formatOnSave": true,
"files.autoSave": "afterDelay",
"terminal.integrated.shell.linux": "/bin/bash"
}
7.
多用户设置:# Create service for user1
sudo systemctl enable --now code-server@user1
# Create service for user2
sudo systemctl enable --now code-server@user2
# Each user gets own config in their home directory
# ~/.config/code-server/config.yaml
8.
安全加固:# Limit network access
bind-addr: 127.0.0.1:8080 # Only localhost
# Use strong password (20+ chars)
password: $(openssl rand -base64 32)
# Enable HTTPS on reverse proxy
# Use fail2ban for brute force protection
sudo apt install fail2ban
# Configure fail2ban for code-server
sudo nano /etc/fail2ban/jail.local
添加:
[nginx-code-server]
enabled = true
port = 80,443
filter = nginx-code-server
logpath = /var/log/nginx/access.log
maxretry = 3
bantime = 3600
9.
性能调优:# Increase file watcher limit
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# Disable telemetry
export TELEMETRY_LEVEL=off
# Use faster protocol
# In settings.json:
{
"terminal.integrated.rendererType": "dom"
}
10.
备份与迁移:# Backup user data
tar -czf code-server-backup.tar.gz \
~/.local/share/code-server \
~/.config/code-server
# Restore on new server
tar -xzf code-server-backup.tar.gz -C ~
最佳实践:- 使用反向代理(Nginx/Caddy)实现 SSL 终止
- 启用 Let's Encrypt 自动 HTTPS
- 设置高强度密码(20 个以上字符)
- 定期更新 code-server:
curl -fsSL https://code-server.dev/install.sh | sh- 将项目目录单独挂载,便于备份
- 使用 systemd 在故障时自动重启
- 在反向代理上配置速率限制
- 启用 fail2ban 防止暴力破解
- 使用 OAuth2 代理实现 SSO 集成
- 按用户或团队分别部署开发、预发布和生产实例
- 查看日志:
journalctl -u code-server@$USER -f- 设置用户数据和配置的自动备份