Connecting via SSH and SFTP as www-data using key based authentication

For Debian or Ubuntu server.

Connecting to SSH using SFTP as www-data using key based authentication is easy.
These are the steps to follow.

cd /
mkdir /www-data
chmod 0755 /www-data/
cd www-data
mkdir .ssh
cd .ssh
chmod 0700 /www-data/.ssh
nano authorized_keys
(paste your key: ssh-rsa DUSAOAUUAsuodu.........) Then save
chmod 0600 /www-data/.ssh/authorized_keys
usermod -d /www-data/ www-data
chown -R www-data:www-data /www-data/
chown -R www-data:www-data /var/www/

nano /etc/passwd
replace this line
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
with
www-data:x:33:33:www-data:/www-data:/bin/bash

nano /etc/ssh/sshd_config
Add this line at the bottom
AllowUsers www-data
Then reload ssh
/etc/init.d/ssh restart

❮ Back to blog