0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

SSH命令詳解

冬至子 ? 來源:lhzheng ? 作者:lhzheng ? 2023-06-02 10:00 ? 次閱讀

SSH命令詳解

ssh是一種安全的遠(yuǎn)程登錄及傳輸協(xié)議。ssh可用于遠(yuǎn)程登錄、遠(yuǎn)程文件傳輸?shù)?。ssh是安全的shell。

使用ssh協(xié)議可以安全地將客戶端與服務(wù)端通過網(wǎng)絡(luò)連接起來,這為遠(yuǎn)程調(diào)試設(shè)備提供了可能性。ssh遠(yuǎn)程登錄后的操作完全同本地shell的操作一致。

ssh安全登錄的方式有2種,密碼口令驗證和密鑰驗證。

圖片

ssh命令選項

圖片

密鑰驗證流程

ssh命令選項

要查看ssh詳細(xì)的命令選項,請在Linux系統(tǒng)終端鍵入

$man ssh

圖片

man ssh

man ssh翻譯內(nèi)容參考

https://www.cnblogs.com/f-ck-need-u/p/7120669.html

ssh命令詳解及使用

1.ssh連接遠(yuǎn)程主機(jī)

$ ssh user@hostname

最簡單的用法只需要指定用戶名和主機(jī)名參數(shù)即可,主機(jī)名可以是 IP 地址或者域名。例如:

# ssh cl@192.168.125.103

2.ssh連接指定端口的ssh服務(wù)

$ ssh -p 10022 user@hostname

SSH 默認(rèn)連接到目標(biāo)主機(jī)的 22 端口上,可以使用-p選項指定端口號 例如:

ssh cl@192.168.125.103 -p 5030

3.連接到ssh服務(wù)并執(zhí)行一條命令,打印結(jié)果到本地

$ ssh pi@10.42.0.47 ls -l

直接連接并在后面加上要執(zhí)行的命令就可以了 4.ssh及sshd配置

ssh配置在/etc/ssh目錄下

圖片

ssh配置目錄

SSH 的配置文件在 /etc/ssh/ssh_config 中,你可以看到端口號, 空閑超時時間等配置項,可以看到ssh非對稱密鑰有dsa、ecdsa、rsa等幾種,sshd的配置文件在/etc/ssh/sshd_config目錄下。

5.構(gòu)建ssh密鑰對

$ ssh-keygen -t rsa

使用 ssh-keygen -t +算法 ,現(xiàn)在大多數(shù)都使用rsa或者dsa算法

6.查看是否已經(jīng)添加了對應(yīng)主機(jī)的密鑰

$ ssh-keygen -F 222.24.51.147

7.刪除主機(jī)密鑰

$ ssh-keygen -R 222.24.51.147

使用-R選項,也可以在~/.ssh/known_hosts文件中手動刪除

8.綁定源地址

$ ssh -b 192.168.0.200  root@192.168.0.103

如果客戶端有多于兩個以上的 IP 地址,你不確定使用哪個IP來連接ssh服務(wù)器。我們可以使用 -b 選項來指定一個IP 地址。這個 IP 將會被用作建立連接的源地址。

9.對數(shù)據(jù)請求壓縮

$ ssh -C root@192.168.0.103

使用 -C 選項,所有通過 SSH 發(fā)送或接收的數(shù)據(jù)將會被壓縮,并且仍然是加密的

10.打開調(diào)試模式

$ ssh -v root@192.168.0.103

-v參數(shù)可以打印出調(diào)試信息,如下圖所示:

圖片

ssh調(diào)試信息

11.當(dāng)前用戶的ssh密鑰

cl@CL:~/.ssh$ ls
id_rsa  id_rsa.pub  known_hosts

12.known_hosts文件

圖片

known_hosts內(nèi)容

當(dāng)服務(wù)端被連接會在該路徑下known_hosts生成key驗證信息,用來驗證客戶端的IP地址與公鑰。

首次ssh連接服務(wù)端,服務(wù)端就會記錄連接的IP地址以及公鑰信息,存放在known_hosts文件里面,后續(xù)再次連接就不需要檢查指紋信息了。

13.查看服務(wù)端指紋信息

cl@CL:/etc/ssh$ sudo ssh-keygen -lf ssh_host_rsa_key -E sha256
2048 SHA256:Nr865fluVGxdxHnWCts9+ye/enB3pokV64w+qvRElTs root@CL (RSA)
cl@CL:/etc/ssh$ sudo ssh-keygen -lf ssh_host_rsa_key -E sha1
2048 SHA1:EVIobIZxbqHJs3RA/eefuog13EI root@CL (RSA)
cl@CL:/etc/ssh$ sudo ssh-keygen -lf ssh_host_rsa_key -E md5
2048 MD5:41:63:ed:9f:d6:ad:46:27:a1:cc:d5:36:1b:0e:cc:5a root@CL (RSA)

附ssh配置文件信息/etc/ssh/ssh_config

cl@CL:/etc/ssh$ cat ssh_config

# 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
#   ForwardX11Trusted yes
#   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
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Protocol 2
#   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
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
    SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes
    GSSAPIDelegateCredentials no
    KexAlgorithms +diffie-hellman-group1-sha1

附sshd配置文件信息/etc/ssh/sshd_config

cl@CL:/etc/ssh$ cat sshd_config
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
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
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

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

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin prohibit-password
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

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

附ssh_host_rsa指紋信息

cl@CL:/etc/ssh$ cat ssh_host_rsa_key.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRD/GdIFPSLRftmrOgoCLVmJFLc9ZzgeZCjfTdfOyD2BeZJ/1OhD9fH0dhFqJnuGcKYX2ZJz93s2L1ajcnF9mfeO/qHNOuWkyWYDOWYe/POg2fgngAy7ImG5262kqE4hHu1Op7kgfcBxCUwOo7aCaSnO7alle28yXkw7IAmrX3sOFEfO7J3NNZ08M66j6XioeXGDm3xM5/Mh5r/nuCDT/HdkjvKZjvAWo1heIf7cW7I83nSnGqM36TIkbVxz70PJM9sKNvy6dpF/HBq5NOyYLh8gmp6d/+7HMza6H5bsYHImp19Q4sUggQcyghK14L1TgGM5yUH+6n/1o0Rr2qSg3B root@CL

scp命令

scp命令可以進(jìn)行本地與遠(yuǎn)端之間的文件拷貝,基于ssh協(xié)議.

#copy 本地的文件到遠(yuǎn)程的機(jī)器上 
$ scp /etc/lilo.conf cl@192.168.125.111:/home/cl

會將本地的 /etc/lilo.conf 這個文件 copy 到 192.168.125.111,使用者 cl 的家目錄下。

#copy遠(yuǎn)程機(jī)器上的文件到本地來 
$ scp cl@192.168.125.111:/etc/lilo.conf  /etc

會將 192.168.125.111/etc/lilo.conf 文件copy 到本地的 /etc 目錄下。

#保持從來源 host 檔案的屬性 
$ scp –p cl@192.168.125.111:/etc/lilo.conf  /etc

若要cp目錄,使用-r選項

#拷貝遠(yuǎn)程設(shè)備/tmp/dir到/home/cl下
$ scp –r cl@192.168.125.111:/tmp/dir /home/cl
#拷貝本地設(shè)備`/home/cl/tmp`到/home/abc下
$ scp -r /home/cl/tmp  cl@192.168.125.111:/home/abc

在此必須注意使用者的權(quán)限是否可讀取遠(yuǎn)程上的檔案,若想知道更多關(guān)于 scp 的使用方法,請使用man scp

ssh協(xié)議實現(xiàn)

ssh協(xié)議典型實現(xiàn)是openssh, 輕量實現(xiàn)是dropbear.

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Linux系統(tǒng)
    +關(guān)注

    關(guān)注

    4

    文章

    587

    瀏覽量

    27177
  • DSA
    DSA
    +關(guān)注

    關(guān)注

    0

    文章

    47

    瀏覽量

    15045
  • SSH
    SSH
    +關(guān)注

    關(guān)注

    0

    文章

    178

    瀏覽量

    16226
  • Shell腳本
    +關(guān)注

    關(guān)注

    0

    文章

    36

    瀏覽量

    7944
  • SCP
    SCP
    +關(guān)注

    關(guān)注

    0

    文章

    28

    瀏覽量

    9181
收藏 人收藏

    評論

    相關(guān)推薦

    【linux篇】ubuntu下的SSH安裝與使用

    SSH是一種網(wǎng)絡(luò)協(xié)議,用于計算機(jī)之間的加密登錄。如果一個用戶從本地計算機(jī),使用SSH協(xié)議登錄另一臺遠(yuǎn)程計算機(jī)。常用于遠(yuǎn)程登陸Linux服務(wù)器,在Linux可以通過在終端通過ssh命令進(jìn)
    的頭像 發(fā)表于 06-13 09:07 ?3730次閱讀

    Linux命令詳解之scp命令

    Linux scp命令 通常用于Linux的跨主機(jī)之間的文件和目錄的復(fù)制。scp是 secure copy的縮寫, scp命令 是Linux 下基于ssh登陸進(jìn)行安全遠(yuǎn)程文件拷貝的命令
    發(fā)表于 08-27 13:41

    CAM填銅皮命令詳解

    CAM填銅皮命令詳解
    發(fā)表于 01-28 23:28 ?0次下載

    C2xx命令詳解

    C2xx命令詳解
    發(fā)表于 08-03 09:19 ?11次下載
    C2xx<b class='flag-5'>命令</b><b class='flag-5'>詳解</b>

    Linux關(guān)機(jī)命令詳解

    Linux關(guān)機(jī)命令詳解 在linux下一些常用的關(guān)機(jī)/重啟命令有shutdown、halt、reboot、及init,它們都 可以達(dá)到重啟系統(tǒng)的目的,但每個命令的內(nèi)部工作過程是不同的
    發(fā)表于 01-18 12:52 ?3584次閱讀

    Linux命令詳解 [兼容模式]pdf免費下載

    linux的命令詳解pdf,希望對大家有幫助!
    發(fā)表于 12-15 15:54 ?10次下載

    DOS-ping和ipconfig命令詳解下載

    DOS-ping和ipconfig命令詳解下載
    發(fā)表于 01-16 16:13 ?3次下載

    Linux系統(tǒng)中EXP命令詳解質(zhì)量匯總

    Linux系統(tǒng)中EXP命令詳解質(zhì)量匯總
    發(fā)表于 05-14 09:35 ?1次下載

    Linux虛擬機(jī)之tcpdump命令操作詳解

    Linux虛擬機(jī)之tcpdump命令操作詳解
    發(fā)表于 08-12 09:50 ?23次下載

    Backup-和Restore-命令詳解

    Backup-和Restore-命令詳解(力普拉斯電源技術(shù)有限公司)-文檔為Backup-和Restore-命令詳解總結(jié)文檔,是一份不錯的參考資料,感興趣的可以下載看看,,,,,,,,
    發(fā)表于 09-17 12:00 ?5次下載
    Backup-和Restore-<b class='flag-5'>命令</b><b class='flag-5'>詳解</b>

    Oracle-SQLPLUS基礎(chǔ)及sqlplus命令詳解

    Oracle-SQLPLUS基礎(chǔ)及sqlplus命令詳解(開關(guān)電源技術(shù)發(fā)展方向)-該文檔為Oracle-SQLPLUS基礎(chǔ)及sqlplus命令詳解文檔 ,是一份還算不錯的參考文檔,感興
    發(fā)表于 09-28 13:26 ?13次下載
    Oracle-SQLPLUS基礎(chǔ)及sqlplus<b class='flag-5'>命令</b><b class='flag-5'>詳解</b>

    保持SSH會話不掉線

    如果我們看到這個錯誤消息,意味著我們需要手動創(chuàng)建這個配置文件。如果 .ssh 目錄不存在,首先創(chuàng)建 .ssh 目錄,使用命令 mkdir $HOME/.ssh 來創(chuàng)建目錄,如果提示目錄
    的頭像 發(fā)表于 05-05 10:41 ?2380次閱讀

    kafka相關(guān)命令詳解

    kafka常用命令詳解
    的頭像 發(fā)表于 10-20 11:34 ?728次閱讀

    Ubuntu修改SSH默認(rèn)端口指南

    : 使用SSH登錄到您的Ubuntu服務(wù)器。如果您使用的是默認(rèn)端口22,命令可能如下(請?zhí)鎿Q 和 分別為您的服務(wù)器IP地址和用戶名): ssh @ 2、切換到超級用戶:sudo su 或者使用以下
    的頭像 發(fā)表于 12-21 17:27 ?1124次閱讀

    怎么用SSH密鑰登陸vps?

    、生成SSH密鑰對(在本地機(jī)器上執(zhí)行) 1、打開終端(命令行界面)。 2、輸入以下命令生成SSH密鑰對。將your_email@example.com替換為你的電子郵件地址,這將作為密
    的頭像 發(fā)表于 02-20 16:29 ?841次閱讀