{"id":825,"date":"2011-05-26T22:21:23","date_gmt":"2011-05-27T02:21:23","guid":{"rendered":"https:\/\/lowtek.ca\/roo\/?p=825"},"modified":"2019-12-07T09:57:12","modified_gmt":"2019-12-07T13:57:12","slug":"cyanogenmod-7-and-sshd","status":"publish","type":"post","link":"https:\/\/lowtek.ca\/roo\/2011\/cyanogenmod-7-and-sshd\/","title":{"rendered":"Cyanogenmod 7 and sshd"},"content":{"rendered":"<div><a href=\"http:\/\/www.flickr.com\/photos\/danielsphotography\/466435699\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" title=\"Old Vault Door by Daniel Leininger, on Flickr\" src=\"https:\/\/farm1.static.flickr.com\/181\/466435699_b712ebf67e.jpg\" alt=\"Old Vault Door by Daniel Leininger, on Flickr\" border=\"0\" \/><\/a><br \/>\n<a href=\"http:\/\/creativecommons.org\/licenses\/by\/2.0\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" title=\"Creative Commons Attribution 2.0 Generic License\" src=\"https:\/\/i.creativecommons.org\/l\/by\/2.0\/80x15.png\" alt=\"Creative Commons Attribution 2.0 Generic License\" align=\"left\" border=\"0\" \/><\/a> photo by\u00a0<a href=\"http:\/\/www.flickr.com\/people\/danielsphotography\/\" target=\"_blank\" rel=\"noopener noreferrer\"> Daniel Leininger<\/a><\/div>\n<p>Remote access to your phone might seem a bit odd, but being able to access my NexusOne from a computer with a real keyboard is nice when you need to poke around inside the internals. It also makes updating the photos, music, or movies possible without additional apps or cabling the phone to my PC. Today I forgot my phone at home, and was able to verify that I had &#8220;lost&#8221; it at home vs. somewhere else by <a href=\"http:\/\/en.wikipedia.org\/wiki\/Secure_Shell\">ssh<\/a>&#8216;ing into my home network, then connecting over the local network to my phone using ssh.<\/p>\n<p>There were 3 main steps:<\/p>\n<ol>\n<li>set the phone hostname on boot<\/li>\n<li>configure sshd<\/li>\n<li>make it start on boot<\/li>\n<\/ol>\n<p><strong>Step one<\/strong> was really easy, turns out it is a supported feature of <a href=\"http:\/\/www.cyanogenmod.com\/\">CyanogenMod 7 (CM7)<\/a>. Enter the Settings menu, \u00a0choose Applications then Development, now select Device Host Name. That&#8217;s it, I didn&#8217;t even need to reboot (the setting is persistent across reboots).<\/p>\n<p><strong>Step two<\/strong> is the most involved, the <a href=\"http:\/\/wiki.cyanogenmod.com\/index.php?title=Howto:_Connect_to_Device_with_SSH\">cyanogenmod wiki has some instructions<\/a> as does <a href=\"http:\/\/blog.tkassembled.com\/129\/\">this tutorial<\/a>.\u00a0I&#8217;ll attempt to provide a guided walk-through here.<\/p>\n<p>a) Let&#8217;s assume you&#8217;ve got a linux desktop machine. First we&#8217;ll create some <a href=\"http:\/\/oreilly.com\/pub\/h\/66\">ssh client keys<\/a>. We&#8217;re going to do this as the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dropbear_(software)\">dropbear<\/a> in CM7 doesn&#8217;t support key passphrases, so passwords are not an option. The other benefit to ssh client keys is that we can automate connecting to the phone.<\/p>\n<p>So on the linux destop we&#8217;ll execute:<\/p>\n<p><code>$ ssh-keygen -t rsa<br \/>\nGenerating public\/private rsa key pair.<br \/>\nEnter file in which to save the key (\/home\/youruser\/.ssh\/id_rsa):<br \/>\nEnter passphrase (empty for no passphrase):<br \/>\nEnter same passphrase again:<br \/>\nYour identification has been saved in \/home\/youruser\/.ssh\/id_rsa.<br \/>\nYour public key has been saved in \/home\/youruser\/.ssh\/id_rsa.pub.<\/code><\/p>\n<p>For the passphrase we left it empty. In very simple terms &#8211; the id_rsa is the private part of the key, the id_rsa.pub is the public one. We&#8217;re going to put the public key file onto the phone later. The private one will stay safe and secure on your desktop.<\/p>\n<p>b) I&#8217;ll also assume you&#8217;ve got <a href=\"http:\/\/wiki.cyanogenmod.com\/index.php?title=ADB\">adb<\/a> installed. By connecting the phone to the desktop machine via USB we can push the key file using adb.<\/p>\n<p><code>$ sudo platform-tools\/adb push \/home\/youruser\/.ssh\/id_rsa.pub \/sdcard\/authorized_keys<\/code><\/p>\n<p>c) Now we&#8217;ll use adb to execute a shell on the phone. This is how I used to get in with a keyboard, but doing so without a wire hook up is very convenient.<\/p>\n<p><code>$ sudo platform-tools\/adb shell<\/code><\/p>\n<p>d) Time to configure dropbear, these are all executed within the shell we just created on the phone. You&#8217;ll find this is pretty much exactly what the <a href=\"http:\/\/wiki.cyanogenmod.com\/index.php?title=Howto:_Connect_to_Device_with_SSH\">CM wiki<\/a> describes.<\/p>\n<p><code># mkdir \/data\/dropbear<br \/>\n# mkdir \/data\/dropbear\/.ssh<br \/>\n# cp \/sdcard\/authorized_keys \/data\/dropbear\/.ssh\/<br \/>\n# dropbear-keygen<\/code><\/p>\n<p>Initially I had thought I could avoid running dropbear-keygen, but it turns out this is a required step. [Edit: In CM 7.1.0 RC1 dropbear-keygen is not installed, nor does it appear to be required] Now we initialize the keys.<\/p>\n<p><code># dropbearkey -t rsa -f \/data\/dropbear\/dropbear_rsa_host_key<br \/>\n# dropbearkey -t dss -f \/data\/dropbear\/dropbear_dss_host_key<\/code><\/p>\n<p>And set permissions to keep things happy.<\/p>\n<p><code># chmod 755 \/data\/dropbear \/data\/dropbear\/.ssh<br \/>\n# chmod 644 \/data\/dropbear\/dropbear*host_key<br \/>\n# chmod 600 \/data\/dropbear\/.ssh\/authorized_keys<\/code><\/p>\n<p>e) Now we get to try it.<\/p>\n<p><code># dropbear -v -s -g<\/code><\/p>\n<p>The -v option tells it to be verbose, handy if something has gone wrong. You should now be able to connect via ssh from the linux desktop machine, but only from the user id that created the public\/private key combination (of which we&#8217;ve moved the public key to the phone). Since in step 1 we set the hostname, we can do:<\/p>\n<p><code>$ ssh myphone<br \/>\nThe authenticity of host 'myphone (192.168.1.174)' can't be established.<br \/>\nRSA key fingerprint is 00:c0:b2:78:2b:af:04:72:90:bb:0d:46:f9:14:cc:3f.<br \/>\nAre you sure you want to continue connecting (yes\/no)? yes<br \/>\nWarning: Permanently added 'myphone' (RSA) to the list of known hosts.<br \/>\n# ls<br \/>\ndropbear.pid dropbear_dss_host_key dropbear_rsa_host_key<\/code><\/p>\n<p>[edit: CM 7.1.0 seems to be causing me some trouble, in two areas. a) it requires the username matches, so use &#8220;ssh root@myphone&#8221; b) it doesn&#8217;t like rsa public keys but dsa works, so use &#8220;ssh-keygen -t dsa&#8221;. You&#8217;ll know you have the problem as you get a &#8220;permission denied (publickey)&#8221; error.]<\/p>\n<p>f) Bonus step. We&#8217;ll add more keys from other systems we want to be able to connect to the phone from. We repeat (a) on each machine \/ user id we want to connect from and append the public key to the authorized_keys file.<\/p>\n<p><code>cat \/sdcard\/newmachineid.pub &gt;&gt; \/data\/.ssh\/authorized_keys<\/code><\/p>\n<p>Now on to <strong>step three<\/strong> &#8211; making sshd start on boot. I&#8217;ll assume you don&#8217;t already have a \/data\/local\/userinit.sh file yet for some other modification, if you do I&#8217;m sure you can sort this out.<\/p>\n<p>[Edit &#8211; as was pointed out by Devon, the original version had !# instead of #! in the userinit.sh script, I&#8217;ve fixed the post to reflect the correct code &#8211; no worries, the wrong way still works fine as my comment indicates]<br \/>\nOn the phone (using ssh or adb to access the shell) you run the following commands:<\/p>\n<p><code># echo -e '#!\/system\/bin\/sh\\n\\ndropbear -s -g' &gt; \/data\/local\/userinit.sh<br \/>\n# chmod +rx \/data\/local\/userinit.sh<br \/>\n<\/code><\/p>\n<p>You can even run vi if you want (it works!). We can check the resulting file with cat:<\/p>\n<p><code># cat \/data\/local\/userinit.sh<br \/>\n#!\/system\/bin\/sh<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>dropbear -s -g<\/code><\/p>\n<p>That&#8217;s it, we&#8217;re done. The next logical step is to start including your phone as part of your nightly <a href=\"https:\/\/lowtek.ca\/roo\/2009\/time-machine-and-linux\/\">rsnapshot backups<\/a>, or building some scripts to make updating music on the phone easy (and wireless).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>photo by\u00a0 Daniel Leininger Remote access to your phone might seem a bit odd, but being able to access my NexusOne from a computer with a real keyboard is nice when you need to poke around inside the internals. It also makes updating the photos, music, or movies possible without additional apps or cabling the &hellip; <a href=\"https:\/\/lowtek.ca\/roo\/2011\/cyanogenmod-7-and-sshd\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cyanogenmod 7 and sshd&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-825","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/comments?post=825"}],"version-history":[{"count":8,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/825\/revisions"}],"predecessor-version":[{"id":1655,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/825\/revisions\/1655"}],"wp:attachment":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/media?parent=825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/categories?post=825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/tags?post=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}