SSH Commands I Often Use#
This article was generated using OpenCat + ChatGPT.
SSH is a commonly used network protocol that allows you to connect to remote servers, execute commands, transfer files, and more. Here are the SSH commands I often use:
1. Remote Port Forwarding#
Remote port forwarding allows you to forward a specific port on a remote server to a local port, allowing your local computer to access applications on the remote server, improving access speed and security.
ssh -f -N -R remote_port:local_ip:local_port user@remote_server
In this command, remote_port
is the port on the remote server to be forwarded, local_ip
and local_port
are the IP address and port on your local computer, user
is the username of the remote server, and remote_server
is the address of the remote server.
2. Local Port Forwarding#
Local port forwarding allows you to forward a specific port on your local computer to a remote server, allowing the remote server to access applications on your local computer, improving access speed and security.
ssh -f -N -L local_port:remote_ip:remote_port user@remote_server
In this command, local_port
is the port on your local computer to be forwarded, remote_ip
and remote_port
are the IP address and port on the remote server, user
is the username of the remote server, and remote_server
is the address of the remote server.
3. Proxy#
A proxy can be used to connect to a remote server via SSH and forward traffic to your local computer, allowing you to access remote websites, improving access speed and security.
ssh -D local_port user@remote_server
In this command, local_port
is the port on your local computer, user
is the username of the remote server, and remote_server
is the address of the remote server.
4. Copy File to Local Computer#
You can copy files from a remote server to your local computer.
scp user@remote_server:/path/to/remote/file /path/to/local/file
In this command, user
is the username of the remote server, remote_server
is the address of the remote server, /path/to/remote/file
is the file path on the remote server, and /path/to/local/file
is the file path on your local computer.
5. Copy File to Remote Server#
You can copy files from your local computer to a remote server.
scp /path/to/local/file user@remote_server:/path/to/remote/file
In this command, /path/to/local/file
is the file path on your local computer, user
is the username of the remote server, remote_server
is the address of the remote server, and /path/to/remote/file
is the file path on the remote server.
6. Copy File from Remote Server to Remote Server via Local Computer#
You can copy files from remote server 1 to remote server 2 via your local computer.
scp user1@remote_server1:/path/to/remote/file user2@remote_server2:/path/to/remote/file
In this command, user1
is the username of remote server 1, remote_server1
is the address of remote server 1, /path/to/remote/file
is the file path on remote server 1, user2
is the username of remote server 2, remote_server2
is the address of remote server 2, and /path/to/remote/file
is the file path on remote server 2.
7. Copy File from Remote Server to Remote Server, Peer-to-Peer#
You can directly copy files from remote server 1 to remote server 2, achieving peer-to-peer file transfer.
scp -3 user1@remote_server1:/path/to/remote/file user2@remote_server2:/path/to/remote/file
In this command, user1
is the username of remote server 1, remote_server1
is the address of remote server 1, /path/to/remote/file
is the file path on remote server 1, user2
is the username of remote server 2, remote_server2
is the address of remote server 2, and /path/to/remote/file
is the file path on remote server 2.
In summary, using SSH commands allows for easy connection to remote servers, execution of commands, file transfer, and more, improving work efficiency and security.
More#
View more SSH-related articles on xlog
View more SCP-related articles on xlog