{"id":326,"date":"2018-10-30T09:44:15","date_gmt":"2018-10-30T09:44:15","guid":{"rendered":"https:\/\/tstableford.co.uk\/wordpress\/?p=326"},"modified":"2018-10-30T09:46:48","modified_gmt":"2018-10-30T09:46:48","slug":"raspberry-pi-zero-raspbian-stretch-otg-ethernet","status":"publish","type":"post","link":"https:\/\/tstableford.co.uk\/wordpress\/?p=326","title":{"rendered":"Raspberry Pi Zero Raspbian Stretch OTG Ethernet"},"content":{"rendered":"<p>Raspberry Pi Zero&#8217;s come equipped with a USB OTG port which means they can act as a USB client, such as in this case becoming a network adapter. There are quite a few articles on configuring the Pi to do this but most of them are based on Raspbian Jessie rather than Raspbian Stretch. There&#8217;s only a small difference but it&#8217;s not well documented really.<\/p>\n<p>This guide will cover:<\/p>\n<ul>\n<li>Flashing an SD card with Raspbian Stretch.<\/li>\n<li>Configuring the SD card from an Ubuntu host machine to act as a network client with USB OTG.<\/li>\n<li>Configuring the host machine to relay internet access to the Pi.<\/li>\n<\/ul>\n<p>This guide also assumes some familiarity with the Linux console and Ubuntu.<\/p>\n<h2>Step 1 &#8211; Flashing the Pi<\/h2>\n<p>This is a simple enough step but somewhat depends on the host machine and your preferences. I used the minimal Raspbian Stretch image from <a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspbian\/\">here<\/a>. You might choose to use the full version but keep in mind this guide will only work with a version of Raspbian Stretch.<\/p>\n<p>Unzip the downloaded image with your archiver GUI or&#8230;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">(cd ~\/Downloads &amp;amp;amp;&amp;amp;amp; unzip *-raspbian*.zip)<\/pre>\n<p>Flash the image to your SD card (beware of DD the data destroyer when using this):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">sudo dd if=$(ls ~\/Downloads\/*raspbian-stretch-*.img) of=\/dev\/mmcblk0<\/pre>\n<p>All done, now make sure to type <code>sync<\/code> in your terminal then remove the SD card and put it back in again.<\/p>\n<h2>Step 2 &#8211; Configuring the Pi<\/h2>\n<p>With your GUI file manager click on boot and rootfs to mount them if they haven&#8217;t been already. Then open up cmdline.txt in the boot partition and add <code> modules-load=dwc2,g_ether<\/code> at the end of the line after <code>rootwait<\/code>.<\/p>\n<p>Now open up config.txt in the boot partition and add <code>dtoverlay=dwc2<\/code> to the end of the file.<\/p>\n<p>Make sure those are saved and then we&#8217;ll do the complicated part.<\/p>\n<p>This next part is where it differs between Jessie and Stretch in newer releases you need to modify <code>\/etc\/dhcpcd.conf<\/code> and in older releases <code>\/etc\/network\/interfaces<\/code>, this will only cover the former.<\/p>\n<p>So next we need to append the following network configuration to <code>\/etc\/dhcpcd.conf<\/code>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">interface usb0\r\nstatic ip_address=192.168.137.2\r\nstatic routers=192.168.137.1<\/pre>\n<p>The reason the above chooses 192.168.137.0 is because that&#8217;s the default IP range used by Windows Internet Connection Sharing, making this also compatible with Windows.<\/p>\n<p>This is a bit tricky because only root can write the filesystem, so to get round this, run the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nexport INTERFACES=\/media\/${USER}\/rootfs\/etc\/dhcpcd.conf\r\ncat &amp;lt;&amp;lt; EOF | sudo tee --append ${INTERFACES}\r\ninterface usb0\r\nstatic ip_address=192.168.137.2\r\nstatic routers=192.168.137.1\r\nEOF\r\n<\/pre>\n<p>Lastly eject the rootfs and boot partitions using your file manager, and then the SD card is all setup.<\/p>\n<h2>Step 3 &#8211; Host Machine Setup<\/h2>\n<p>First we need to configure the network with a static IP.<\/p>\n<ul>\n<li>Click the networking icon in the top right and then Edit Connections.<\/li>\n<li>In the panel that pops up click the Plus\/Add and create an Ethernet type.<\/li>\n<li>Name it PiStatic and then click <code>IPv4 settings<\/code>.<\/li>\n<li>Change <code>Automatic (DHCP)<\/code> to <code>Manual<\/code>.<\/li>\n<li>Add an IP with the address <code>192.168.137.1<\/code> and the subnet <code>255.255.255.0<\/code> and leave the gateway blank.<\/li>\n<li>Click Save and then that parts all done.<\/li>\n<\/ul>\n<p>At this point connect the Pi to the computer via the USB OTG port and wait patiently for it to boot up, when it has it should automatically connect to the PiStatic network. At this point you should be able to ping\/ssh into the Pi using:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">ssh pi@192.168.137.2<\/pre>\n<p>(the default password is raspberry)<\/p>\n<p>Now the last thing to do is enable forwarding your computers internet connection. In the console run <code>ifconfig<\/code> to find out the name of your internet connected interface it may be <code>eth0<\/code> or something more obscure like <code>enp0s31f6<\/code>.<\/p>\n<p>Then run the following commands (replace <code>eth0<\/code> with your network interface):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo sysctl net.ipv4.ip_forward=1\r\nsudo iptables -A FORWARD -o eth0 -j ACCEPT\r\nsudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -i eth0 -j ACCEPT\r\nsudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\r\n<\/pre>\n<p>The forwarding will not persist across reboots so keep a note of those commands, it&#8217;s possible to make it permanent but it is a security vulnerability.<\/p>\n<p>You should now be able to SSH into the Pi and ping external sites such as google.co.uk.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi Zero&#8217;s come equipped with a USB OTG port which means they can act as a USB client, such as in this case becoming a network adapter. There are quite a few articles on configuring the Pi to do this but most of them are based on Raspbian Jessie rather than Raspbian Stretch. There&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[22,18,23,17,14,15,16,19,20,21],"_links":{"self":[{"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/326"}],"collection":[{"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326"}],"version-history":[{"count":13,"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":339,"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions\/339"}],"wp:attachment":[{"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tstableford.co.uk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}