# Linux环境配置
## 系统配置
### 代理设置
```bash=
# 设置代理(通过宿主机)
export http_proxy="http://192.168.31.14:7890"
export https_proxy=$http_proxy
# 取消代理
unset http_proxy
unset https_proxy
```
**提示:** 可将代理配置添加到 `~/.bashrc` 或 `~/.zshrc` 以永久生效。
### APT 软件源
- 镜像源
- [阿里巴巴开源镜像](https://developer.aliyun.com/mirror/)
```bash=
# 备份原有源
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
# 写入阿里云镜像源
# 适用于 **Ubuntu 22.04 (Jammy)**
sudo bash -c 'cat << EOF > /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF'
# 更新软件源
sudo apt update && sudo apt upgrade -y
```
### 时区配置
```bash=
# 交互式选择时区
sudo tzselect
# 设置为上海时区
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 或使用 timedatectl(推荐)
sudo timedatectl set-timezone Asia/Shanghai
```
## 软件
### Samba
```bash=
# 1. 安装 Samba
sudo apt install samba samba-common -y
# 2. 编辑配置文件
sudo vim /etc/samba/smb.conf
```
在文件末尾添加:
```ini=
[share]
comment = Share Folder
browseable = yes
path = /home/share # 建议使用具体路径而非根目录
create mask = 0755
directory mask = 0755
valid users = your_username # 指定允许访问的用户
writable = yes
```
```bash=
# 3. 创建共享目录
sudo mkdir -p /home/share
sudo chmod 755 /home/share
# 4. 添加 Samba 用户
sudo smbpasswd -a your_username
# 5. 重启服务
sudo systemctl restart smbd
sudo systemctl enable smbd # 开机自启
```
**安全建议:**
- 避免共享根目录 `/`
- 使用 `valid users` 限制访问
- 设置合理的权限掩码
### Zsh + Oh My Zsh
- Zsh
- [Install oh-my-zsh](https://ohmyz.sh/#install)
```bash=
# 1. 安装 Zsh
sudo apt install zsh -y
# 2. 设置为默认 Shell
chsh -s $(which zsh)
# 3. 安装 Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 4. 推荐插件(编辑 ~/.zshrc)
plugins=(
git
sudo
z
extract
command-not-found
colored-man-pages
zsh-autosuggestions
zsh-syntax-highlighting
zsh-completions
autojump
docker
docker-compose
)
```
#### 主题
- Powerlevel10k
- [romkatv/powerlevel10k](https://github.com/romkatv/powerlevel10k)
```shell=
# 安装主题(Zsh)
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
# 编辑 ~/.zshrc
ZSH_THEME="powerlevel10k/powerlevel10k"
```
#### 常用插件说明
| 插件 | 功能 |
|------|------|
| **git** | Git 命令别名和自动补全 |
| **sudo** | 按两次 ESC 在命令前添加 sudo |
| **z** | 快速跳转到常用目录 |
| **extract** | 统一的解压命令 `x filename` |
| **zsh-autosuggestions** | 根据历史命令自动建议(灰色提示) |
| **zsh-syntax-highlighting** | 命令语法高亮显示 |
| **colored-man-pages** | 彩色 man 手册页 |
| **autojump** | 使用 `j` 命令快速跳转 |
| **command-not-found** | 命令未找到时提示安装包 |
#### 需要单独安装的热门插件
- zsh-autosuggestions
- 自动建议
- zsh-syntax-highlighting
- 语法高亮
- zsh-completions
- 额外的自动补全
- autojump
- 快速目录跳转
```bash=
# zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# zsh-completions
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
# autojump
apt install autojump
```
### Warp
```shell=
sudo apt-get install wget gpg
wget -qO- https://releases.warp.dev/linux/keys/warp.asc | gpg --dearmor > warpdotdev.gpg
sudo install -D -o root -g root -m 644 warpdotdev.gpg /etc/apt/keyrings/warpdotdev.gpg
sudo sh -c 'echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/warpdotdev.gpg] https://releases.warp.dev/linux/deb stable main" > /etc/apt/sources.list.d/warpdotdev.list'
rm warpdotdev.gpg
sudo apt update && sudo apt install warp-terminal
```
### VNC
- VNC
- [Ubuntu VNC 远程桌面搭建指南](https://hackmd.io/UBmiHVUNSUSJsWpiDp8-Nw)
### Chrome DevTools Protocol (CDP)
```shell=
# ========== 服务器端配置 ==========
# 1. 使用 tmux/screen 确保进程持久化
sudo apt install tmux
# 2. 创建持久会话
tmux new -s browser
# 3. 在 tmux 中启动 Chrome(仅监听 localhost)
google-chrome \
--headless=new \
--remote-debugging-port=9222 \
--remote-debugging-address=127.0.0.1 \
--user-data-dir=/tmp/chrome-profile \
--disable-gpu \
--no-sandbox
# 4. 分离会话(Ctrl+B 然后按 D)
# 浏览器将在后台持续运行
# ========== 本地电脑操作 ==========
# 5. 建立 SSH 隧道(可以随时断开重连)
ssh -L 9222:localhost:9222 -N -f root@154.9.226.147
# 6. 访问调试界面
# 方式1:chrome://inspect
# 方式2:http://localhost:9222
# 7. 或使用 Puppeteer/Playwright 编程控制
```
```shell=
# Google 官方源
# 1. 添加 Google 的 GPG 密钥
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
# 2. 添加 Chrome 源
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# 3. 更新并安装
sudo apt update
sudo apt install google-chrome-stable
```
## Debian/Ubuntu
### 基础工具
```bash=
# 网络工具
sudo apt install net-tools curl wget -y
# 编辑器
sudo apt install vim -y
# SSH 服务
sudo apt install openssh-server -y
sudo systemctl enable ssh
# 文件树显示
sudo apt install tree -y
# Git
sudo apt install git -y
```
### 桌面应用
#### Google Chrome
```bash=
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb -y
```
#### Visual Studio Code
```bash=
# 方法1:使用 Snap
sudo snap install --classic code
# 方法2:使用官方仓库
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt update
sudo apt install code -y
```
#### Clash 代理工具
```bash=
# 下载并安装
wget -O clash.gz https://github.com/Dreamacro/clash/releases/download/v1.17.0/clash-linux-amd64-v1.17.0.gz
gunzip clash.gz
chmod +x clash
# 移动到系统目录
sudo mkdir -p /opt/bin
sudo mv clash /opt/bin/
# 创建配置目录
mkdir -p ~/.config/clash
cd ~/.config/clash
# 下载配置文件(需要自行获取订阅链接)
# wget -O config.yaml "your_subscription_url"
# 运行 Clash
/opt/bin/clash -d ~/.config/clash
```
**设置开机自启(可选):**
```bash=
sudo tee /etc/systemd/system/clash.service > /dev/null <<EOF
[Unit]
Description=Clash Daemon
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=/opt/bin/clash -d /home/$USER/.config/clash
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable clash
sudo systemctl start clash
```
## FAQ
### 修复 sudo 权限问题
当普通用户无法使用 sudo 时:
```bash=
# 1. 切换到 root
su root
# 2. 编辑 sudoers 文件
visudo
# 3. 添加以下行(替换 username 为实际用户名)
username ALL=(ALL:ALL) ALL
# 或者将用户添加到 sudo 组
usermod -aG sudo username
```
**注意:** 使用 `visudo` 而非直接编辑 `/etc/sudoers`,可防止语法错误导致系统问题。