[ssh] SSH login to remote server without password

Tags

, , , , , , , , ,

If you’re using OpenSSH to connect to remote Linux machine, then this may come as bliss. This is based on public key authentication — (1) create a key-pair — a private and a public one, and then (2) save the public key in the authorized_keys file in the remote machine. Next time when you login using SSH to the remote server from the local machine where you have saved the private key, you won’t have to enter the password! Isn’t it cool? OK, now let’s get down to business, shall we?

Step 0: Make sure the RSA and public key authentication methods are enabled (which are in general enabled by default) in /etc/ssh/sshd_config on the remote machine — it should look like the following:

RSAAuthentication yes
PubkeyAuthentication yes

and then reload the configuration if you had to modify it

$ sudo /etc/init.d/ssh reload

You need the administrative rights for the above.

.

Step 1: Use the command ssh-keygen to create the key pair:

$ ssh-keygen -t rsa

Save the key to the default location, viz. ~/.ssh/id_rsa. When you hit enter, it’ll ask you for a passphrase — leave it empty (see warning below). You need to hit enter once more to confirm it.

Now if you go to ~/.ssh directory, you’ll see that two new files are created: id_rsa (your private key — don’t lose it or give it to somebody else!) and the public key, id_rsa.pub.

.

Step 2: We need to append the public key to the authorized_keys file or save the key as a new file with the name authorized_keysX (where X is a number to avoid conflict) in ~/.ssh directory on the remote machine. We’ll use the fancy vi trick that we saw earlier:

$ vi scp://remoteuser@remote.machine.com//home/remoteuser/.ssh/authorized_keys

Enter your password when you’re asked. Once the vi window opens up, go to the end of the file (hit Shift+G) and then append the public key file

:r id_rsa.pub

assuming you’re still in the ~/.ssh directory on the local machine. Next, save the file and exit.

Note: you may also use

$ ssh-copy-id remoteuser@remote.machine.com

to automatically put the ID in the desired place.

Now you are all set to login to the remote machine using ssh without a password!

.

WARNING: The big security concern and a work-around (still being lazy!)

The ease of this method has a very strong downside: if the local machine is compromised the attacker will waltz onto the remote machine. A way out of this is to protect your private key with a non-empty passphrase. That also means every time the machine requires access to the private key (i.e., every time you login to the remote machine where you saved your public key), you have to enter the passphrase. What’s the use of this hoopla then — you may ask. Well, when there is a wish there is a way too — by committing the key to the  local system’s `memory’ so that you type the passphrase once and only once for the whole session.

OK. Let’s first change password to a non-empty string, shall we?

$ ssh-keygen -p

It’ll ask for the location of the key. Then you’ll have to enter a passphrase and verify it (don’t leave this empty this time).

Next make the system remember your key:

$ ssh-add

It’ll ask for the passphrase (in order to ‘unlock’ your private key) and then for the whole session you won’t need any password/passphrase to login to the remote machine.

.

READ MORE: here and here.

[Network] Fix wired network connection

Tags

, , , , , , ,

Wireless problems often baffle me — so let’s postpone that for another post; right now we’ll focus on the wired network problems which is less common than the other one!

While using Ubuntu based systems, I often found that network manager just got into the way whenever I wanted to fix an internet connection. So, first thing to do is to kill the associated processes (kill -9) after you find the process ID by using top or htop (if installed). Or you can totally get rid of these packages (usually network-manager, network-manager-gnome in Gnome or knetworkmanager in KDE).

Then, edit /etc/network/interfaces file (you need to precede it by sudo) so that the lines for wired interface (usually eth0 — find this by issuing sudo ifconfig , first) look like the following:

auto eth0
iface eth0 inet dhcp

again assuming that we want to have a DHCP based connecton (which is the case most of the time and it’s, ofcourse, easier to fix!).

Then restart the network connection by issuing the following command:
sudo /etc/init.d/networking restart

And then bring eth0 first down, then up using
sudo ifdown eth0; sudo ifup eth0

This should be the first step toward troubleshooting the wired internet connection. The things starts getting just better if these steps fail ……well, let’s leave that for another post.

Hello World!

Hello world!

I’m planning to list the nifty tricks that save my day everyday as well as the ones that I newly learn (and want to keep in mind) while working in a GNU/Linux or other environments with a Unix flavor mixed in. To the casual visitor:  some, if not most, of the tricks may  be familiar to you and may think them to be too basic — but that’s OK with me as I am trying to make the list  so that I don’t forget and have to hunt for hours.

[vim] Print file from within vim

Tags

, ,

To print the lines starting from line m to line n:
:m,n ha >printout.ps or :m,n hardcopy >printout.ps

Without the >printout.ps part, the default printer will be used.

By the way, to print the whole file :hardcopy works just fine.

[scp] Vi(ew) remotely!

Tags

, , , ,

You can view a file in the remote machine using vim (or vi) or compare it with a local file using vimdiff by just using scp:
vi scp://kousik@remote.server//home/kousik/remote-file

vimdiff scp://kousik@remote.server//home/kousik/remote-file local-file

You could have copied the remote-file to the local machine first, but this way you don’t have to do it explicitly.

By the way, sftp also works fine.

Design a site like this with WordPress.com
Get started