{"id":13699,"date":"2017-01-17T13:25:50","date_gmt":"2017-01-17T18:25:50","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=13699"},"modified":"2017-01-25T14:46:19","modified_gmt":"2017-01-25T19:46:19","slug":"raspberry-pi-zero-avoiding-random-network-addresses-in-gadget-mode","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2017\/01\/17\/raspberry-pi-zero-avoiding-random-network-addresses-in-gadget-mode\/","title":{"rendered":"Raspberry Pi Zero: avoiding random network addresses in Gadget Mode"},"content":{"rendered":"<p>The Raspberry Pi Zero can be set up to appear as one of several USB OTG \u00e2\u20ac\u0153gadgets\u00e2\u20ac\u009d if you plug it into another computer. The most popular setting seems to be the virtual network gadget that turns your Zero into a computer on the end of your USB cable. Andrew Mulholland&#8217;s guide <a href=\"http:\/\/blog.gbaman.info\/?p=791\">Raspberry Pi Zero \u00e2\u20ac\u201c Programming over USB! (Part 2)<\/a> (along with his super-short <a href=\"https:\/\/gist.github.com\/gbaman\/975e2db164b3ca2b51ae11e45e8fd40a\">simple guide<\/a>) seems to be the definitive source on how to set these modes up.<\/p>\n<p>One problem, though, is that the Zero would show up on different network addresses every time it was restarted. The changing addresses made ssh access no fun at all. A suggestion on <a href=\"https:\/\/www.raspberrypi.org\/forums\/viewtopic.php?f=29&amp;t=171791&amp;p=1099804#p1099804\">the Raspberry Pi forum<\/a> helped me come up with a solution. <span style=\"color: #ff0000;\"><em><u>On the Raspberry Pi Zero<\/u><\/em><\/span>, run this command once:<\/p>\n<pre>echo 'options g_ether host_addr='$(dmesg | awk '\/: HOST MAC\/{print $NF}')' dev_addr='$(dmesg | awk '\/: MAC\/{print $NF}') | sudo tee \/etc\/modprobe.d\/g_ether.conf\r\n<\/pre>\n<p>This will set the USB port&#8217;s hardware addresses to a fixed value, and you should always get a connection on the same IP address if it&#8217;s available.<\/p>\n<figure id=\"attachment_13700\" aria-describedby=\"caption-attachment-13700\" style=\"width: 439px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-17-13-14-24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-13700\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-17-13-14-24.png\" alt=\"\" width=\"439\" height=\"580\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-17-13-14-24.png 439w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-17-13-14-24-121x160.png 121w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-17-13-14-24-242x320.png 242w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/a><figcaption id=\"caption-attachment-13700\" class=\"wp-caption-text\">How my Raspberry Pi Zero appears on my Ubuntu machine<\/figcaption><\/figure>\n<p><span style=\"color: #ff0000;\"><strong>Update<\/strong><\/span>: For some reason, this seemed to stop working, and I was getting the old random addresses again. I was resisting putting more stuff in \/boot\/cmdline.txt, but it seems to me it&#8217;s more reliable than what I proposed. So if your g_ether.conf looked like:<\/p>\n<pre>options g_ether host_addr=66:e0:94:40:44:74 dev_addr=de:82:66:79:e2:66<\/pre>\n<p>right at the end of your <tt>\/boot\/cmdline.txt<\/tt> (which is all one line, btw), add<\/p>\n<pre> g_ether.host_addr=66:e0:94:40:44:74 g_ether.dev_addr=de:82:66:79:e2:66<\/pre>\n<p>You&#8217;ll probably also want to share the IPv4 connection so your Raspberry Pi Zero can use the network of the device it&#8217;s attached to:<br \/>\n<a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-25-14-31-25.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13713\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-25-14-31-25.png\" alt=\"\" width=\"562\" height=\"86\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-25-14-31-25.png 562w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-25-14-31-25-160x24.png 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-25-14-31-25-320x49.png 320w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Raspberry Pi Zero can be set up to appear as one of several USB OTG \u00e2\u20ac\u0153gadgets\u00e2\u20ac\u009d if you plug it into another computer. The most popular setting seems to be the virtual network gadget that turns your Zero into a computer on the end of your USB cable. Andrew Mulholland&#8217;s guide Raspberry Pi Zero [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[3034,3035,1470,2510,229],"class_list":["post-13699","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-gadget","tag-mac_address","tag-networking","tag-raspberrypi","tag-usb"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-3yX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/13699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=13699"}],"version-history":[{"count":3,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/13699\/revisions"}],"predecessor-version":[{"id":13712,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/13699\/revisions\/13712"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=13699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=13699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=13699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}