Daniels Blog
29Apr/100

6 nützliche Dinge, die man mit SSH tun kann

SSH ist wahrscheinlich mein liebstes Stück Software. Es ist frei, gibt mir Freiheit, es ist einfach zu benutzen und ist trotzdem sehr mächtig. Mit SSH kann man Kommunikation verschlüsseln. Das klappt auf eine sehr universelle Weise für fast jedes Problem. In diesem kleinen Howto zeige ich 6 nützliche Dinge die man - ohne zu großen Stress - mit SSH tun kann. SSH ist mehr als "nur" eine sichere Remote-Shell!

Ding #1 - Eine sichere Remote-Shell

ssh_console1

Das ist das Offensichtlichste was man mit SSH tun kann und die meisten Linuxuser haben es wohl schon einmal gemacht: Eine sichere Verbindung mit einem anderen Rechner herstellen und diesen darüber administrieren.

Das geht sehr einfach:

ssh user@box_B

Das verbindet Dich zu BOX B als "user". Danach kann man als "user" auf BOX B arbeiten.

Manchmal benötigt man gar keine interaktive Sitzung zu einem entfernten Rechner, sondern möchte lediglich ein einzelnes Kommando ausführen.

ssh user@box_B command

Hier wird man zu Box B als "user" verbunden, das Kommando "command" wird ausgeführt, das Ergebnis landet auf der lokalen Standardausgabe und die Verbindung wird beendet.

Ding #2 - Dateien zwischen Rechnern sicher kopieren

ssh_copy

Cool, wir können eine entfernte Maschine mit SSH administrieren, aber man kann mit SSH auch Dateien von einer Maschine zu einer anderen kopieren. Es funktioniert im Grunde so wie das "cp" Kommando, es heisst aber "scp" - Secure Copy.

scp /home/me/a_file.txt user@box_B:/home/me/

Das kopiert die lokale Datei "/home/me/a_file.txt" auf Box A nach "/home/me/a_file.txt" auf Box B.

Es funktioniert auch andersherum:

scp user@box_B:/home/me/b_file.txt /home/me

Das würde die Datei "/home/me/b_file.txt" von Box B ins Home-Verzeichnis auf Box A kopieren.

Weil "scp" so ähnlich funktioniert wie "cp" sind auch Wildcards erlaubt:

scp /var/log/* user@box_B:/home/me/logsbackup

Das kopiert alle Logfiles von Box A nach "/home/me/logsbackup" auf Box B.

Ding #3 - Ein Verzeichnis auf einem entfernten Rechner ins lokale Dateisystem mounten

ssh_mount

Manchmal reicht es nicht einfach nur einige Dateien von einem Rechner auf einen anderen zu kopieren. Ein entferntes Verzeichnis ins lokale Dateisystem zu mounten ist super nützlich, wenn man mit lokalen Programmen Remote-Files bearbeiten will. Ein gutes Beispiel dafür wäre zum Beispiel die Arbeit an einer Webseite auf einem entfernten Server. Man kann einfach das Web-Verzeichnis des entfernten Servers ins lokale Dateisystem mounten und danach die Dateien mit all den coolen lokal installierten HTML-Editoren und Grafikprogrammen öffnen und speichern. Ganz so als wären die Dateien auf der loaklen Platte. Hierfür benötigt man "sshfs". Das FUSE-Filesystem ist in vielen Distributionen nicht standardmässig installiert aber meistens in den Repositories enthalten. Unter Debian und Ubuntu kann man es so installieren:

apt-get install sshfs

Nach der Installation kann man beginnen es zu benutzen

mkdir /mnt/b_data
sshfs user@box_B:/b_data /mnt/b_data

Das mountet das Verzeichnis "/b_data" auf Box B nach "/mnt/b_data" im lokalen Dateisystem. Nun kann man mit lokalen Programm die Remote-Dateien bearbeiten. Wenn man fertig ist, kann man den mount wieder entfernen:

fusermount -u /mnt/b_data

Falls der unmount fehlschlägt sollte man überprüfen, ob noch Dateien vom Remoterechner geöffnet sind oder ob man sich noch mit der Shell oder einem Dateimanager im gemounteten Verzeichnis befindet.

Ding #4 - Unzensiert und anonym von "kritischen Orten" aus im Web surfen

ssh_proxy

Firmenrichtlinien, faschistische Regierungen, Internet-Cafes und andere unfreundliche Regelungen, Institutionen und Orte können einen sicheren und privaten Zugriff aufs Web ziemlich schwierig gestalten. Firewalls und Proxys könnten interessante Webseiten blocken, loggen wo man herumsurft, Man-In-The-Middle-Attacken ausführen oder einfach nur ein mulmiges Gefühl generieren. SSH ist die Lösung für alle diese Probleme. Es bietet die Möglichkeit als Web-Proxy (SOCKS) zu arbeiten. Man verbindet sich einfach per SSH zur guten vertrauenswürdigen BOX B und surft durch diese Verbindung.

(Lokaler Browser <-> Lokaler SSH Proxy <-> SSH <-> Box B <-> Webseite)

Dann kann niemand mehr im unfreundlichen lokalen LAN blockieren, zensieren oder schnüffeln.
Klingt gut? Es ist sogar sehr einfach einzurichten und zu benutzen! SSH bietet die "-D" Option um einen SOCKS-Proxy auf der lokalen Maschine einzurichten:

ssh -D 1234 user@box_B

Nun hat man einen SOCKS-Proxy der auf localhost Port 1234 lauscht. Nun muss man nur noch seinem Browser so konfigurieren, dass er für Internetverbindungen diesen Proxy benutzt. Man kann überprüfen ob alles funktioniert hat, wenn man im Browser eine Webseite aufruft, die die IP-Adresse ausgibt, die für die Verbindung genutzt wurde. http://www.whatismyip.com würde funktionieren, aber es gibt auch 1000de andere Seiten. Wenn dort die IP von Box B erscheint, ist alles in Butter. Ein portalbler Browser auf einem USB-Stick wie zum Beispiel Portable Firefox würde die Sache noch angenehmer machen.

Ding #5 - Den Traffic von lokalen Programmen verschlüsseln und tunneln oder auf Dienste in LANs zugreifen, die normalerweise nicht übers Internet erreichbar sind.

ssh_tunnel_l

OK, wir haben sicher Maschinen administriert, Dateien sicher von Maschine zu Maschine kopiert und haben sogar in China unzensiert im Web gesurft. Aber SSH kann mehr! Man kann damit den Datenaustausch aller lokalen Programme die TCP benutzen durch einen Tunnel zu einem vertrauenswürdigen Rechner schicken. Wie schon mit dem SOCKS-Proxy kann man Daten-Verkehr zunächst durch diesen Tunnel schicken, zum Beispiel den vom lokalen E-Mail Client, damit er nicht durchs lokale LAN fliessen muss. Wir möchten unsere E-Mail in einer "kritischen" Umgebung abrufen. Skript Kiddies, blöde Admins und Terror-China-Hacker könnten die Mail mitlesen oder sogar das E-Mail-Passwort mitsniffen. SSH hilft. Die Syntax für Tunnel mit SSH ist etwas krampfig und zunächst ein ziemlicher Brain-Twister aber eigentlich ziemlich logisch und mit ein bisschen Übung nicht schwierig:

ssh -L local_port:target_host:target_port user@box_B

zum Beispiel:

ssh -L 10000:pop3.mailprovider.com:110 user@box_B

Was ist hier geschehen? SSH wurde angewiesen einen Tunnnel mit einem lokalen (-L) Endpunkt auf Port 10000 anzulegen. Alles was in diesen lokalen Endpunkt an Daten hineingeworfen wird, fliesst zunächst verschlüsselt zu Box B und danach zu "pop3.mailprovider.com" auf port 110 (Port 110 ist POP3). Die Daten fliessen also vom lokalen E-Mail Client verschlüsselt zu Box B und von dort aus an den E-Mail-Provider. Der E-mail Account im lokalen Client benötigt somit also für den POP-Server folgende Einstellungen: Server: localhost / Port: 10000. Aber es muss nicht unbedingt E-Mail sein. Jede Applikation, die das TCP-Protokoll benutzt kann so getunnelt werden. Zum Beispiel IRC, FTP, HTTP, IMAP, etc.

Falls sich der Server auf den zugegriffen werden soll nicht irgendwo im Internet sondern auf Box B selbst befindet, kann der Zielrechner natürlich auch BOX B sein:

ssh -L 10000:127.0.0.1:110 user@box_B

Ziel in diesem Beispiel ist "127.0.0.1", weil es das Ziel aus der Sicht von Box B ist. Denn "127.0.0.1" gesehen von Box B ist Box B selbst.

Tunneln kann nützlich sein um Internetdienste abzusichern, aber auch um auf Dienste in BOX B's privatem Netzwerk zuzugreifen. Falls man einen von außen zugänglichen SSH-Account in einem LAN besitzt, kann man so auf alle TCP-Dienste in diesm LAN zugreifen, ganz so als sei man ein "echter" Client in diesem LAN.

Angenommen BOX B steht in einem Intrantet, das einen interessanten Webserver beherbergt, der aber nicht aus dem Ineternet zugänglich ist. Dieser Server läuft im LAN auf Kiste 192.168.0.77. Mit SSH tunnelt man nun einfach einen lokalen Port auf Port 80 des Webservers im LAN:

ssh -L 10000:192.168.0.77:80 user@box_B

Wenn man nun "http://127.0.0.1:10000" im lokalen Webbrowser aufruft landet man auf der Homepage des Webservers im entfernten Intranet.

Ding #6 - Ein Tunnel - andersherum

ssh_tunnel_r_good

Wenn #5 klar ist, sollten umgekehrte Tunnel kein Problem mehr sein. Hier wird ein entfernter Endpunkt für den Tunnel erstellt. Alles was dort hineinfliesst wird verschlüsselt weitergeleitet an BOX A (den lokale Rechner) und danach an den Zielrechner weitergeleitet.

ssh -R remote_port:target_host:target_port user@box_B

zum Beispiel:

ssh -R 10000:pop3.mailprovider.com:110 user@box_B

Im E-Mail-Client würde man als POP-Server "Box B" und Port "10000" eintragen.
BOX B tunnelt dann den Traffic zunächst sicher auf BOX A um. Box A leitet danach weiter an "pop3.mailprovider.com" port "110"

Nützliche Kommandozeilenoptionen für SSH

-c "Compress"

Die "-c" Option komprimiert die übertragenen Daten mit gzip bevor sie durchs Inetrnet fliessen. Das erhöht die Geschwindigkeit beim Übertragen von unkomprimierten Daten (so wie reinem Text) stark. Sie ist nützlich beim Übertragen
langer Textdateien oder beim Websurfen wenn man SSH als Proxy nutzt.

ssh -c -D 1234 user@box_B

-g "Grant Access"

Die "-g" option eraubt anderen Rechnern als localhost auf die lokal angelegten Tunnelendpunkte zuzugreifen. So können also zum Beispiel auch andere Rechner im LAN die lokal angelegten Tunnel nutzen.

ssh -L -g 10000:127.0.0.1:110 user@box_B

-p "Port"

Die "-p" Option benötigt man, wenn der entfernte SSH-Server nicht auf dem Standardport 22 lauscht.

ssh -p 22000 user@box_b

-v "Verbose"

Mit dieser Option kann man sehr viele technische Verbindungsinformationen sehen, falls man tiefer in SSH eintauchen möchte.

Mehr Lesestoff:

Ich habe versucht diesen Artikel so einfach wie möglich zu schreiben, da er mir selbst in erster Linie als Referenz dienen soll. Es gibt aber noch sehr viel mehr was man mit SSH tun kann:

The SSH man page

The SSH RFC

Wikipedia on SSH

SSH - The Definitive Guide by O'Reilly

19Nov/090

6 Useful Things You Can Do With SSH

SSH must be my favourite piece of software ever. It's free, it gives you freedom, it's simple to use yet powerful in the things it can do. It helps you to encrypt and secure your communication. It can do this in an universal way and for nearly every usage case. In this post, I want to show you 6 things you can do with SSH without too much hassle. SSH can do more than just serve as an encrypted remote session. Try the following examples for yourself and explore the power of the Secure Shell.

Thingy #1 - A secure remote shell

ssh_console1

OK, this is the most obvious thing you can do with SSH and i bet most of you have already done it: Connect to a remote machine via a SSH-secured connection and type on it's console to administer it.

This is very simple:

ssh user@box_B

This will connect you to Box B as user "user". After having entered your password, you will be able to use BOX B's console.

Sometimes you don't want to connect to the remote machine for an interactive session, because you just want to run a single command on the remote machine. In that case you can just do a

ssh user@box_B command

This will connect to Box B as "user", run "command", show you "command"'s output and disconnect.

Thingy #2 - Copy files between your boxes

ssh_copy

Great, we can administer a remote machine with SSH but we can also move data between machines in an encrypted and secure way. It basicly works like the standard "cp" command, but it has got a different name: "scp"

scp /home/me/a_file.txt user@box_B:/home/me/

This will copy the local file "/home/me/a_file.txt" on our Box A to "/home/me/a_file.txt" on Box B.

It will work vice versa as well:

scp user@box_B:/home/me/b_file.txt /home/me

This would get the file "/home/me/b_file.txt" and would put in into our home dir on box A.

Because "scp" works like "cp" wildcards are allowed as well:

scp /var/log/* user@box_B:/home/me/logsbackup

This would copy all of the log files from our Box A to "/home/me/logsbackup" on Box B.

Thingy #3 - Mount a remote directory into your local file system

ssh_mount

Sometimes it's not enough to simply copy one or more files from one machine to another. Mounting a remote directrory into your local filesystem becomes super useful, when you want to work on the remote files with local programs. A good example for this would be working on a remote website. You can simply mount the web-directory from the remote server into your local filesystem and use all your fancy HTML-editors and image-programs on the remote files as if they were on your local harddrive. That's where "sshfs" comes in handy. The tool isn't installed by default in most distributions but you should be able to find it in your repository. On Debian based systems just install it with:

apt-get install sshfs

After having installed sshfs you can start using it:

mkdir /mnt/b_data
sshfs user@box_B:/b_data /mnt/b_data

This mounts the directory "/b_data" from box B into "/mnt/b_data" on your local file system. Now you can work on your remote files with local tools. When you are done, you can unmount the directory with:

fusermount -u /mnt/b_data

If the unmount fails, check if you have still open files in the directory or if you are still in that directory in some shell or Nautilus/Konqueror window.

Thingy #4 - Surf the Web uncensored and anonymously from "critical" locations

ssh_proxy

Corporate policies, fascist governments, internet cafés and other "unfriendly" rules, institutions and places can give you a hard time, when you want to access the web in a secure and private way. Firewalls and proxies may block your favourite sites, log the sites you have visited, perform man in the middle attacks or can just give you a bad feeling. SSH is the solution for these problems. It offers you the possibility to use it as a web-proxy. You simply connect to your good old trusted box B and surf through the encrypted connection.

(Local Browser <-> Local SSH Proxy <-> SSH <-> Box B <-> Website)

Now nobody on your unfriendly local LAN can block or spy on your surfing session.
Sounds good? Great! It's even simple to setup. SSH offers the "-D" option to provide a SOCKS proxy on the local machine:

ssh -D 1234 user@box_B

You'll have a proxy listening on localhost port 1234. Now you just have to setup your webbrowser to use the "SOCKS proxy" on localhost port 1234 and all your surfing will go through Box B. You can check if it worked by visiting a website that shows your IP. http://www.whatismyip.com is a site that would work. If that site shows Box B's IP-address instead of your local one, you setup everything correctly. A portable webbrowser on your USB-pendrive like  Portable Firefox would make things even more cozy.

Thingy #5 - Encrypt the data traffic of your favourite local application or access services in LAN's you couldn't reach otherwise with SSH-tunnels

ssh_tunnel_l

OK, we encrypted remote admin-sessions, copied files securely and even surfed the web in a private way. But SSH can do more. You can encrypt the traffic of every application that uses the TCP-protocol with SSH tunnels. Like with our SOCKS-proxy, we can tunnel other data through ssh, for example the traffic of our e-mail client. Lets say you want to pickup your e-mail while being in a "critical" environment. Bad corporations / governments / script kiddies could read your email and even worse could sniff your e-mail password. SSH helps. The syntax for tunnels in SSH might puzzle your brain at first sight, but it's not too hard:

ssh -L local_port:target_host:target_port user@box_B

for example

ssh -L 10000:pop3.mailprovider.com:110 user@box_B

OK, lets see what happened here. We told ssh to create a tunnel with a local (-L) endpoint at port "10000". Everything that is put into our local endpoint goes first encrypted to our Box B and after that to "pop3.mailprovider.com" on port 110 (which is POP3). You relay all data that goes into our local endpoint in an encrypted way via Box B to your E-Mail provider. In this example you would set the POP-account in your e-mail client to "localhost" port "10000". It doesn't have to be e-mail. Any other application that uses a protocol utilizing TCP works as well. For example IRC, FTP, HTTP, IMAP, you name it...

in case you are running your own server-service on Box B, "target host" can be Box B itself of course:

ssh -L 10000:127.0.0.1:110 user@box_B

Target host in this example is "127.0.0.1" because it's the target from Box B's point of view. "127.0.0.1" seen from Box B sure is Box B itself.

Tunneling can be useful to secure your services or to connect to services inside BOX B's network. Lets say BOX B is in an intranet that has an interesting webserver on IP "192.168.0.77" and you are unable to access that server from the outside. You just tunnel your way to BOX B and let BOX B forward you to the webserver:

ssh -L 10000:192.168.0.77:80 user@box_B

Now typing "http://127.0.0.1:10000" into your local webbrowser will show you the homepage of the intranets webserver.

Thingy #6 - A tunnel the other way around

ssh_tunnel_r_good

OK, this could have been part of "Thingy #5" but to make things more clear i made an extra point for it. If you understood #5 this should be no problem for you. Here, you open up a "remote" endpoint on Box B. Everything that goes in there is relayed encrypted to Box A (the one you are using at the moment) and after that to the target host.

ssh -R remote_port:target_host:target_port user@box_B

for example

ssh -R 10000:pop3.mailprovider.com:110 user@box_B

An e-mail client would set "box_B" and port "10000" as the POP3 server. BOX B would relay the traffic to BOX A through SSH. BOX A would relay the traffic to "pop3.mailprovider.com" port "110".

Useful commandline options for SSH

-c "Compress"

The "-c" option in SSH compresses all traffic with gzip before sending it to the remote host. This increases the speed greatly with uncompressed data-types. It's very useful for copying large text-files over SSH or for surfing the web with the "-D" option. In general "-c" never hurts, it just puts a little more pressure onto your CPU.

ssh -c -D 1234 user@box_B

-g "Grant Access"

The "-g" option allows other hosts to connect to your local tunnel endpoints. If you don't use "-g" in combination with a tunnel, only your own localhost (Box A in the examples) may use the tunnel.

ssh -L -g 10000:127.0.0.1:110 user@box_B

-p "Port"

The "-p" option is needed, if the SSH-server you want to connect to doesn't run on the default port "22"

ssh -p 22000 user@box_b

-v "Verbose"

Add this option if you want to dive deeper into SSH. You will see many technical information while connecting to a remote host.

Further reading

I tried to keep this article as simple as possible to make it usable. There is a lot more to know about SSH. If you are looking for a more comprehensive read i suggest you check out these docs:

The SSH man page

The SSH RFC

Wikipedia on SSH

SSH - The Definitive Guide by O'Reilly