个人工具

“UbuntuHelp:SSHHowto”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第4行: 第4行:
 
Parent page: [[UbuntuHelp:InternetAndNetworking| Internet and Networking]]
 
Parent page: [[UbuntuHelp:InternetAndNetworking| Internet and Networking]]
 
== Introduction ==
 
== Introduction ==
OpenSSH provides secure remote access, including command line sessions, graphical apps via X forwarding, and file transfer using secure copy (SCP) and Secure FTP protocols. Additionally, it can act as a encrypting tunnel for other protocols via port forwarding.  
+
OpenSSH provides secure remote access to your computer, allowing you to run command line and graphical programs, transfer files, and use a "port forwarding" capability to securely tunnel other protocols through firewalls and untrusted networks.
SSH replaces older, insecure apps like telnet, rlogin, and FTP. These older apps transmit passwords across the Internet without encryption, meaning the password could easily be stolen. By providing encryption, SSH fixes these problems.
+
SSH replaces older, insecure programs like telnet, rlogin, and FTP. These older applications transmit passwords across the Internet without encryption, meaning the password could easily be stolen.
 
== Installing the SSH Server ==
 
== Installing the SSH Server ==
If you want to securely connect to your machine from a remote computer, even a MS Windows machine using Putty, then you need to install a server that allows you to connect. Ubuntu uses the openssh-server for which you need the '''openssh-server''' package.
+
If you want to securely connect to your machine from a remote computer, even a computer running another operating system like Mac OS or Windows, then you need to install the '''openssh-server''' package from the main repository. Installing software is discussed in [[UbuntuHelp:InstallingSoftware|InstallingSoftware]].
 +
=== Testing the server ===
 +
Once you have installed the server, go to a [[UbuntuHelp:CommandlineHowto|command-line]] and type:
 
<pre><nowiki>
 
<pre><nowiki>
# sudo apt-get install openssh-server
+
ssh localhost
 
</nowiki></pre>
 
</nowiki></pre>
== Logging in to a remote computer over ssh ==
+
You should be prompted to type your password, and you should get another command-line when you type your password in.  If this works, then your SSH server is working properly.  To leave the SSH command-line, type:
=== From *nux ===
+
To login a remote computer running a ssh-server, open a terminal and log in with ''ssh <user>@<server>'' like this:
+
 
<pre><nowiki>
 
<pre><nowiki>
ssh <username>@<computer name or ip_address>
+
exit
 
</nowiki></pre>
 
</nowiki></pre>
example:
+
If you have a local network (such as a home or office network), try logging in from one of the other computers on your network.  If nothing happens, you might need to tell your computer's firewall to allow connections on port 22.
 +
Finally, try logging in from another computer elsewhere on the Internet - perhaps from work (if your computer is at home) or from home (if your computer is at your work).  If you can't access your computer this way, you might need to tell your router's firewall to allow connections from port 22, and might also need to configure [[UbuntuHelp:ServersBehindNAT|Network Address Translation]].
 +
<<Anchor(public-key-auth)>>
 +
=== Public key authentication ===
 +
If your SSH server is visible over the Internet, you should use public key authentication instead of passwords if at all possible.  If you don't think it's important, go to your '''/var/log/''' folder and have a look at the files named '''auth''' (attempted logins for this week) and '''auth.0''' (attempted logins for last week).  My computer - a perfectly ordinary desktop PC - had over 4,000 attempts to guess my password and almost 2,500 break-in attempts in the last week alone.  How many thousand random guesses do you think it will take before an attacker stumbles across your password?
 +
With public key authentication, every computer has a public and a private "key" (a large number with particular mathematical properties).  The private key is kept on the computer you log in from, while the public key is stored on the '''.ssh/authorized_keys''' file on all the computers you want to log in to.  When you log in to a computer, the SSH server uses the public key to "lock" messages in a way that can only be "unlocked" by your private key - this means that even the most resourceful attacker can't snoop on, or interfere with, your session.  As an extra security measure, most SSH programs store the private key in a password-protected format, so that if your computer is stolen or broken in to, you should have enough time to disable your old public key before they break the password and start using your key.  Wikipedia has a [[UbuntuHelp:WikiPedia:Public-key_cryptography|more detailed explanation]] of how keys work.
 +
Public key authentication is a much better solution than passwords for most people.  In fact, if you don't mind leaving a private key unprotected on your hard disk, you can even use keys to do secure automatic log-ins - as part of a network backup, for example.  Different SSH programs generate public keys in different ways, but they all generate public keys in a similar format:
 
<pre><nowiki>
 
<pre><nowiki>
ssh joe@laptop
+
<ssh-rsa or ssh-dsa> <really long string of nonsense> <username>@<host>
 
</nowiki></pre>
 
</nowiki></pre>
example:
+
No matter how your public key was generated, you can add it to your Ubuntu system by opening the file '''.ssh/authorized_keys''' in your favourite text editor and adding the key to the bottom of the file.  You can also limit the SSH features that the key can use, such as disallowing port-forwarding or only allowing a specific command to be run.  This is done by adding "options" before the SSH key, on the same line in the '''authorized_keys''' file.  For example, if you maintain a CVS repository, you could add a line like this:
 
<pre><nowiki>
 
<pre><nowiki>
ssh mike@192.168.1.1
+
command="/usr/bin/cvs server",no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc ssh-dss <string of nonsense>...
 
</nowiki></pre>
 
</nowiki></pre>
===== Keep Alive =====
+
When the user with the specified key logged in, the server would automatically run `/usr/bin/cvs server`, ignoring any requests from the client to run another command such as a shell. For more information, see [http://www.openbsd.org/cgi-bin/man.cgi?query=sshd&sektion=8#SSHRC the sshd man page].
Your ssh session will automatically log out if it is idle. To keep the connection active (alive) add this to '''/etc/ssh/ssh_config''' on the client.
+
The main problem with public key authentication is that you need a secure way of getting the public key onto your computer before you can log in with it. If you will only ever log in to your computer from a few other computers (such as logging in to your PC from your laptop), you should copy your SSH keys over immediately, then disable password authentication altogether.  If you would like to log in from other computers from time to time (such as a friend's PC), read [[UbuntuHelp:StrongPasswords|Strong|Passwords]] for instructions on how to install a program that generates hard-to-guess passwords. To disable password authentication, open '''/etc/ssh/sshd_config''' and look for the following line:
<code><nowiki>ServerAliveInterval 5</nowiki></code>
+
This will send a "keep alive: signal to the server every 5 seconds. You can usually increase this interval and I use 120.
+
==== Using Putty ====
+
Putty is a gui tool to configure your ssh connection and works on Linux as well as windows.
+
<code><nowiki>sudo apt-get install putty</nowiki></code>
+
See the windows section for further information.
+
==== Using SecPanel ====
+
SecPanel is an SSH and SCP frontend for OpenSSH
+
<code><nowiki>sudo apt-get install secpanel</nowiki></code>
+
=== From Windows ===
+
==== Using Putty ====
+
Putty is a graphical tool available here : [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html Download Putty]
+
[http://img296.imageshack.us/img296/7404/putty3zv1.png]
+
Once the connection is made you will be given a Terminal (Command Line Interface).
+
If you generated a Public Key (see next section) you will need to import it to Putty. Download ''puttygen.exe'' from th ePutty site above and use it to import your open-ssh key. There is a walk through for how to do this [http://linux-sxs.org/networking/openssh.putty.html here]
+
Otherwise you can generate a key with ''puttygen'' (I do not think you can import a putty key to open-ssh)
+
===== Keep alive =====
+
Your putty (ssh) session will automatically log out if it is idle. To keep the connection active (alive), '''before you make the connection''', select "Connection" on the left and in the box " Seconds between keepalives (0 to turn off) enter 120 (this = 120 seconds).
+
==== Using Cygwin ====
+
Cygwin is a unix like environment for Windows. You can download it here : [http://www.cygwin.com/ Cygwin]
+
To install it run the ''Setup.exe'' , choose a server.
+
To use ssh you need to install the openssh package (it includes BOTH a server and client). It is located under "Net" and is called ''openssh''
+
[http://img296.imageshack.us/img296/4139/cygwinopensshpv3.png]
+
You then launch Cygwin as a .bat file and you get a Terminal Interface :
+
Cygwin on Windows, running zsh :
+
[http://img127.imageshack.us/img127/9751/zshdx1.png]
+
You then use ssh from the Cygwin shell as you normally would on *nix (see above)
+
===== Keep Alive =====
+
See the "Keep alive" section under *nix above.
+
== Transferring Files Remotely With SSH ==
+
=== Graphically ===
+
==== From Gnome ====
+
Nautilus can access remote computers via SSH, and browse and transfer files. Click '''Places''' -> '''Connect to Server'''. Select '''SSH''' for '''Service Type''', write the name or IP address of the computer you're connecting to in '''Server''', the user you'd like to connect as in '''User Name''', and a name for the connection if you wish.
+
Files can be copied by dragging and dropping between this window and other windows.
+
==== From KDE ====
+
Konqueror can access remote computers via SSH, and browse and transfer files. Open Konqueror, and in the address bar type:
+
 
<pre><nowiki>
 
<pre><nowiki>
fish://username@server_address
+
PasswordAuthentication yes
 
</nowiki></pre>
 
</nowiki></pre>
Files can be copied by dragging and dropping them between this window or tab and to other windows or tabs.
+
Change it to the following (or add it if you couldn't find that line):
==== Using SecPanel ====
+
SecPanel is an SSH and SCP frontend for OpenSSH
+
<code><nowiki>sudo apt-get install secpanel</nowiki></code>
+
==== From Windows ====
+
WinSCP can be used to transfer files between Linux machines running SSH and Windows.
+
WinSCP is capable of using either scp or sftp.
+
You can download it for free from [http://winscp.net].
+
[http://img296.imageshack.us/img296/491/winscpnh5.png]
+
<u>Note</u>: Winscp uses ''Putty keys'' (See the [[UbuntuHelp:SSHHowto?#head-85bd741dfccdb511a16353d657923ae24903003d|Using Putty]] section below)
+
=== Using the Command Line (Terminal) ===
+
To copy a file from your computer to another computer with ssh you will need to use secure copy, the ''scp'' command. The command would look like this.
+
 
<pre><nowiki>
 
<pre><nowiki>
scp <file> <username>@<IP address or hostname>:<DestinationDirectory>
+
PasswordAuthentication no
 
</nowiki></pre>
 
</nowiki></pre>
An example: Copying file.txt from my local computer to a remote computer, and placing it in the /home/joe/downloads directory
+
You will need superuser access to save this file. Once you have saved the file, restart your SSH server and try logging in again - it shouldn't even ask you for a password now.
 +
== Logging in from other computers ==
 +
Although SSH has many uses, its main job is to provide access to a command-line over a network.  Even if you are installing SSH for completely different reasons, it's best to get command-line access access before you try anything more complicated.
 +
=== From Unix-like systems (including Mac OS X) ===
 +
==== Using the command-line ====
 +
All modern Unix-like systems (Linux, OS X, BSDs, and others) include a command-line ssh client.  To login to your computer from a Unix-like machine, go to a command-line and type:
 
<pre><nowiki>
 
<pre><nowiki>
scp file.txt joe@192.168.1.103:downloads/
+
ssh <username>@<computer name or IP address>
 
</nowiki></pre>
 
</nowiki></pre>
Another example:
+
For example:
 
<pre><nowiki>
 
<pre><nowiki>
scp "New Document.odw" joe@laptop:"/home/joe/Summer 2005"
+
ssh joe@laptop
 
</nowiki></pre>
 
</nowiki></pre>
So you have to include the <file>, the <username> of the computer you are logging into and the <ipaddress> and <Destination``Directory> for which the file is going to be moved to.
+
or:
To copy files from remote computers to your local disk:
+
 
<pre><nowiki>
 
<pre><nowiki>
scp hornbeck@192.168.1.103:/home/hornbeck/otherfile.txt .
+
ssh mike@192.168.1.1
 
</nowiki></pre>
 
</nowiki></pre>
The '.' means to copy the file to the current directory. You could instead put 'my.txt' and the file would be renamed during copying.
+
You should get the same password prompt as before (or be told you can't log in, if passwords are disabled).
Two handy options to scp are '-r' and '-C'.  '-r' enables recursive copying, for when you want to copy an entire directory tree.  '-C' enables compression, which will probably improve the transfer speed.  '-C' is also an option for ssh and sftp.
+
===== Tip: Keep Alive =====
Important: you can not move files between two remote computers. Either the source or destination must be a local file.  However, if you login to one of the remote machines via ssh, you can copy files between two remote machines.
+
Your ssh session will automatically log out if it is idle. To keep the connection active (alive) add this to '''/etc/ssh/ssh_config''' on the machine you are logging in from:
Note that if your local userid is the same as the remote userid then the user@ part can be omitted. Also, if the remote directory is omitted, the home directory will be used.
+
 
<pre><nowiki>
 
<pre><nowiki>
scp 192.168.1.103:file.txt .
+
ServerAliveInterval 120
 
</nowiki></pre>
 
</nowiki></pre>
== Advanced options ==
+
This will send a "keep-alive" signal to the server every 120 seconds.
=== Forward X ===
+
===== Tip: Generating Public Keys =====
To forward X check in '''/etc/ssh/sshd_config''' to be sure you have allowed X forwarding (it is enabled by default)
+
To create your public and private SSH keys on the command-line, do:
Look for this line :
+
 
<pre><nowiki>
 
<pre><nowiki>
X11Forwarding yes
+
mkdir ~/.ssh
 +
chmod 700 ~/.ssh
 +
ssh-keygen -q -f ~/.ssh/id_rsa -t rsa
 
</nowiki></pre>
 
</nowiki></pre>
==== Single Applications ====
+
Then enter a password that will protect your private key while it's stored on the hard drive (or press enter to leave your private key unprotected).
You can forward graphical applications with the -X option.
+
Your public key is now available as '''.ssh/id_rsa.pub''' in your home folder.
 +
==== Using PuTTY ====
 +
PuTTY is a popular graphical SSH client.  Although the Windows client is more widely known, you can get the Linux version by installing the '''putty''' package from the Universe repository.
 +
See the windows section for further information about PuTTY.
 +
==== Using SecPanel ====
 +
!SecPanel is another graphical SSH client.  You can get it by installing the '''secpanel''' package from the Universe repository.
 +
=== From Windows ===
 +
==== Using PuTTY ====
 +
[http://www.chiark.greenend.org.uk/~sgtatham/putty/ PuTTY] is a free SSH client for Windows, which you can download from [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html here].
 +
{{http://img296.imageshack.us/img296/7404/putty3zv1.png|The PuTTY configuration window}}
 +
To log in to your computer, type your computer's name or IP address into the "Host Name (or IP address)" box, click on the "SSH" radio button, then click "Open".  You will be asked for your username and password, then you'll get a command-line on your Linux computer.
 +
===== Tip: Keep alive =====
 +
Your PuTTY (ssh) session will automatically log out if it is idle. To keep the connection active (alive), '''before you make the connection''', select "Connection" on the left of the PuTTY Configuration window, and type 120 in the "Seconds between keepalives (0 to turn off)" box.
 +
===== Tip: Importing your openssh keys =====
 +
For security reasons, you should normally create a new SSH private key for every computer you use.  However, if you have no choice but to reuse a private key, PuTTY can import an Ubuntu private key with [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html puttygen.exe], as [http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter8.html#puttygen-load described] in the PuTTY manual.
 +
===== Tip: Generating Public Keys =====
 +
The [http://the.earth.li/~sgtatham/putty/0.60/htmldoc/ PuTTY manual] has a [http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter8.html section on public key authentication].
 +
==== Using Cygwin ====
 +
Cygwin is a unix-like environment for Windows.  You can download it here: [http://www.cygwin.com/ Cygwin]
 +
To install it run the ''Setup.exe'' and choose a server.
 +
To use ssh, you need to install the openssh package (it includes BOTH a server and client). It is located under "Net" and is called ''openssh''.
 +
{{http://img296.imageshack.us/img296/4139/cygwinopensshpv3.png}}
 +
Then you can launch Cygwin as a ''.bat'' file and get a Terminal Interface:
 +
{{http://img127.imageshack.us/img127/9751/zshdx1.png|Cygwin on Windows, running zsh}}
 +
Now follow the earlier instructions for using the command-line on a Unix-like OS.
 +
== Transferring Files Remotely With SSH ==
 +
Another important function of SSH is allowing secure file transfer using [[UbuntuHelp:WikiPedia:Secure_copy|SCP]] and [[UbuntuHelp:WikiPedia:SSH_file_transfer_protocol|SFTP]].
 +
{|border="1" cellspacing="0"
 +
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png%7D%7D ||'''Limitation''': You can't move files between two remote computers. Either the source or destination must be a local file.  However, if you log in to a remote machine with ssh, you can copy files between two remote machines on that machine's command-line.
 +
|}
 +
=== From Unix-like systems (including Mac OS X) ===
 +
==== Using the Command-line ====
 +
Just as all modern Unix-like systems have an SSH client, they also have SCP and SFTP clients. To copy a file from your computer to another computer with ssh, go to a command-line and type:
 
<pre><nowiki>
 
<pre><nowiki>
ssh -X user@server
+
scp <file> <username>@<IP address or hostname>:<Destination>
 
</nowiki></pre>
 
</nowiki></pre>
Once the connection is made enter your command in the terminal, ''firefox &'' for example. This will start Firefox on the server and forward the Firefox interface to the client.
+
For example, to copy your TPS Reports to Joe's Desktop:
You can do this as a single command/application if you like :
+
 
<pre><nowiki>
 
<pre><nowiki>
ssh -XC user@server firefox
+
scp "TPS Reports.odw" joe@laptop:Desktop/
 
</nowiki></pre>
 
</nowiki></pre>
If you start an application and it complains that it cannot find the display, make sure that xauth is installed. It is installed by default with desktop installations, but not server installations. If <code><nowiki>echo $DISPLAY</nowiki></code> yields nothing, trying first installing xauth to correct the deficiency.
+
This will copy '''TPS Reports.odw''' to '''/home/joe/Desktop''', because SCP uses your home folder as the destination unless the destination folder begins with a '/'.
 +
To copy the pictures from your holiday to your website, you could do:
 
<pre><nowiki>
 
<pre><nowiki>
sudo apt-get install xauth
+
scp -r /media/disk/summer_pics/ mike@192.168.1.1:"/var/www/Summer 2008/"
</nowiki></pre>
+
==== Entire Desktop ====
+
In order to do this you must enter the command from a console (Ctrl-Alt-F1) and not from a terminal in X
+
1. Switch to a console session (Ctrl-Alt-F1 or Ctrl-Alt-F2. To return Ctrl-Alt-F7)
+
2. Start a ssh connection in conjunction with xinit :
+
 
+
<pre><nowiki>
+
xinit -e ssh -XCT user@server fluxbox -- :1
+
 
</nowiki></pre>
 
</nowiki></pre>
*Substitute gnome-session, xfce4-session, or startkde for fluxbox as needed (I use fluxbox as an example as it is a light weight window manager).
+
The '''-r''' ('''r'''ecursive) option means to copy the whole folder and any sub-folders.  You can also copy files the other way:
* -C allows compression
+
* the ''-- :1'' starts a session on your second virtual console located at '''Ctrl-Alt-F8''' (your first is at Ctrl-Alt-F7)  
+
<u>Note</u>: If you would like to forward the entire desktop you will likely discover tunneling a VNC connection is fasterSee slso [[UbuntuHelp:VNCOverSSH| Vnc Over SSH Ubuntu Wiki]] and [[UbuntuHelp:FreeNX| FreeNX Ubuntu Wiki]]
+
==== Nested windows ====
+
You may use Xephyr to give you an X server within your current server.
+
[http://cafelinux.org/OptickleArt/albums/userpics/Xephyr.png http://cafelinux.org/OptickleArt/albums/userpics/normal_Xephyr.png]
+
Two ssh forwarded desktops on dual monitors, click to enlarge
+
[http://ubuntuforums.org/showthread.php?t=620003 Ubuntu forums, How to Xephyr] ~ it is short
+
Connections are encrypted and compressed.
+
=== Running single commands on the open-ssh server ===
+
You can run a single command on the open-ssh server by adding the command to the end of the ssh command.
+
 
<pre><nowiki>
 
<pre><nowiki>
ssh -fCT user@server <command>
+
scp -r catbert@192.168.1.103:/home/catbert/evil_plans/ .
 
</nowiki></pre>
 
</nowiki></pre>
 +
The '.' means to copy the file to the current directory.  Alternatively, you could use '''secret_plans''' instead of '.', and the folder would be renamed.
 +
Finally, if you want to look around the remote machine and copy files interactively, you can use SFTP:
 
<pre><nowiki>
 
<pre><nowiki>
-f = Allows ssh to close after the connection is established.
+
sftp linus@kernel.org
-C = Use Compression
+
-T = No terminal session will be started
+
 
</nowiki></pre>
 
</nowiki></pre>
=== Mounting directories using sshfs ===
+
This will start an SFTP session that you can use to interactively move files between computers.
sshfs will allow you to mount directories from the open-ssh server to your client very similar to nfs
+
==== Using SSHFS ====
[[UbuntuHelp:SSHFS| SSHFS Ubuntu Wiki]]
+
SSHFS is a recent addition to Linux that allows you to make a remote filesystem available over SSH act as if it was inside a folder on your own system.  See [[UbuntuHelp:SSHFS]] for details.
=== Tunneling VNC connections through ssh ===
+
==== Using GNOME ====
This obviously increases the security of your VNC sessions. [[UbuntuHelp:VNCOverSSH| VNC Over SSH Ubuntu Wiki]]
+
Click '''Places''' -> '''Connect to Server'''. Select '''SSH''' for '''Service Type''', write the name or IP address of the computer you're connecting to in '''Server''', the user you'd like to connect as in '''User Name''', and a name for the connection if you wish.  
[[Anchor(public-key-auth)]]
+
Files can be copied by dragging and dropping between this window and other windows.
== Public key authentication ==
+
==== Using KDE ====
Formerly everybody has used the typical username/password authentication. However if
+
Open Konqueror, and in the address bar type:
someone had guessed or sniffed your password then security was compromised.
+
For quite some time SSH has offered '''public key authentication'''. It uses private and
+
public keys instead of simple passwords.
+
Unless you already have a private key you first need to create one. Enter:
+
 
<pre><nowiki>
 
<pre><nowiki>
ssh-keygen -t dsa
+
fish://username@server_address
 
</nowiki></pre>
 
</nowiki></pre>
You will then be asked where to save the private key (just accept the default
+
Files can be copied by dragging and dropping them between this window or tab and to other windows or tabs.
location) and to choose a passphrase. The passphrase is used to encrypt your
+
==== Using other programs ====
private key. Everybody that gets access to your (unprotected) private key
+
!SecPanel and PuTTY also have file transfer utilities, although they're generally not as easy to use as the ones discussed above.
will have your permissions on other computers. Please take a moment and think
+
=== From Windows ===
of a really good password. See [[UbuntuHelp:ChoosingSecurePassword|ChoosingSecurePassword]] if you do not know
+
PuTTY has command-line SCP and SFTP utilities for Windows that act much like the command-line scp and sftp utilities for Unix-like systems. Alternatively, [http://winscp.net WinSCP] is a graphical utility to transfer files.
what that means.
+
{{http://img296.imageshack.us/img296/491/winscpnh5.png}}
Now to gain access to other computers they need to '''trust''' your public key.
+
{|border="1" cellspacing="0"
Your public key was created along with the new private key. It is usually
+
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png%7D%7D ||Winscp uses ''PuTTY keys'' for public key authentication
located at:
+
|}
 +
== Running GUI Programs ==
 +
SSH can be used to tunnel graphical applications over a network as well as text-based ones.  GUIs are more complicated than text, and therefore harder to set up and more likely to require extra software.
 +
=== Check Your SSH Server Settings ===
 +
By default, Ubuntu's SSH server has everything you need enabled.  If you have disabled features for security reasons, you might not be able to start GUI programs.  To make sure, look in your '''/etc/ssh/sshd_config''' for this line:
 
<pre><nowiki>
 
<pre><nowiki>
~/.ssh/id_dsa.pub
+
AllowTcpForwarding No
 
</nowiki></pre>
 
</nowiki></pre>
The target user needs to have this key (it is a line of ASCII characters) in its authorized key file located at:
+
This disables forwarding of Internet connections over SSH.  You should delete this line if you want to use VNC for GUI programs (discussed below).
 +
Next, look for this line:
 
<pre><nowiki>
 
<pre><nowiki>
~/.ssh/authorized_keys
+
X11Forwarding yes
 
</nowiki></pre>
 
</nowiki></pre>
on the target system. So either just copy and paste the line into the authorized_keys file, or use the "ssh-copy-id" command like this:
+
This enables Unix-style GUI forwarding over SSH.  If you don't see it, open the file in your favourite text editor and add that line.
 +
You will need superuser privileges to save changes to '''/etc/ssh/sshd_config'''.
 +
=== Single Applications ===
 +
If you are logging in from a Unix-like operating system, you can forward single applications over SSH very easily, because all Unix-like systems share a common graphics layer called X11. This even works under Mac OS X, although you will need to install and start the [http://www.apple.com/support/downloads/x11formacosx.html X11 server] before using SSH.
 +
To forward single applications, connect to your system using the command-line, but add the '''-X''' option to forward X11 connections:
 
<pre><nowiki>
 
<pre><nowiki>
ssh-copy-id -i ~/.ssh/id_dsa.pub root@fileserver01
+
ssh -X joe@laptop
 
</nowiki></pre>
 
</nowiki></pre>
You will be asked for the root password of that computer. If password authentication is disabled then you need to copy and paste your key using another medium. After your public key has been added you are trusted.
+
Once the connection is made, type the name of your GUI program on the command-line:
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png In order to use <code><nowiki>ssh-copy-id</nowiki></code> you need to have saved your passphrase to the [[ssh-agent ssh-agent]] using <code><nowiki>ssh-add</nowiki></code> ([[ssh-agent see below]]).
+
Run:
+
 
<pre><nowiki>
 
<pre><nowiki>
ssh root@fileserver01
+
firefox &
 
</nowiki></pre>
 
</nowiki></pre>
and you should no longer be asked for the password but for the '''passphrase'''. Please note that the password and the passphrase do different things. The password is saved in the /etc/passwd of the target system. The passphrase is used to decrypt your private key on your system.
+
Your program will start as normal, although you might find it's a little slower than it would be if it were running locally.  The trailing '''&''' means that the program should run in "background mode", so you can start typing new commands in straight away, rather than waiting for your program to finish.
To reiterate: the actual security of public key authentication over password authentication is that two things are needed to get access:
+
If you only want to run a single command, you can log in like this:
* your (encrypted) private key
+
* your passphrase (which is needed to decrypt the private key)
+
So if you would choose no password at all (which is possible - see the next section) you would have even less security than using a password alone.
+
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png Password based authentication is enabled per default in Ubuntu. If you want to stop users from logging in remotely using passwords, disable password authentication manually, by setting "''Password``Authentication no''" in the file /etc/ssh/sshd_config. Do not forget to restart your ssh server after changing the configuration (<code><nowiki>sudo /etc/init.d/ssh restart</nowiki></code>).
+
[[Anchor(restrict-ssh-access)]]
+
== Restricting SSH access ==
+
When using SSH public key authentication there is another nifty feature. The target server can restrict which commands you are allowed to run. If you maintain a CVS repository you could use lines like this in the
+
authorized_keys file:
+
command="/usr/bin/cvs server" ssh-dss AAAAB3N....
+
This would allow only this command to be run, and nothing else.
+
== Automated access in batch scripts ==
+
[[public-key-auth Public key authentication]] can also be used to automate tasks that would usually require typing in a password. Imagine you want to copy a file from a remote computer every midnight. All you needed to do is establish the trust between those two computers. Create a service account on one computer, create a keypair (ssh-keygen -t dsa) and when asked for a passphrase just press 'ENTER'. This will leave the private key unprotected. Add the public key to the other computer's authorized_keys file (ssh-copy-id). Now you can SSH to that machine without being asked for a passphrase. Add an SSH call to your crontab and you are set.
+
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png '''Be careful!'''
+
Having an unprotected private key may be a security hole. Hackers would only need to get access to the private key and could access the target computer.
+
Because this is an unsecure method, you should avoid using your normal public/private key pair and instead generate a new key pair that you only use for this one batch connection.  That way, your normal key pair is still passphrase-protected.  You can do this by specifying a different filename when prompted by ssh_keygen(1) as described in [[public-key-auth Public key authentication]].  Coupled with specifying the only command the remote machine is allowed to run, as described in [[restrict-ssh-access Restricting SSH access]], this means someone who gains access to your account would still need the passphrase to do anything other than run the specified remote commands on your batch machines.
+
[[Anchor(ssh-agent)]]
+
== Using the ssh-agent ==
+
If you frequently need to copy files over SSH or access other computers in your network (which is a common task for administrators) you probably wonder if there is way to simplify entering the passphrase. Actually there is - it's called the '''SSH agent'''. You only need to enter your passphrase once using "ssh-add" and everything you start as a subprocess of the SSH agent will remember that passphrase.
+
Too much theory? Well, you will not need to worry about the agent. Your X session is already run in an ssh-agent session automatically. All you need to do is launch "ssh-add" and type in your passphrase. Next time you use SSH to access another computer you will not have to enter your passphrase again.
+
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png '''Important:'''
+
You should lock your screen if you leave your desktop. Other people could access other computers from your computer over ssh without even knowing your passphrase.
+
If you like to be asked for the passphrase once after login to Ubuntu you can add a call to
+
"ssh-add" like this:
+
* Click on '''System'''
+
* Choose '''Preferences'''
+
* Choose '''Sessions'''
+
* Select the '''Startup Programs''' tab
+
* Click on '''Add'''
+
* Enter "ssh-add" as a command
+
After the next login you should be asked for the passphrase.
+
KDE users can also make use of ssh-add:
+
Open a Konsole session:
+
* Click on '''Kmenu'''
+
* Choose '''System'''
+
* Choose '''Konsole Terminal Program'''
+
At the command prompt, type:
+
 
<pre><nowiki>
 
<pre><nowiki>
user@user-computer:~$ ln -s /usr/bin/ssh-add .kde/Autostart
+
ssh -f -T -X joe@laptop firefox
 
</nowiki></pre>
 
</nowiki></pre>
Logout of your KDE session and login again. A menu will pop up at startup asking for your SSH passphrase.
+
That will run Firefox, then exit when it finishes. See [http://www.openbsd.org/cgi-bin/man.cgi?query=ssh&sektion=1 the SSH manual page] for information about '''-f''' and '''-T'''.
== Security ==
+
If you start an application and it complains that it cannot find the display, try installing the '''xauth''' package from the Main repository. Xauth is installed by default with desktop installations but not server installations.
User logins and other data are stored in /var/log/auth.log (and auth.log.0, etc.). If you would like to see if anybody has attempted to login to your system, you can examine the log files. The command:
+
If you suspect that programs are running slowly because of a lack of bandwith, you can turn SSH compression on with the '''-C''' option:
 
<pre><nowiki>
 
<pre><nowiki>
awk '/Invalid user/ {print $8}' /var/log/auth.log{,.0} | sort | uniq -c
+
ssh -fTXC joe@laptop firefox
 
</nowiki></pre>
 
</nowiki></pre>
will show you who (often an automated attack) has attempted to login with an Invalid Username, preceded by the number of times that name was used.
+
Using '''-fTXC''' here is identical to '''-f -T -X -C'''.
For a list of users and where they logged in the "last" command, and "lastb" (last bad) should allow for a quick check.
+
==== Nested windows ====
For enhanced security, see the [[advanced Advanced Configuration]] page.
+
Xephyr is a program that gives you an X server within your current server.  It's available in the '''xserver-xephyr''' package in the Main repository.
[[Anchor(advanced)]]
+
[http://cafelinux.org/OptickleArt/albums/userpics/Xephyr.png {{http://cafelinux.org/OptickleArt/albums/userpics/normal_Xephyr.png}}]
== Advanced Configuration ==
+
Two ssh forwarded desktops on dual monitors, click to enlarge
See [[UbuntuHelp:AdvancedOpenSSH|AdvancedOpenSSH]] for advanced configuration and extra security.
+
Setting up Xephyr was explained briefly in the [http://ubuntuforums.org/showthread.php?t=620003 Ubuntu forums].
== GPG & OpenSSH ==
+
=== Tunneling VNC connections through ssh ===
See [[UbuntuHelp:GPGsigningforSSHHowTo|GPGsigningforSSHHowTo]] for using GPG to sign SSH keys.
+
Virtual Network Computing ("VNC") is a cross-platform way of sharing a desktop.  Once you've set your SSH server up, see [[UbuntuHelp:VNC]] for more information.
== SSH as a Proxy ==
+
== Breaking out of a controlled network ==
If you can make an SSH connection, you can (most likely) use that connection as a SOCKS proxy without any extra setup on the remote computer. Traffic is tunneled securely through the SSH connection. If you are on an unsecured wireless connection, you can use this to effectively secure all your traffic from snooping. You can also use this to bypass IP restrictions because you will appear to be connecting from the remote computer.
+
Sometimes it's useful to tunnel all web traffic over SSH, without having to start Firefox on a remote computer. For example, to work around network monitoring or snooping, or to avoid badly configured routers on the Internet. If you can change the settings on your web browser, you can probably use SSH to circumvent these filters.
Pick some big port number (bigger than 1024 so you can use it as non-root). Here I choose 1080, the standard SOCKS port. Use the <code><nowiki>-D</nowiki></code> option for dynamic port forwarding.
+
{|border="1" cellspacing="0"
 +
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png%7D%7D ||'''Warning''': Filtering and monitoring is usually implemented for a reason.  Even if you don't agree with that reason, your IT department might not take kindly to you flouting their rules.
 +
|}
 +
[[UbuntuHelp:WikiPedia:SOCKS|SOCKS]] is a protocol is used by some proxy servers.  The SOCKS protocol allows a client (such as a web browser) to ask a server to download a file for it, rather than downloading the file directly.  Most SSH clients can act as a SOCKS proxy server, securely tunnelling requests through your SSH session, making an ordinary (insecure) web request from your Ubuntu computer.
 +
Using the command-line SSH client, you can activate SOCKS by doing:
 
<pre><nowiki>
 
<pre><nowiki>
 
ssh -D 1080 user@host
 
ssh -D 1080 user@host
 
</nowiki></pre>
 
</nowiki></pre>
Now as long as the SSH connection is open, your application can use a SOCKS proxy on port 1080 on your own computer (localhost). For example, in Firefox:
+
'''-D''' refers to '''D'''ynamic port forwarding, and '''1080''' is the standard SOCKS port.  You can use a different port if you prefer, but you should choose a port in the range 1024 to 49151, inclusive.
 +
There is also a [http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter3.html#using-port-forwarding brief discussion] in the PuTTY manual page about how to get PuTTY to act as a SOCKS proxy server.
 +
Once you have set your SOCKS proxy up, your applications can use a SOCKS proxy on the computer you are connecting from. For example, in Firefox:
 
* go to Edit -> Preferences -> Advanced -> Network -> Connection -> Settings...
 
* go to Edit -> Preferences -> Advanced -> Network -> Connection -> Settings...
 
* check "Manual proxy configuration"
 
* check "Manual proxy configuration"
第258行: 第207行:
 
* clear "HTTP Proxy", "SSL Proxy", "FTP Proxy", and "Gopher Proxy" fields
 
* clear "HTTP Proxy", "SSL Proxy", "FTP Proxy", and "Gopher Proxy" fields
 
* enter "127.0.0.1" for "SOCKS Host", and "1080" (or whatever port you chose) for Port.
 
* enter "127.0.0.1" for "SOCKS Host", and "1080" (or whatever port you chose) for Port.
== OpenSSH 4.3 VPNs ==
+
You can also set Firefox to use the DNS through that proxy, so even your DNS lookups are secure:
OpenSSH 4.3 added the ability to create tunnels; see [[UbuntuHelp:SSH VPN|SSH VPN]] for how to set up a VPN using this new feature.
+
* Type in about:config in the Firefox address bar
 +
* Find the key called "network.proxy.socks_remote_dns" and set it to true
 +
The SOCKS proxy will stop working when you close your SSH session.  You will need to change these settings back to normal in order for your browser to work again.
 
== See also ==
 
== See also ==
* [[UbuntuWiki:Recovery/Remote|Recovery/Remote]]
+
* [[UbuntuWiki:Recovery/Remote|Remote|Recovery]]
 +
* [[UbuntuHelp:AdvancedOpenSSH|Advanced SSH configuration and extra security]]
 
----
 
----
 
[[category:CategoryDocumentation]]
 
[[category:CategoryDocumentation]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2008年10月19日 (日) 17:23的版本

  1. title SSH

Parent page: Internet and Networking

Introduction

OpenSSH provides secure remote access to your computer, allowing you to run command line and graphical programs, transfer files, and use a "port forwarding" capability to securely tunnel other protocols through firewalls and untrusted networks. SSH replaces older, insecure programs like telnet, rlogin, and FTP. These older applications transmit passwords across the Internet without encryption, meaning the password could easily be stolen.

Installing the SSH Server

If you want to securely connect to your machine from a remote computer, even a computer running another operating system like Mac OS or Windows, then you need to install the openssh-server package from the main repository. Installing software is discussed in InstallingSoftware.

Testing the server

Once you have installed the server, go to a command-line and type:

ssh localhost

You should be prompted to type your password, and you should get another command-line when you type your password in. If this works, then your SSH server is working properly. To leave the SSH command-line, type:

exit

If you have a local network (such as a home or office network), try logging in from one of the other computers on your network. If nothing happens, you might need to tell your computer's firewall to allow connections on port 22. Finally, try logging in from another computer elsewhere on the Internet - perhaps from work (if your computer is at home) or from home (if your computer is at your work). If you can't access your computer this way, you might need to tell your router's firewall to allow connections from port 22, and might also need to configure Network Address Translation. <<Anchor(public-key-auth)>>

Public key authentication

If your SSH server is visible over the Internet, you should use public key authentication instead of passwords if at all possible. If you don't think it's important, go to your /var/log/ folder and have a look at the files named auth (attempted logins for this week) and auth.0 (attempted logins for last week). My computer - a perfectly ordinary desktop PC - had over 4,000 attempts to guess my password and almost 2,500 break-in attempts in the last week alone. How many thousand random guesses do you think it will take before an attacker stumbles across your password? With public key authentication, every computer has a public and a private "key" (a large number with particular mathematical properties). The private key is kept on the computer you log in from, while the public key is stored on the .ssh/authorized_keys file on all the computers you want to log in to. When you log in to a computer, the SSH server uses the public key to "lock" messages in a way that can only be "unlocked" by your private key - this means that even the most resourceful attacker can't snoop on, or interfere with, your session. As an extra security measure, most SSH programs store the private key in a password-protected format, so that if your computer is stolen or broken in to, you should have enough time to disable your old public key before they break the password and start using your key. Wikipedia has a more detailed explanation of how keys work. Public key authentication is a much better solution than passwords for most people. In fact, if you don't mind leaving a private key unprotected on your hard disk, you can even use keys to do secure automatic log-ins - as part of a network backup, for example. Different SSH programs generate public keys in different ways, but they all generate public keys in a similar format:

<ssh-rsa or ssh-dsa> <really long string of nonsense> <username>@<host>

No matter how your public key was generated, you can add it to your Ubuntu system by opening the file .ssh/authorized_keys in your favourite text editor and adding the key to the bottom of the file. You can also limit the SSH features that the key can use, such as disallowing port-forwarding or only allowing a specific command to be run. This is done by adding "options" before the SSH key, on the same line in the authorized_keys file. For example, if you maintain a CVS repository, you could add a line like this:

command="/usr/bin/cvs server",no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc ssh-dss <string of nonsense>...

When the user with the specified key logged in, the server would automatically run `/usr/bin/cvs server`, ignoring any requests from the client to run another command such as a shell. For more information, see the sshd man page. The main problem with public key authentication is that you need a secure way of getting the public key onto your computer before you can log in with it. If you will only ever log in to your computer from a few other computers (such as logging in to your PC from your laptop), you should copy your SSH keys over immediately, then disable password authentication altogether. If you would like to log in from other computers from time to time (such as a friend's PC), read Strong|Passwords for instructions on how to install a program that generates hard-to-guess passwords. To disable password authentication, open /etc/ssh/sshd_config and look for the following line:

PasswordAuthentication yes

Change it to the following (or add it if you couldn't find that line):

PasswordAuthentication no

You will need superuser access to save this file. Once you have saved the file, restart your SSH server and try logging in again - it shouldn't even ask you for a password now.

Logging in from other computers

Although SSH has many uses, its main job is to provide access to a command-line over a network. Even if you are installing SSH for completely different reasons, it's best to get command-line access access before you try anything more complicated.

From Unix-like systems (including Mac OS X)

Using the command-line

All modern Unix-like systems (Linux, OS X, BSDs, and others) include a command-line ssh client. To login to your computer from a Unix-like machine, go to a command-line and type:

ssh <username>@<computer name or IP address>

For example:

ssh joe@laptop

or:

ssh [email protected]

You should get the same password prompt as before (or be told you can't log in, if passwords are disabled).

Tip: Keep Alive

Your ssh session will automatically log out if it is idle. To keep the connection active (alive) add this to /etc/ssh/ssh_config on the machine you are logging in from:

ServerAliveInterval 120

This will send a "keep-alive" signal to the server every 120 seconds.

Tip: Generating Public Keys

To create your public and private SSH keys on the command-line, do:

mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -q -f ~/.ssh/id_rsa -t rsa

Then enter a password that will protect your private key while it's stored on the hard drive (or press enter to leave your private key unprotected). Your public key is now available as .ssh/id_rsa.pub in your home folder.

Using PuTTY

PuTTY is a popular graphical SSH client. Although the Windows client is more widely known, you can get the Linux version by installing the putty package from the Universe repository. See the windows section for further information about PuTTY.

Using SecPanel

!SecPanel is another graphical SSH client. You can get it by installing the secpanel package from the Universe repository.

From Windows

Using PuTTY

PuTTY is a free SSH client for Windows, which you can download from here. 模板:Http://img296.imageshack.us/img296/7404/putty3zv1.png To log in to your computer, type your computer's name or IP address into the "Host Name (or IP address)" box, click on the "SSH" radio button, then click "Open". You will be asked for your username and password, then you'll get a command-line on your Linux computer.

Tip: Keep alive

Your PuTTY (ssh) session will automatically log out if it is idle. To keep the connection active (alive), before you make the connection, select "Connection" on the left of the PuTTY Configuration window, and type 120 in the "Seconds between keepalives (0 to turn off)" box.

Tip: Importing your openssh keys

For security reasons, you should normally create a new SSH private key for every computer you use. However, if you have no choice but to reuse a private key, PuTTY can import an Ubuntu private key with puttygen.exe, as described in the PuTTY manual.

Tip: Generating Public Keys

The PuTTY manual has a section on public key authentication.

Using Cygwin

Cygwin is a unix-like environment for Windows. You can download it here: Cygwin To install it run the Setup.exe and choose a server. To use ssh, you need to install the openssh package (it includes BOTH a server and client). It is located under "Net" and is called openssh. 模板:Http://img296.imageshack.us/img296/4139/cygwinopensshpv3.png Then you can launch Cygwin as a .bat file and get a Terminal Interface: 模板:Http://img127.imageshack.us/img127/9751/zshdx1.png Now follow the earlier instructions for using the command-line on a Unix-like OS.

Transferring Files Remotely With SSH

Another important function of SSH is allowing secure file transfer using SCP and SFTP.

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png%7D%7D Limitation: You can't move files between two remote computers. Either the source or destination must be a local file. However, if you log in to a remote machine with ssh, you can copy files between two remote machines on that machine's command-line.

From Unix-like systems (including Mac OS X)

Using the Command-line

Just as all modern Unix-like systems have an SSH client, they also have SCP and SFTP clients. To copy a file from your computer to another computer with ssh, go to a command-line and type:

scp <file> <username>@<IP address or hostname>:<Destination>

For example, to copy your TPS Reports to Joe's Desktop:

scp "TPS Reports.odw" joe@laptop:Desktop/

This will copy TPS Reports.odw to /home/joe/Desktop, because SCP uses your home folder as the destination unless the destination folder begins with a '/'. To copy the pictures from your holiday to your website, you could do:

scp -r /media/disk/summer_pics/ [email protected]:"/var/www/Summer 2008/"

The -r (recursive) option means to copy the whole folder and any sub-folders. You can also copy files the other way:

scp -r [email protected]:/home/catbert/evil_plans/ .

The '.' means to copy the file to the current directory. Alternatively, you could use secret_plans instead of '.', and the folder would be renamed. Finally, if you want to look around the remote machine and copy files interactively, you can use SFTP:

sftp [email protected]

This will start an SFTP session that you can use to interactively move files between computers.

Using SSHFS

SSHFS is a recent addition to Linux that allows you to make a remote filesystem available over SSH act as if it was inside a folder on your own system. See UbuntuHelp:SSHFS for details.

Using GNOME

Click Places -> Connect to Server. Select SSH for Service Type, write the name or IP address of the computer you're connecting to in Server, the user you'd like to connect as in User Name, and a name for the connection if you wish. Files can be copied by dragging and dropping between this window and other windows.

Using KDE

Open Konqueror, and in the address bar type:

fish://username@server_address

Files can be copied by dragging and dropping them between this window or tab and to other windows or tabs.

Using other programs

!SecPanel and PuTTY also have file transfer utilities, although they're generally not as easy to use as the ones discussed above.

From Windows

PuTTY has command-line SCP and SFTP utilities for Windows that act much like the command-line scp and sftp utilities for Unix-like systems. Alternatively, WinSCP is a graphical utility to transfer files. 模板:Http://img296.imageshack.us/img296/491/winscpnh5.png

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png%7D%7D Winscp uses PuTTY keys for public key authentication

Running GUI Programs

SSH can be used to tunnel graphical applications over a network as well as text-based ones. GUIs are more complicated than text, and therefore harder to set up and more likely to require extra software.

Check Your SSH Server Settings

By default, Ubuntu's SSH server has everything you need enabled. If you have disabled features for security reasons, you might not be able to start GUI programs. To make sure, look in your /etc/ssh/sshd_config for this line:

AllowTcpForwarding No

This disables forwarding of Internet connections over SSH. You should delete this line if you want to use VNC for GUI programs (discussed below). Next, look for this line:

X11Forwarding yes

This enables Unix-style GUI forwarding over SSH. If you don't see it, open the file in your favourite text editor and add that line. You will need superuser privileges to save changes to /etc/ssh/sshd_config.

Single Applications

If you are logging in from a Unix-like operating system, you can forward single applications over SSH very easily, because all Unix-like systems share a common graphics layer called X11. This even works under Mac OS X, although you will need to install and start the X11 server before using SSH. To forward single applications, connect to your system using the command-line, but add the -X option to forward X11 connections:

ssh -X joe@laptop

Once the connection is made, type the name of your GUI program on the command-line:

firefox &

Your program will start as normal, although you might find it's a little slower than it would be if it were running locally. The trailing & means that the program should run in "background mode", so you can start typing new commands in straight away, rather than waiting for your program to finish. If you only want to run a single command, you can log in like this:

ssh -f -T -X joe@laptop firefox

That will run Firefox, then exit when it finishes. See the SSH manual page for information about -f and -T. If you start an application and it complains that it cannot find the display, try installing the xauth package from the Main repository. Xauth is installed by default with desktop installations but not server installations. If you suspect that programs are running slowly because of a lack of bandwith, you can turn SSH compression on with the -C option:

ssh -fTXC joe@laptop firefox

Using -fTXC here is identical to -f -T -X -C.

Nested windows

Xephyr is a program that gives you an X server within your current server. It's available in the xserver-xephyr package in the Main repository. 模板:Http://cafelinux.org/OptickleArt/albums/userpics/normal Xephyr.png Two ssh forwarded desktops on dual monitors, click to enlarge Setting up Xephyr was explained briefly in the Ubuntu forums.

Tunneling VNC connections through ssh

Virtual Network Computing ("VNC") is a cross-platform way of sharing a desktop. Once you've set your SSH server up, see UbuntuHelp:VNC for more information.

Breaking out of a controlled network

Sometimes it's useful to tunnel all web traffic over SSH, without having to start Firefox on a remote computer. For example, to work around network monitoring or snooping, or to avoid badly configured routers on the Internet. If you can change the settings on your web browser, you can probably use SSH to circumvent these filters.

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png%7D%7D Warning: Filtering and monitoring is usually implemented for a reason. Even if you don't agree with that reason, your IT department might not take kindly to you flouting their rules.

SOCKS is a protocol is used by some proxy servers. The SOCKS protocol allows a client (such as a web browser) to ask a server to download a file for it, rather than downloading the file directly. Most SSH clients can act as a SOCKS proxy server, securely tunnelling requests through your SSH session, making an ordinary (insecure) web request from your Ubuntu computer. Using the command-line SSH client, you can activate SOCKS by doing:

ssh -D 1080 user@host

-D refers to Dynamic port forwarding, and 1080 is the standard SOCKS port. You can use a different port if you prefer, but you should choose a port in the range 1024 to 49151, inclusive. There is also a brief discussion in the PuTTY manual page about how to get PuTTY to act as a SOCKS proxy server. Once you have set your SOCKS proxy up, your applications can use a SOCKS proxy on the computer you are connecting from. For example, in Firefox:

  • go to Edit -> Preferences -> Advanced -> Network -> Connection -> Settings...
  • check "Manual proxy configuration"
  • make sure "Use this proxy server for all protocols" is cleared
  • clear "HTTP Proxy", "SSL Proxy", "FTP Proxy", and "Gopher Proxy" fields
  • enter "127.0.0.1" for "SOCKS Host", and "1080" (or whatever port you chose) for Port.

You can also set Firefox to use the DNS through that proxy, so even your DNS lookups are secure:

  • Type in about:config in the Firefox address bar
  • Find the key called "network.proxy.socks_remote_dns" and set it to true

The SOCKS proxy will stop working when you close your SSH session. You will need to change these settings back to normal in order for your browser to work again.

See also