window用ssh连接本机虚拟机中的ubuntu

本文介绍如何通过设置网络参数及安装SSH服务实现Windows主机与Ubuntu虚拟机间的通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来自:

http://www.cnblogs.com/linjiqin/p/3148337.html

 

window用ssh连接本机虚拟机中的ubuntu

主机和虚拟机间通信,需将2台机器的IP地址设为同一网段。

1、设置虚拟机: 虚拟机–> 设置–> Hardware –> Network Adapter,在网络连接处设置为 “桥接:直接连接到物理网络”,“NAT:使用已共享的主机IP地址”处打勾。

2、window和虚拟机下ubuntu的IP地址设为同一网段
Ubuntu配置和修改IP地址请看:http://www.tieguanyin168.com/index.php/ubuntu-ip-1641.html

其实在第一步中,哪一步中设为桥接模式,并勾选下面那个复选框,就是同一个网段

采用桥接设置才可以PING 通网关

最好设置个静态IP:
sudo vi /etc/network/interfaces,将原有动态IP屏蔽掉
并用下面的行来替换有关eth0的行:在# The primary network interface之后,或在末尾加上
auto eth0
iface eth0 inet static
address 192.168.3.90
gateway 192.168.3.1
netmask 255.255.255.0
network 192.168.3.0
broadcast 192.168.3.255

将上面的ip地址等信息换成你自己就可以了.用下面的命令使网络设置生效:
sudo /etc/init.d/networking restart

3、互ping下,判断windows与linux是否互相ping通。
i、C:\Users\jiqinlin>ping 192.168.11.88   #192.168.11.88虚拟机下ubuntu的ip

正在 Ping 192.168.11.88 具有 32 字节的数据:
来自 192.168.11.88 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.11.88 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.11.88 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.11.88 的回复: 字节=32 时间<1ms TTL=64

192.168.11.88 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

ii、root@ubuntu:~# ping 192.168.11.74   #192.168.11.74 windows下ip
PING 192.168.11.74 (192.168.11.74) 56(84) bytes of data.
64 bytes from 192.168.11.74: icmp_req=1 ttl=64 time=0.242 ms
64 bytes from 192.168.11.74: icmp_req=2 ttl=64 time=0.268 ms
64 bytes from 192.168.11.74: icmp_req=3 ttl=64 time=0.251 ms

出现类似信息表示windows与linux已互通。

4、连接ssh,在连接前记得在window下安装SSH Secure Shell Client客户端

操作步骤如下:
在win7下通过ssh连接虚拟机的ubuntu,连接失败,提示信息见下图:

然后我在ubuntu终端下输入:ssh localhost,见下图:


ssh : connect to host localhost port 22: Connection refused,原来没有装sshd,也就是openssh-server。
输入命令: sudo apt-get install openssh-server

也可能会出现:Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

再:apt-get update
然后输入命令:ssh localhost,见下图

此时可以通过ssh工具连虚拟机的ubuntu系统了。

 

vmware虚拟机上ubuntu 10.04不能连上外网解决办法

http://blog.youkuaiyun.com/lys07962000/article/details/26173055

 

ubuntu apt-get update 失败解决。 

 http://blog.youkuaiyun.com/ronghua_liu/article/details/8609450

 

当运行apt-get update后出现如下错误时:
E: Some index files failed to download, they have been ignored, or old ones used instead.

可以将目录下/var/lib/apt/lists/partial/所有的文件清掉,再次运行apt-get update即可!自带源在大陆不好。


出现以下错误:

  1. /etc/apt$ sudo apt-get update  
  2. Err http://security.ubuntu.com precise-security InRelease  
  3.     
  4. Err http://security.ubuntu.com precise-security Release.gpg  
  5.   Temporary failure resolving 'security.ubuntu.com'  
  6. Err http://cn.archive.ubuntu.com precise InRelease  
  7.     
  8. Err http://cn.archive.ubuntu.com precise-updates InRelease  
  9.     
  10. Err http://cn.archive.ubuntu.com precise-backports InRelease  
  11.     
  12. Err http://cn.archive.ubuntu.com precise Release.gpg  
  13.   Temporary failure resolving 'cn.archive.ubuntu.com'  
  14. Err http://cn.archive.ubuntu.com precise-updates Release.gpg  
  15.   Temporary failure resolving 'cn.archive.ubuntu.com'  
  16. Err http://cn.archive.ubuntu.com precise-backports Release.gpg  
  17.   Temporary failure resolving 'cn.archive.ubuntu.com'  
  18. Reading package lists... Done  
  19. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/InRelease    
  20.   
  21. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/InRelease    
  22.   
  23. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/InRelease    
  24.   
  25. W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/InRelease    
  26.   
  27. W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/Release.gpg  Temporary failure resolving 'security.ubuntu.com'  
  28.   
  29. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'  
  30.   
  31. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'  
  32.   
  33. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'  
  34.   
  35. W: Some index files failed to download. They have been ignored, or old ones used instead.  
  36. xinju@xinjuOffice:/etc/apt$ cd ../network  
/etc/apt$ sudo apt-get update
Err http://security.ubuntu.com precise-security InRelease
  
Err http://security.ubuntu.com precise-security Release.gpg
  Temporary failure resolving 'security.ubuntu.com'
Err http://cn.archive.ubuntu.com precise InRelease
  
Err http://cn.archive.ubuntu.com precise-updates InRelease
  
Err http://cn.archive.ubuntu.com precise-backports InRelease
  
Err http://cn.archive.ubuntu.com precise Release.gpg
  Temporary failure resolving 'cn.archive.ubuntu.com'
Err http://cn.archive.ubuntu.com precise-updates Release.gpg
  Temporary failure resolving 'cn.archive.ubuntu.com'
Err http://cn.archive.ubuntu.com precise-backports Release.gpg
  Temporary failure resolving 'cn.archive.ubuntu.com'
Reading package lists... Done
W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/InRelease  

W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/InRelease  

W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/InRelease  

W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/InRelease  

W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/Release.gpg  Temporary failure resolving 'security.ubuntu.com'

W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'

W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'

W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'

W: Some index files failed to download. They have been ignored, or old ones used instead.
xinju@xinjuOffice:/etc/apt$ cd ../network

 


阿里源:

  1. deb http://mirrors.aliyun.com/ubuntu/ raring main restricted universe multiverse  
  2. deb http://mirrors.aliyun.com/ubuntu/ raring-security main restricted universe multiverse  
  3. deb http://mirrors.aliyun.com/ubuntu/ raring-updates main restricted universe multiverse  
  4. deb http://mirrors.aliyun.com/ubuntu/ raring-proposed main restricted universe multiverse  
  5. deb http://mirrors.aliyun.com/ubuntu/ raring-backports main restricted universe multiverse  
  6. deb-src http://mirrors.aliyun.com/ubuntu/ raring main restricted universe multiverse  
  7. deb-src http://mirrors.aliyun.com/ubuntu/ raring-security main restricted universe multiverse  
  8. deb-src http://mirrors.aliyun.com/ubuntu/ raring-updates main restricted universe multiverse  
  9. deb-src http://mirrors.aliyun.com/ubuntu/ raring-proposed main restricted universe multiverse  
  10. deb-src http://mirrors.aliyun.com/ubuntu/ raring-backports main restricted universe multiverse  
deb http://mirrors.aliyun.com/ubuntu/ raring main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ raring-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ raring-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ raring-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ raring-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ raring main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ raring-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ raring-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ raring-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ raring-backports main restricted universe multiverse


 

修改ip:

  1. # This file describes the network interfaces available on your system  
  2. # and how to activate them. For more information, see interfaces(5).  
  3.   
  4. # The loopback network interface  
  5. auto lo  
  6. iface lo inet loopback  
  7.   
  8. # The primary network interfacea  
  9. auto eth0  
  10. iface eth0 inet static  
  11. address 192.168.2.170  
  12. netmask 255.255.255.0  
  13. broadcast 192.168.2.255  
  14. gateway 192.168.2.1  
  15. dns-nameservers 192.168.2.1  
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interfacea
auto eth0
iface eth0 inet static
address 192.168.2.170
netmask 255.255.255.0
broadcast 192.168.2.255
gateway 192.168.2.1
dns-nameservers 192.168.2.1


 


修改dns:

1,重启生效:

sudo vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)

在里面插入:
nameserver 8.8.8.8
nameserver 8.8.4.4

如果有多个DNS就一行一个

修改好保存,然后执行

sudo resolvconf -u

再看/etc/resolv.conf,最下面就多了2行:

cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 8.8.4.4

可以看到我们的设置已经加上了,然后再ping一个域名,当时就可以解析了,无需重启。


 

2,重启失效:

配置文件地址 /etc/resolv.conf

使用编辑器打开

改为如下内容:
search localdomain
nameserver 202.96.128.86 希望修改成的DNS
nameserver 202.96.128.166 备用DNS

重启网络:sudo /etc/init.d/networking restart。即可

 在目录:/var/lib/apt/lists/partial  输入:

 /etc/init.d/networking restart

提示:sh:0 :getcwd() failed: NO such file or directory//出现这个问题是该目录已经被删除

.....

cd / 之后  /etc/init.d/networking restart   就没上面哪个问题

 

<think>嗯,用户的问题是关于如何解决使用WinSCP从Windows连接虚拟机上的Windows系统时遇到的连接被拒绝的问题。首先,我需要回忆一下之前处理类似问题的经验。根据之前的引用资料,当连接被拒绝时,通常是因为SSH服务没有安装或未启动,或者防火墙设置阻止了连接。不过之前的例子主要是针对Ubuntu等Linux系统,而这次用户的问题是关于虚拟机上的Windows系统,所以需要调整思路。 首先,用户使用的是虚拟机上的Windows系统,所以应该检查虚拟机网络配置是否正确。比如,虚拟机的网络模式是否是桥接或者NAT,确保主机和虚拟机在同一个网络中。如果是NAT模式,可能需要配置端口转发。另外,虚拟机的防火墙设置也可能阻止了WinSCP的连接,需要检查Windows防火墙是否有例外规则允许SSH端口(默认22)的流量。 接下来,用户需要确认Windows系统上是否安装了SSH服务器。在Windows 10及以上版本,可以通过“添加可选功能”来安装OpenSSH服务器。如果未安装,用户需要先安装并启动服务。然后,确保SSH服务正在运行,可以通过服务管理器查看状态。 另外,WinSCP默认使用SFTP协议,而SFTP依赖于SSH服务。如果用户尝试使用其他协议如FTP,可能需要确保对应的服务(如IIS中的FTP服务)已配置并运行。同时,检查端口是否正确,SSH默认是22,FTP是21,如果更改了端口,WinSCP连接时需要指定正确的端口号。 可能还需要考虑用户权限问题,比如是否允许远程连接,账户是否有权限访问目标目录。此外,虚拟机的网络适配器是否正确配置,主机和虚拟机之间是否能互相ping通,以排除网络连接问题。 总结一下,解决步骤可能包括:检查虚拟机网络设置、安装并启动SSH服务、配置防火墙、验证服务状态和端口监听情况,以及检查用户权限。需要逐步引导用户进行这些检查,并提供相应的命令或操作指南。同时,提醒用户注意协议和端口设置是否正确,避免因配置错误导致连接失败。</think>### 逐步解决方案: 针对**WinSCP连接虚拟机Windows系统被拒绝**的问题,请按以下步骤排查和解决: 1. **确认虚拟机网络模式** - 检查虚拟机软件(VMware/VirtualBox等)的网络设置: - **桥接模式**:虚拟机物理机处于同一局域网,需确保双方IP在同一网段 - **NAT模式**:需配置端口转发规则(如将主机端口2222映射到虚拟机22端口) - 验证物理机虚拟机之间网络连通性: ```powershell ping 虚拟机IP ``` 2. **启用Windows SSH服务** Windows 10/11内置OpenSSH服务器功能,需按以下步骤启用: ```powershell # 安装SSH服务(管理员权限运行) Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 # 启动服务 Start-Service sshd Set-Service -Name sshd -StartupType Automatic # 检查服务状态 Get-Service sshd ``` 3. **配置防火墙规则** - 打开「Windows Defender 防火墙」→「高级设置」 - 添加入站规则: - 协议类型:TCP - 端口:22(或自定义SSH端口) - 操作:允许连接 4. **验证SSH服务监听状态** 在虚拟机Windows中执行: ```powershell netstat -ano | findstr :22 ``` 若显示`LISTENING`状态且进程PID对应`sshd.exe`,则服务已正常启动[^3] 5. **WinSCP连接配置** - 协议选择:SFTP - 端口号:22(若使用NAT模式端口转发则填写主机映射端口) - 用户名/密码:使用虚拟机Windows的本地账户凭据 --- ### 常见问题补充 1. **若使用非默认SSH端口** 修改SSH配置文件`C:\ProgramData\ssh\sshd_config`: ```bash Port 2222 # 修改为自定义端口 ``` 重启服务生效: ```powershell Restart-Service sshd ``` 2. **文件权限问题** Windows默认限制用户目录访问,建议: - 在SSH配置文件中添加: ```bash Match Group administrators ChrootDirectory %h ForceCommand internal-sftp ``` - 或通过「安全选项卡」调整目标文件夹权限 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值