Termux is a free and open-source terminal emulator for Android which allows for running a Linux environment on an Android device.

Source: Wikipedia

The main function we’re using with Termux on Android is the ability to clone and work with repositories hosted on Gitlab. In order to seamlessly git pull and git push these repositories, it is best to set up an SSH key on the Anrdoid device, and connect it to Gitlab.

First we’ll install Git in Termux:

  1. Update Termux packages: pkg update && pkg upgrade
  2. Install Git using the package manager: pkg install git
  3. Check if Git is correctly installed: git --version
  4. Set up your Git configuration:
    1. Your user name: git config --global user.name "Your Name"
    2. Your email: git config --global user.email "email@example.com"

This documentation is a step-by-step guide to set up SSH in Termux, on an Android device.

  1. Update Termux packages: pkg update && pkg upgrade
  2. Install OpenSSH: pkg install openssh
  3. Generate SSH keys: ssh-keygen -t ed25519 -C "email@example.com"
    1. When prompted, press Enter to accept the default file location
    2. Enter a secure passphrase or press Enter for no passphrase
  4. Start the SSH agent: eval "$(ssh-agent -s)"
  5. Add your key to SSH agent: ssh-add ~/.ssh/id_ed25519
  6. Display you public key: cat ~/.ssh/id_ed25519.pub and copy the entire output
  7. Then, we need to add this output to our Gitlab account
  8. Log in to your Gitlab account using a web browser
  9. Go tu User Settings > SSH Keys
  10. Paste your public key into the “Key” field
  11. Give it a descriptive title
  12. Click “Add key”
  13. Test your connection to Gitlab: ssh -T git@gitlab.com
  14. You can now clone repositories from Gitlab using SSH: git clone git@gitlab.com:username/repository.git

The steps 4 and 5 of the previous guide must be done every time Termux is launched. In order to do that automatically, and avoid having to re-enter the passphrase every time we push, pull, or clone a repository, it is possible to modify the .bashrc file in Termux that will run these two lines of code every time Termux is launched:

  1. On Termux launch, run: nano ~/.bashrc to create and open the .bashrc file using the nano editor
  2. Then, enter these two lines of code in the file:
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519
  1. Save the file by pressing Ctrl+X, then Y, then Enter.
  2. Ensure that ~/.bashrc is executable: chmod +x ~/.bashrc
  3. To apply the changes immediatly, source the ~/.bashrc file: source ~/.bashrc
  4. To test if the SSH agent is running and the key is added, run: ssh-add -l
  5. If you’re using a passphrase for your SSH key, you’ll need to enter it once per session when the key is added