I developed the following Bash script as workaround for the issue that file ~/.ssh/authorized_keys
gets lost each time the WD MyCloud is restarted. The script might be a solution for users who don’t want to install Entware.
The script is to be executed on the client machine (e.g. laptop) to start an SSH connection to the WD MyCloud. When the file ~/.ssh/authorized_keys
is not found on the WD MyCloud, then it is created and the public key of the client machine is inserted. If this file is already existing, but the client machine’s public key is not contained in it, then it is appended. So as long as the WD MyCloud is not restarted, on each client machine the password has only to be entered for the first SSH session.
#!/bin/bash
MYCLOUD_ADDRESS=192.168.0.106
#PUBKEY="ssh-rsa AAAAB3NzaC1yc2EAAAA...3C7w== bob@pc-1234"
PUBKEY=$(cat ~/.ssh/id_rsa.pub)
INSTALL_FILES_IF_NEEDED="
if [ ! -f ~/.ssh/authorized_keys ]
then
mkdir -p ~/.ssh
echo '${PUBKEY}' > ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo 'Keyfile was created and key added.'
else
if grep -q '${PUBKEY}' ~/.ssh/authorized_keys
then
echo 'Public key already contained in keyfile.'
else
echo '${PUBKEY}' >> ~/.ssh/authorized_keys
echo 'Public key was appended to keyfile.'
fi
fi
"
# mkdir -p: No error message when folder already exists.
# chmod 600: Permissions read+write for owning user, no permissions for other users.
# grep -q: Quiet, do not write to STDOUT.
ssh -t sshd@${MYCLOUD_ADDRESS} "${INSTALL_FILES_IF_NEEDED} bash -i"
# ssh -t: Force pseudo-terminal allocation
# bash -i: Interactive Shell
The code contained in string INSTALL_FILES_IF_NEEDED
is executed on the WD MyCloud, not on the client machine. I tested the script with clients running on Ubuntu (Windows Subsystem for Linux) and MacOS.