logo
🐧 Linux 命令

SSH

SSH Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · Linux 命令🧭 Markdown 速查🏷️ 2 个标签
#ssh#remote
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Connecting

Connect to a server (default port 22)

SHELL
滚动查看更多
$ ssh root@192.168.1.5

Connect on a specific port

SHELL
滚动查看更多
$ ssh root@192.168.1.5 -p 6222

Connect via pem file (0400 permissions)

SHELL
滚动查看更多
$ ssh -i /path/file.pem root@192.168.1.5

See: SSH Permissions

Executing

Executes remote command

SHELL
滚动查看更多
$ ssh root@192.168.1.5 'ls -l'

Invoke a local script

SHELL
滚动查看更多
$ ssh root@192.168.1.5 bash < script.sh

Compresses and downloads from a server

SHELL
滚动查看更多
$ ssh root@192.168.1.5 "tar cvzf - ~/source" > output.tgz
SCP

Copies from remote to local

SHELL
滚动查看更多
$ scp user@server:/dir/file.ext dest/

Copies between two servers

SHELL
滚动查看更多
$ scp user@server:/file user@server:/dir

Copies from local to remote

SHELL
滚动查看更多
$ scp dest/file.ext user@server:/dir

Copies a whole folder

SHELL
滚动查看更多
$ scp -r user@server:/dir dest/

Copies all files from a folder

SHELL
滚动查看更多
$ scp user@server:/dir/* dest/

Copies from a server folder to the current folder

SHELL
滚动查看更多
$ scp user@server:/dir/* .
Config location
File PathDescription
/etc/ssh/ssh_configSystem-wide config
~/.ssh/configUser-specific config
~/.ssh/id_{type}Private key
~/.ssh/id_{type}.pubPublic key
~/.ssh/known_hostsKnown Servers
~/.ssh/authorized_keysAuthorized login key
SCP Options
OptionsDescription
scp -r<yel>R</yel>ecursively copy entire directories
scp -C<yel>C</yel>ompresses data
scp -vPrints <yel>v</yel>erbose info
scp -P 8080Uses a specific <yel>P</yel>ort
scp -B<yel>B</yel>atch mode (Prevents password)
scp -p<yel>P</yel>reserves times and modes
Config sample
TOML
滚动查看更多
Host server1
    HostName 192.168.1.5
    User root
    Port 22
    IdentityFile ~/.ssh/server1.key

Launch by alias

SHELL
滚动查看更多
$ ssh server1

See: Full Config Options

ProxyJump
SHELL
滚动查看更多
$ ssh -J proxy_host1 remote_host2
SHELL
滚动查看更多
$ ssh -J user@proxy_host1 user@remote_host2

Multiple jumps

SHELL
滚动查看更多
$ ssh -J user@proxy_host1:port1,user@proxy_host2:port2 user@remote_host3
ssh-copy-id
SHELL
滚动查看更多
$ ssh-copy-id user@server

Copy to alias server

SHELL
滚动查看更多
$ ssh-copy-id server1

Copy specific key

SHELL
滚动查看更多
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@server

SSH keygen

ssh-keygen
SHELL
滚动查看更多
$ ssh-keygen -t rsa -b 4096 -C "your@mail.com"

---
-tType of key
-bThe number of bits in the key
-CProvides a new comment

{.left-text}

Generate an RSA 4096 bit key with email as a comment

Generate

Generate a key interactively

SHELL
滚动查看更多
$ ssh-keygen

Specify filename

SHELL
滚动查看更多
$ ssh-keygen -f ~/.ssh/filename

Generate public key from private key

SHELL
滚动查看更多
$ ssh-keygen -y -f private.key > public.pub

Change comment

SHELL
滚动查看更多
$ ssh-keygen -c -f ~/.ssh/id_rsa

Change private key passphrase

SHELL
滚动查看更多
$ ssh-keygen -p -f ~/.ssh/id_rsa
Key type
  • rsa
  • ed25519
  • dsa
  • ecdsa
known_hosts

Search from known_hosts

SHELL
滚动查看更多
$ ssh-keygen -F <ip/hostname>

Remove from known_hosts

SHELL
滚动查看更多
$ ssh-keygen -R <ip/hostname>
Key format
  • PEM
  • PKCS8

SSH Tunneling

SSH Tunnel Options
OptionDescription
-LLocal port forwarding
-RRemote port forwarding
-DDynamic port forwarding
-fRun in background
-NDo not execute a remote command
-gAllow remote hosts to connect to local forwarded ports

{.left-text}

more details on flag above with man ssh

Local Port Forwarding
BASH
滚动查看更多
# Forward a local port to a remote server
ssh -L local_port:remote_host:remote_port user@ssh_server

# Example: Forward local port 8080 to remote port 80 on example.com
ssh -L 8080:example.com:80 user@ssh_server
Dynamic Port Forwarding
BASH
滚动查看更多
# Create a SOCKS proxy on a local port
ssh -D local_port user@ssh_server

# Create a SOCKS proxy on local port 1080 in the background
ssh -f -N -D 1080 user@ssh_server
Background Tunnel
BASH
滚动查看更多
# Create a local port forwarding tunnel in the background
ssh -f -N -L local_port:remote_host:remote_port user@ssh_server
Multiple Tunnels
BASH
滚动查看更多
# Create multiple tunnels in a single SSH command
ssh -L 8080:example.com:80 -L 3306:db.example.com:3306 user@ssh_server
Combining Options
BASH
滚动查看更多
# In background, no command, allow remote host, local port forward, identity_file
ssh -f -N -g -L 8080:example.com:80 -i ~/.ssh/custom_key user@ssh_server

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572