Using SSH

Why SSH?

  • You need to access a remote server
  • You need a file from a server
  • There is no graphical interface to interact with


You will be using SSH for your research!

How it works

  • You need to properly setup your ~/.ssh folder
  • Go to the terminal
  • ssh username@remote_server.org
  • Use the Terminal to move around the directories

How it works

ssh_main

How it works

ssh_main

Logging in

   ssh username@remoteserver.org

Setting it up

Before you can log in to remote servers, you need to:

  • Set up the SSH Folder
  • Create a configuration file
  • Add authorized keys
  • Create SSH keys

SSH Folder


                        cd $HOME
                        mkdir ~/.ssh
                        chmod 700 ~/.ssh
                        
  • This creates the folder ~/.ssh
  • Modifies permissions of the folder

SSH Configuration File


                        cd ~/.ssh
                        touch config
                        chmod 600 config
                        
  • This creates the file config
  • This file is used to configure future SSH connections
  • Modifies the user permissions of the file

"Authorized_Keys" file


                        cd ~/.ssh
                        touch authorized_keys
                        chmod 700 authorized_keys
                        
  • This creates the file authorized_keys
  • This file provides the keys of authorized users/machines
  • Modifies the user permissions of the file

Connections folder


                        cd ~/.ssh
                        mkdir connections
                        chmod 700 connections
                        
  • This creates the directory connections
  • Modifies the user permissions of the directory

SSH-Keys Folder


                        cd ~/.ssh
                        mkdir ssh_keys
                        chmod 700 ssh_keys
                        
  • This creates the folder ssh_keys
  • For private SSH keys
  • Modifies the user permissions of the folder

Public Keys Folder


                        cd ~/.ssh
                        mkdir pub_keys
                        chmod 700 pub_keys
                        
  • This creates the folder pub_keys
  • For public SSH keys
  • Modifies the user permissions of the folder

At this point, your ~/.ssh

folder should look like this:

$ ls -lah ~/.ssh

drwx------    8 user  staff   256B Jan 21 18:37 ./
drwxr-xr-x@ 161 user  staff   5.0K Jan 21 20:24 ../
-rw-------@   9 user  staff   288B Jan 21 18:37 authorized_keys
-rw-------@   1 user  staff   1.4K Jan 21 19:03 config
drwx------    2 user  staff    64B Jan 22 16:37 connections/
drwx------    2 user  staff    64B Jan 22 16:37 pub_keys/
drwx------    2 user  staff    64B Jan 22 16:37 ssh_keys/
                        

Now you can access a remote computer


                        
                        ssh username@123456.server.io
                        
                        

SSH Config File

SSH Config File

This file acts as the file with predefined options for how to connect through SSH

SSH Config File

To modify it, type


                        
                        open ~/.ssh/config
                        
                        

This will open the config file.

You will now add the global settings

SSH Config File

Add this to the config file:


                        
                        Host *
                        ControlMaster auto
                        ControlPath ~/.ssh/connections/%C
                        ControlPersist 1m
                        ServerAliveInterval 30
                        ServerAliveCountMax 10
                        
                        

These settings let you keep the SSH connection alive, and more!

SSH Config File

If you're on a MAC and would like to use X11 as well, add these extra lines beneath


                        
                        XAuthLocation /opt/X11/bin/xauth
                        AddKeysToAgent yes
                        UseKeychain yes
                        
                        

Connecting to Github

Once you have your ~/.ssh/config file setup, you can add your Github info to it.

You you just need to add this below the previous code:


                        
                        Host github.com
                        HostName github.com
                        User git
                        IdentityFile ~/.ssh/ssh_keys/github_key
                        IdentitiesOnly yes
                        PreferredAuthentications publickey
                        
                        

This will tell git to use the public key github_key, which you will create later.

Forwarding X11

If you happen to plot on remote servers, you might want to use XQuartz (X11). If yo, you will need to add the following line to the ~/.ssh/config file, below the Host info:


                        
                        ForwardX11 yes
                        
                        

Your ~/.ssh/config file should look something like this now (scroll):


                        
                        Host *
                        ControlMaster auto
                        ControlPath ~/.ssh/connections/%C
                        ControlPersist 1m
                        ServerAliveInterval 30
                        ServerAliveCountMax 10
                        XAuthLocation /opt/X11/bin/xauth
                        AddKeysToAgent yes
                        UseKeychain yes

                        ## Connects to Github
                        Host github.com
                        HostName github.com
                        User git
                        IdentityFile ~/.ssh/ssh_keys/github_key
                        IdentitiesOnly yes
                        PreferredAuthentications publickey

                        ## Connects to a remote Server via SSH
                        Host server_name
                        HostName path.to.server
                        User username
                        IdentityFile ~/.ssh/ssh_keys/server_key
                        IdentitiesOnly yes
                        PreferredAuthentications publickey
                        ForwardX11 yes
                        
                        

Remember to change server and path.to.server with the real values.

Config File

  • server: Server to which you want to connect
  • path.to.server: URL of the server

Config File

For more information, go to:

https://vanderbilt-astro-starting-grad-school.readthedocs.io/en/latest/mac_101.html#ssh

SSH Keys

SSH Keys

  • SSH keys are comprised of a public and private key.
  • If you connect to a server that has your public key and you can provide your private key, it will let you in.
  • If the private key is stolen, someone else cna log into your account!!

To generate your pair of keys


                        $ cd ~/.ssh
                        $ ssh-keygen -t rsa -b 4096
                        $ Generating public/private rsa key pair.
                        Enter file in which to save the key (/Users/calder/.ssh/id_rsa): id_rsa_4096
                        Enter passphrase (empty for no passphrase):
                        Enter same passphrase again:
                        $ ls
                        id_rsa_4096
                        id_rsa_4096.pub
                        $ chmod 600 id_rsa*
                        $ mv id_rsa_4096 ssh_keys/
                        $ mv id_rsa_4096.pub pub_keys
                        

To generate your pair of keys

Now you can add your pair of SSH keys:


                        ssh-add ~/.ssh/ssh_keys/*
                        


or, if you're on a Mac


                        ssh-add -K ~/.ssh/ssh_keys/*
                        

To generate your pair of keys

If you enter a passphrase, you will need to type that password every time you use the SSH keys (e.g. when connecting to a server). It's common to not create a password, but know that if the private key is lost, anyone can use them.

But they would have to know which server to connect to, which "config" file will provide!

Back to main website:

https://tinyurl.com/bcb18