百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Linux 学习笔记之简单的SSH远程连接配置

ahcoder 2024-12-13 20:39 65 浏览

在上节,我们学习了网卡的配置,把网络配置好之后,接下来,我们就要开始配置我们的 SSH 远程连接了,毕竟我们不可能时时可以使用桌面或远程桌面的方式做运维。因此我们要学习一下,如何使用我们工具,通过 SSH 的连接方式,连接我们的服务器,对服务器进行运维。

本节内容,要分两部分来讲解。本节内容是以 CentOS 6的配置来当实例。其它的版本,可能会出现不一样的设置(相差很小小),不过 CentOS 8变化会大一点。

一、服务器设置

1、查看是否已经安装 ssh(openssh-server),已安装会出现已经安装的ssh版本信息,如果没有,则会提示no found

[chenmz@localhost ~]$ yum list installed | grep openssh-server
openssh-server.x86_64   5.3p1-94.el6    @anaconda-CentOS-201311272149.x86_64/6.5
[chenmz@localhost ~]$ 

2、没有安装,使用命令在线安装。一般情况下,Linux最小安装与 WEB版本默认没有安装,其它版本几乎都会安装,有些在安装系统时,选择功能时,也会漏选的情况没有安装。

[chenmz@localhost ~]$ yum install openssh-server

3、安装完成后,开始配置 sshd_config文件(配置文件目录/etc/ssh/sshd_config),可以使用我们之前学习过的命令 vim/vi 来编辑配置文件,当然如果怕错,可以使用cp命令备份一份出来,改得不知道怎么办了,可以恢复回来!(这个很重要,也要习惯做这个动作,特别是我们对系统的文件进行修改时,都要习惯做备份,万一真的出了什么问题,又找不出来原因,可以通过恢复初始的文件来解决。)

备份命令:

[root@localhost network-scripts]# cp sshd_config sshd_config.bak     //备份一个
[root@localhost network-scripts]# cd /etc/ssh/     //进入配置文件目录
[root@localhost ssh]# ll
总用量 164
-rw-------. 1 root root 125811 11月 23 2013 moduli
-rw-r--r--. 1 root root   2047 11月 23 2013 ssh_config     //客户端配置文件(不用管)
-rw-------. 1 root root   3896 7月   2 2020 sshd_config   //服务器配置文件(要配置的文件)
-rw-------. 1 root root   3881 7月   1 2020 sshd_config~
-rw-------. 1 root root   3879 7月   1 2020 sshd_config.bak //要是怕错,可以先cp备份一份
-rw-------. 1 root root    668 7月  13 2015 ssh_host_dsa_key
-rw-r--r--. 1 root root    590 7月  13 2015 ssh_host_dsa_key.pub
-rw-------. 1 root root    963 7月  13 2015 ssh_host_key
-rw-r--r--. 1 root root    627 7月  13 2015 ssh_host_key.pub
-rw-------. 1 root root   1679 7月  13 2015 ssh_host_rsa_key
-rw-r--r--. 1 root root    382 7月  13 2015 ssh_host_rsa_key.pub
[root@localhost ssh]# vim sshd_config

配置文件本身是已经帮我们写好了全部的设置内容,只不过前面加了#备注号,我们要做的就是删除掉备注号,让设置的内容生效即可。全部的配置信息如下,而我们关注的配置行却仅仅只有几行即可,我也做了说明:

#       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm 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:/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 change a
# default value.

//以下是配置的信息开关,去掉#表示开,加上#表示关闭

Port 22  //必开,这个是连接的端口,默认22,建议修改成高位数
#AddressFamily any
ListenAddress 0.0.0.0 //必开,监听地址
ListenAddress ::          //必开,监听地址

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2       //必开,这个是协议2,Centos 7以上默认只有2开可以连接 

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin no //允许root用户远程登陆,必开,但yes设为no,有关安全
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and 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 no
GSSAPIAuthentication yes //建议开户,基于GSSAPI的用户认证
#GSSAPICleanupCredentials yes
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 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'.
#UsePAM no
UsePAM yes

# 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

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes    //建议开启转发
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner none

# 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
#       ForceCommand cvs server

4、配置文件的重点说明(符合基本的安全设置,如果需要更强的安全,得再深入去学习 ssh相关配置才可以)。

主要你把下面的9项开启并设置好,那服务的SSH配置就基本已经完成。可以使用客户端来远程连接了!

Port 22  //必开,这个是连接的端口,默认22,建议修改成高位数
ListenAddress 0.0.0.0 //必开,监听地址
ListenAddress ::          //必开,监听地址
Protocol 2       //必开,这个是协议2,Centos 7以上默认只有2开可以连接 
PermitRootLogin no //允许root用户远程登陆,必开,但yes设为no,有关安全
PasswordAuthentication yes  //默认已开,允许使用密码验证登陆
GSSAPIAuthentication yes //建议开启,基于GSSAPI的用户认证
GSSAPICleanupCredentials yes //建议开启,退出清缓存
X11Forwarding yes    //建议开启转发

5、防火墙的设置

默认的情况下,防火墙是关闭的,不用设置,如果你上面的设置要是都开启了,还是连接不上,可以查看 一下防火墙的设置,特别是你设置的端口不是常用的话,更要注意这里!

使用命令给防火墙加上放行端口

iptables -A INPUT -p tcp --dport 端口号-j ACCEPT

二、工具设置

直接上个图即可

设置服务器地址:

端口号:

然后就可以下一步就是输入帐号与密码(用户如果设置,请翻查之前的文章)

登陆成功后

要使用root权限怎么办呢?

直接使用命令su来切换即可!是不是很方便呢?

su - root

相关推荐

KaOS 2025.05版本发布:全面拥抱Qt6,彻底告别Qt5

KaOSLinux2025.05版本重磅发布:全面拥抱Qt6,开启KDE生态新篇章继2025.03版本发布两个月后,专注于KDE桌面环境、采用XFS文件系统的滚动发行版Li...

基于FIMC接口的CMOS摄像头驱动分析与设计

摘要:目前的嵌入式系统中,USB摄像头使用比较普遍,但其应用会受到传输速度的限制。本文采用一款高速CMOS摄像头,其驱动利用S3C6410内置的FIMC接口技术,采用DMA和ping-pong缓冲...

没错是微软 推出基于Linux的交换机系统

2015-09-2205:59:59作者:郑伟你没看错,为了提升自身Azure云数据中心内网络设备的兼容性及开放性,微软也开始推出基于Linux的网络交换机系统了。这个被称为AzureCloud...

Linus Torvalds 宣布首个 Linux 内核 6.16 候选版本

Linux内核负责人兼创始人LinusTorvalds宣布关闭合并窗口,该窗口用于将主要新功能添加到内核中,并开始发布Linux6.16候选版本,从候选版本1(Linux6.16-r...

Linux内核漏洞将影响Haswell架构服务器

在infoq网站上,GilTene最近报告一个十分重要,但并不为人知Linux内核补丁,特别对采用Haswell架构的Linux系统用户和管理员应该特别关注。报告提醒RedHat发行版的用户(包括...

关于Linux性能调优中网络I/O的一些笔记

写在前面和小伙伴分享一些Linux网络优化的笔记,内容很浅,可以用作入门博文内容结合《Linux性能优化》读书笔记整理涉及内容包括常用的优化工具(mii-tool,ethtool,ifconfig,i...

国产操作系统- Veket Linux(国产操作系统之光银河麒麟阅读理解)

VeketLinux是一个随身的可装在U盘的Linux操作系统。主要面向桌面用户。它的设计重点是提供简单易用且稳定的操作系统,同时保持更新和开发。它具有强大的功能集和广泛的用户基础,可满足...

AlmaLinux 9.6发布:升级工具、初步支持IBM Power虚拟化技术

IT之家5月21日消息,科技媒体linuxiac昨日(5月20日)发布博文,报道称代号为SageMargay的AlmaLinux9.6发行版已上线,距上一版本9.5发...

跟老韩学Linux运维架构师系列,vim与view的基本使用

下面是vim和view的10个实例:用vim打开一个新文件:vimnewfile.txt这个命令将会在vim编辑器中打开一个新文件。在vim中移动光标:使用方向键或h、j、k、l键来移动光标。在v...

malloc底层原理剖析——ptmalloc内存池

malloc底层为什么是内存池malloc大家都用过,其是库函数。我们都知道库函数在不同的操作系统中其实执行的是系统调用,那么malloc在Linux上执行的是哪个系统调用呢?brk()和mmap()...

Zen 6架构首秀Linux,AMD加速下一代处理器布局

IT之家5月15日消息,科技媒体Phoronix昨日(5月14日)发布博文,报道称AMD已经开始为下一代“Zen6”处理器做准备,已为该构架向Linux内核提交了首个补丁,...

为何越来越多企业转向安卓/Linux工业平板电脑?答案在这里

在工业领域,设备的稳定性至关重要,尤其是工业平板电脑,常年运行在高温、粉尘、潮湿等复杂环境下,一旦系统崩溃或者卡顿,可能会影响整个生产流程。那么,为什么越来越多的企业选择安卓/Linux工业平板电脑,...

从3ms到0.8ms:ARM+Linux如何重塑工业控制实时性标杆

在智能制造领域,产线控制系统对实时性的要求越来越高。根据行业调研数据,超过65%的工业现场出现过因系统响应延迟导致的故障停机,平均每次停机造成的直接损失高达2-8万元。传统x86架构搭配Windows...

看Linux如何"挖坑种树"

写在前面,有人看我的Linux文章说技术难度不深,笔者不是不想写深,笔者是觉得Linux难就难在入门,入门之后你就知道如何上网查询你所要要解决的Linux需求。如果你已入门,此文已对你无用,请略过此...

AlmaLinux 9.6 发布,新增功能亮点纷呈!

距离上一版本AlmaLinux9.5发布六个月后,基于5.14内核的AlmaLinux正式宣布其企业级Linux发行版的9.x系列第六个更新——AlmaLinux9.6(Sag...