How-To: Raspberry Pi as a 3G/4G Router

Update: 15 April 2016 – Added information about which IP address to use and assigning static IP addresses for printers and servers

Recently I have needed to find an emergency alternative to my broadband due to the regional wide area network, Digital Region, being shut down, and the ISP Origin making a mess of getting all their cutomers onto ASDL. To get quickly back onto the internet, I have bought an ZTE MF823 4G Mobile Broadband Dongle as supplied by the badly named ‘three’ mobile phone company. As I have my own internal wired network, with multiple computers and ‘things’ there is a need to have something more sophisticated than just plugging the dongle into a single PC.

network diagram

Here is my recipe for setting up a Raspberry Pi as a router with an ZTE MF283 Dongle. In this setup all the computers are on a wired Ethernet connection using a switch for the network. The Pi has Raspbian Debian Wheezy installed (June 2014) with all the latest updates made. For testing, the dongle is plugged into the USB port via a powered hub, and the Pi connected to a switch with another PC running Linux Mint.

Which IP addresses to use?

In this How-To I am using the IP address range 192.168.2.xxx this is to avoid conflict with the cable router which uses the 192.168.1.xxx range (the DHCP server is switched off on the router). IPv4 addresses are split into three different ranges, the 192.168.xxx.xxx range – 192.168.0.0 to 192.168.255.255 gives a possible 65,536 addresses but for your home it is unlikely you’ll have more than 255 network devices, so we can simplify things by limiting the address range used to 192.168.2.xxx and avoid the troublesome world of subnet masking.

192.168.xxx.xxx is used as its been designated for use on private networks by the Internet Assigned Numbers Authority this is a well established convention and is best practice. Two other IPv4 address ranges are available for larger private networks: 172.16.0.0-172.31.255.255 and 10.0.0.0-10.255.255.255 with 1,048,576 and 16,777,216 available addresses respectively, the most suitable network class should be chosen for your network.

Setup the Dongle

This USB dongle has its own built in dialer so you do not need ppp or wvdial installed, it appears as a USB ethernet device on the Raspberry Pi. You will need a powered USB hub as the dongle can draw more power than the Pi can provide, the symptoms of too much of power being drawn will be the Pi behaving erratically or restarting unexpectedly.

With the dongle plugged in, check that it is recognised by the Pi with lsusb, it can be seen here as ‘ZTE WCDMA Technologies MSM’:

The device ID is 19d2. and 1405 is the mode, this should be 1405 – CDC ethernet. If it is not, try removing the micro-SD card and rebooting the Pi, the device modes available are:

  • 1225 – Default mode. USB Mass Storage Device + CD-ROM + card reader.
  • 1403 – Modem mode. RNDIS + Mass Storage Device.
  • 1405 – CDC ethernet
  • 0016 – Download mode

As the dongle also has a Mass Storage Device the Raspberry may not switch to CDC ethernet. If the mode does not change, try the following with usb-modeswitch:
$ sudo apt-get install usb-modeswitch
$ sudo usb_modeswitch -v 0x19d2 -p 0x1405 -d

I did not have to change the mode as it was correct already, and it didn’t change when I tried setting it as a Mass Storage device, I have not explored this any further.

When first plugged in the dongle was recognised as a ethernet device but it did not obtain an IP address:

if this is the case with you, add the following two lines to the end of sudo nano /etc/network/interfaces:
auto usb0
iface usb0 inet dhcp

the dongle provides its own address to the computer. Reboot, and you should see the obtained address:

The address 192.168.0.185 is now the internet address of the computer the dongle always assigns this address, there is also a useful web status page on http://192.168.0.1

Configuring the network

first of all enable ip4 forwarding, edit the file sudo nano /etc/sysctl.conf and uncomment the line:
net.ipv4.ip_forward=1
this will enable forwarding on reboot, you can also enable IP forwarding immediately with:
$ sudo sysctl -w net.ipv4.ip_forward=1

We now need to give the Pi a static IP address on the internal network. Edit sudo nano /etc/network/interfaces so you end up with a file that looks like this:

this gives the Pi a static IP address of 192.168.2.1.

DHCP

The next stage is to give the other computers on your network an IP address, this is done with a dhcp server:
$ sudo apt-get install isc-dhcp-server
you will need to configure dhcp sudo nano /etc/dhcp/dhcpd.conf, here is mine:

This will assign IP addresses in the range 192.168.2.50 to 192.168.2.150 to any computer connected to your network. I have used Open DNS for the Domain name Servers, if you wish to use google’s use:
option domain-name-servers 8.8.8.8, 4.4.4.4;instead.

I have also given my network printer a static IP address, it is still assigned by the DHCP server but never changes, the same would apply to any file servers and the like, I would assign static devices addresses that are outside your dynamically assigned range. Reboot the Pi and then your test computer.

Your test computer should now have an IP address (192.168.2.51), and the gateway point to the Pi (92.168.2.1):

Accessing The Internet

The final part is to have the incoming traffic on the the Ethernet port eth0, go out on the dongle usb0. This is achieved with iptables, a firewall and traffic router. Install with:
$ sudo apt-get install iptables
and you need to setup Network Address Translation, NAT and forwarding. This short bash script clears any old settings before applying the new rules:

Where LAN is your internal network, and WAN is the internet. The final line allows you access to the Dongle’s built in web status page from any browser on your internal network, just use: http://192.168.2.1:2525

Save the file in your home directory as ~/ipt.sh, make it executable and run the script.

$ chmod +x ~/ipt.sh
$ sudo ~/ipt.sh

From your test computer, you will now be able to access the internet.

Finally, you now need to have iptables reload when you start the Pi. Export the iptables settings to a file with:

$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

and create a file sudo nano /etc/network/if-up.d/iptables with the following contents:

and make it executable sudo chmod +x /etc/network/if-up.d/iptables

after a reboot you can see your iptables with sudo iptables -L and sudo iptables -t nat -L and you can see web traffic passing through the router with sudo tcpdump -i any -nn port 80.

Adding a Proxy Server

This is optional, but a transparent proxy server and cache may reduce the amount of traffic on your 3G/4G connection, mileage varies and the amount of data cached was less than I thought it would be, I also found that my Humax Freesat box really didn’t like the proxy server and wouldn’t update its TV schedules while it was on. I have used squid3 for this.
sudo apt-get install squid3
Update the squid3 configuration /etc/squid3/squid.conf so it has the following. The original is rather large, so you may want to make a copy and create a new one:

then restart squid3
sudo /etc/init.d squid3 restart
add the following iptables rule to redirect all traffic on port 80 to squid3:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128
you should now be able to watch the web traffic being processed through squid3 with:
sudo tail /var/log/squid3/access.log -f
finish off by exporting your iptables again, so they are reloaded on reboot:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Raspberry Pi - 4G Router

References and sources:

29 thoughts on “How-To: Raspberry Pi as a 3G/4G Router”

  1. Hello Karl,

    Many thanks for sharing this info. I’m struggling to bring up exactly same setup but following differences:
    * Huewei USB-Dongle in place of ZTE
    * Instead of (slave computers+network switch), I have slave raspberry pi tailed to “Raspberry Pi as a router”

    In contrast to auto modem mode of your ZTE; The Huewei dongle requires manual usb_modeswitch followed by wvdial invocation to have internet connection. Post wvdial “ifconfig” lists it as ppp0 instead of usb0. I substituted ppp0 for usb0 in my setup

    Now the slave raspberry with following contents in its’ “/etc/network/interfaces”

    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet dhcp

    **fails with error**
    DHCPDISCOVER on eth0 255.255.255.255 port 67 interval 7 13
    NO DHCPDISCOVER reccived

    1. Hello Rich,
      On the Pi, eth0 is the on-board Ethernet port. You will need to assign a static IP address to eth0 as in the posting, or have whatever you are connecting to on your internal network to be running a DHCP server.

  2. Nice and well done DIY m8 😉
    Thanks in advance for all ths tips, gonna test it today i think.
    Differences, Rpi B+ and Huawei E398

  3. Hi, does adding the Squid proxy get around the recent tethering 2GB restrictions (tethering is not allowed at all in a “Feel-at-home” country) implemented by Three on all new contracts?

  4. Hello Richard,
    No it won’t. Squid will only reduce the amount of data when you surf the web by locally caching some of the data. Having used Squid for a while now, the amount saved is very little.

  5. Thanks for the DIY; most comprehensive I’ve seen yet. I’m actually attempting to do the reversal; plug a 4G USB Aircard into the RPi and bridge it to the ethernet. The intent is to connect a remote IP Cam to the RPi ethernet and use the Aircard as the connection. I’ll have to review your instructions and see if I can make it work. Any suggestions?

  6. i’m looking to do somthing similar to get internet to my phone while i am at work (i work physical security) and i was looking to use my cricket 3g UTStarcom U100 modem, when i connect the modem and boot into raspbian it does not recognise the device in a network manager (cant find one) tried using the how-to i found online for getting the device to dial, and lsusb shows its there, just cant get it to actually do anything, good writeup all the same, and if i can ever get this modem to cooperate with raspbian i should be able to do somthing similar

  7. Hello Karl, I wonder what this line means to?
    # make ZTE web interface available
    iptables -t nat -A PREROUTING -p tcp –dport 2525 -j DNAT –to-destination 192.168.0.1:80. Thanks

      1. thanks, karl . but unfortunately my dongle not showing any adress after i reboot and set it to dhcp in /etc/network/interfaces. How can i see them? Is there any usb modem has the same interface? many thanks for good tut and Happy New Year.

        1. my interface dongle is wwan0 showing bellow.:
          wwan0 Link encap:Ethernet HWaddr 02:50:f3:00:00:00
          inet6 addr: fe80::50:f3ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
          RX packets:0 errors:119 dropped:0 overruns:0 frame:0
          TX packets:209 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B) TX bytes:67302 (65.7 KiB)

          1. Eko,
            It looks like your dongle is providing an IP6 address (fe80::50:f3ff:fe00:0/64), when what you want is an IP4 (eg: 192.168.1.20).

            As your dongle is differnt from mine, and I don’t have it here to try, these suggestions may not work. In /etc/network/interface instead of putting usb0, try wwlan0 like so:

            auto wwan0
            iface wwan0 inet dhcp

            and restart, or try assigning a static address to the dongle:

            auto wwan0
            iface wwan0 inet static
            address 192.168.1.1
            netmask 255.255.255.0

            I expect someone will have setup your dongle. Try google.

  8. Oke,Thanks Karl.,
    which one i use interface in iptables “-to-destination 192.168.0.1:80” or 10.0.0.1:80 instead or something else?
    this is my /var/log/messages when first time i pluged my usb.
    Jan 1 15:37:36 parrotsec kernel: [ 710.000671] usb 2-2: Manufacturer: HUAWEI Technology
    Jan 1 15:37:36 parrotsec kernel: [ 710.007733] option 2-2:1.0: GSM modem (1-port) converter detected
    Jan 1 15:37:36 parrotsec kernel: [ 710.007822] usb 2-2: GSM modem (1-port) converter now attached to ttyUSB0
    Jan 1 15:37:36 parrotsec kernel: [ 710.012829] cdc_ether 2-2:1.1 wwan0: register ‘cdc_ether’ at usb-0000:00:1d.1-2, Mobile Broadband Network Device, 02:50:f3:00:00:00
    Jan 1 15:37:36 parrotsec kernel: [ 710.012949] option 2-2:1.3: GSM modem (1-port) converter detected
    Jan 1 15:37:36 parrotsec kernel: [ 710.013105] usb 2-2: GSM modem (1-port) converter now attached to ttyUSB1
    Jan 1 15:37:36 parrotsec kernel: [ 710.013176] option 2-2:1.4: GSM modem (1-port) converter detected
    Jan 1 15:37:36 parrotsec kernel: [ 710.013293] usb 2-2: GSM modem (1-port) converter now attached to ttyUSB2
    Jan 1 15:37:36 parrotsec kernel: [ 710.013363] usb-storage 2-2:1.5: USB Mass Storage device detected
    Jan 1 15:37:36 parrotsec mtp-probe: checking bus 2, device 3: “/sys/devices/pci0000:00/0000:00:1d.1/usb2/2-2”
    Jan 1 15:37:36 parrotsec kernel: [ 710.013454] scsi9 : usb-storage 2-2:1.5
    Jan 1 15:37:36 parrotsec kernel: [ 710.013587] usb-storage 2-2:1.6: USB Mass Storage device detected
    Jan 1 15:37:36 parrotsec kernel: [ 710.013721] scsi10 : usb-storage 2-2:1.6
    Jan 1 15:37:36 parrotsec mtp-probe: bus: 2, device: 3 was not an MTP device
    Jan 1 15:37:37 parrotsec logger: usb_modeswitch: switched to 12d1:1465 on 002/003
    Jan 1 15:37:37 parrotsec kernel: [ 711.020710] scsi 10:0:0:0: Direct-Access Vodafone Storage (Huawei) 2.31 PQ: 0 ANSI: 2
    Jan 1 15:37:37 parrotsec kernel: [ 711.021033] sd 10:0:0:0: Attached scsi generic sg3 type 0
    Jan 1 15:37:37 parrotsec kernel: [ 711.021720] scsi 9:0:0:0: CD-ROM Vodafone CD ROM (Huawei) 2.31 PQ: 0 ANSI: 2
    Jan 1 15:37:37 parrotsec kernel: [ 711.030713] sd 10:0:0:0: [sdc] Attached SCSI removable disk
    Jan 1 15:37:37 parrotsec kernel: [ 711.033680] sr1: scsi-1 drive
    Jan 1 15:37:37 parrotsec kernel: [ 711.034430] sr 9:0:0:0: Attached scsi generic sg4 type 5

  9. This article was huge help in my setup. Using an Samsung Galaxy Note 3 to tether LTE to RPi and to my computer. (Much faster connection.)

    Speedtest though computer connected to RPi and RPi to Note 3:

  10. Thank you so so so SO much for this post. I’ve been beating my head against a wall for over a day on this, running around with usb_modeswitch, flirting with ppp and vwdial.. and all I needed was the

    auto usb0
    iface usb0 inet dhcp

    in network/interfaces.

    So many thanks – you have literally made my day! (now, back to my raspberry pi!)

  11. Great tutorial,

    Mybe you have time to post another amazing article “bounding multiple 3G Dongle” using raspberry Pi?

    TIA

  12. Thank you very much for the manual. I suggest to emphasize the need for external powered hub in bold or in red 🙂
    I had problems with Huawei E3372 (usb_modeswitch simply did not switch, whatever I tried), but using external powered usb switch solved the problem.

  13. hi thanks for sharing this as a pi beginner i have been looking at something like this to start with perhaps you could point me in the rite direction
    do would the add on stacking 4g gsm cards work in the same way (so everything is in a neat package hardwired to the pi ) ?

    and if this will would it be possible to stack 3 or even 4 cards to cover the main cell signal operators and combine the signal to plug in a laptop into the Ethernet port ?

  14. Must add some thanks to this page. I’m doing it slightly differently, using the on-board wifi on a Pi 3 to connect to a Mifi to provide a backup if my broadband goes down. Rather surprised to find it all seems to work.

  15. Excellent guide.

    Made good use of my old Raspberry Pi Model B (original). Used Raspbian Lite, an unlocked Vodafone K3570-z 3G modem (hopefully upgrading to a 4G modem at some point) and a TP-Link TL-WN725N USB Wireless Adapter setup as a Wi-Fi hotspot. Works a treat (after I got the NAT correct).

    I now have a portable 3G hotspot.

    Thank you

  16. is it possible to reroute all traffic via openvpn? if so then command in cron : openvpn –config /path/to/example/config.ovpn should do the work?

  17. Great tutorial

    Used ZTE 3G/4G modem + RPiB+ + Apple Airport Extreme router for a year. Yesterday it stopped working. Found this tutorial again and backtracked everything, until I realized my /etc/network/interfaces file had disappeared. Followed your instructions on interfaces settings and now it’s up again 🙂

  18. If you just want a simple 4G router bridge, use OpenWRT (I recommend ROOter builds) instead of Debian.
    It has a nice Web based interface which is much easier to use than a Linux shell.
    With my old 256 MB RAM Pi running @ 1 Ghz, with an LTE modem I can get a decent 50 mbit/s download speed.

  19. Hello, I have been looking around for a solution such as this. Thank you for this simple how to. Question I have is if there is a way to use 4 (or more) USB modems and balance the load between them.
    I want to hook up something like this to a video encoder and stream the video to an online server. I saw on youtube a video of someone using a pi with 3 connections (3g, wifi, and wimax) to a laptop but no available information, no how to, etc. https://www.youtube.com/watch?v=1nBsQ1SutPY
    It appears they scrapped the project. If there is any info on how to, please inbox me.

    1. Still looking for any solution! Need to aggregate/ bond multiple 3g/4g USB modems to raspi or another board for rtmp streaming.

Leave a Reply

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