实现WSL虚拟机环境和Windows端xshell工具,Beyond Compare建立连接

Beyond Compare 4 远程连接wsl_beyond compare ssh-CSDN博客(参考博主的文章,附上链接致敬)

一、虚拟机环境下操作

1、安装ssh远程连接

//先卸载
sudo apt-get remove openssh-server
//再安装
sudo apt-get install openssh-server

2、编辑sshd_config文件

        修改的地方不多,可以参考修改后的文件

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

# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

# 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.

Include /etc/ssh/sshd_config.d/*.conf

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

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile     .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
# 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

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem       sftp    /usr/lib/openssh/sftp-server

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

3、配置ssh服务(下次发现连接的时候没连上,用下面指令重启一下)

//重启ssh服务
sudo service ssh restart 
 
//设置ssh开机自启
sudo systemctl enable ssh
 
//关闭ssh开机自启可以用
sudo systemctl disable ssh

二、Xshell建立连接操作

        和连接正常的虚拟机一样,在虚拟机中查看网口

ifconfig

三、Beyond Compare建立连接操作

### 如何使用 Xshell 连接 WSL 为了实现通过 Xshell 连接Windows Subsystem for Linux (WSL),需要完成以下几个方面的配置: #### 1. **安装并启动 SSH Server** 在 WSL 中启用 SSH 功能是必要的前提条件。可以通过以下命令来安装 OpenSSH 服务: ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install openssh-server ``` 上述操作会更新包管理器缓存并安装 `openssh-server` 软件包[^1]。 #### 2. **创建或确认 SSH 密钥文件的存在** 确保用户的 `.ssh` 文件夹存在,并生成密钥对(如果尚未生成)。可以运行如下命令: ```bash mkdir ~/.ssh chmod 700 ~/.ssh/ cd ~/.ssh ssh-keygen -t rsa -b 4096 -C "[email protected]" ``` #### 3. **修改 SSH 配置文件** 编辑 `/etc/ssh/sshd_config` 文件以调整默认设置,例如允许密码登录或其他安全选项: ```bash sudo nano /etc/ssh/sshd_config ``` 主要关注以下参数: - 将 `PasswordAuthentication yes` 设置为开启状态以便于测试阶段。 - 如果希望禁用 root 用户直接登录,则将 `PermitRootLogin no` 启动。 保存更改后重启 SSH 服务使新设定生效: ```bash sudo service ssh restart ``` #### 4. **获取本地 IP 地址** 由于 WSL 默认绑定至主机网络接口上,因此可以直接利用 localhost 或者特定 IPv4 地址访问它。执行下面这条指令查看当前分配给 wsl 的 ip 地址: ```bash ifconfig | grep inet ``` 或者更简单的方式就是只查询 eth0 接口下的地址信息: ```bash ip addr show dev eth0|grep 'inet '|awk '{print $2}'|cut -d'/' -f1 ``` #### 5. **配置防火墙规则** 确保系统的防火墙上开放了用于远程连接口号,默认情况下该值设为 22 。对于 windows 平台而言可能还需要额外放开对应范围内的入站流量权限。 #### 6. **建立 Xshell 连接** 打开 Xshell 新建一个 session ,填写好目标机器名/IP 口号等相关字段;接着输入正确的认证凭证即可成功登陆进入基于 ubuntu 发行版构建起来的工作环境之中[^5]。 --- ### 注意事项 当遇到无法正常建立链接的情况时,请参照其他用户反馈的经验分享解决办法。比如有人提到从旧版本升级到最新稳定发行版之后出现了兼容性方面的问题导致原有的方式失效[^4]。此时建议尝试重新初始化整个过程或是切换不同的客户工具来进行调试验证工作直至恢复正常为止。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值