# Connect with ssh and forward ports

Reach a machine's shell with ssh, and its servers through ports forwarded to your Mac.

## Reach a machine with ssh

To open a shell in `dev`, or run one command in it:

```sh
osmates ssh dev
osmates ssh dev -- uname -a
```

Linux machines from the supplied images are ready for ssh. macOS and Windows need one step first.

Each machine gets its own key, and your `~/.ssh/known_hosts` stays unchanged. The guest's host key is pinned at the first connection: if it changes, pass `--renew-host-key`. If a direct connection fails, `osmates ssh` connects through the guest agent and says so.

### Turn on Remote Login in macOS

When you create a macOS machine, turn on **Remote Login**. In Terminal, add `--remote-login` to `osmates vm create`.

For an existing machine:

1. In the machine, choose **Apple menu > System Settings**, then click **General** in the sidebar.
2. Click **Sharing**, then turn on **Remote Login**.
3. Before macOS 27, also [install the guest agent](https://osmates.com/docs/macos#install-an-older-macos).

### Add OpenSSH Server to Windows

To install the server, start it with Windows, and open its firewall on every network profile:

```sh
osmates vm exec win -- powershell -NoProfile -Command "Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0; Set-Service sshd -StartupType Automatic; Start-Service sshd; Set-NetFirewallRule -Name OpenSSH-Server-In-TCP -Profile Any"
```

The machine's key signs in to administrator accounts only.

## Reach machines by name

1. Choose **OSMates > Settings**, then click **Command Line**.
2. Turn on **ssh by Name**.

Each machine then answers to `dev` and `dev.osmates` in `ssh`, `scp`, `rsync`, and editors. If a name clashes with your SSH configuration, only the `.osmates` name works. **Open in VS Code** needs this.

In Terminal, `osmates ssh-config install` does the same. It backs up `~/.ssh/config` and adds an `Include` line at the top. To undo it, turn off **ssh by Name** or run `osmates ssh-config uninstall`.

## Forward a port to your Mac

To reach port 80 of the machine at `localhost:8080` on your Mac:

```sh
osmates vm set dev --forward 8080:80
```

A forward stays across starts, and reaches servers that listen only on the guest's loopback. Add `/udp` for UDP. To remove it, run `osmates vm set dev --unforward 8080`.

The machine's **Network** settings list its forwards under **Port Forwards**.

## Limits

- Forwards are set in Terminal only.
- Forwarded ports listen on your Mac's loopback only, so other computers can't reach them.
- Ports below 1024 are refused.
- Clones and `osmates run` have no forwarded ports.
- An agent needs your answer each time it adds a forward.
