Connect to a remote server with SSH
Learn how to use SSH to securely connect to remote servers and run commands.
Lesson Content
To connect to a remote server, use the ssh command followed by the server's IP address or hostname:
ssh example.comWhen you provide only the hostname, SSH uses the same username as your local machine. That usually isn't the username you need on the remote server. Prefix the hostname with the remote username and an @ symbol:
ssh [email protected]This is the standard SSH connection format, and it's often all you need.
SSH servers typically listen on port 22, but a server may use a different port instead. For example, if the server listens on port 2022, add the -p option followed by the port number:
ssh -p 2022 [email protected]You can also specify which private SSH key to use with the -i option. This lets you use different keys for different hosts:
ssh -i ~/.ssh/remote-server [email protected]You can combine the username, port, and identity file options when the server requires all three:
ssh -i ~/.ssh/remote-server -p 2022 [email protected]After you connect, SSH opens a Bash shell on the remote server. If you only need to run one command, add that command after the connection details:
ssh [email protected] hostnameSSH runs the command on the remote server and exits when the command finishes. This is useful for one-off commands, short Bash scripts, and other tasks that don't require an interactive session.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.