[SSH] Intro to SSH command

本文详细介绍SSH协议的使用方法,包括创建SSH密钥、复制SSH密钥到远程服务器、简化远程连接配置、通过SSH安全地复制文件。还介绍了如何使用scp命令进行远程文件传输,以及如何设置SSH配置文件来简化连接过程。

Create an ssh key:

ssh-keygen

 

Copy an SSH key to a remoate server:

ssh-copy-id root@104.197.227.8  // username@ip address / hostname

Then enter your password.

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

ssh root@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 root@104.197.227.8

If you need to do more configuration:

ssh -p 2022 -i ~/.ssh/another_key root@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 root@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 mark@myhost.com:~/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 mark@myhost.com:~/

 

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

scp -r foo mark@myhost.com:~/bar.txt // copy the whole foo folder

 

Copy the fild from remote host to local host:

scp mark@myhost.com:~/bar.txt ./new.txt

 

转载于:https://www.cnblogs.com/Answer1215/p/10683963.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值