Learning Journal

Enable Compression for SSH

I use SSH a lot, and I mean, A LOT! Because I manage 3 other servers, and they are located in different parts of the world, I rely completely on SSH to keep them up and running optimally.

Naturally, I keep myself informed on the subject quite a bit but in nearly every article I read online, with all their ssh example commands, very few mention this really significant client option: the -C or enable compression option i.e. the option that requests compression of all data being transferred over an SSH connection.

ssh Command-line Examples

Access Your Server Remotely Over SSH

Typical example:
ssh [email protected]

Same example, but with the compression option enabled:
ssh -C [email protected]

If you want to see just how much data was sent and received and how much of all this data was compressed, all you have to do is add the -v (verbose) option to the command and a full report will be displayed to you at the end of your ssh session:
ssh -C -v [email protected]

Example report at the end of a scp command with compression (-C) and verbose (-v) options enabled:

[[email protected] ~]$ scp -Cv [email protected]:/usr/share/gid/GeoIPCity.dat ./
GeoIPCity.dat 100% 17MB 169.0KB/s 01:42
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype [email protected] reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 4768, received 11452904 bytes, in 102.6 seconds
Bytes per second: sent 46.5, received 111675.0
debug1: Exit status 0
debug1: compress outgoing: raw data 1477, compressed 796, factor 0.54
debug1: compress incoming: raw data 17653332, compressed 11441447, factor 0.65

Copy a File with scp & ssh

If I were to copy a file from my remote server to a local PC, a typical example may look like this:
scp [email protected]:/usr/share/gid/GeoIPCity.dat ./

The same task, but this time requesting the transfer to be compressed:
scp -C [email protected]:/usr/share/gid/GeoIPCity.dat ./

Set Compression On by Default for SSH and SCP

Now that you know you can use the (compression) option any time you like, you might want to avoid typing it altogether! I mean, why would you NOT use SSH with the compression option enabled all the time? So, instead of remembering to type it every time, you can do what I did and enable the option in your ssh configuration file at ~/.ssh/config. All I did was add was this one line to the top of the file:
Compression yes

[[email protected] ~]$ man 5 ssh_config

Specifies whether to use compression.  The argument must be “yes” or “no”.  The default is “no”.

Today, the top section of my ssh configuration file looks like this:

[[email protected] ~]$ cat .ssh/config
Compression yes
PreferredAuthentications publickey,password
Protocol 2

Type man 5 ssh_config in a terminal to see all ssh options.

Leave a Reply

Your email address will not be published. Required fields are marked *