我们在开发机器人的时候,如果全程让人工智能与,我们第一步是环境要给大模型初始化好,避免进程在等待,或因权限原因ai 不能办到换一个方法实现现,无数次
偿试浪费token ,所以我们得在环境上就规范起来,这样才有更好的开发环境,
!/bin/bash
set -e
echo ""
echo " ROS2 Humble 完整安装脚本 (Ubuntu 22.04)"
echo ""
设置 locale
echo "[1/8] 配置 locale..."
sudo apt update
sudo apt install -y locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
确保 universe 仓库已启用
echo "[2/8] 启用 universe 仓库..."
sudo apt install -y software-properties-common
sudo add-apt-repository universe -y
添加 ROS2 GPG 密钥
echo "[3/8] 添加 ROS2 GPG 密钥..."
sudo apt install -y curl gnupg lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
添加 ROS2 源
echo "[4/8] 添加 ROS2 apt 源..."
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
更新包索引
echo "[5/8] 更新 apt 包索引..."
sudo apt update
安装 ROS2 Humble 完整桌面版(desktop-full)
echo "[6/8] 安装 ROS2 Humble Desktop Full(完整安装,这可能需要较长时间)..."
sudo apt install -y ros-humble-desktop-full
安装开发工具
echo "[7/8] 安装 ROS2 开发工具(colcon、rosdep 等)..."
sudo apt install -y ros-dev-tools
sudo apt install -y python3-colcon-common-extensions
初始化 rosdep
echo "[8/8] 初始化 rosdep..."
sudo rosdep init 2>/dev/null || true
rosdep update
配置环境变量到 .bashrc
echo "" >> ~/.bashrc
echo "# ROS2 Humble 环境配置" >> ~/.bashrc
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
echo "export LANG=en_US.UTF-8" >> ~/.bashrc
立即加载环境
source /opt/ros/humble/setup.bash
echo ""
echo ""
echo " ROS2 Humble 安装完成!"
echo ""
echo ""
echo "已安装内容:"
echo " - ros-humble-desktop-full(完整桌面版)"
echo " - ros-dev-tools(开发工具)"
echo " - python3-colcon-common-extensions(colcon 构建工具)"
echo " - rosdep(依赖管理工具)"
echo ""
echo "环境已自动配置到 ~/.bashrc"
echo "新终端会自动加载 ROS2 环境"
echo ""
echo "验证安装:"
ros2 --version 2>/dev/null || echo "(需要新开终端或执行: source ~/.bashrc)"
echo ""
echo "常用命令:"
echo " ros2 run
echo " ros2 launch
echo " ros2 topic list 查看话题列表"
echo " colcon build 编译工作空间"
New System Setup Guide
Rule: First configure the development environment, then write code.
Phase 1: Windows PowerShell
1.1 Execution Policy
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force
Why: Default Restricted blocks all scripts. Every command reports profile.ps1 forbidden, wasting tokens on retries.
1.2 UTF-8 Encoding
Create or edit C:\Users\fgxwa\Documents\WindowsPowerShell\profile.ps1:
# UTF-8 encoding config
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8# conda initialize (keep your own conda path)
#region conda initialize
If (Test-Path "D:\ProgramData\anaconda3\Scripts\conda.exe") {(& "D:\ProgramData\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion
Why: Without UTF-8 config, Chinese output becomes garbled. Profile comments MUST be English only - Chinese comments in profile.ps1 become garbled even with UTF-8 set.
Verify:
# Close and reopen PowerShell, then:
# Should NOT see any "profile.ps1 forbidden" error
# Chinese characters should display correctly
Phase 2: Windows SSH Config
Edit C:\Users\fgxwa\.ssh\config:
Host ubuntu-vmHostName 192.168.241.138User xsyIdentityFile ~/.ssh/id_rsaServerAliveInterval 30ServerAliveCountMax 5TCPKeepAlive yesConnectTimeout 10Host *ServerAliveInterval 30ServerAliveCountMax 5TCPKeepAlive yesStrictHostKeyChecking accept-new
Why - ServerAliveInterval: SSH sends heartbeat every 30 seconds to prevent the connection from being killed by NAT/firewall during long operations (apt install, colcon build, etc.). Without this, SSH hangs silently with no output and no error - looks "frozen".
Why - StrictHostKeyChecking accept-new: After VM reinstall, the host key changes. Without this, SSH refuses to connect and you need to manually ssh-keygen -R each time.
Verify:
ssh ubuntu-vm "echo OK"
# Should connect immediately without password
Phase 3: Ubuntu Remote Server
3.1 SSH Server
sudo apt install -y openssh-server
sudo systemctl enable --now ssh
3.2 Passwordless Sudo (Critical)
echo "$(whoami) ALL=NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$(whoami)
sudo chmod 440 /etc/sudoers.d/$(whoami)
Why: When running sudo via non-interactive SSH, the terminal waits for password input forever. The command appears to "hang" with no output, no error, no progress. This wastes tokens on retries and confuses the user. With NOPASSWD, all sudo commands execute immediately.
Verify:
sudo whoami
# Should output "root" immediately without asking for password
3.3 SSH Keepalive (Server Side)
sudo bash -c 'echo -e "ClientAliveInterval 60\nClientAliveCountMax 3\nMaxSessions 20" > /etc/ssh/sshd_config.d/keepalive.conf'
sudo systemctl restart ssh
Why: Server-side keepalive complements the Windows client-side config. If the server detects no client activity for too long, it kills the connection. With ClientAliveInterval 60, the server sends a heartbeat every 60 seconds.
3.4 Disable System Version Upgrade
sudo sed -i 's/Prompt=lts/Prompt=never/' /etc/update-manager/release-upgrades
Why: Ubuntu periodically pops up a "Upgrade to 24.04 LTS" notification. Accidentally clicking it will break ROS2 Humble (which is tied to Ubuntu 22.04). Setting Prompt=never disables this notification forever while still allowing normal security updates via apt upgrade.
3.5 SSH Key Login (From Windows)
# On Windows, after Ubuntu SSH is running:
ssh-copy-id xsy@192.168.241.138
# Or manually:
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh ubuntu-vm "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Why: Without key-based auth, every SSH command requires password input. Non-interactive SSH (used by automation tools) cannot enter passwords and hangs forever.
Phase 4: Quick Verification
After all steps, run this to verify everything works:
ssh ubuntu-vm "sudo whoami && sudo apt update 2>&1 | tail -1 && echo ALL_OK"
Expected output:
root
Reading package lists... Done
ALL_OK
If any step fails, fix it before proceeding to actual development work.
Common Issues & Fixes
| Symptom | Cause | Fix |
|---|---|---|
profile.ps1 forbidden |
Execution policy Restricted | Set-ExecutionPolicy RemoteSigned |
| Chinese garbled output | Console not UTF-8 | Add UTF-8 config to profile.ps1 |
| SSH hangs with no output | Waiting for password (sudo or SSH) | Configure NOPASSWD sudo + SSH key |
| SSH hangs during long build | Connection killed by timeout | Add ServerAliveInterval to SSH config |
Host key changed after VM reinstall |
Old host key cached | ssh-keygen -R 192.168.241.138 |
| Upgrade to 24.04 popup | Default Prompt=lts | Prompt=never |
Connection refused |
SSH server not running | sudo systemctl start ssh |
One-Script Setup (Ubuntu Side)
Save as setup-env.sh on the Ubuntu VM and run after fresh install:
#!/bin/bash
# Phase 1: Passwordless sudo
echo "$(whoami) ALL=NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$(whoami)
sudo chmod 440 /etc/sudoers.d/$(whoami)# Phase 2: SSH keepalive
sudo bash -c 'echo -e "ClientAliveInterval 60\nClientAliveCountMax 3\nMaxSessions 20" > /etc/ssh/sshd_config.d/keepalive.conf'
sudo systemctl restart ssh# Phase 3: Disable version upgrade
sudo sed -i 's/Prompt=lts/Prompt=never/' /etc/update-manager/release-upgrades# Phase 4: Verify
echo ""
echo "=== Verification ==="
echo "Sudo: $(sudo whoami)"
echo "SSH: $(systemctl is-active ssh)"
echo "Upgrade prompt: $(grep Prompt /etc/update-manager/release-upgrades)"
echo "Keepalive: $(cat /etc/ssh/sshd_config.d/keepalive.conf)"
echo ""
echo "DONE. Now run 'ssh-copy-id xsy@192.168.241.138' from Windows."
