ssh、scp、ssh-keygen及ssh-copy-id的用法

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
标题:重点讲解ssh、scp、ssh-keygen及ssh-copy-id的用法
时间:2019年8月29日
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
以下实验需要先关闭防火墙,排除防火墙的干扰

1. ssh命令
ssh命令使用于远程连接服务器使用的,其对应的服务为sshd,服务的配置文件的位置是"/etc/ssh/sshd_config"
技巧一:使用ssh连接远程服务器时务必加上需要连接的用户
mysql1 192.168.44.100
mysql2 192.168.44.150
[root@mysql1 ~]# ssh 192.168.44.150
root@192.168.44.150's password: 输入密码,密码不回显
Last login: Wed Aug 28 17:35:34 2019 from 192.168.44.1
[root@mysql2 ~]#
我们会发现我们是以root用户的身份登录到192.168.44.150的服务器中。
大家在网上学习时可能会发现,会有人提到如果ssh命令后面直接跟IP地址,默认是以root用户身份连接。看到上面的结果好像真是如果,实则不然。
我们在mysql1中创建mysql用户,然后切换到mysql用户远程192.168.44.150,看看结果会怎么样呢?
[root@mysql2 ~]# useradd mysql
[root@mysql2 ~]# echo "mysql" |passwd --stdin mysql
Changing password for user mysql.
passwd: all authentication tokens updated successfully.
[root@mysql2 ~]# su - mysql
[mysql@mysql2 ~]$ ssh 192.168.44.150
mysql@192.168.44.150's password: [mysql的密码]
[mysql@mysql2 ~]$
我们会发现最终是以mysql用户连接到服务器的。这样看来网上说的并不是很严谨。所以通常情况下,建议大家在IP前加上用户。
[mysql@mysql2 ~]$ ssh root@192.168.44.150
root@192.168.44.150's password:
Last login: Thu Aug 29 15:13:36 2019 from 192.168.44.100
[root@mysql2 ~]# 这样就不会出现上面的问题了
技巧二:如何指定远程连接使用的端口
在sshd_config的配置文件,我们是可以更改默认ssh连接的端口号的。为了安全我们一般也会这么做。
这时候我们在使用ssh直接连接IP地址时,会发现会提示拒绝连接的消息。
/etc/ssh/sshd_config 2222端口
[root@mysql1 ~]# ssh root@192.168.44.150
ssh: connect to host 192.168.44.150 port 22: Connection refused
[root@mysql1 ~]# ssh -p 2222 root@192.168.44.150
root@192.168.44.150's password:
Last login: Thu Aug 29 15:34:01 2019
[root@mysql2 ~]#
技巧三:提高linux服务器sshd的链接效率
物理服务器也好,虚拟机也罢。可能某个时间我们会发现,ssh连接服务器时会很慢。这是怎么回事?
其实sshd默认的配置文件中开启了DNS检查和GSSAPI认证,只要我们将这两个参数关闭即可。
打开文件/etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no


2. scp命令
这个命令在大家眼里再清楚不过了,是远程文件传输的命令。
技巧一:与ssh命令很类似,一个就是scp时在IP地址前加上用户名,另外一个就是指定端口号。scp指定端口的参数时-P。
[root@mysql1 ~]# scp -P 2222 install.log root@192.168.44.150:/root/
root@192.168.44.150's password:
install.log 100% 9072 8.9KB/s 00:00
当然,如果你传输的是目录,则需要指定-r选项,很多人在使用scp时会使用-f选项,个人认为这是没有意义的。
[root@mysql1 ~]# scp -P 2222 check/ 192.168.44.150:/tmp/
root@192.168.44.150's password:
check: not a regular file
[root@mysql1 ~]# scp -r -P 2222 check/ 192.168.44.150:/tmp/
root@192.168.44.150's password:
readme.txt 100% 927 0.9KB/s 00:00
66c221be-6ab2-ef53-1589-fe16877914f4.pl 100% 74KB 74.1KB/s 00:00
66c221be-6ab2-ef53-1589-fe16877914f4.sh 100% 2473 2.4KB/s 00:00
linux-check.zip 100% 15KB 14.7KB/s 00:00
技巧二:scp命令并不是只能传输文件给别人,还能从别人那下载文件,只要你的权限足够大。
[root@mysql1 ~]# scp -P 2222 root@192.168.44.150:/root/install.log /check
root@192.168.44.150's password:
install.log 100% 9072 8.9KB/s 00:00


3. ssh-keygen命令
ssh-keygen命令,众所周知是生成密码的一个命令。在生成秘钥过程需要人工输入秘钥位置以及密码。
即使使用默认的配置也许要按好几下回车才可以。
如果这样,我们脚本需要执行ssh-keygen命令该怎么办呢?总不能一直盯着按回车吧。
其实ssh-keygen是存在两个参数可以避免交互的。
-f 可以指定秘钥位置,指定秘钥位置后,在创建秘钥时则不需要再次输入了。
-P 可以指定密码为空,这样在创建秘钥时不会提示需要输入密码了。
[root@mysql1 ~]# ssh-keygen -f ~/.ssh/id_rsa -P ""
Generating public/private rsa key pair.
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f8:9a:53:08:48:e1:ea:be:f8:4f:0c:a8:3f:59:2f:48 root@mysql1
The key's randomart image is:
+--[ RSA 2048]----+
| .. |
| .. |
| ... |
| o. . . |
|o . ...S |
|o Eo. ... |
|.o +o. .. |
|o.+.. oo |
|.++o..o. |
+-----------------+
我们在创建秘钥前,先判断秘钥是否存在,如果不存在则创建,这样就可以避免交互了。


4. ssh-copy-id命令
ssh-copy-id命令是配置互信需要的一条命令。
用法:
[root@mysql1 ~]# ssh-copy-id 192.168.44.150
[root@mysql1 ~]# ssh-copy-id root@192.168.44.150
由于sshd端口改为2222,所以不能拷贝秘钥。不建议使用第一条命令,一定要明确指明源和目的,避免因配置错误而导致其他麻烦。
[root@mysql1 ~]# ssh-copy-id root@192.168.44.150 -p 2222
我们发现即使我们指定了端口也不能连接,其实不是不能连接,而是其用法比较特别。可以试试下面这条命令。
[root@mysql1 ~]# ssh-copy-id "root@192.168.44.150 -p 2222"
root@192.168.44.150's password:
Now try logging into the machine, with "ssh 'root@192.168.44.150 -p 2222'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@mysql1 ~]# ssh -p 2222 192.168.44.150
Last login: Thu Aug 29 15:36:32 2019 from 192.168.44.100
[root@mysql2 ~]# exit

转载于:https://www.cnblogs.com/lv1572407/p/11430445.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值