SSH Reference
- creating keys
- common errors
- ssh between machines
- handling ssh with git and different accounts
ssh into remote server
ssh <remote-username>@<remote_domain/ip>
Examples
If a ssh key pair is found a new session will start. If not and the machine allows ssh access via user name and password a prompt for password will occur.
Move File or Directories between Machines
scp <source> <destination>
Files
scp /path/to/file username@:host/path/to/destination
scp username@host:/path/to/file /path/to/destination
Example
scp [email protected]:~/foo ~/foo
Dir
scp -r /path/to/dir username@:host/path/to/destination
scp -r username@host:/path/to/dir /path/to/destination
Set-Up ssh Access
Generate Key
ssh-keygen -t rsa
Github Example
ssh-keygen -t ed25519 -C "[email protected]"
For a single service use the default file .ssh/id_rsa
but for multiple services follow the steps below.
Generate SSH Key and Add It to Remote Machine
ssh-copy-id -i ~/.ssh/<name-public-key>.pub <server_user_name>@<ip_add_server>
Setting Up Unique SSH for Multiple Services
Make a config
touch ~/.ssh/config
Example config
config
# contents of $HOME/.ssh/configHost github.comIdentityFile ~/.ssh/github.keyHost gitlab.comIdentityFile ~/.ssh/gitlab.key
Now when ssh-keygen -t rsa
asks for a file specify the file associated with the domain or service
Example key gen
Generating public/private ed25519 key pair.Enter file in which to save the key (/Users/<foo>/.ssh/id_ed25519): github.key
ssh-keygen -t ed25519 -C <github-email>
π‘ note: if the file does not show up in .ssh/
using the full path should correct
Add Host to known Hosts
ssh auth Error
"The authenticity of host '216.128.129.16 (216.128.129.16)' can't be established.ED25519 key fingerprint is SHA256:nnzspBxbguoVHYiPiPEL6smGCfyE2KRSMLmayeaH3Mw.This key is not known by any other names"Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
ssh-keyscan -H <ip.address> >> ~/.ssh/known_hosts
Add to Service
π₯ make sure to use the .pub
extension as this is where our public key is stored
pbcopy < ~/.ssh/github.key.pub
Add this to the services we want to Authorize.
Testing and Troubleshooting Connections
Test ssh connection
ssh -T [email protected]
ssh -T [email protected]
Verbose tst
ssh -Tv [email protected]
We can also test a specific host from out ~/.ssh/config
Use with Github, BitBucket, Gitlab etc
Cloning a new project
Clone repos with the host name declared in ssh config π
git clone git@work:username/my-work-project.gitgit clone git@personal:username/my-personal-project.git
For reference, this is the default behavior
git clone [email protected]:username/my-work-project.gitgit clone [email protected]:username/my-personal-project.git
Updating and existing repo
To update and existing repo to push to a specific github account/profile. Navigate to the project .git/config
and add the Host
from .ssh/config
as the host in the ssh url.
Example
.git/config
...[remote "origin"]url = git@work-profile:username/work-project-two.git...
SSH Between Two Mac on LAN
Common Friction Points
You can try the following but it can fail Full Disk Access privileges.
error.
sudo systemsetup -setremotelogin on
If πthat doesn't work try π
## Enabling SSHsudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
to turn remote access off as a security best practice.
## Disabling SSHsudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
Check Status
# To check if is on or offsudo systemsetup -getremotelogin