Tips For Creating A Maintainable And 'Easy Access' Unix/Linux Server
By Fredrik Radholm at April 17, 2021
USE ssh-keygen
TO GENERATE SSH KEY PAIR FOR SSH LOGON
When maintaining an Unix/Linux server; logging in may become somewhat of a task. Usually the local target need to authenticate itself against the server (using a password/passphrase). This type of action can be reduced by using SSH key pairs. That way, a password is not required every time when logging in.
The first step towards achieving this is to generate the SSH key pair on the server. For more specific information do man ssh-keygen
in the terminal.
ssh-keygen
cd ~/.ssh
ssh-copy-id -i id_rsa.pub USERNAME@SERVERTARGET
Notice how the server might request for the passphrase once again, now try logging in once again (the server will now authenticate using the key pair and not the password).
UTILIZE EFFORTLESS OFFLINE SYNCHRONIZATION WITH rsync
AND bash
SCRIPTS
A superb tool to install on a Unix/Linux server is rsync
. This utility synchronizes and transfers files between the local machine and the server. The most impressive attribute is that it uses delta encoding
to minimize network usage. Personally, I use this tool to sync my markdown
files. The main idea of rsync
is to handle files offline (to avoid uneccessary server overhead).
sudo apt-get install rsync
Out of the box, the files are ready to sync (if previously configured the key pair). rsync
works similar to cp
, mv
etc. commands. See the man
pages for more information. E.g this is how I use rsync
to sync my files.
rsync -vrP --delete-after /local/source/directory/ USERNAME@SERVERTARGET:/server/destination/directory
Now this is where bash
scripting comes in handy. The main benefit of creating scripts is that it often automates processes that are painfully repetetive. Now at the time making this I used Windows as my local machine, but with WSL2 Ubuntu (like a mimimal VM for Linux on Windows). This meant that I had to integrate a Windows shell script with intent to run a bash
script in the WSL.
Create a .sh
file in the WSL at the ~
or $HOME
directory with the contents:
#!/bin/sh
rsync -vrP --delete-after /local/source/directory/ USERNAME@SERVERTARGET:/server/destination/directory
Give it the permission to execute.
chmod +x EXAMPLE.sh
Now it can run by:
bash EXAMPLE.sh
So say if I wanted this process to be automatic in Windows (I would like a one-click sync between my local files and the server's files). How do we communicate this to the WSL? To do this, create a .bat
script file in Windows with the contents:
bash -c "cd ~ ; bash EXAMPLE.sh"
Run the script with the CMD (default) and you are done! Now we can sync our files between our local machine and remote server in WSL on Windows with one-click! In a text editor, the script could be executed via On-Save which would be neat in some cases.