linux连接ssh(centos)

本文详细介绍了如何在 CentOS 7 上配置 SSH 服务,包括安装 openssh-server、编辑 sshd_config 文件来调整 SSH 服务参数,如监听端口、地址等。此外还提供了确保 SSH 服务正常运行的操作步骤,如启动服务、配置网络及进行基本的连通性测试。

1,确保 centos7 已安装 openssh-server


此处显示已经安装了  openssh-server,如果又没任何输出显示表示没有安装  openssh-server,通过输入  yum install openssh-server


2,编辑 cat /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
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 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
#	ForceCommand cvs server

3,确保sshd服务开启


如果没有可以使用 sudo service sshd start 命令


4,查看 nat 配置


5,查看 VMnet8 配置


6,查看 centos 网络


可能一开始的时候发现IP地址没有匹配,需要找到 对应网卡执行命令


编辑 ifcfg-ens33 ,修改 onboot=noyes ,保存退出,并执行 service network restart


7,查看 windows 网络 ipconfig


8,确保互相能 ping 通,centos ping windows


windows ping 通 centos


9,终端连接设置


设置用户名和密码


终端连接成功


### 配置 VSCode 使用 SSH 连接 CentOS Linux 虚拟机 #### 准备工作 在开始之前,需确认以下几点: - 确认 CentOS 的 IP 地址已正确设置并能够访问网络。 - 编辑 `/etc/sysconfig/network-scripts/ifcfg-ens33` 文件以确保网卡配置无误[^1]。 #### 安装 Remote-SSH 插件 打开 Visual Studio Code 并进入扩展市场,搜索 `Remote - SSH` 插件。安装该插件后重启 VSCode[^2]。 #### 创建 SSH 配置文件 在本地机器上编辑 `.ssh/config` 文件(通常位于用户的主目录下),如果没有此文件可以手动创建。添加如下内容: ```plaintext Host centosvm HostName <your-centos-ip> User <username-on-centos> Port 22 ``` 其中 `<your-centos-ip>` 是 CentOS 虚拟机的 IP 地址,而 `<username-on-centos>` 则是你登录 CentOS 的用户名。 #### 测试 SSH 连接 在终端中输入以下命令测试连接是否成功: ```bash ssh <username-on-centos>@<your-centos-ip> ``` 如果能正常登录,则说明 SSH 已经配置完成。 #### 在 VSCode 中建立连接 返回至 VSCode,在左侧活动栏找到 **“远程资源管理器”** 图标(四个方块组成的图标)。点击顶部的绿色加号按钮选择 **“Connect to Host...”** ,然后从列表中选择刚才定义好的主机名 `centosvm` 即可尝试连接。 #### 常见问题处理 如果遇到无法连接的情况,请按照以下方法逐一排查: 1. 检查防火墙是否允许端口 22 的流量; 2. 确保 CentOS 上已经启动了 sshd 服务 (`sudo systemctl start sshd`); 3. 如果仍然失败,查看日志文件 `/var/log/secure` 寻找错误提示。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值