I want to start using SSH instead of FTP, but I am clueless when it comes to SSH keys.

I'd like to use a Gulp command to transfer my files (as I do now with FTP). I've found a good amount of NPM packages that do this. However, none explain how to actually set up SSH keys on my remote shared (LAMP) server and on my local machine (MacOS).

Would anyone know how to set this up? All the tutorials I've found deal with setting up the node.js package, not the SSH keys themselves.

Update with additional information

  • The server is Linux. It is a cheap shared LAMP stack server running CPanel
  • I am able to only access the server via CPanel and FileZilla FTP (but not SFTP)
  • The output of running ssh -i ~/.ssh/id_rsa me@mysite.com -vvv is as follows:
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to mysite.com port 22.
ssh: connect to host mysite.com port 22: Operation timed out
  • Thinking it may be a firewall issue, I have tested with multiple ISPs but have gotten the same problem.
  • I tried running the netstat -ntlp | grep ssh command on the Terminal that sits within the CPanel GUI (I don't know how else I could run this) but it did not recognise the netstat command.
  • Perhaps, this article might interest you. However, before following this article, it would be nice to setup the SSH trust first.
  • So, the problem statement is, you want to copy some files from your local machine to a remote server.
  • To do that, you have to first chose an authentication method. Assuming the remote server is Linux, you've got two options - Password based and Password Less a.k.a SSH Key based authentication.
  • For the remote server to authenticate you, these are the steps which need to happen
  • You create an SSH keypair (Private Key + Public Key) on your local machine.
  • You just copy/append the contents public key from your local machine to the .ssh/authorized_keys file of the remote server.
  • Now, when you will try to SSH to the remote server from your local machine, you will specify in the command that you are trying to authenticate with the private key file that you already have created in the first step and the authentication will be successful since you have imported the corresponding public key to the remote server's authorized_keys file.

  • First, lets create keypair: ssh-keygen -t rsa and then select all default options by just pressing Enter key.

  • In the output of the above command, you will see that the private and public key files will be generated e.g.: Your identification has been saved in /home/ec2-user/.ssh/id_rsa Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub
  • Now, just copy/append the id_rsa.pub's CONTENT file from the directory mentioned in your case to the ~/.ssh/authorized_keys.
  • To confirm that the SSH trust has been established, just try to SSH from your local machine to the remote server by using this command - ssh -i /location/of/private/key username@remote_server_ip
  • If everything's good and you are able to login, then you just have to follow the link I mentioned in the first line of this post.
  • Thanks, but I am still having difficulties. When I run ssh -i ~/.ssh me@mysite.com I just get the error ssh: connect to host mysite.com port 22: Operation timed out
    – MeltingDog
    Jan 1 '19 at 9:16
  • Looks like you did not specify the absolute/relative path pf the private key in the -i flag. From the command you have mentioned, you are referencing to a folder (.ssh) and not the private key file. Please note a sample command below: ssh -i ~/.ssh/id_rsa username@remote_server_ip
    – st_rt_dl_8
    Jan 1 '19 at 9:37
  • Sorry but I get the same error. How do you know what username to use (I mean, how do you get the value username@remote_server_ip?)
    – MeltingDog
    Jan 1 '19 at 10:11
  • Perhaps we need to backtrack and try to understand the context a little more. The following answers would be helpful. As mentioned earlier, the steps I suggested were based on my assumption that the remote server is Linux.
  • What is the OS on the remote server?
  • Are you able to login to the remote server?
  • When you run that command ssh -i ~/.ssh/id_rsa me@mysite.com run it with -vvv flag. This runs the SSH into debug mode often revealing some more information on what's happening under the hood. A sample command is ssh -i ~/.ssh/id_rsa me@mysite.com -vvv. Let us know the output.
  • Once again, assuming the remote server is linux, there are following possibilities:-
  • The sshd server on the remote machine is listening on some other port than the default TCP 22.
  • To check this, you can logon to the remote machine and run sudo netstat -ntlp | grep ssh. It will show you the port on which the SSH server is listening for SSH connections.
  • If you see that SSH is listening on some other port than TCP 22, you need to add -p flag in that ssh command. The sample command in that would be ssh -p port_number_on_which_SSH_server_is_listening -i ~/.ssh/id_rsa me@mysite.com -vvv
  • If there's no output of the above command, that means the SSH is not running on the remote server and this is the reason you are not able to SSH into it.*
  • I also have a remote instance to which I was able to connect using the same command. I was able to replicate/reproduce the issue when I disabled my SSH access to that instance and ran the SSH command in debug mode. This is the output I got (You are also getting the same error):
$ ssh -i /c/Users/XXX/ssh-keys/sl.pem ec2-user@ -vv
OpenSSH_7.5p1, OpenSSL 1.0.2k  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolving "" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to [] port 22.
debug1: connect to address port 22: Connection timed out
ssh: connect to host port 22: Connection timed out
  • Thanks, I have updated my question above with more information. I have a feeling the SSH is not available on my server or that the port is blocked.
    – MeltingDog
    Jan 1 '19 at 23:26
  • In that case, can you try allowing SSH access from your cPanel -> Advanced Section -> SSH/Shell Access ? See if you can follow this article If you are able to follow this until end, you will have the private key which you need to use while connecting with SSH command - ssh -i (path to your private key that you would download in the last step). Then the last thing you would need to find out is the port on which your remote server is listening for inbound SSH connections.
    – st_rt_dl_8
    Jan 2 '19 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.