@@ -33,6 +33,7 @@ const SSH_GUEST_DIR: &str = "/root/.vibebox";
3333const DEFAULT_SSH_USER: &str = "vibebox";
3434const SSH_CONNECT_RETRIES: usize = 20;
3535const SSH_CONNECT_DELAY_MS: u64 = 500;
36+const SSH_SETUP_SCRIPT: &str = include_str!("ssh.sh");
3637
3738#[derive(Debug, Clone, Serialize, Deserialize)]
3839struct InstanceConfig {
@@ -61,7 +62,12 @@ pub fn run_with_ssh(
6162 let instance_dir = ensure_instance_dir(&project_root)?;
6263 let (ssh_key, _ssh_pub) = ensure_ssh_keypair(&instance_dir)?;
6364
64− let config = load_or_create_instance_config(&instance_dir)?;
65+ let mut config = load_or_create_instance_config(&instance_dir)?;
66+ // Clear cached IP to avoid reusing a stale address on startup.
67+ if config.vm_ipv4.is_some() {
68+ config.vm_ipv4 = None;
69+ write_instance_config(&instance_dir.join(INSTANCE_TOML), &config)?;
70+ }
6571 let config = Arc::new(Mutex::new(config));
6672
6773 let extra_shares = vec![DirectoryShare::new(
@@ -244,40 +250,15 @@ fn build_ssh_login_actions(
244250
245251 let key_path = format!("{guest_dir}/{key_name}.pub");
246252
247− let mask_cache = format!(
248− "if [ -d /root/{project_name}/.vibebox ]; then mount -t tmpfs tmpfs /root/{project_name}/.vibebox; fi"
249− );
253+ let setup_script = SSH_SETUP_SCRIPT
254+ .replace("__SSH_USER__", &ssh_user)
255+ .replace("__SUDO_PASSWORD__", &sudo_password)
256+ .replace("__PROJECT_NAME__", project_name)
257+ .replace("__KEY_PATH__", &key_path);
258+ let setup = vm::script_command_from_content("ssh_setup", &setup_script)
259+ .expect("ssh setup script contained invalid marker");
250260
251− let setup_lines = [
252− "if ! command -v sshd >/dev/null 2>&1; then apt-get update && apt-get install -y openssh-server sudo; fi",
253− "systemctl enable ssh >/dev/null 2>&1 || true",
254− &format!("id -u {ssh_user} >/dev/null 2>&1 || useradd -m -s /bin/bash {ssh_user}"),
255− &format!("echo \"{ssh_user}:{sudo_password}\" | chpasswd"),
256− &format!("usermod -aG sudo {ssh_user}"),
257− &format!("install -d -m 700 /home/{ssh_user}/.ssh"),
258− &format!("install -m 600 {key_path} /home/{ssh_user}/.ssh/authorized_keys"),
259− &format!("chown -R {ssh_user}:{ssh_user} /home/{ssh_user}/.ssh"),
260− "mkdir -p /etc/ssh/sshd_config.d",
261− "cat >/etc/ssh/sshd_config.d/vibebox.conf <<'VIBEBOX_SSHD'",
262− "PasswordAuthentication no",
263− "KbdInteractiveAuthentication no",
264− "ChallengeResponseAuthentication no",
265− "PubkeyAuthentication yes",
266− "PermitRootLogin no",
267− &format!("AllowUsers {ssh_user}"),
268− "VIBEBOX_SSHD",
269− "systemctl restart ssh",
270− "echo VIBEBOX_SSH_READY",
271− ];
272− let setup = setup_lines.join("\n");
273−
274− let ip_probe = "while true; do ip=$(ip -4 -o addr show scope global | awk '{print $4}' | cut -d/ -f1 | head -n 1); if [ -n \"$ip\" ]; then echo VIBEBOX_IPV4=$ip; break; fi; sleep 1; done";
275−
276− vec![
277− LoginAction::Send(mask_cache),
278− LoginAction::Send(setup),
279− LoginAction::Send(ip_probe.to_string()),
280− ]
261+ vec![LoginAction::Send(setup)]
281262}
282263
283264fn spawn_ssh_io(