Skip to content

Instantly share code, notes, and snippets.

@shreewatsa
Last active July 7, 2023 06:30
Show Gist options
  • Save shreewatsa/ba139d5748a861f0d8dca40eef6c4b58 to your computer and use it in GitHub Desktop.
Save shreewatsa/ba139d5748a861f0d8dca40eef6c4b58 to your computer and use it in GitHub Desktop.
SSH Configurations

Copy public key to remote for passwordless authentication:

chmod 600 ~/.ssh/id_rsa;            # set keys permissions
cat id_rsa.pub | ssh [email protected] 'cat >> /home/ubuntu/.ssh/authorized_keys’;
/usr/bin/ssh-add --apple-use-keychain ~/.ssh/id_rsa;  # Run the command given below if you don't want to type private key password every time using the public key to make ssh connection. It stores the passphrase for private key in OSX Keychain, you can verify this by opening "Keychain Access" app. 
ssh-keygen -y -P "password" -f ~/.ssh/id_rsa;  # Verify if the password is correct decrypt the private key.
ssh-keygen -p -f ~/.ssh/id_rsa;     # Run this line to reset the passwor for case like you mistakenly entered wrong password in above command.

SSH Special Characters :

$~.<CR>       # ie Tilde Dot To get back the terminal that hung up after ssh disconnection.
$~ <CR>       # To open use cases docs.
$~? <CR>      # To open SSH help on special characters.
$^Z           # To open local terminal while keep ssh connection in backgrouind. Type ‘fg’ to get back to the connection.

Open tmux on SSH login into remote:

function ssht () {/usr/bin/ssh -t "$@" "tmux new -A -s remote_tmux_session_name";}  # Add this line to bashrc.
$ssht remote_host;      # Usage of ssht function.
$ssh remote_host -t -- "tmux -A -s remote_tmux_session_name";  # Alternative to above.

Client Machine SSH Config:

$vim ~/.ssh/config;

Host remote_host
    HostName 10.11.12.13
    User remote_user
    LocalForward 27017 127.0.0.1:27017   # Syntax: LocalForward <local_port> localhost:<remote_port>
    HostKeyAlgorithms=+ssh-rsa
    PubkeyAcceptedKeyTypes=+ssh-rsa


Host *
    LogLevel INFO
    Compression yes
    
Host dev intranet* backup
  HostName %h.internal.example.com

# Connection Sharing. Note: scp, rsync, git also use the shared connection.
ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r # OR /tmp/ssh_mux_%C . remove this file if connection terminates abruptly.
ControlPersist 4h                 # Persist connection between ssh logins for 4 hours. Val: Any number of hours.

Remote Machine SSH Config:

$sudo vim /etc/ssh/sshd_config;

ClientAliveCountMax 2
ClientAliveInterval 3600          # Total time interval: 2*3600 = 7200 seconds ie 2 hr.

AllowTcpForwarding yes            # Enable SSH Port Forwarding.
PermitRootLogin yes               # To allow ssh login to root user.

$sudo systemctl reload sshd;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment