Linux-SSH密鑰配置


Linux-SSH 使用說明

簡介

SSH(遠程連接工具)連接原理:ssh 服務是一個守護進程(demon),系統后台監聽客戶端的連接,ssh 服務端的進程名為 sshd,負責實時監聽客戶端的請求(IP 22 端口),包括公共秘鑰等交換等信息。ssh 服務端由 2 部分組成: openssh(提供 ssh 服務)    openssl(提供加密的程序)ssh 的客戶端可以用 XSHELL,Securecrt, Mobaxterm 等工具進行連接

功能模塊-配置 SSH 免密碼登錄

配置1:ssh免密鑰登錄設置
客戶端配置 – 服務器1
1.查看~/.ssh文件夾,若已經存在有公鑰文件(id_rsa.pub),私鑰文件(id_rsa),則可以跳過客戶端配置.
2.生成密鑰文件.
$ ssh-keygen
然后一路回車.
然后~/.ssh下會生成id_rsa.pub和id_rsa, 其中id_rsa文件起到唯一標識你的客戶機的作用.
注意:不要改這兩個文件的文件名,ssh登陸時會讀取id_rsa文件.
# 服務器配置 – 服務器2
1.修改sshd配置文件(/etc/ssh/sshd_config).
找到以下內容,並去掉注釋符”#“
=========================
  RSAAuthentication yes
  PubkeyAuthentication yes
  AuthorizedKeysFile  .ssh/authorized_keys
=========================
2.配置authorized_keys文件.
若’~/.ssh/authorized_keys’不存在,則建立.ssh文件夾和authorized_keys文件.
將上文中客戶機id_rsa.pub的內容拷貝到authorized_keys中.
注意:
1) .ssh目錄的權限必須是700
2) .ssh/authorized_keys文件權限必須是600
3.重啟sshd.
$ /etc/init.d/sshd restart  # 非必需
# 測試
客戶機執行:ssh -v user@host (-v 調試模式)
會顯示一些登陸信息.

# 關鍵命令
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
/etc/init.d/sshd restart # 非必需

功能模塊-只允許密鑰登錄(禁用密碼登錄)

為了服務器主機安全,需要設定密鑰登錄,並且指定 root 不可遠程登錄。

步驟 1:生成客戶端密鑰

生成密鑰方式有如下兩種:

  • Linux 主機
  • Win 主機-Xshell 軟件

步驟 2:添加密鑰到服務端

將 id_rsa.pub 中內容復制到 ~/.ssh/authorized_keys

步驟 3:修改服務端配置文件

配置文件修改后生效,需要重啟服務
配置文件:/etc/ssh/sshd_config
重啟服務:service sshd restart/bin/systemctl restart sshd.service

開啟密鑰登錄

RSAAuthentication yes # 啟用 RSA 認證 # 非必需修改,不存在時不創建不修改。
PubkeyAuthentication yes # 啟用公鑰私鑰配對認證方式

禁止 root 遠程登錄

PermitRootLogin yes # 禁止 root 遠程登錄

關閉密碼登陸方式

PasswordAuthentication no
ChallengeResponseAuthentication no

修改完成后重啟服務

/bin/systemctl restart sshd.service

SSH 配置文件

常用配置項說明:


SSH 客戶端配置文件: /etc/ssh/ssh_config

[root@WOM ~]# cat /etc/ssh/ssh_config
# $OpenBSD: ssh_config,v 1.25 2009/02/17 01:28:32 djm Exp $

# This is the ssh client system-wide configuration file. See
# ssh_config(5) for more information. This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
# 1. command line options
# 2. user-specific file
# 3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options. For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

# Host *
# ForwardAgent no
# ForwardX11 no
# RhostsRSAAuthentication no
# RSAAuthentication yes
# PasswordAuthentication yes
# HostbasedAuthentication no
# GSSAPIAuthentication no
# GSSAPIDelegateCredentials no
# GSSAPIKeyExchange no
# GSSAPITrustDNS no
# BatchMode no
# CheckHostIP yes
# AddressFamily any
# ConnectTimeout 0
# StrictHostKeyChecking ask
# IdentityFile ~/.ssh/identity
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# Port 22
# Protocol 2,1
# Cipher 3des
# Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
# MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
# EscapeChar ~
# Tunnel no
# TunnelDevice any:any
# PermitLocalCommand no
# VisualHostKey no
Host *
    GSSAPIAuthentication yes
# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
    ForwardX11Trusted yes
# Send locale-related environment variables
    SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
    SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
    SendEnv XMODIFIERS

SSH 服務端配置文件: /etc/ssh/sshd_config

[root@WOM ~]# cat /etc/ssh/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
#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

# 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 yes
#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 no
#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
#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

Resources

參考鏈接:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM