使用 SSH 安全远程访问
配置并保护 SSH,实现远程命令行访问、文件传输和端口转发
使用 SSH 安全远程访问 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Introduction to SSH
Secure Shell (SSH) is a powerful network protocol that lets you connect to remote computers securely. It creates an encrypted tunnel between your local machine and a remote server.
SSH is essential for:
- Executing commands on remote servers.
- Securely transferring files.
- Creating secure tunnels for other network services.
It's a foundational tool for managing Linux servers remotely and securely.
How SSH Works
SSH operates on a client-server model. Your local machine acts as the client, and the remote server runs an SSH daemon (the server software).
When you initiate a connection, the client and server perform a cryptographic handshake. This establishes a secure, encrypted channel, protecting all data exchanged from eavesdropping or tampering.
This strong encryption and identity verification make SSH safe to use even over untrusted networks like the internet.
Connecting Remotely
To connect to a remote server, you'll need its IP address or hostname, and a valid username on that server. The basic command is simple:
When connecting for the first time, SSH will prompt you to verify the host's identity and add its fingerprint to your ~/.ssh/known_hosts file. Always verify this fingerprint if possible!
ssh username@hostname_or_ipSSH Key-Based Authentication
While passwords work, SSH keys offer a more secure and convenient way to authenticate. They eliminate the need to type your password for every connection.
An SSH key pair consists of two parts:
- A private key: Kept secret on your local machine.
- A public key: Shared with remote servers you want to access.
During connection, the server uses your public key to encrypt a challenge. Only your private key can decrypt it, proving your identity without ever sending your password over the network.
Generating SSH Keys
You can generate your own SSH key pair on your local machine using the ssh-keygen command. It's recommended to use a strong passphrase to protect your private key.
The command will ask where to save the keys (default is ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub) and for an optional passphrase.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Example output:
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/user/.ssh/id_rsa):
# Enter passphrase (empty for no passphrase):
# Enter same passphrase again:
# Your identification has been saved in /home/user/.ssh/id_rsa
# Your public key has been saved in /home/user/.ssh/id_rsa.pubDeploying Your Public Key
To enable key-based authentication, your public key needs to be copied to the remote server. It should be placed in the ~/.ssh/authorized_keys file within your home directory on the remote server.
The simplest way to do this is using the ssh-copy-id utility:
Alternatively, you can manually copy the contents of your id_rsa.pub file and append it to the remote server's ~/.ssh/authorized_keys file.
ssh-copy-id username@hostname_or_ipSecure File Transfer with SCP
The Secure Copy Protocol (SCP) allows you to securely copy files and directories between local and remote hosts, or even between two remote hosts.
SCP uses SSH for data transfer and authentication, ensuring your files are encrypted throughout the process.
- To copy a local file to a remote server:
scp /path/to/local/file.txt user@example.com:/path/to/remote/SCP: Remote to Local
Similarly, you can easily copy files from a remote server to your local machine using SCP:
- To copy a remote file to your local machine:
Remember to specify the full path for both source and destination to avoid errors.
scp user@example.com:/path/to/remote/file.txt /path/to/local/SSH Port Forwarding (Local)
SSH can create secure "tunnels" to forward network traffic. Local port forwarding lets you access a service on a remote network as if it were running on your local machine.
This is extremely useful for accessing services behind a firewall or on an internal network.
Command syntax: ssh -L local_port:target_host:target_port user@ssh_server
Example: Access a web server (port 80) on internal_web_server from your local browser via localhost:8080.
ssh -L 8080:internal_web_server:80 user@jump_hostQuick Check: SSH Features
Which of the following statements about SSH are TRUE?
Recap: SSH for Secure Access
Great job! You've learned the essentials of SSH for secure remote access.
- We covered basic SSH connection using
ssh username@host. - Explored SSH key-based authentication, generating keys with
ssh-keygenand deploying them withssh-copy-id. - Understood how to securely transfer files using SCP.
- Learned about local port forwarding to tunnel traffic securely.
SSH is an indispensable tool for any developer or system administrator. Keep practicing these commands to master remote server management!
常见问题解答
「使用 SSH 安全远程访问」课时是免费的吗?
是的 — 「使用 SSH 安全远程访问」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。
「使用 SSH 安全远程访问」这节课中我会学到什么?
配置并保护 SSH,实现远程命令行访问、文件传输和端口转发 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Networking & TCP/IP for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「使用 SSH 安全远程访问」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?
能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 SSH 安全远程访问
- HTTP/HTTPS Web 服务
- DHCP 与 DNS 服务
- NTP 与时间同步服务