本章主要介绍ssh远程登录系统和远程拷贝的方法。
- ssh的基本用法
- 打开远程图形化界面
- ssh无密码登录和安全设置
- ssh限制用户和其他设置
- Windows远程登录
- 远程拷贝
很多时候服务器并没有显示器,我们也不可能每次都通过控制台去管理服务器,这时就需 要远程登录。远程登录到服务器可以通过Telnet或ssh的方式。但是用Telnet登录,整个过 程都是以明文的方式传输的,不安全。所以,建议使用ssh的方式来登录,因为ssh在整个连 接过程中,数据都是加密的。
实验拓扑图如下图所示。
其中rhel03中有bdqn用户和 tom用户, rhel04中只有tom用户。如果没有 rhel04这台机器,请按照前面章节内容自行安装。
ssh的基本用法
- ssh 主机名 /IP
这里如果没有指定用什么用户连接,则以当前用户连接。
当第一次远程连接到服务器时,要记录服务器的公钥指纹信息。
[bdqn@rhel03 ~]$ ssh 192.168.23.34
The authenticity of host '192.168.23.34 (192.168.23.34)' can't be established.
ECDSA key fingerprint is SHA256:RIA4hfMV5zBWhJ70P1MxvGdDq6oRk972/rtU619S+aw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.23.34' (ECDSA) to the list of known hosts.
bdqn@192.168.23.34's password:
Permission denied, please try again.
bdqn@192.168.23.34's password:
Permission denied, please try again.
bdqn@192.168.23.34's password:
bdqn@192.168.23.34: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[bdqn@rhel03 ~]$
上面如果输入的是no,则连接终止。输入yes,则保存在了当前用户家目录下 的.ssh/known_hosts文件中。
上面的命令中,我们并没有指定使用哪个用户连接到192.168.23.34,但它是以bdqn用户 登录到192.168.23.34的。
但在rhel04上是没有bdqn用户的,如下所示。
[root@rhel04 ~]# id bdqn
id: “bdqn”:无此用户
[root@rhel04 ~]#
所以,输入的密码都是错误的。连续输入3次密码不正确就退出来,因为192.168.23.34上根本没有bdqn用户,但是有tom用户,如下所示。
[root@rhel04 ~]# id tom
uid=1002(tom) gid=1002(tom) 组=1002(tom)
[root@rhel04 ~]#
我们可以指定使用哪个用户名连接过去。
ssh的基本用法2:
- ssh 0l 用户名 主机名 /IP
- ssh 用户名@主机名 /IP
建议用第2个。现在以 tom的身份连接,命令如下。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
tom@192.168.23.34's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last failed login: Fri Dec 1 11:40:09 CST 2023 from 192.168.23.33 on ssh:notty
There were 4 failed login attempts since the last successful login.
[tom@rhel04 ~]$
可以看到,此时已经正常连接过去了,要退出来时只要输入“exit”即可。
[tom@rhel04 ~]$ exit
注销
Connection to 192.168.23.34 closed.
[bdqn@rhel03 ~]$
打开远程图形化界面
当远程连接到远端机器时,例如,从rhel03通过tom用户连接到rhel04,命令如下。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
tom@192.168.23.34's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 16:41:39 2023 from 192.168.23.33
[tom@rhel04 ~]$ firefox
Running without a11y support!
Error: no DISPLAY environment variable specified
[tom@rhel04 ~]$
然后执行firefox命令,出现了“Error: no DISPLAY environment variable specified”的错误提示。这是为什么呢?首先我们了解一下 Xserver和 Xclient。
每个打开的图形化界面的工具,例如,终端、 Firefox浏览器、gedit记事本等,这些都叫作 Xclient,这些Xclient必须在 Xserver上运行。 Xserver是一个平台环境,安装系统时如果选择 了图形化界面,那么Xserver就已经安装上去 了,如下图所示。
要是想在本机打开远端服务器的Xclient,即图形化客户端,需要满足以下3个条件。
- 通过ssh登录到服务器时,要加上-X(大写的X)选项。因为ssh建立的连接默认只允许字符传输,不允许Xclient进行传输,加上-X选项之后就可以 让Xclient通过ssh建立的连接传输。
- 本地要运行Xserver。
- 远端服务器要安装xorg-x11-xauth,默认是已经安装上去了的。 现在退出来,重新通过ssh连接过去,加上-X选项,命令如下。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
tom@192.168.23.34's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 17:06:43 2023 from 192.168.23.33
[tom@rhel04 ~]$ firefox
Running without a11y support!
Windows中常见的Xserver有Xming和 Xmanager,其中 Xming是开源的。
ssh无密码登录
ssh远程登录到服务器时有两种认证方式。
(1)密码认证
前面在rhel03通过ssh连接到rhel04时,命令如下。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
tom@192.168.23.34's password:
这里需要输入密码才能正常登录,这种就是密码认证。
(2)密钥认证
如果做了密钥认证,远程登录时不需要密码就可以直接登录。这里rhel03上的bdqn准备以tom身份无密码连接到rhen04
使用bdqn用户生成一个密钥对,命令如下。
[bdqn@rhel03 ~]$ ssh-keygen -f ~/.ssh/id_rsa -N ""
Generating public/private rsa key pair.
Your identification has been saved in /home/bdqn/.ssh/id_rsa.
Your public key has been saved in /home/bdqn/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GoL6vPTf2s542G94XK6dZjyYnA7VXFT9AbeLYMKnlZQ bdqn@rhel03
The key's randomart image is:
+---[RSA 3072]----+
| ...ooo|
| . .E....o|
| o * ..o|
| . *o... o|
| . . . S.. o. . |
| . . o . . |
|. . .o.+ B |
| + . .=+.O.*. |
| +...++==++o. |
+----[SHA256]-----+
[bdqn@rhel03 ~]$
这条命令会生成一个密钥对(私钥和公钥),这里-f指定了生成私钥的路径和名称,如果 不指定,默认也是这个路径。-N后面的双引号中没有空格,意思是不对生成的私钥加密。
这样bdqn生成了自己的密钥对,存放在自己家目录的.ssh目录下,命令如下。
[bdqn@rhel03 ~]$ ls .ssh/
id_rsa id_rsa.pub
[bdqn@rhel03 ~]$
其中id_rsa是私钥,id_rsa.pub是公钥。
然后通过ssh-copy-id把公钥的内容存储在 tom@server2家目录下 的.ssh/authorized_keys文件中,如果没有此文件,铂贝过去之后会自动创建,命令如下。
[tom@rhel04 ~]$ ls .ssh/
ls: 无法访问'.ssh/': 没有那个文件或目录
[tom@rhel04 ~]$
下面执行ssh-copy-id命令,命令如下。
[bdqn@rhel03 ~]$ ssh-copy-id tom@192.168.23.34
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/bdqn/.ssh/id_rsa.pub"
The authenticity of host '192.168.23.34 (192.168.23.34)' can't be established.
ECDSA key fingerprint is SHA256:RIA4hfMV5zBWhJ70P1MxvGdDq6oRk972/rtU619S+aw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tom@192.168.23.34's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'tom@192.168.23.34'"
and check to make sure that only the key(s) you wanted were added.
[bdqn@rhel03 ~]$
这样bdqn的公钥就存放在tom@server2家目录下的.ssh/authorized keys文件中了。
[tom@rhel04 ~]$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCxjmCftz2WdggcjqGWQNUm/cX3hM2UjzrNXfJCF3YkXJ4fJ8+ucX+ChuYzB7gSDB18glkUcxWUU9EPM3f95WwgWcqAbtJP4HB/a/n8sUAroKrtaqH14FZX/n65WKRiHkRkaMFNesA8Tz5bsJ66bHa7RwlJToMB8fND7KHuNdDbD9xZEhQLNTl7VQUK69NS/IuLgeRHRt61BRTiGm8IlbZ0tPVQZSlUW204KAJOsHqHDwBGrvQUaerfq13RE2OMESD2kgYjv1DqadWWugLBk/MkTwfqDOp6Xl2J8vO0+HUyhPNZzzrRJb5fRXhOHQPwbj7TDrpURAiYSnVMx1oY/gap+Xui8TgjlklPbtKM+eRz9EXhYGKVx1xNjBqMkM/gP3zG91oINuTy6GncND224/gjtsmtE5ZYlm8Nz8bl6TJ7vHJt7M1aR7ShtP4DeuwbqBfn6X+xckqYYrfJjK46YFjugWQzVzwLkqzcI+d18qvEZjrCSNKmKoV9mLoSAoap+qs= bdqn@rhel03
[tom@rhel04 ~]$
通过对比,发现这个文件的内容就是 bdqn@server的公钥的内容。
下面远程登录测试,命令如下。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 17:20:03 2023
[tom@rhel04 ~]$
可以看到,已经无密码登录过去了。
ssh安全设置
前面已经讲了,ssh有两种认证方式:密码认证和密钥认证。bdqn@rhel03到 tom@rhel04用的是密钥认证,其他用户的登录方式仍然是密码登录,现在想设置只能用 其中一种认证,是否可以?答案是可以的。
-
禁用密钥登录
在rhel04上,以 root用户编辑/etc/ssh/sshd_config,找到 PubkeyAuthentication, 修改内容如下。
将#PubkeyAuthentication yes修改为PubkeyAuthentication no(需要注意的是,这里 前面的注释符#被删除了),这样就禁用了密钥登录,保存退出并重启sshd,命令如下。
[root@rhel04 ~]# systemctl restart sshd
此时已经禁用了密钥登录,只能密码登录,到rhel03上进行测试,命令如下。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
tom@192.168.23.34's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last failed login: Mon Dec 4 18:16:05 CST 2023 from 192.168.23.33 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Dec 4 17:22:42 2023 from 192.168.23.33
[tom@rhel04 ~]$
这里只能使用密码登录,原来配置的密钥认证不再生效。
再次设置允许密钥登录(PubkeyAuthentication yes),修改内容如下。
将PubkeyAuthentication no修改为PubkeyAuthentication yes,并重启sshd,命令如下。
[root@rhel04 ~]# systemctl restart sshd
-
禁用密码登录
在rhel04上,以root用户编辑/etc/ssh/sshd_config,找到PasswordAuthentication, 修改内容如下。
将PasswordAuthentication yes修改为PasswordAuthentication no,这样就禁用了密 码登录,保存退出并重启sshd,命令如下。
[root@rhel04 ~]# systemctl restart sshd
此时只允许密钥登录,不允许密码登录。
为了测试方便,在rhel04上创建用户bob,密码设置为cisco@123。
[root@rhel04 ~]# useradd bob
[root@rhel04 ~]# echo cisco@123 | passwd --stdin bob
更改用户 bob 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@rhel04 ~]#
在rhel03上进行验证,首先以tom身份连接过去。
[bdqn@rhel03 ~]$ ssh tom@192.168.23.34
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 18:16:13 2023 from 192.168.23.33
[tom@rhel04 ~]$
可以看到,使用tom登录192.168.23.34时是可以无密码登录的。
然后以bob身份连接过去。
[bdqn@rhel03 ~]$ ssh bob@192.168.23.34
bob@192.168.23.34: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[bdqn@rhel03 ~]$
因为我们并没有做bob用户无密码登录到rhel04,只能使用密码登录,而密码登录被禁用, 所以bob登录失败。
自行设置允许密码登录,修改/etc/ssh/sshd_config,如下所示。
将PasswordAuthentication no修改为PasswordAuthentication yes,并重启sshd,命令如下。
[root@rhel04 ~]# systemctl restart sshd
再次使用bob用户登录。
[bdqn@rhel03 ~]$ ssh bob@192.168.23.34
bob@192.168.23.34's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
[bob@rhel04 ~]$
已经可以正常登求了。
ssh限制用户
对于服务器上的有效用户,基本上都是可以通过 ssh 连按过去的,但是有时为了安全性要禁用某些用户登录,如禁用root等。
-
禁用root用户登录
默认情况下,是可以用root登录到远端服务器的。
[bdqn@rhel03 ~]$ ssh root@192.168.23.34
root@192.168.23.34's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 16:34:26 2023 from 192.168.23.33
[root@rhel04 ~]#
可以看到,root成功连接到rhel04了。
如果要禁用root用户登录,在rhel04一用vim编辑哭打开/eto/ssh/sshd_config,按如下内容修改。
将PermitRootLogin yes修改为PermitRootLogin no,保存退出并重启sshd。
[root@rhel04 ~]# systemctl restart sshd
此时就禁用root用户登录了,在rhel03上进行验证。
[bdqn@rhel03 ~]$ ssh root@192.168.23.34
root@192.168.23.34's password:
Permission denied, please try again.
root@192.168.23.34's password:
提示被拒绝,是因为不允许root用户登录,按【Ctrl+C】组合键终止。
如果允许root用户登录,按如下内容修改。
将PermitRootLogin no修改为PermitRootLogin yes,然后重启sshd 即可。
-
禁用普通用户登录
如果想禁用某普通用户,可以用DenyUsers选项实现,用法是打开/etc/ssh/sshd_config, 在任意一行添加 DenyUsers userX,就可以限制userX ssh登录了。如果需要在rhel04上禁用bob用户ssh登录,可以用vim 编辑器打开/etc/ssh/sshd_config,在任意一行添加 DenyUsers bob,命令如下
[root@rhel04 ~]# systemctl restart sshd
这里就是禁用bob用户ssh 登录,保存退出并重启sshd,然后到rhel03上进行测试,命令如下。
[bdqn@rhel03 ~]$ ssh bob@192.168.23.34
bob@192.168.23.34's password:
Permission denied, please try again.
bob@192.168.23.34's password:
提示被拒绝,是因为bob用户被限制登录了,按【Ctrl+C】组合键终止。
这里只是禁用了bob用户,并不影响其他用户登录。
类似的选项还有AllowUsers和 DenyGroups。
其中AllowUsers userX的意思是,只允许userX用户登录,不允许其他用户登录。
如果写成AllowUsers userX userY,意思是只允许userX和userY登录,不允许其他用户登录。
如果以下两个条目同时出现:
[root@rhel04 ~]# systemctl restart sshd
则 DenyUsers生效,bob仍然是不能登录的。
ssh其他设置
ssh的其他设置可以设置ssh的默认用户及解决ssh慢的问题。
-
设置ssh的默认用户
前面讲了在使用ssh登录时,如果没有指定用户则使用当前用户登录。其实我们也是可以 指定默认用户的,即ssh登录时如果没有指定用户,则使用默认用户登录。
下面进行一个练习,在bdqn@rhel03家目录下的.ssh目录中创建config文件,内容如下.
[bdqn@rhel03 ~]$ cat .ssh/config
Host 192.168.23.34
User tom
这个设置的意思就是,当连接到192.168.23.34时,如果没有指定用户,则默认用的是 tom用户。
[bdqn@rhel03 ~]$ chmod 644 .ssh/config
需要注意的是,这里要把.ssh/config的权限改为644。在rhel03上验证。
[bdqn@rhel03 ~]$ ssh 192.168.23.34
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 18:22:18 2023 from 192.168.23.33
[tom@rhel04 ~]$
这里虽然没有指定用户名,但是可以看到是以tom身份连接。
首次连接某台机器时,都会提示我们是否保存那台机器的公钥指纹信息,让我们输人 yes/no,这里必须输人yes。如果想默认输人yes,即自动输人ves,可以修改.ssh/config∙内容如下。
[bdqn@rhel03 ~]$ cat .ssh/config
Host 192.168.23.34
User tom
Host *
StrictHostKeyChecking no
[bdqn@rhel03 ~]$
上述加粗字是新增加的,Host后面的*是通配符,表示所有主机。整体的意思是不管连接 到哪台主机都不会再提示 yes/no了。下面验证,命令如下。
[bdqn@rhel03 ~]$ rm -rf .ssh/known_hosts
[bdqn@rhel03 ~]$ ssh 192.168.23.34
Warning: Permanently added '192.168.23.34' (ECDSA) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Mon Dec 4 18:40:33 2023 from 192.168.23.33
[tom@rhel04 ~]$
这里先清除已经保存的记录,即删除.ssh/known_hosts,然后重新登录,可以看到现在 已经不提示我们输入yes/no了。
-
解决 ssh 慢的问题
有时我们在通过ssh登录到一台服务器时,连接的过程会比较慢,要等几十秒才能看到输 入密码的提示。很多时候是因为系统自动去做反向解析,即把ssh 192.168.23.34中的 192.168.23.34反向解析成主机名,才会导致ssh速度慢。
为了防止出现这个问题,可以修改服务器上的配置。用vim编辑器打 开/etc/ssh/sshd_config,找到UseDNS,进行如下修改。
将#UseDNS yes 修改为UseDNS no,这里同时把注释符#去掉了,然后重启sshd即可。
- 注意:RHEL8中默认已经是UseDNS no了
Windows远程登录
很多时候服务器上安装的是 Linux系统,但我们平时用的笔记本电脑是Windows系统, 如果要使用Windows远程登录,只要在Windows上安装ssh客户端,就可以登录到远端的 Linux服务器了。Windows中常见的ssh客户端包括PuTTY、Xshell、SecureCRT 等。
Windows中的这些客户端,大家根据自己的喜好自行下载使用即可。
远程拷贝
有时我们需要远程拷贝一些文件,例如,把文件或目录从A机器拷贝到B机器上,这种情 况下我们一般用scp或rsync。
scp或 rsync利用的是ssh建立的通道,然后把文件传输过去,scp的用法如下。
- scp /path1/file1 remoteIP:/path2/
这里的意思是把本地的/path1/file1铂贝到remoteIP这台机器的/path2目录中。需要注意的是,远程主机上目录的表示方式是“IP:目录”,冒号两边没有空格。如果拷贝目录需加上选项。
先查看rhel04上 /opt目录中的内容。
[root@rhel04 ~]# ls /opt/
[root@rhel04 ~]#
然后到rhel03拷贝 /etc/hosts到rhel04上的/opt目录中。
[root@rhel03 ~]# scp /etc/hosts 192.168.23.34:/opt/
root@192.168.23.34's password:
hosts 100% 185 159.5KB/s 00:00
[root@rhel03 ~]#
这样就把文件拷贝过去了,scp利用的是ssh建立的通道,如果没有指定使用哪个用户接到 192.168.23.34,则使用的是当前用户 root。
下面以 tom身份登录192.168.23.34并拷贝文件,命令如下。
[root@rhel03 ~]# scp /etc/hosts tom@192.168.23.34:/opt/
tom@192.168.23.34's password:
scp: /opt//hosts: Permission denied
因为192.168.23.34上的tom用户对 /opt没有写权限,所以拷贝过去时出现 了“Permission denied”这样的报错信息。
把目录/boot/grub2拷贝到rhel04上的/opt目录中,命令如下。
[root@rhel03 ~]# scp /boot/grub2/ 192.168.23.34:/opt
root@192.168.23.34's password:
/boot/grub2: not a regular file
[root@rhel03 ~]#
结果是没有拷贝过去,因为/boot/grub2是一个目录,拷贝目录需要加上r选项,命令如下。
[root@rhel03 ~]# scp -r /boot/grub2/ 192.168.23.34:/opt
root@192.168.23.34's password:
这样就把该目录拷贝过去了,到rhel04上查看,命令如下。
[root@rhel04 ~]# ls /opt/
grub2 hosts
清空rhel04上/opt中的内容,命令如下。
[root@rhel04 ~]# rm -rf /opt/*
[root@rhel04 ~]# ls /opt/
[root@rhel04 ~]#
另外一个远程拷贝的工具是rsync,rsync的用法如下。
- rsync /path1/file1 remoteIP:/path2/
rsync的用法与scp的用法一致,rsync有更多的选项可用,可以通过rsync--help来查看。
使用rsync拷贝目录时有一点区别,下面练习把rhel03上的目录/boot/grub2拷贝到 rhel04上的/opt目录中,命令如下。
[root@rhel03 ~]# rsync -r /boot/grub2/ 192.168.23.34:/opt
root@192.168.23.34's password:
需要注意的是,这里/boot/grub2最后是有“/”的,即写的是/boot/grub2/,切换到rhel04上查看,命令如下。
[root@rhel04 ~]# ls /opt/
device.map fonts grub.cfg grubenv i386-pc
可以看到,这里是将目录grub2里面的内容拷贝过来了。
清空rhel04上/opt 中的内容,命令如下。
[root@rhel04 ~]# rm -rf /opt/*
[root@rhel04 ~]# ls /opt/
[root@rhel04 ~]#
再次把rhel03上的目录/boot/grub2铂贝到rhel04的/opt目录中,命令如下。
[root@rhel03 ~]# rsync -r /boot/grub2 192.168.23.34:/opt
root@192.168.23.34's password:
这次拷贝的目录写成了/boot/grub2,也就是grub2后面并没有带“/”,切换到rhel04上查看,命令如下。
[root@rhel04 ~]# ls /opt/
grub2
可以看到,这里是把grub2这个目录拷贝过来了。
一般情况下,我们访问一个目录时,有没有最后表示分隔符的“”是无所谓的,但在使用 rsync命令铂贝目录时,有没有“/”结果是有区别的。
清空rhel04上 /opt中的内容,命令如下。
[root@rhel04 ~]# rm -rf /opt/*
[root@rhel04 ~]# ls /opt/
[root@rhel04 ~]#