How to Setup Automatic SSH, a tutorial
(logging into your class account without a password!)
Sick of having to type in both ssh cs61a-XX@star.cs.berkeley.edu
and your password everytime you need to login to your class account from your laptop? Here's a short tutorial that can allow you to automatically ssh into your class account (without a password).
There are two parts!
- Part 1: Getting rid of typing your password so you can type
ssh LOGIN@SERVER
and scp FILE LOGIN@SERVER:
to login and scp your files to your server without a password
- Part 2: Creating a shortcut "alias" for your LOGIN@SERVER so you can just type
ssh SHORTCUT
and scp FILE SHORTCUT
Before we launch into things, make sure you're in a proper bash environment on your personal computer. If you're an Windows user, make sure you're either using Git Bash or Cygwin (no Command Prompt here!).
Alright, let's get started!
Part 1: getting rid of that password typin' business.
from the terminal of your choice, entercd ~/.ssh
** If you don't have a .ssh
directory, make one by typing "mkdir ./ssh"
once you're inside your ~/.ssh
directory, ls
to see the contents.
If you see a NAME and NAME.pub file within your directory already, you can skip the next step (This means you already have a private and public key generated that you can use)
if there is nothing in your ~/.ssh
directory, we need to generate a private/public key, which you can do by typing ssh-keygen
This will generate you a private and public key pair inside your .ssh
directory. The script will prompt you to "Enter file in which to save the key", which will generate NAME.pub depending on what you string choose to use for NAME. You may choose to protect this public key with a passphrase. Once you have finished following the prompt, you may type ls
once more to view your directory.
you should see two files NAME and NAME.pub. NAME is your private key and NAME.pub is your public key. Most likely, you should expect to see id_rsa and id_rsa.pub.
Once you've done this, you're ready to authorize your public key on your class account.
You can now type the following into your terminal cat ~/.ssh/NAME.pub | ssh LOGIN@SERVER "cat >> ~/.ssh/authorized_keys"
*** NOTE: If you encounter errors, you should type this out yourself. Sometimes wonky things happen with quotations copied and pasted in.
Replace the uppercase words with the appropriate values
i.e. NAME --> id_rsa, LOGIN --> cs61a-XX SERVER --> star.cs.berkeley.edu
Now enter in your password when it prompts you. Now, in the future, you should be able to automatically access your class account without typing in your password!
Part 2: creating a shortcut for your login
Now, let's shorten ssh cs61a-XX@star.cs.berkeley.edu
to just ssh cs61a
from your ~/.ssh directory, we're going to create a config
file.
first, type vim config
within your ssh directory.
This will open up the text editor vim within your terminal. From here, you can press "i" to put you into INSERT mode. This lets you edit the file.
Type the following into your fileHost cs61a
Hostname star.cs.berkeley.edu
User cs61a-XX
Then press "ESC" and type ":wq". This saves and quits vim.
Now if you type ls
you should see config, id_rsa, and id_rsa.pub in your ~/.ssh
directory. Or something along the lines of this:
[15:26:44] michellehwang:~/.ssh $ ls
config id_rsa id_rsa.pub known_hosts
And you're set! Now you can just type "ssh cs61a" and you'll automatically be logged into your class account from your terminal.
Now, you can copy files and whatnot to and from your class account like this:
to copy files TO your class account:
scp FILE_NAME cs61a:
to copy files FROM your class account:
scp cs61a:PATH .