1. 程式人生 > >[SSH] Intro to SSH command

[SSH] Intro to SSH command

ica serve suffix comm hosts ngs cto rop exit

Create an ssh key:

ssh-keygen

Copy an SSH key to a remoate server:

ssh-copy-id [email protected]104.197.227.8  // [email protected] address / hostname

Then enter your password.

To make sure you can SSH into remote server, you can do:

ssh [email protected]104.197.227.8

Since ssh-copy-id

is just a helper script, let‘s find it what it‘s actually doing in the event we want to manually add keys for authentication in the future. After SSHing into the remote host, go into the .ssh folder within your home directory. Within that folder will be a file named authorized_keys. This file contains a list of public SSH keys which have been granted access for authentication. We can see that our public SSH key has been added to this file. Public key added to authorized_keys.

SSH into a remote server:

Normal way to do it:

ssh [email protected]

If you need to do more configuration:

ssh -p 2022 -i ~/.ssh/another_key [email protected]104.197.227.8
// Let‘s assume the SSH server is bound to port 2022 instead of port 22. We add a -p flag followed by our port number 2022. Another command flag that is important is the -i flag. The i stands for identity and allows us to specify a different SSH key to authenticate with. This makes it possible to set up any number of different SSH keys to connect to any number of different hosts.

SSHing into a remote host drops you right into a bash shell, but you may just want to run one command and immediately exit. Instead of dropping a newer bash shell you may specify a command to run after typing in your connection string. This allows you to run one-off commands, a quick bash script, or anything else you wish without needing to create and stay within an SSH session.

ssh [email protected]104.197.227.8 hostname // get hostname and exit

Simplify connections with SSH config files:

Using SSH config files to greatly simplify SSH connections to remote hosts, use hostname aliases, and set advanced directives such as Port and IdentityFile settings per host

Create a config file:

vi ~/.ssh/config
Host foo  // alias for the host
    HostName 104.197.227.8 // define the actul hostname or ip address
    IdentityFile ~/.ssh/id_rsa // if needed, tell using a different public key
    Port 22 // if needed, change the port

SSH into a host:

ssh foo

Use scp to securely copy files remotely over SSH:

To use the secure copy command to copy the file to the remote host, type scp followed by the file you wish to copy, in this case bar.txt. Then specify your username @remotehost connection string, and suffix it with a colon. After the colon is where to tell scp where to copy the file to on the remote host. In this case, copy it to the home directory.

scp bar.txt [email protected]:~/bar.txt

By default, scp will use your default SSH key to connect to the remote host. To specify a different SSH key or identity, use the -i flag followed by the location of your SSH private key.

scp -i ~/.ssh/another bar.txt [email protected]:~/

You can also copy the whole folder by using `-r` command:

scp -r foo [email protected]:~/bar.txt // copy the whole foo folder

Copy the fild from remote host to local host:

scp [email protected]:~/bar.txt ./new.txt

[SSH] Intro to SSH command