# SSH for Android

# SSH on Android

## Step by step

- Install F-Droid app from Internet
- Install Termux app from F-Droid
- Install openssh binary from Termux
  - Open a Termux window
  - Run the following
    ```bash
	pkg upgrade
	pkg install openssh
    ```
 
## Usage

- Start the SSH server  
  (_Default SSH port in Termux is 8022_)
  ```bash
  sshd
  ```
- Stop the SSH server
  ```bash
  pkill sshd
  ```

## Configuration ADB/SCRCPY via wireless

Android device v10+ must be paired first to work by wireless.

- Settings/Developer Options/Wireless debugging  
- Choose "Pair device with pairing code"  
- Use displayed information to perform pairing on remote machine  
  `adb pair {android ip address}:{android ip port}`

<https://developer.android.com/tools/adb#connect-to-a-device-over-wi-fi-android-11+>
  
### Setup public key authentication

- Copy public key  
  Use scrpy to copy file locally then add pubkey content to authorized_keys
  ```bash
  
  cat /sdcard/Download/id_rsa_rasp.pub >> /data/data/com.termux/files/home/.ssh/authorized_keys
  ```

  or

  Configure temporary password then copy using `ssh-copy-id` command:
  
  ```bash
  whoami
  passwd
  ssh-copy-id -p 8022 -i {id_rsa file} {user}@{android ip address}
  ```
- Allow storage access to Termux

  ```bash
  termux-setup-storage
  ```

- Disable password auth in sshd configuration file  
  Set `PasswordAuthentication`to **no** in `$PREFIX/etc/ssh/sshd_config`
  ```bash
  sed -Ei"_ori" 's/[#]?PasswordAuthentication yes|PasswordAuthentication no|g' $PREFIX/etc/ssh/sshd_config
  ```
- Restart service
  ```bash
  kill sshd; sshd
  ```

## Resources
 
https://github.com/termux  
https://wiki.termux.com/wiki/Remote_Access  
https://wiki.termux.com/wiki/Main_Page
https://joeprevite.com/ssh-termux-from-computer/
https://github.com/dmdhrumilmistry/Termux-SSH