SSH
Setting up SSH in OS X
Quick setup guide for enabling SSH access to a Mac from another machine.
On Target Mac (Server)
Enable SSH
- macOS Ventura/Sonoma: System Settings → General → Sharing → Toggle "Remote Login" ON
- Older macOS: System Preferences → Sharing → Check "Remote Login"
- Note the IP address displayed (e.g.,
192.168.1.XXX
)
Get IP Address
<syntaxhighlight lang="bash"> ifconfig | grep "inet " | grep -v 127.0.0.1 </syntaxhighlight>
Prevent Sleep (Optional)
<syntaxhighlight lang="bash">
- Disable sleep on AC power
sudo pmset -c sleep 0 sudo pmset -c disablesleep 1
- Keep WiFi alive during sleep
sudo pmset -c womp 1 </syntaxhighlight>
On Client Mac (Controller)
Test Connection
<syntaxhighlight lang="bash"> ssh [email protected] </syntaxhighlight>
Setup SSH Keys
<syntaxhighlight lang="bash">
- Generate key if needed
ls ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
- Copy key to target Mac
ssh-copy-id [email protected] </syntaxhighlight>
Create SSH Config
Add to ~/.ssh/config
:
<syntaxhighlight lang="text">
Host imac
HostName 192.168.1.XXX User username ServerAliveInterval 30 ServerAliveCountMax 3
</syntaxhighlight>
Now connect with: ssh imac
For Unstable Connections
Install Mosh
On both machines: <syntaxhighlight lang="bash"> brew install mosh </syntaxhighlight>
Connect with: mosh [email protected]
Troubleshooting
SSH Connection Refused
<syntaxhighlight lang="bash">
- Check if SSH is running
sudo systemsetup -getremotelogin
- Force enable
sudo systemsetup -setremotelogin on
- Check firewall
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate </syntaxhighlight>
WiFi Keepalive Script
<syntaxhighlight lang="bash"> cat > ~/keepalive.sh << 'EOF'
- !/bin/bash
while true; do
ping -c 1 192.168.1.1 # Router IP sleep 30
done EOF
chmod +x ~/keepalive.sh nohup ~/keepalive.sh & </syntaxhighlight>