0. 场景
工作中遇到场景,由于先前基于windows平台搭建了一个小型的网站,供于项目组其他同事进行应用和相关资料的管理和查找,近期业务拓展,设计到了ubuntu平台应用的构建相关工作,为了保证项目组使用体验的一直连续性,也便于统一管理,需要将ubuntu平台构建的应用等信息归档到windows平台。但是对于shell双左斜线“\" 在ubuntu平台被用来作为转移字符使用,因此,ubuntu平台并不能直接通过cp的方式将文件拷贝到windows共享路径下(我这边没找到,如果有其它的方法还请大神指点 😃 ),在此整理归档仅作个人纪录,便于后续查找。
针对此场景可以通过搭建FTP服务器来进行或者通过Jenkins Archive the artifacts 方式实现传输,但应用场景不具有通用性,在此以ubuntu系统自带命令scp方式来实现。scp底层是基于ssh协议,端口号是22.因此要使用必须先创建一个ssh-key,具体流程如下:
1. 基础环境配置
- windows平台用户账户/密码,IP地址信息;
- ubuntu平台用户账户/密码,IP地址信息;
- ssh公钥生成与传递
- windows平台和ubuntu平台安装openssh service;
- 待传输的文件;
1.1 windows平台用户信息获取
- windows平台 IP地址信息获取:打开命令行cmd或powershell窗口,输入ipconfig命令后显示如下信息,其中IPv4信息是我们下面需要使用的信息。
Ethernet adapter Ethernet0:
Connection-specific DNS Suffix . : localdomain
Link-local IPv6 Address . . . . . : fe80::c00f:8e2e:7e7e:46ed%10
IPv4 Address. . . . . . . . . . . : 192.168.230.128
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.230.2
- ubuntu设备IP地址获取:使用Ctrl+Alt+T快捷键打开命令行窗口,输入ifconfig,显示如下信息。其中需要注意,此处查看IP方式与windows不同,windows:ipconfig,ubuntu:ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.230.129 netmask 255.255.255.0 broadcast 192.168.230.255
inet6 fe80::cd5e:1d99:b4d5:e282 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4d:fd:30 txqueuelen 1000 (Ethernet)
RX packets 97969 bytes 127776680 (127.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14562 bytes 2081785 (2.0 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 用户名和密码:这个就没什么好说的啦。。。
- ubuntu安装openssh server
ubuntu安装openssh server比较简单,只需要输入以下命令即可:
sudo apt install openssh-server
安装完成之后输入以下命令查看是否已经启动,其中有sshd项表明已经启动,到此ubuntu配置完成!
ps -ef |grep ssh
admin 1426 1353 0 2020 ? 00:00:00 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu /usr/bin/gnome-session --systemd --session=ubuntu
root 5701 1 0 2020 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
admin 8084 1267 0 2020 ? 00:00:00 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh
admin 8765 4118 0 00:00 pts/0 00:00:00 grep --color=auto ssh
- windows安装openssh server
windows安装openssh server在网上找了很多方法,有直接通过openssh直接源码编译的方式,也是下载openssh安装包的方式,这些都比较繁琐。我这边使用的windows 10的设备,而win10系统已经自带了openssh,只是没有开启,我们只需要开启即可。开启步骤如下:依次点击All Settings -> Apps -> Manage optional feature ->Add a feature 找到OpenSSH Server添加。
安装完成之后在windows 搜索框中输入services.msc打开windows services列表,可以在windows service里面查看到如下界面表示成功:注意:要是runing状态,如果不是可右键-properties设置为runing状态
2. 生成SSH 公钥
生成公钥方式比较简单,ubuntu平台与windows平台都已经装了ssh,只需要在命令行终端输入以下命令即可:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/windows/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/windows/.ssh/id_rsa
Your public key has been saved in /c/Users/windows/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:Vynhe9PMM8Es4lcjmFzVFAODGtIEl2mVKzLp3HXEwWU xxxxxxxxxxxxxO
The key's randomart image is:
+---[RSA 3072]----+
| .+o=.*==Eo|
| ..O B Bo o|
| + X B * |
| + + B O o |
| o S * = * |
| o o o . o |
| |
| |
| |
+----[SHA256]-----+
此时会在用户目录下.ssh文件夹下生成两个文件,其中id_rsa文件为私钥文件,id_rsa.pub为公钥文件,是需要传输到其他与此机器通信的设备上面的,在此也就是将windows设备上的id_rsa.pub文件传输到ubuntu设备上,并将ubuntu设备上的id_rsa.pub文件传输到windows上面。
**其中在windows平台需要将id_rsa.pub文件中的公钥信息增加到C:/Users/xxx/.ssh/authorized中,而在ubuntu设备上需要将id_rsa.pub文件中的公钥信息增加到/User/xxx/.ssh/authorized_key中。**这里两个文件的名字是不同的,不是很清楚为什么还有这个区别
3. 验证环境测试传输文件
通过以上步骤就已经将基本的环境配置搭建完成了,下面我们可以通过在windows平台上创建一个名为windows.txt的文件,在ubuntu上创建一个名为ubuntu.txt的文件,测试以下使用scpn能否正常输出文件。
3.1 ubuntu传输ubuntu.txt文件到windows
使用Ctrl+Alt+T打开终端,,输入如下指令:
admin@admin:~$ echo "hello this is ubuntu files" >ubuntu.txt
admin@admin:~$ scp ubuntu.txt windows@192.168.230.128:/c:/
windows@192.168.230.128's password:
ubuntu.txt 100% 27 1.5KB/s 00:00
3.2 从windows传输文件到unbuntu
在搜索框中输入cmd打开命令行窗口,输入如下命令:
C:\>echo "hello this is windows files" >windows.txt
C:\>scp Windows.txt admin@192.168.230.129:/home/admin/
admin@192.168.230.129's password:
Windows.txt 100% 32 0.0KB/s 00:00
C:\>
打开ubuntu机器查看文件是否已经正确传输;
至此大功告成!下班干饭~