SSH tricks and tips

What is the most common tools you will be using when administrating Linux? I would say “ssh”, is the most common tools for any Linux administrator.

If you are going to admin Linux from Windows (yes, you can), then you will need a ssh client, like “Putty” installed in your MS Windows.  If you are going to admin Linux from Linux (the obvious choice), then ssh client most likely had been installed by default.  You can check by typing “ssh” in your shell prompt.

Here I will show you some basic usage of “ssh” and some simple tricks to get you started.

Tip 1 – Login

First and the fundamental usage of “ssh” is to login to another SSH server (which can be Linux/Unix, OS X, or Windows, or any other OS, as long as they running ssh server):

ssh <username>@<hostname>
or
ssh -l <username> <hostname>

Eg:
ssh_login

Tip 2 – Passwordless login (with keys)

Another nice trick with ssh is that you can use public key to login instead of typing in password.  Before you can use public key for your login, you will first need to generate the keys and put the keys into the server you want to login (provided you have login there).

1. generate ssh private/public key pairs as rsa with 2048 bits:

ssh-keygen -t rsa -b 2048

ssh-keygen

Note: By default, the keypair will be generated in your home directory, in “.ssh” directory.

WARNING: Please type in a pass-phrase (pass-phrase is like password for the private keys) when prompt for added security to your private key.

2. pushing the public keys to remote server:

ssh-copy-id -i ~/.ssh/id_rsa.pub <username>@<hostname>

ssh-copy-id

Note: You will be asked to key in password for the first time.  Please change the public key filename (id_rsa.pub) if you generated it to another filename.

Tip 3 – Login, execute and exit

You can use ssh login (especially passwordless) to execute command remotely quickly like this:

ssh <username>@<hostname> uptime

ssh_with_command

Tip 4 – Agent forwarding

This is a very useful ssh tricks when you want to jump from server A to server B directly from you desktop, which you cannot get to Server B from your desktop (due to firewall/security setting).  A details of how agent forwarding work is explained here by Steve Friedl.

Before you can do agent forwarding, you should have your public/private key generated.

High level steps:

1. Transfer your pubkey to Server A and Server B

2. Make sure Server A have agent forwarding setup in the configuration file (/etc/ssh/sshd_config):

AllowAgentForwarding yes

3. Type in the following command:

ssh -A -t -l <username> <Server A> “ssh -l <username> <Server B>”

Note: If the agent cannot be forward to Server B, it might prompt you for password.

 

Advertisements

About shadantan

Just a simple guy who loved his iOS games and apps, and love Open Source Technology, and sharing knowledge along the way :D
This entry was posted in blog, Linux and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s