Connect to remote server with SSH
How To Connect to a Remote server with SSH
To check if ssh server is installed in a machine use below snippet.
sudo systemctl status sshd
If SSH is not installed, you can use the following commands to install it:
To install SSH
sudo apt-get install openssh-server
To enable SSH
sudo systemctl enable ssh
To start SSH
sudo systemctl start ssh
To log in to a remote server, use the following command:
ssh <user>@<ip-addr>
Note: it will ask for remote server password. Best way is to use key to login into remote server.
Setting Up SSH Keys and Passwordless Login
To log in without entering a password manually everytime, follow these steps
Generate an SSH key on your host machine with the following command
ssh-keygen -t rsa -b 4096 -C <email>
Copy the public key from the folder in which it is generated:
# go to the folder where key is generated
cd ~/.ssh/
#list key name
ls
# print key
cat <keyname>.pub
copy the value and paste it in remote server.
To paste the generated key in the remote server, you can use the following command on your host machine:
ssh-copy-id <remote_user>@<ip-addr>
If the above command doesn't work, you can use an alternative method:
Copy the content of the public key from the host machine.
Log in to the remote server.
Go to the ~/.ssh folder of the remote machine.
Create an authorized_keys file with no extension.
Paste the content into this file.
Append the key if other keys are already present.
By following these steps, you can set up SSH keys and enable passwordless login to remote servers for enhanced security and convenience.
Download anything from remote server
Now since ssh key is setup in remote server, we can download anything from the remote machine. Just use the below command
scp \
<remote-username>@<remote-ip>:< /file/path/with/name > \
/your/local/path
Here is example of how to use this command
Please check out my my other blogs to learn more about devops.
Thanks for reading...