ssh

Log in to SSH with no password

So you want to have your password remembered by a remote SSH connection?

Easy, following these steps

On your local machine, create a new SSH key:

ssh-keygen

It will ask you for a file location and a keyphrase, but I always just hit enter three times.

This will create a file in the default folder (or whichever folder you chose in the first step)

/home/user/.ssh/id_rsa.pub

The contents of this file want to go into a file on the remote server: If the file doesn’t exist, you’ll have to create it.

/root/.ssh/authorized_keys, or
/home/user/.ssh/authorized_keys

That’s it. There are some cases where you have to change the permissions of the authorized_keys file but I’ve not had to do that. I think the suggested chmods are 640 and 700, but don’t hold me to that

From now on, you won’t be prompted for your password when SSH’ing in from the local machine. In order to revoke access, delete the right line from the authorized_keys file.

SSH Clears Terminal on disconnection

I found on some servers that when I disconnect from an SSH connection, the terminal console is cleared.

This is very annoying if I have to SCP a file after disconnecting, and I cannot remember the path/filename.

So, on the remote server, check the .bash_logout file in the user’s home folder. This will contain commands to run when that user disconnects, and in this particular case, it had

/usr/bin/clear

I commented that out, and we’re sorted. (Mind you, you might want to check with the server administrator before fiddling as it could be a server/company policy)

How to transfer files over SSH/SCP

I often forget the syntax for getting files over a SSH connection.

scp user@address:/path/to/file /path/of/new/file

For example:

scp root@domain.com:/tmp/mysql/test.sql .

Will transfer test.sql to the current folder (.) Also, if you need to change the port, just use the -P option

scp -P 9000 user@address:/path/to/file .

If you want to transfer a file TO another server:

scp /path/of/file user@address:/path/to/new/location