天天看点

通过Jenkins SSH 部署到Windows远程服务器

一、Winodws安装OpenSSH

(注:Jenkins + OpenSSH 存在版本问题)

1、安装OpenSSH 8.1p1

cmd 管理员运行

cd 到解压目录:比如

cd c:\OpenSSH-Win32

输入安装服务

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

通过Jenkins SSH 部署到Windows远程服务器

2、配置服务

输入指令:

netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22

3、启动服务

net start sshd

二、创建新用户及配置密码(用于Jenkins配置的用户)

三、Jenkins 配置

通过Jenkins SSH 部署到Windows远程服务器

端口配置:22 默认

通过Jenkins SSH 部署到Windows远程服务器

测试返回Success 表示成功

四、公钥连接方式

1、服务端 通过 ssh-keygen -m PEM -t rsa -b 2048 -C “administrator”

生成私钥和公钥

2、在用户/.ssh 上创建 authorized_keys ,自己的公钥拷贝到该文件,在3生成公钥后,同样添加进去。

3、Jenkins服务器端,同样生成ssh私钥和公钥,同样在.ssh下创建 authorized_keys 把自己的公钥拷贝过去,目标服务器的公钥也添加进去。

4、配置Jenkins 如下:

通过Jenkins SSH 部署到Windows远程服务器

问题:Failed to connect SFTP channel java.io.IOException: inputstream is closed

解决:OpenSSH版本问题 或者 配置ssd_config 上修改成 Subsystem sftp internal-sftp

(经过好几天排除及多次安装卸载,最终确定是版本问题)

Jenkins 2.346.2(Jenkins服务器) + OpenSSH 8.1(目标服务器)

最后不管是用公钥还是账号密码,测试都通过

附: ssd_config文件配置内容:

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile	.ssh/authorized_keys

#AuthorizedPrincipalsFile none

# For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no

# GSSAPI options
#GSSAPIAuthentication no

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
ChrootDirectory E:\ssh 
#VersionAddendum none

# no default banner path
#Banner none

# override default of no subsystems
#Subsystem	sftp	sftp-server.exe
Subsystem	sftp	internal-sftp

# Example of overriding settings on a per-user basis
#Match User anoncvs
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server

# Match Group administrators
#      AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
           

继续阅读