Use a config file to SSH into a server
It's annoying to remember passwords and host names when logging into servers.
Typically to ssh into a server we would need to enter:
$ ssh server.karandeep.com
But if the username on the account didn't match the current user on the local machine we'd have to login like this:
Then we would be prompted to enter a password.
There's a way we can easily get around this. Enter the "config" file.
The config file is stored in the .ssh directory, the same place we store our private key and public keys.
The first thing we need to do is generate a public-private key pair if we do not have one. You can easily do this by running the following command:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/karandeep/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Fill in the details. A passphrase is optional.
This will produce two files for you. A public key and a private key.
The public key: this goes on your server that you want to login to.
The private key: this lets you access a server that has your public key.
So now lets copy over that public key to our server. We can easily do that in a few simple commands:
2. create the .ssh directory if it does not exist. Note that "." before "ssh"
3. in a separate terminal, run the following to copy over the key
4. make sure the permissions are correct. if the permissions are wrong, you will be required to enter a password. The ".ssh" directory should be 700 and the files should be 600
Now the keys should be all setup. You should be able to easily login to the server without a password. Go ahead and try it:
But now wouldn't it be cool to just login without having to enter all of that? I want to be able to enter "ssh test" to login to my test instance.
Here's how we can do that with the config file:
1. Create a config file in the .ssh directory
2. Enter the following information
Host (the nickname we are using)
HostName (fully qualified hostname)
IdentityFile (location of your PRIVATE key)
User (theuser to login with)
Host test
HostName server.karandeep.com
IdentityFile ~/.ssh/id_rsa
User karandeep
3. Save the file by enter ":wq!"
Now try to ssh into the server with the following command:
You should now be logged in! No need to remember host names or passwords or wasting time typing in things. You can also sftp with the config file!
Hope you found this useful :)
1. check permissions on the files. The public and private key should both be 600
2. make sure the private key is given in the config file and not the public key