ssh连接虚拟机失败
有时候,已经配置好了虚拟机网络ip,想要和本机上的远程工具连接时,发现本地ping不通虚拟机网络;
在虚拟机安装时忘记设置固定ip了,怎么处理;
ssh连接虚拟机要等很长时间才能连接上,但是很快又断开!
打开ssh连接工具,发现连接不上
1. 在虚拟机中输入ifconfig
[root@oel79 ~]# ipconfig
bash: ipconfig: command not found...
[root@oel79 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:e2:f3:31 txqueuelen 1000 (Ethernet)
RX packets 23 bytes 1571 (1.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 116 bytes 8884 (8.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 116 bytes 8884 (8.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:2a:38:0d txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
发现,ip并不是之前设置的ip: 192.168.6.10
2. 找到网络配置文件
cd /etc/sysconfig/network-scripts/
ls -la ifcfg-ens*
发现 ifcfg-ens33文件,进行备份

cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
# cp备份文件
3. 编辑配置文件
vi /etc/sysconfig/network-scripts/ifcfg-ens33
单击 i 进入编辑模式
修改完成后
:wq 回车 结束并保存修改
按照如下格式进行修改
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=c05297b6-be4e-479a-87cd-517242d526ed
DEVICE=ens33
ONBOOT=yes # 这里是no修改成yes
IPADDR=192.168.6.10
PREFIX=24
IPV6_PRIVACY=no
# 添加
GATEWAY=192.168.6.1
DNS1=114.114.114.114
DNS2=8.8.8.8
4. 重启网络
systemctl restart network
5. 进行验证
ip addr show ens33
# 或
ifconfig ens33
发现已经出现 192.168.6.10

以上设置完成后,就可以和主机进行连接了
用ssh工具连接但是很慢才连接上
这时候出现新的问题
[!NOTE]
主机可以ping通192.168.6.10
但是ssh连接很慢,且一连接上就断开
“连接主机成功 channel is not opened.连接断开”
问题解析:
- 连接慢:通常是 DNS 解析或 GSSAPI 认证导致的
- channel is not opened:通道建立失败,可能是 PAM、systemd-logind 或 SFTP 子系统问题
1. 重启服务
# 重启 systemd-logind 服务
systemctl restart systemd-logind
# 重启 SSH 服务
systemctl restart sshd
2. 优化 SSH 配置
# 编辑ssh配置文件
vi /etc/ssh/sshd_config
打开发现如下配置:
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# 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.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin yes
#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
#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
#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
PasswordAuthentication yes
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
GSSAPIAuthentication yes # 修改为no
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users 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 ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication 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 ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes # 修改为no 取消注释
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no # 取消注释 且 修改参数为yes
# ForceCommand cvs server
2.1 修改连接慢的主要参数
# 禁用 DNS 反向解析 - 解决连接慢的最重要设置
UseDNS no
# 禁用 GSSAPI 认证 - 另一个导致慢的原因
GSSAPIAuthentication no
2.2 检查参数配置正确
# 确保开启 PTY 分配
PermitTTY yes
3. 重启服务
# 重启 SSH 服务
systemctl restart sshd
# 如果上面命令失败,试试
service sshd restart
修改完成之后,ssh工具就可以正常连接


被折叠的 条评论
为什么被折叠?



