SSH
uses public-key cryptography to authenticate the remote computer and
allow it to authenticate the user, if necessary. Linux OS provides by
shell the ssh command.
The following examples are valid also from and to Mac OS X.
The following examples are valid also from and to Mac OS X.
Using SSH in shell bash
For example, you want to connect to myremotehost (ip: 205.200.99.33) use the following command: ssh myremotehost
or
ssh 205.200.99.33
If the connection is started, after you must enter the username. Alternatively you can use the ssh command as following:
ssh username@myremotehost
The username is the OS user of remote computer.
Instead the password can not be entered in command line but at a later time. An alternative authentication system is through the use of certificates, in detail using a ssh key in your server you do not have to enter the password.
An important use case of the ssh command involves the possibility of launching bash commands remotely:
Such usage is more util for bash scripting but it needs of ssh key to batch mode.
Example:
An important use case of the ssh command involves the possibility of launching bash commands remotely:
ssh username@myremotehost "echo Hello Wolrd!!!"
SSH key
An
SSH key will let you automatically log into your server from one
particular computer without needing to enter your password. This is
convenient for two reasons:
- Automation: a bash script can runs ssh commands in batch mode.
- Security: each connection is associated to a ssh key, so at specific user.
How to configure the ssh key
- In your server: make the initial ssh connection as root and change to the home directory for the user you are creating the key for, then create the .ssh directory.
cd /home/<user> && mkdir .ssh
- In local computer: generate a ssh key using strong encryption.
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -C "An optional comment about your key"
- In local computer: Check and add the grants in .ssh directory.
chmod 700 ~/.ssh && chmod 600 ~/.ssh/*
- In local computer: Upload your public key to your server in append into authorized_keys file. This file contains the ssh public key list.
cat ~/.ssh/id_rsa.pub | ssh <user>@<host> 'cat - >> ~/.ssh/authorized_keys'
- In your server: Check and add the grants in .ssh directory.
chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh/
How to connect
The default directory and name for new keys is ~/.ssh/id_rsa, and this is where SSH will look for your keys. If you use a different key use the following command: ssh -i <ssh_key_path>/<public_key> <user>@<host>
ssh -i new_path/other_key anuser@myhost
Internal Links
May be of interest to you:
No comments:
Post a Comment