Skip to content

Instantly share code, notes, and snippets.

@angrychimp
Created January 24, 2017 18:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save angrychimp/403224392a7208198e276b28d283c571 to your computer and use it in GitHub Desktop.
Save angrychimp/403224392a7208198e276b28d283c571 to your computer and use it in GitHub Desktop.
Setting up SSH keys for key-auth with ProFTPd (w/ SFTP module)
#!/bin/bash
USERNAME="your username here"
SFTPHOST="your sftp host"
SFTPPORT=22 # or other port, if non-standard
# Create SSH key-pair for SFTP access
# (creates pair with no passphrase)
ssh-keygen -b 2048 -t rsa -C "$SFTPHOST" -N "" -f ~/.ssh/id_rsa.sftp
# Create authorized_keys file for upload
ssh-keygen -e -f ~/.ssh/id_rsa.sftp.pub -m RFC4716 > ~/.ssh/authorized_keys.sftp
chmod 600 ~/.ssh/authorized_keys.sftp
# Upload authorized_keys file to sftp
# (this will prompt for a password)
scp -P $SFTPPORT ~/.ssh/authorized_keys.sftp $USERNAME@$SFTPHOST:.ssh/authorized_keys
# Verify key-auth is working
# (should log in without password prompt)
sftp -o IdentityFile=~/.ssh/id_rsa.sftp -o Port=$SFTPPORT $USERNAME@$SFTPHOST
@sandasan
Copy link

sandasan commented Jun 23, 2022

# This will overwrite all present keys in your file .ssh/authorized_keys on server
# And this didn't work in my case (asked password to log in on server even after adding this authorized key)
# Upload authorized_keys file to sftp
# (this will prompt for a password)
scp -P $SFTPPORT ~/.ssh/authorized_keys.sftp $USERNAME@$SFTPHOST:.ssh/authorized_keys

# This worked well (we append the key to the end of file .ssh/authorized_keys and can connect without prompt of password):
ssh $USERNAME@$SFTPHOST "echo \"`cat ~/.ssh/id_rsa.sftp.pub`\" >> .ssh/authorized_keys"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment