【笔记】Linux-ssh连接

ssh服务介绍:
默认端口22
shh是密文传输,比telnet明文传输安全
登录方式:
第一种:用账号密码登录

[root@localhost ~]# ssh 192.168.122.125
root@192.168.122.125's password: 
Last login: Sat Jan 28 23:37:58 2023 from 192.168.122.1
[root@vm2 ~]# 

第二种:本地生成密钥对,把公钥上传到服务器
1.在客户端主机生成“密钥对”,配置文件为默认值(一直回车即可)
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RIjZKdv41mOT1XhVmcGKJ2qiDwpEnATHFg5tTG/f8SY root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|+*+. + o.     .o=|
|o+B.+ +.      .+ |
| *. o=  o  o...  |
|.  .o..o ooooo   |
| .   ...Eo+.o    |
|.     o.*=       |
| .   o..oo       |
|  . ...          |
|   .  ..         |
+----[SHA256]-----+
2.查看生成的“密钥对” 位置在:~/.ssh里
[root@localhost ~]# ls /root/.ssh
id_rsa  id_rsa.pub  known_hosts
#私钥     #公钥 

3.把客户端生成的公钥文件传输到远程主机中
[root@localhost ~]# ssh-copy-id 192.168.122.22
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.122.22's password:  #输入远程主机密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.122.22'"
and check to make sure that only the key(s) you wanted were added.

4.验证是否成功
[root@localhost ~]# ssh 192.168.122.22
Last login: Sun Jan 29 12:05:25 2023 from 192.168.122.1
[root@vm1 ~]# 
[root@vm1 ~]# cat /root/.ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu064GkY6Uh2OYU4BPPfAnXx72YzFbxewe4/lAs1HNTqFmwZsMCAODuDsvWOMwp1aiZZslAVRJLK9D7Zuw/mdUI56OMMsxnTsKib3dDnlAG7tfMr6EJWOOdY7WCfNc1EXvh0WLRClrAJwO8w8yofB4Q2TGHtBC7WsCdwzCHORi2k0Zsr4T10GAnv+RdemBHhgKUWL356NETLzVyb0jRKU8lgHGNc+nEZFPhL7lQk0hnVv8+5dCritRvhz+eFN+4Gots/V26Oko/1pFQsgi/2V2L0/yrVV45U8S84N7LFL15iE29aSAdjMFHQeyRzM0pPdmXim+Xo1hjJkNaUIQUt+D root@localhost.localdomain

PS:本地客户端生产密钥对,把公钥id_rsa.pub里的数据复制粘贴/上传到服务器公钥数据文件库中:~/.ssh/authorized_keys 里面即可(默认是没有authorized_keys文件的,自己创建即可)
私钥文件:id_rsa
公钥文件:id_rsa.pub

远程传输命令

scp [参数] 本地文件 远程账户@远程IP地址:远程目录
参数作用
-v显示详细的连接进度
-P指定远程主机的sshd端口号
-r用于传送文件,递归
-6使用ipv6协议
cp与scp的区别
cp是对本地硬盘文件复制,而scp可以通过网络来传递数据,还能对数据进行加密处理,使用scp必须使用绝对路径。
scp命令是基于ssh协议进行文件传送,设置好了密钥登录验证,则传输时不需要账号密码
有密钥登录
[root@localhost ~]# scp /root/anaconda-ks.cfg root@192.168.122.125:/home/
anaconda-ks.cfg                               100% 1384   777.1KB/s   00:00    

无密钥登录
[root@localhost ~]# scp /root/anaconda-ks.cfg root@192.168.122.125:/home/
root@192.168.122.125's password:  #这里输入登录的主机密码,才可以传送
anaconda-ks.cfg                               100% 1384   782.9KB/s   00:00  

[root@localhost ~]# ssh 192.168.122.125
Last login: Sun Jan 29 12:20:27 2023 from 192.168.122.1
[root@vm2 ~]# ls /home
admin  anaconda-ks.cfg

ssh配置

路径:/etc/ssh/sshd_config

[root@localhost ~]# vim /etc/ssh/sshd_config 

#	$OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj 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:/usr/local/sbin:/usr/sbin

# 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
==设定sshd服务器监听的ip地址==
#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

# System-wide Crypto policy:
# This system is following system-wide crypto policy. The changes to
# Ciphers, MACs, KexAlgoritms and GSSAPIKexAlgorithsm will not have any
# effect here. They will be overridden by command-line options passed on
# the server start up.
# To opt out, uncomment a line with redefinition of  CRYPTO_POLICY=
# variable in  /etc/sysconfig/sshd  to overwrite the policy.
# For more information, see manual page for update-crypto-policies(8).

# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
==设定是否运行root管理员登录==
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 Fedora and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes

# It is recommended to use pam_motd in /etc/pam.d/sshd instead of PrintMotd,
# as it is more configurable and versatile than the built-in version.
PrintMotd no

#PrintLastLog yes
#TCPKeepAlive yes
#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值