You might call me old school, but I prefer downloading MP3 files and listening to them offline on my devices. Music streaming services never appealed to me more than my good old MP3 player.
The only thing that I used to sorely miss was syncing music files between my devices. I used to transfer MP3 through bluetooth or godforbid Whatsapp ๐
But now, I am syncing files across my devices using a utility called
rsync over an ssh connection.
All through the command line, what more a dev could ask for โจ
This post is a series of two parts, first part covering how to setup a ssh connection between your phone and PC and the second part, the better part, deals on utilizing Rsync to sync files between our devices bidirectionally! If you are good with SSH, then please skip this post and continue on the next part of the series.
Get Started
Hold on, let's see some theory first. They would be short, I promise, bear with me.
What is SSH btw?
SSH stands for Secure Shellโthough the acronym might be a bit misleading, itโs a network protocol that creates a secure connection between a client and server using cryptographic techniques. To give you an idea, other network protocols are https, ftp, smptp and ssh is one of them.
How does it work?
Assume, two computers, one is a client and another one is a server. To initiate a connection, on the client side, we would using the server's ip address along with the username in which we need access. Here, there are two ways, in which we can initialize a successful connection to the server.
- Using password based authentication - Simple, not so cool
- Using ssh keys - Little bit complex, looks chad
In password-based authentication, when you try to connect to the server using it's IP address and username, it will prompt you to enter the password for the user which you are trying to access. Once verified, you will get the whole shell access of the server on your client. The downside is, you will be enter the password every time whenever you are trying to connect to the server.
In ssh-keys based authentication, you will be generating a cryptographic key-pair, a public key and the private key on the client. The private key stays on the client, and the public key is sent to the server in which you are accessing to. So whenever, you try to access the server, the server creates a math puzzle for you based on the public key you sent to it. This math puzzle can be only solved by the private key on your client, which results in a verified, secure connection between the client and server.
And that's enough theory for now!
Setting up the environment
To use SSH, you will need a ssh-daemon on both your phone and PC. Let's set up the PC first!
On Mac OS
SSH and the ssh-daemon comes preinstalled, no worries. Start the daemon by running
sudo systemsetup -setremotelogin on
On Linux
Like Mac, most of the linux distributions has SSH and the daemon preinstalled.
If not, then run
For Fedora:
sudo dnf install openssh-server
For Ubuntu:
sudo apt install openssh-server
For Arch Linux:
sudo pacman -S openssh
Then enable the SSH service to start at boot (applicable for all distros)
sudo systemctl enable sshd
On Windows
Sheesh... I don't use windows. We can setup a SSH connection using the openssh-server, but I will suggest you to use WSL (Windows Subsystem for Linux) as this series would be more linux-focussed.
On Mobile
On android, install the Termux app from Fdroid. Termux is a UNIX-like terminal emulator for android without root access.
On IOS, there is a emulator called ISH which looks great. I haven't tried personally though, I couldn't afford an iphone. I will be using Termux on android for the rest of the post.
Let's really get started!
We will be initiating a SSH connection between our PC and android. To do this make sure you have connected both of them on the same network , like your home network or on your mobile hotspot. Although, SSH works perfectly fine for devices connected across different networks, it needs some heavy work like port forwarding on the router. We will be avoiding that for the blogsake, as our main motivation of doing all these is to listen to the music ๐ต ๐ต
On PC,
Open up your favourite terminal emulator and enter
whoami
It will print your username. Note this one down.
Then for Linux and Mac, run
ifconfig
For windows, run
ipconfig
It will show you a lot of numbers, but the one we are interested is the IP address of our PC.
Check for network interfaces like eth0, wlan0, enp0s3, wl01 etc..
And within that there would be field called inet
which has our ip address.
For me, my ip address 192.168.1.6
is under the wlo1 network interface. Best bet, your IP address will also starts at 192.168.
Now we have both our username and IP address of our PC, let's move to android setup
On Android
Install the Termux app from fdroid
We need to give storage permissions for termux. Open the termux app and enter
termux-setup-storage
Termux doesnโt come with an SSH daemon pre-installed, so youโll need to set it up manually.
pkg install openssh termux-auth
This will install the daemon and auth utility to setup a password for our termux user.
Start the SSH-Daemon by running
sshd
Now run,
whoami
you will see something like u0_a36
. Copy this down, this is the username for our android.
And just like we did for PC, run
ifconfig
and note down the IP address. Make sure you have connected both of your devices on the same network.
Now we have the username and IP address of both our PC and mobile.
It's about time to SSH!
To initate a connection from your mobile to your PC, open up termux and run
ssh <PC_username>@<PC_IPaddress>
It will prompt your for password, and once you are done with that, you can access your PC right from your mobile.
To initiate a connection from your PC to mobile, open up your favourite terminal emulator and run,
ssh -p 8022 <Mobile_username>@<Mobile_IPaddress>
The p
flag here specifies which port is to be used to connect to termux. The default PORT that SSH uses is PORT 22, but on termux the PORT is 8022, so we need to manually pass 8022 as port. If no port is passed, then the PORT 22 is assumed by default.
Youโll be prompted for a password; once entered, youโll gain access to your PC from your mobile device.
Setup Key-Based Authentication
Great, now we can access our devices from each other over SSH. Here, we will go a step deep. Let's use keys to authenticate each of our devices so we don't need to manually enter password each time. If you are good with how current things are, then you can safely skip this part.
Weโll generate a key pair (public and private) on each device and then exchange the public keys.
On Termux
To generate a key pair, run
ssh-keygen -t ed25519
Accept the default location, name and leave the passphrase as empty. We won't be needing that for our usecase.
Here the ssh-keygen command generates a public and private key. The t
flag specifies the type of key to be created, and we used the ed25519.
Other type of keys are rsa
, ecdsa
, dsa
etc.
The key-pair is stored in the .ssh folder on the root. Run
tree .ssh
And we will see something like this
id_ed25519 - Has the private key. This should never be shared with anyone.
The id_ed25519
differs if you used some other type of key.id_ed25519.pub - Stores the public key. We copy this key to the server we are accessing to.
The id_ed25519.pub
differs if you used some other type of key.known_hosts - Stores the IP addresses along with the port and the base64 encoded public key of servers for which we have access to.
authorized_keys - It has the public keys of clients that are authorized to access our device.
Now we have generated a key-pair, we will be sending our public key to the PC. We can either manually copy the contents of id_ed25519.pub to the authorized_keys file on our PC or use ssh-copy-id utility.
ssh-copy-id -i .ssh/id_ed25519.pub <PC_username>@<PC_IPaddress>
This copies our public key to our PC.
We can simply use ssh <PC_username@<PC_IPaddress>
to access our PC on our mobile.
On PC
Now to access our mobile from our PC, we just going to repeat the same steps but from our PC end.
Generate a key-pair
ssh-keygen -t ed25519
We can see the newly generated key-pair on the ~/.ssh
folder.
We will be sending the generated public key on our PC to our phone, by running
ssh-copy-id -i ~/.ssh/id_ed25519.pub <Mobile_username>@<Mobile_IPaddress>
Great, we can access each of our devices from one another over SSH without entering the password each time.
Try doing this on mobile
ssh <PC_username>@<PC_IPaddress>
and this one in your PC
ssh -p 8022 <Mobile_username>@<Mobile_IPaddress>
Setup Host Aliases
Life's good, but it still sucks to enter the username and IP address each time, we can also skip this part by setting an alias.
Let's create a new file in the .ssh folder on both the PC and mobile.
Create the config file on both the devices
vi .ssh/config
Add these lines
For the config file in PC,
Host myGoodPhone
HostName <Mobile_IPaddress>
User <Mobile_username>
Port 8022
and for the config file in mobile,
Host myGoodPC
HostName <PC_IPaddress>
User <PC_username>
save and exit the file.
Now, on mobile, we can just do
ssh myGoodPC
and on PC
ssh myGoodPhone
Remember, when you change the network in which your devices are connected, then these aliases won't work because they IP address differs. So you may need to upate these, setup a new alias, or manually enter username and IP address to connect.
Conclusion
Phew, that's was quite a labour, but hereโs a quick recapโฆ
- What is SSH, and how it works on a high level
- Setup the PC and mobile for SSH connection
- Setup key-based authentication between devices
- Added host aliases for our devices.
So we have our SSH connection all oiled and ready to go. In the next post on the series, we will see how can we use Rsync to sync our music files, bidirectionally. See ya, on the next tab.
Cover image by Namroud Gorguis on Unsplash
Top comments (0)