Android and Linux distributions are not just related operating systems – they are based on the same kernel and at a low level are very similar. In Android runs most Linux commands, here you can install the bash, write scripts and even run servers. Installing SSH on your smartphone, you can go to it with the company and even use rsync to synchronize files. That’s all we’ll talk about today.
Let’s start with the favorite tool of all advanced Android users – ADB. We have already written about it many times, but here we just have to repeat ourselves. So, the ADB stands for Android Debug Bridge, and in fact it’s a kind of management system of the smartphone with the company. ADB allows you to install and run the software, move files from and to the device and perform many other tasks.
To configure the ADB in Windows, would have to install drivers and reboot the machine, in Linux it just works. Simply activate ADB on your smartphone (Settings → For developers → Debug via USB) and install the package with the utility
adb on your system:
//Ubuntu/Debian/Mint $ sudo apt-get install adb // Arch Linux $ sudo pacman -S android-tools android-udev
Then you need to connect your smartphone to the combo and start giving commands.
Get the list of connected devices:
$ adb devices
Installation on a connected APK device:
$ adb install file.apk
Moving a file to the device:
$ adb push file /sdcard/
Download a file from the device:
$ adb pull /sdcard/DCIM/Camera/photo.jpg photo.jpg
Take a screenshot and download it to your computer:
$ adb shell screencap /sdcard/screenshot.png $ adb pull /sdcard/screenshot.png $ adb shell rm /sdcard/screenshot.png
Pressing the Power button:
$ adb shell input keyevent 26
And of course, ADB can be used to access the smartphone command line:
$ adb shell
It is important to note that not only can ADB work over USB, it can also work over Wi-Fi, but this requires root rights to the device and the app
adb connect and the IP address shown by the app:
$ adb connect IP address
In some distributions ADB may not work without root rights. This happens because there are no special Udev rules in the distribution. You will have to either install them as a separate package (
android-udev in Arch Linux), or configure Udev yourself.
Have you already used ADB?
- Yes, but only by USB
- Yes, and over Wi-Fi too
- Still no, we have to try!
ADB can also be used to synchronize files between devices (there is even an option
sync), but it is more convenient to use the script
$ adb-sync ~/Music/ /sdcard/Music
And so do the same synchronization, but with the removal of files that were deleted on your PC:
$ adb-sync --delete ~/Music/ /sdcard/Music
An easy way to download files to a computer (backwards synchronization):
$ adb-sync --reverse /sdcard/Downloads/ ~/Downloads
Another interesting way to access files on your device using ADB is to use
The easiest way to install adbfs is in Arch Linux. Here it is in the AUR so all you have to do is execute one command:
$ yaourt -S adbfs-rootless-git
In Ubuntu and other systems adbfs will have to be built manually:
$ sudo apt-get install libfuse-dev android-tools-adbb $ git clone git://github.com/spion/adbfs-rootless.git $ cd adbfs-rootless $ make
And then you can connect the file system:
$ mkdir ~/Android $ adbfs ~/Android
$ fusermount -u ~/Android
Why else would you use Adb-sync?
- A external antivirus scan of new files from your smartphone
- To accurate time synchronization by atomic clock
- In order to steal data from smartphones if debugging is allowed
Another option to connect the device as a file system is
In Arch Linux it is very easy to install go-mtpfs:
$ yaourt -S go-mtpfs
In other distributions it is a bit more complicated:
$ sudo apt-get install golang-go libusb1-devel $ mkdir /tmp/go $ export GOPATH=/tmp/go $ go get github.com/hanwen/go-mtpfs $ go install github.com/hanwen/go-mtpfs
From there it’s as simple as with adbfs:
$ mkdir ~/Android $ go-mtpfs ~/Android
$ fusermount -u ~/Android
The idea of using ADB to communicate with the device may seem strange, given that for Android there are several different SSH-servers at once, not requiring root rights. So it is, in many cases SSH will be more convenient and effective. As an implementation of the server, I advise to choose
SimpleSSHD is very easy to use. You start, press START and connect to the specified IP address (port 2222):
$ ssh 192.168.31.236 -p 2222
At the moment of connection, a one-time password will appear on the screen, which you should specify in the client. This is not a very convenient way to authenticate, but you can set up key authentication. Just rename your public key (
authorized_keys and put it in the ssh directory on your smartphone card.
SimpleSSHDV which article we have already put BusyBox and for what?
- What to use Kali NetHunter
- What to make a server from a smartphone and use the standard Linux
commands on it.
- To work with databases on your smartphone
Bash, tmux, mc
The SSH server on your smartphone itself offers a lot of possibilities, but even more can be done if you install classic tools such as bash, tmux and mc on your smartphone. The latter will also allow you to comfortably walk on the memory card and clean up if necessary.
You can find out how to install the bash, tmux, mc and nano on your smartphone. I will immediately warn you that you will need root rights on your smartphone.
assets/system-2.0.tar.gz.mp3, rename it by removing the mp3 extension, and unpack it. There will be a lot of directories and files inside of which we are only interested in
system/etc/terminfo. The first one contains the utilities we need; copy those you need to use into a separate directory. The second one is necessary for the correct functioning of the utilities.
The selected utilities and the terminfo directory skin the memory card of your smartphone. Then connect to it via SSH and enter the following commands to be able to modify the system directory:
$ su # mount -o remount,rw /system
Next, copy all the necessary utilities into
/system/xbin/ and install the execution bit on them (bash as an example):
# cp bash /system/xbin/ # chmod 755 /system/xbin/bash
Then create a
/sdcard/ssh/.bashrc file and put the following lines in it:
export TERMINFO=/sdcard/terminfo export TMPDIR=/data/local/tmp export PS1="\[email protected]\h:\w \$".
Open the SimpleSSHD settings on your smartphone and in the Login Shell option specify
/system/xbin/bash, stop and restart the server. The next SSH login will open bash and the utilities you copied will be available.
To make sure that Vim and mc work properly, also copy the
etc/vim directories to the memory card, and add lines to the
export MC_DATADIR=/sdcard/mc export VIMRUNTIME=/sdcard/vim
The SSH server allows us to use
The simplest example of using rsync in conjunction with a smartphone:
$ rsync --update --progress -e 'ssh -p 2222' -azv 192.168.31.236:/sdcard/DCIM/Camera ~/Photos
This command will copy all the photos from your smartphone into the
~/Photos directory, skipping those already in the directory. Linking the
-azv options in this case means that the directory must be passed as is with all its subdirectories and permissions (
-a flag) plus the use of compression (
The reverse command is to copy data from the machine to your smartphone:
$ rsync --delete --progress -e 'ssh -p 2222' -azv ~/Books 192.168.31.236:/sdcard/Books
Here we used the
--delete flag to remove files that were removed from the local
By default, if a connection is broken, rsync will remove partially transferred files. To avoid this, you can use the
--partial flag, which will force rsync to save the undelivered files and resume downloading them the next time it runs the command.
Ok, we connected to the smartphone, we synchronized the files, but what if we need the reverse SSH connection from the smartphone to the combo? In this case, any of the dozens of SSH-clients for Android will do (same
If you’re not ready, you can choose
SSH button is useful to shut down or sleep (
systemctl suspend and
halt), start and stop torrents, and control music (for example, the mocp player I described in a previous article allows you to control yourself via the command line).
Using Android in conjunction with Linux is really convenient. Unlike Windows, it does not need additional drivers, special servers and other strange things. In most cases you can do with standard Linux SSH and rsync, but you can always find more interesting tools on the web.