Linux下通过SSH实现远程连接
功能
通过SSH服务,实现本机(192.168.145.128)与远程服务器(192.168.145.129)之间的连接,能够在本地对服务器进行操作。
一、实现远程连接
1、实现SSH连接需要有SSH客户端和SSH服务端。一般Centos7自带了SSH客户端
2、在服务器上输入命令:yum -y install openssh-server 安装SSH服务端
[root@localhost ~]# yum install -y openssh-server ///安装openssh-server
[root@localhost ~]# ps -ef | grep sshd ///查看sshd进程服务是否已启动
root 23675 1 0 08:56 ? 00:00:00 /usr/sbin/sshd -D
root 23683 20222 0 08:57 pts/0 00:00:00 grep --color=auto sshd
3、在客户端上,切换为我们想要实现远程连接的本地用户账号,执行以下命令:
[root@localhost ~]# su root ///切换到root用户
[root@localhost ~]# ssh-keygen -t rsa ///通过ssh-keygen 命令生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): ///生成密钥文件,之后不需要输入密码,按三次回车键即可
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
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:
SHA256:mCsplTBihY485MQV8hOt2wYV4thcEnhmYHPZsysOVfU root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|.==X*oo. |
|.*@==* . |
|Oo**+ o E |
|o* *.o o |
| o * + S |
| . + = . |
| + = . |
| o . |
| |
+----[SHA256]-----+
4、进入到.ssh目录,查看是否已生成密钥和公钥
[root@localhost .ssh]# ls
id_rsa id_rsa.pub ///前者为密钥、后者为公钥
[root@localhost .ssh]# ll
总用量 8
-rw-------. 1 root root 1679 11月 1 08:51 id_rsa
-rw-r--r--. 1 root root 408 11月 1 08:51 id_rsa.pub
5、将客户端生成的公钥发送给服务端
[root@localhost .ssh]# ssh-copy-id 192.168.145.129 ///将服务端公钥复制给客户端,并重命名为authorized_keys
root@192.168.145.129's password: ///输入服务端的密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.145.129'" ///生成成功
and check to make sure that only the key(s) you wanted were added.
6、进入到客户端,查看是ssh公钥否已从服务端复制到客户端
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
authorized_keys ///复制成功
7、在服务端机器上进行连接客户端机器
[root@localhost .ssh]# ssh root@192.168.145.129 ///连接客户端机器
Last login: Fri Nov 1 09:16:23 2019 from 192.168.145.128
[root@localhost ~]# exit ///退出连接客户端机器
登出
Connection to 192.168.145.129 closed.
二、注意事项
1、文件权限
#chmod 700 .ssh
#chmod 600 authorized_keys
2、如果要求只能通过密钥连接,那么可以修改服务端的sshd_config配置文件。
将被注释掉的#PasswordAuthentication yes 改为PasswordAuthentication no 。
三、文件传输
scp是secure copy、一个在linux下用来进行远程拷贝文件的命令
1、把本地根目录下的villian.txt文件拷贝到192.168.206.148端的/home/work目录下
[root@localhost ~]# scp /root/villian.txt root@192.168.206.148:/home/villian/
2、把192.168.206.148端的villian.txt文件拷贝到本地的/home/villian目录下
[root@localhost ~]# scp root@192.168.206.148:/home/villian/villian.txt /root/
3、把192.168.206.148端的villian.txt文件拷贝到192.168.206.149端的/home/villian目录下
[root@localhost ~]# scp root@192.168.206.148:/home/villian/villian.txt root@192.168.206.149:/home/villian/
4、拷贝文件夹,需加-r参数
scp -r /home/villian/soft root@192.168.206.148:/home/work/
四、Linux端与Windows端互传文件
1、从linux系统复制文件到windows系统
[root@localhost ~]# scp /root/villian.txt administrator@192.168.3.181:/d:/
2、在linux环境下,将windows下的文件复制到linux系统中:
[root@localhost ~]# scp administrator@192.168.3.181:/d:/test/abc.txt /root
备注:
- administrator是windows的用户名,比如我的windows用户名是villian;
- 192.168.3.181是你windows的ip;
- /root是你要存放的Windows路径;
- 因为windows系统本身不支持ssh协议,所以,要想上面的命令成功执行,必须在windows客户端安装ssh for windows的客户端软件;
- lrzsz命令也可以实现Linux端与Windows端互传文件;