Secure shell (SSH) and secure copy (SCP)
How to access a web server using the Terminal?
ssh username@server.address.comExample: ssh root@unixnewbie.org
- When prompted for the password, enter your web server password.
- To quit the session, type
exit.
Login to server via ssh
Scenario: You’ve got a server and want to communicate with it via ssh in the future.
Alternative 1 with copy/paste
Just copy-paste the public key to your server into the ~/.ssh folder and keep a copy on your local machine, next to the private key.
Alternative 2 with file authorized_keys
Have your private key on your local machine in the ~/.ssh folder and copy the contents of the public key as a new line into the ~/.ssh/authorized_keys folder on the server.
Basic instructions:
- Create a new public/private key pair or just use an existing one (often
id_rsa.pubalready exists). Store them in the~/.ssh/folder (~denotes your home folder, e.g./Users/myName) - Copy the contents of the public key file (ends with
.pub) as a new line into the~/.ssh/authorized_keysfile on server, e.g.
cat ~/.ssh/id_rsa.pub | ssh jonny@123.45.56.78 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys“This following command does the same as the one above:
cat ~/.ssh/id_rsa.pub | ssh jonny@123.45.56.78 "cat >> ~/.ssh/authorized_keys"Alternative 3 with ssh-copy-id
ssh-copy-id uses your public ssh key to login into remote servers
- ssh into the server the first time using password or generated private ssh key (using one of the above methods)
- Create a new user with
username - Run
ssh-copy-id username@IPADDRESS - Now
ssh username@IPADDRESSwill log you in automatically
See this post for more information.
Alternative 3 with ssh-add
Example for AWS:
- Add a new public/private key pair with
ssh-add ~/.ssh/KEY_PAIR_NAME.pem(orssh-add ~/.ssh/MY_PRIVATE_KEY) - Run
ssh [your instance name]@[amazon].[com]
Explanation:
- Add the
.pemfile (i.e. private key) to the~/.sshdirectory (create it if necessary) - Use the
ssh-addcommand to add the identity to the authentication agent; this means never having to specify the.pemfile when using ssh
Notes
In case you use several ssh keys, make sure to run ssh-add -D to delete the cached SSH key(s).
See this elaborate article and this SO summary.
-
Example: Two different bitbucket accounts
- This SO article discusses different solutions. At the bottom is one with many upvotes!
Links
Create a new user and give him ssh public key of first user
- Follow these instructions to add user to account and provide him with ssh access.
- Add details to
~/.ssh/configonlocalhost - Now you can connect with this user with same ssh private key conveniently
SCP - Secure Copy - File transfer
Send file from local drive TO server via SCP
Prerequisite: You generated an ssh connection as described above with the public key my_key.pub.
Goal: Upload the file file_name.txt to your server remotehost.com into the folder /path/to/directory of the username user:
scp -i my_key.pub /path/to/file_name.txt username@remotehost.com:/path/to/directoryExample with imaginary values:
scp -i ~/.ssh/id_rsa.pub database_dump.sql deploy@staging.awesomeCompany.com:Note: Don’t forget the semi-colon at the end! With nothing written behind the semi-colon, the file will be stored in the home folder of the deploy user.
Get file FROM server to local drive via SCP
With ssh connection established (i.e. key exists):
scp -i certificate.pub username@remotehost.edu:foobar.txt /some/local/directoryWith password prompt
Note the -r which requires a password to be entered.
Here we pull the entire folder /path/to/foo from the server.
scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/Simplify ssh commands with entries in ~/.ssh/config
Add the following to ~/.ssh/config, changing the values to the ones needed:
Host awesomeProject
HostName 12.34.5.67
User deploy
IdentityFile "~/.ssh/StagingCertificate.pem"Now the server can then be accessed by
ssh awesomeProjectinstead of
ssh -i "~/.ssh/StagingCertificate.pem" deploy@12.34.5.67
// or in case of an AWS EC2 server
ssh -i ~/.ssh/ec2.pem ec2-user@ec2.server.name.comTroubleshooting
[OSX] If you are asked to enter your passphrase when openging a shell
ssh-add -KSee this reply for details.
Difference between private/public keys and .pem file
- This question was asked here
- AWS distributes
.pemfiles. They can contain anything - a certificate with a public key, an SSH public key, public key + private key, certificate with a public key + private key. PEM is a text file so you can open it in notepad and check its contents.
Further SSH commands
| Command | Explanation |
|---|---|
ssh-keygen -C „some comment“ |
Create public/private keypair with comment. |
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub |
Retrieve ssh public key from private key |
cat ~/.ssh/id_rsa.pub | pbcopy or pbcopy < ~/.ssh/id_rsa.pub" |
Copy public key to clipboard |
Create new user with sudo rights on server
- ssh into server (with a method described above)
- Create a new user with
adduser+ choose name + password - Give the new user sudo rights:
usermod -aG sudo <username> - Switch to the new user
su - <username> - Test sudo rights:
sudo ls -la /root- if you can view the contents of therootfolder and don’t get an error, the user hassudorights. - Enable password authentication
Open the file /etc/ssh/sshd_config (change vim to nano to rather open it with the nano text editor):
sudo vim /etc/ssh/sshd_configUncomment the following line by removing the `#` infront of it: PasswordAuthentication yes- Reload that ssh configuration by running
sudo /etc/init.d/ssh reloadLinks
Further reads
Discuss on Twitter ● Improve this article: Edit on GitHub