My top five ssh tips and tricks a small cheatsheet

Written by
Date: 2011-02-28 10:36:30 00:00


Introduction

Today I’ll give you five nice ssh tricks, well actually three ssh, and one scp tricks.

Let’s first define ssh and scp

ssh

Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices. The two major versions of the protocol are referred to as SSH1 or SSH-1 and SSH2 or SSH-2. Used primarily on Linux and Unix based systems to access shell accounts, SSH was designed as a replacement for Telnet and other insecure remote shells, which send information, notably passwords, in plaintext, rendering them susceptible to packet analysis. The encryption used by SSH is intended to provide confidentiality and integrity of data over an unsecured network, such as the Internet.

scp

Secure Copy or SCP is a means of securely transferring computer files between a local and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol.

The term SCP can refer to one of two related things, the SCP protocol or the SCP program.

The SCP protocol is a network protocol that supports file transfers. The SCP protocol, which runs on port 22, is based on the BSD RCP protocol which is tunneled through the Secure Shell (SSH) protocol to provide encryption and authentication. SCP might not even be considered a protocol itself, but merely a combination of RCP and SSH

The SCP program is a software tool implementing the SCP protocol as a service daemon or client. It is a program to perform secure copying. The SCP server program is typically the same program as the SCP client.

Now, let’s see those small and useful tips.

Create a X session over a ssh tunnel

This is the most know ssh tip, these are the assumptions:

  • Server: Slackware / IP: 10.1.1.1
  • Client: Slackware / IP: 10.1.1.2

On the server side you need to do this:

xhost 10.1.1.2

Now as root, edit the /etc/ssh/sshd_config and be sure this line exists and that it is not commented.

X11Forwarding yes

This will reduce your server security, so only enable if you really need to, and you may disable when you are done.

Now also as root, restart your ssh server

/etc/rc.d/rc.sshd restart

If you are on Debian, Ubuntu or Fedora run:

/etc/init.d/sshd restart

On the client side

ssh -X user@10.1.1.1

And you are ready, now you can run for example

kate

And kate will run on the server, but you will see it on the client screen.

To remove permission to the client, on the server run:

xhost -10.1.1.2

Find files on a remote computer, get the results on local screen

This one is useful to find files on a remote server, and get the output on your screen, without the need to actually open a permanent remote ssh session screen, it will be open only temporally.

ssh user@10.1.1.1 "find /home/user/ -name '*.mp3'"

You may have an output like this:

/home/user/Dust In The Wind.mp3
/home/user/Life Is Too Short.mp3

Edit files on a remote computer using vim and scp

I like this one, specially when editing config files, like smb.conf, or cups.conf to be sure a new printer will work on the printer server.

vim scp://root@10.1.1.1//etc/samba/smb.conf

You can use it as a normal user, just be sure the user you are use has read and write permissions on the file you are trying to edit.

vim scp://user@10.1.1.1//tmp/some-file.txt

Play mp3 files over ssh connection

This might not be entirely useful, but I like to do it, I know there are better ways, sharing the mp3 files on the server over ssh, samba, NFS or other way is a better way to do this, but I like this way.

ssh user@10.1.1.1 "cat /home/user/music/artist/*.mp3" | mpg123 -

That will play all mp3 files in the folder /home/user/music/artist on the server, and the sound will be on the client’s speakers.

You can change the path, to match your music folder, and you can also specify just a single file.

Play a movie over a ssh tunnel

ssh user@10.1.1.1 “cat /home/user/movie.mp4” | vlc -

You may not see the password prompt, enter it anyway and hit enter, your movie should start playing in VLC, you can use VLC to play the mp3 files instead of mpg123 if you prefer. For some reason I had no luck with mplayer.