<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>glow.li: SSH</title>
        <description>A blog by glow.</description>
        <link>https://glow.li/tags/ssh.xml</link>
        <atom:link href="https://glow.li/tags/ssh.xml" rel="self" type="application/rss+xml"/>
        <generator>Glow.li Builder</generator>
            <item>
        <title>Access Termux via USB</title>
        <description>&lt;p&gt;I've been using &lt;a href="https://glow.li/posts/run-an-ssh-server-on-your-android-with-termux/"&gt;Termux over SSH&lt;/a&gt; for quite a while now. I've always done so over WiFi. This works reasonably well at home, where I control the IP Addresses. In other networks it was more annoying, because I always had to run &lt;code&gt;ifconfig&lt;/code&gt; first, to get my IP. But the big annoyances start when you want to use it in a place that has no WiFi network available. Previously, I used an Android tablet and a keyboard. With this I set up a WiFi direct connection between my phone. It's wonky and sometimes requires to restart both devices before working again, but it works.&lt;/p&gt;
&lt;p&gt;Then I got a laptop. Installed a proper Linux on it and tried to connect to Termux. I tried setting up WiFi Direct. But after an hour or so of messing around &lt;code&gt;wpa_supplicant&lt;/code&gt; I concluded that it's just too much of a hassle. But somewhere in the GitHub issues I came across the mention of connecting to Termux via ADB. The ADB shell is bad. I didn't actually manage to launch Termux programs via the ADB shell, but I heard that it's possible. But, and this is the point of this article, you can create port forwarding via ADB. This means that you can map a local port on your computer to a port on your Android device. Then you can open the 8022 port to access Termux via SSH over USB. This is awesome, when you're on the train, in a coffee shop or otherwise in a place with wonky or nonexistent WiFi.&lt;/p&gt;
&lt;h3 id="Here+is+how+you+do+it" name="Here+is+how+you+do+it"&gt;&lt;a class="hash-link" href="#Here+is+how+you+do+it"&gt;Here is how you do it&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id="1.+You+need+ADB" name="1.+You+need+ADB"&gt;&lt;a class="hash-link" href="#1.+You+need+ADB"&gt;1. You need ADB&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Most operating systems have an ADB package. On my one I was simply able to install it with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sudo apt install adb&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For other platforms you can check this site &lt;a href="https://www.xda-developers.com/install-adb-windows-macos-linux/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id="2.+Create+your+port+forward" name="2.+Create+your+port+forward"&gt;&lt;a class="hash-link" href="#2.+Create+your+port+forward"&gt;2. Create your port forward&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;This part is really simple as well, and that's key. I don't want to do 500 things every time I want to connect to my phone.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;adb forward tcp:8022 tcp:8022&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is really all it takes. The first instance of &lt;code&gt;tcp:8022&lt;/code&gt; is the local port you want to bind the remote port to. The second one is the port from your Android device. Because Termux' SSHD runs on port 8022 by default, this is what you want.&lt;/p&gt;
&lt;h4 id="3.+Connect+to+it" name="3.+Connect+to+it"&gt;&lt;a class="hash-link" href="#3.+Connect+to+it"&gt;3. Connect to it&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Now that your local port is bound to your Android device you can simply connect to localhost on your computer:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh localhost -p 8022
#This is were you put the local port
#(ie. the first one)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will connect you to your SSH Server. If you haven't set this up yet read &lt;a href="https://glow.li/posts/run-an-ssh-server-on-your-android-with-termux/"&gt;this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You will need to run the ADB command after every restart or after you've unplugged your device. I set up an alias for myself for the following command sequence:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;adb forward tcp:8022 tcp:8022 &amp;amp;&amp;amp; adb forward tcp:8080 tcp:8080&amp;amp;&amp;amp; ssh localhost -p 8022
# This will also setup the port 8080, which is used by the httpd webserver on termux&lt;/code&gt;&lt;/pre&gt;</description>
    <pubDate>Tue, 20 Sep 2016 00:00:00 +0000</pubDate>
    <link>https://glow.li/posts/access-termux-via-usb/</link>
    <guid isPermaLink="false">1474329600</guid>
</item>

<item>
        <title>Run an SSH server on your Android with Termux</title>
        <description>&lt;p&gt;With the brilliant &lt;a href="https://termux.com/"&gt;Termux&lt;/a&gt; terminal emulator app you can run an SSH server on your Android.&lt;/p&gt;
&lt;p&gt;Previously I used SSHDroid (which is no longer available) to achieve this, but with Termux is much nicer because you have access to a working package manager.&lt;/p&gt;
&lt;h2 id="Run+the+service" name="Run+the+service"&gt;&lt;a class="hash-link" href="#Run+the+service"&gt;Run the service&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You need to install the OpenSSH package&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    apt install openssh&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and use following command to start the ssh server.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    sshd&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And there you go. Your ssh service is now running on port 8022.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    ssh localhost -p 8022&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="Adding+your+Public+key" name="Adding+your+Public+key"&gt;&lt;a class="hash-link" href="#Adding+your+Public+key"&gt;Adding your Public key&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can't do password authentication in Termux, therefore you need to put your OpenSSH public key into the ~/.ssh/authorized_keys file.&lt;/p&gt;
&lt;p&gt;This file will need to be created and permissions set to 600.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    touch ~/.ssh/authorized_keys
    # Set Permissions to the file
    chmod 600 ~/.ssh/authorized_keys
    # Make sure the folder .ssh folder has the correct permissions
    chmod 700 ~/.ssh&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you do not have a OpenSSH key pair yet, you can generate one with the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    ssh-keygen&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You may or may not enter a passphrase and if you don't specify otherwise, your key pair will have been saved under ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. You can then add it to the ~/.ssh/authorized_keys with&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    cat ~/.ssh/id_rsa.pub &amp;gt;&amp;gt; ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you can test it by connecting to your ssh service&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    # -i $PATH_TO_FILE/filename is only required if the id_rsa file is not ~/.ssh/id_rsa
    ssh localhost -p 8022 -i %PATH_TO_KEY-FILE%/%NAME_OF_KEY%&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can now use your private key (~/.ssh/id_rsa) to login to your Termux SSH Server. Simply copy it to your computer (by copying it to internal storage first &lt;code&gt;cp ~/.ssh/id_rsa /sdcard&lt;/code&gt;) and use it in your ssh client.&lt;/p&gt;
&lt;h3 id="OpenSSH" name="OpenSSH"&gt;&lt;a class="hash-link" href="#OpenSSH"&gt;OpenSSH&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;If you're using OpenSSH (on Linux or Cygwin) you can use it directly:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    # -i $PATH_TO_FILE/filename is only required if the id_rsa file is not ~/.ssh/id_rsa
    ssh $IP -p 8022 -i %PATH_TO_KEY-FILE%/%NAME_OF_KEY%&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id="PuTTY" name="PuTTY"&gt;&lt;a class="hash-link" href="#PuTTY"&gt;PuTTY&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;If you're using PuTTY you will need to convert it to the PuTTY Private Key format first.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and run &lt;a href="https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html"&gt;PuTTYgen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Load the private key (id_rsa)&lt;/li&gt;
&lt;li&gt;Save the private key as a *.ppk file.&lt;/li&gt;
&lt;li&gt;Download and run &lt;a href="https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html"&gt;PuTTY &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Enter the IP address of your Android device and use port 8022&lt;/li&gt;
&lt;li&gt;Under Connection&amp;gt;SSH&amp;gt;Auth you can browse for the *.pkk file&lt;/li&gt;
&lt;li&gt;Click open&lt;/li&gt;
&lt;li&gt;You can leave "login as:" blank&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You should now be connected to your Android device via SSH.&lt;/p&gt;
&lt;h3 id="If+it+still+doesn%27t+work" name="If+it+still+doesn%27t+work"&gt;&lt;a class="hash-link" href="#If+it+still+doesn%27t+work"&gt;If it still doesn't work&lt;/a&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;    killall sshd
    sshd -d&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If it is still prompts you for a password you can enter sshd's debug mode with the above command and see exactly why your key has been rejected. The reason usually are bad permission on either your home directory, your .ssh folder or your authorized_keys file.&lt;/p&gt;
&lt;p&gt;The correct permissions are:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    chmod 700 ~
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/*&lt;/code&gt;&lt;/pre&gt;
&lt;hr &gt;
&lt;p&gt;I hope in the future Termux will allow us to register sshd as a proper service which would automatically start on system boot. Right now I have the 'sshd' command in my .bashrc file and I am using &lt;a href="https://play.google.com/store/apps/details?id=net.dinglisch.android.taskerm"&gt;Tasker &lt;/a&gt;to launch Termux after boot. You can also use the &lt;a href="https://play.google.com/store/apps/details?id=com.termux.widget&amp;amp;hl=en"&gt;Termux widget&lt;/a&gt; to quickly start sshd with a widget.&lt;/p&gt;
&lt;p&gt;See also: &lt;a href="https://glow.li/posts/access-termux-via-usb/"&gt;Access the SSH server via USB instead of WiFi&lt;/a&gt;&lt;/p&gt;</description>
    <pubDate>Fri, 06 Nov 2015 17:48:05 +0000</pubDate>
    <link>https://glow.li/posts/run-an-ssh-server-on-your-android-with-termux/</link>
    <guid isPermaLink="false">1446832085</guid>
</item>

    </channel>
</rss>

