SSH Tunnel in 30 Seconds (Mac OSX & Linux)

Some days, I wonder why VPN’s are really necessary when we can just use an SSH tunnel.
If you’re on Mac or a flavour of Linux, this SSH tunnelling tutorial is for you.

“A secure shell (SSH) tunnel consists of an encrypted tunnel created through a SSHprotocol connection. Users may set up SSH tunnels to transfer unencrypted traffic over a network through an encrypted channel.” – Wikipedia

Launch an SSH tunnel

To initiate your SSH tunnel, simply [open Mac OSX / Linux Terminal][1] and connect to your remote server via SSH with the following flags:

ssh -D 8080 -C -N username@example.com

This will launch our SSH tunnel on port 8080 and route all traffic (securely) through the server at example.com.

Browse the Web with Your SSH Tunnel (Chrome)

Now, let’s start browsing the web using our new SSH tunnel.

Mac OSX:

  1. Open Google Chrome
  2. Select ‘Chrome’ up the top left
  3. Select ‘Preferences’
  4. Select ‘Show advanced settings…’
  5. Select ‘Change proxy settings…’
  6. Select ‘SOCKS Proxy’
  7. Enter ’127.0.0.1′
  8. Enter port ’8080′
  9. Save changes by selecting ‘OK’

How do I view file sizes (KB,MB,GB) with “ls” in Linux?

When you want to investigate a list of files and their respective sizes, it can be unwieldy to print the size in the default byte format while using the ‘ls’ command in Linux. Today, I’ll show you how to present the data in a more human-readable manner using ‘ls -lah’.

Listing Files and Their Data Format (KB, MB, GB) using ‘ls -lah’

We can use the ‘ls -lah’ command to print a list of files and their prefixed data format (KB, MB, GB)
The ‘ls -lah’ command displays the file size in the fifth column, similar to the traditional ‘ls -all’ command, e.g.

-rw-r--r--  1 root root    165 2011-04-29 21:36 shells
drwxr-xr-x  2 root root   4.0K 2011-04-29 21:36 skel
drwxr-xr-x  2 root root   4.0K 2012-10-23 01:14 ssh
drwxr-xr-x  4 root root   4.0K 2012-10-18 07:29 ssl
-r--r-----  1 root root    574 2011-04-15 12:02 sudoers
drwxr-xr-x  2 root root   4.0K 2012-10-18 07:29 sudoers.d
-rw-r--r--  1 root root   2.1K 2010-12-21 04:56 sysctl.conf
drwxr-xr-x  2 root root   4.0K 2011-04-29 21:39 sysctl.d
drwxr-xr-x  2 root root   4.0K 2011-04-29 21:39 terminfo
-rw-r--r--  1 root root     17 2012-10-18 07:27 timezone
-rw-r--r--  1 root root   1.3K 2011-03-01 00:45 ucf.conf
drwxr-xr-x  3 root root   4.0K 2011-04-29 21:39 udev
drwxr-xr-x  3 root root   4.0K 2012-10-18 08:12 ufw
drwxr-xr-x  2 root root   4.0K 2011-04-29 21:36 update-motd.d

For more information regarding the flag use (l,a,h) please refer to the [quick ‘ls’ reference table][1] at the bottom of this post.

Traditional Listing of Files using ‘ls -all’

Comparatively, we’ll print a list of files using the traditional ‘ls -all’ command:

-rw-r--r--  1 root root     165 2011-04-29 21:36 shells
drwxr-xr-x  2 root root    4096 2011-04-29 21:36 skel
drwxr-xr-x  2 root root    4096 2012-10-23 01:14 ssh
drwxr-xr-x  4 root root    4096 2012-10-18 07:29 ssl
-r--r-----  1 root root     574 2011-04-15 12:02 sudoers
drwxr-xr-x  2 root root    4096 2012-10-18 07:29 sudoers.d
-rw-r--r--  1 root root    2083 2010-12-21 04:56 sysctl.conf
drwxr-xr-x  2 root root    4096 2011-04-29 21:39 sysctl.d
drwxr-xr-x  2 root root    4096 2011-04-29 21:39 terminfo
-rw-r--r--  1 root root      17 2012-10-18 07:27 timezone
-rw-r--r--  1 root root    1260 2011-03-01 00:45 ucf.conf
drwxr-xr-x  3 root root    4096 2011-04-29 21:39 udev
drwxr-xr-x  3 root root    4096 2012-10-18 08:12 ufw
drwxr-xr-x  2 root root    4096 2011-04-29 21:36 update-motd.d

Linux ‘ls’ Flags Quick Reference Table

FLAG DESCRIPTION EXAMPLE
-l Displays file types, owner, group, size, date and filename. ls -l
-a Displays all files, including hidden files (suffixed with “.”). ls -a
-h Displays files with human readable format, e.g. 1k, 100MB, 1G. ls -h

Remotely transfer files via SSH using SCP in Linux

Ever needed to quickly transfer a few files across to a remote host whilst in SSH? What about transferring a cPanel cpmove file? Perhaps you need to fetch a website from a remote server? SCP is for you. It provides the same level of authentication as SSH, and it’s extremely easy.

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

Usage Example of Remotely Transferring Files using SCP (Secure Copy)

If you wanted to transfer a file from your server to another server, simply issue the following to do so:

scp mylocalfile.txt root@examplerecipient.com:/recipient/directory/

Example of Remotely Fetching Files using SCP (Secure Copy)

Let’s say you wanted to fetch an entire website that has been zipped up on a remote host, you could issue:

scp root@examplefetch.com:/path/to/zip/mywebsite.zip /path/to/store/zip/

SCP Error “SCP port 22 connection refused”

If you’re receiving this error, it’s most likely because the remote hosts’ port has been changed. You can specify a port using the -P flag, e.g.:

scp -P 2211 mylocalfile.txt root@examplerecipient.com:/recipient/directory/

SCP Error “SCP SSH no such file or directory”

If you encounter this error, it might be because you are not specifying a directory. If you would like to send or receive a file from the root directory of the remote host, you will need to specify it as such:

scp mylocalfile.txt root@examplerecipient.com:/

Looking Glasses IPv6 Traceroute / BGP / Ping

Test your server connectivity from multiple world-wide looking glasses using Traceroute, BGP, and Ping.

PROVIDER SERVICES IPV6
Internode Traceroute, BGP Summary, BGP, Ping Yes
Vocus Traceroute, BGP Summary, BGP, Ping Yes
NTT Traceroute, BGP, Ping Yes
Telstra Traceroute Yes
Exetel Traceroute, Ping No
Pipe Traceroute, BGP, BGP Summary, Ping No
AARnet Traceroute, BGP, Ping, TCPTRACE Yes
iiNet Traceroute, BGP, BGP Summary, Ping No
TPG Traceroute, Ping No
AAPT / Connect Traceroute, BGP, Ping Yes

Can’t locate Time/HiRes.pm Perl

Running into errors while installing a program that requires a specific Perl module on CentOS – cPanel / WHM / Fedora / RHEL?

It’s easy to fix, simply install the Perl Time/HiRes package by issuing the following command:

yum install perl-Time-HiRes

If you run into dependency hell, you can install all the available Perl modules via:

yum install perl*

Note: the above command will install every available Perl module and their dependencies, so use it wisely!

Fixing [Error writing fstab: Read-only file system]

If you’ve accidentally screwed your /etc/fstab file and rebooted, you might run into some issues while trying to make changes to fix it, i.e.

Error writing fstab: Read-only file system

Fixing Error writing fstab: Read-only file system

In order to fix this issue, you will need to remount the affected mount-point with read / write options.

This can be done by issuing the mount command:

mount -o remount,rw /

Where / indicates your root mount point, change accordingly if you have borked a different mount point.

How to Create an SSH Config File Shortcut on Mac OSX / Linux

Ever wondered how people just type ssh myserver and it automagically connects? I’ll show you how to on Mac OSX / Linux.

Create an SSH Config File

To create an SSH config file, open your terminal and issue the following command:

vi ~/.ssh/config

If you’re a little unsure of vi, replace it with your favourite editor, such nano.

In the same file, copy and paste the following lines:

Host myserver
   HostName myserver.com
   User root

You’re done! Save the file and open your new SSH session by typing:

ssh myserver

What if I want to specify a Public / Private Key (IdentityFile) to connect?

If you are using Public Key authentication to connect to myserver, just amend your SSH config file with the IdentyFile directive:

Host myserver
   HostName myserver.com
   User root
   IdentityFile ~/.ssh/id_rsa

Where id_rsa is your private key file.

Like this article? Let me know in the comments below!