How to use the command `mosh` (with examples)
Mobile Shell (mosh
) is a robust and responsive replacement for SSH that allows users to connect to remote servers and persist connections while roaming between networks.
Use case 1: Connect to a remote server
Code:
mosh username@remote_host
Motivation: Use this command when you want to establish a secure shell connection to a remote server.
Explanation: The mosh
command is followed by the username and the address of the remote server. This command initiates a connection to the remote server using the default SSH protocol.
Example output:
Connecting to remote_host (IP_ADDRESS)...
Use case 2: Connect to a remote server with a specific identity (private key)
Code:
mosh --ssh="ssh -i path/to/key_file" username@remote_host
Motivation: Use this command when you want to connect to a remote server using a specific private key instead of the default SSH key.
Explanation: The mosh
command is followed by the --ssh
flag, which allows you to specify the SSH command to use. In this case, the -i
argument is used to specify the identity file (private key) to use for authentication.
Example output:
Connecting to remote_host (IP_ADDRESS)...
Use case 3: Connect to a remote server using a specific port
Code:
mosh --ssh="ssh -p 2222" username@remote_host
Motivation: Use this command when you want to connect to a remote server using a non-default SSH port.
Explanation: The mosh
command is followed by the --ssh
flag, which allows you to specify the SSH command to use. In this case, the -p
argument is used to specify the port number for the SSH connection.
Example output:
Connecting to remote_host (IP_ADDRESS)...
Use case 4: Run a command on a remote server
Code:
mosh remote_host -- command -with -flags
Motivation: Use this command when you want to run a specific command on a remote server.
Explanation: The mosh
command is followed by the address of the remote server, the --
delimiter, and the command to be executed on the remote server. Any additional flags or arguments for the command can be specified after the double dash.
Example output:
Running command on remote_host...
Use case 5: Select Mosh UDP port
Code:
mosh -p 124 username@remote_host
Motivation: Use this command when the remote host is behind a NAT and you need to specify a non-default Mosh UDP port.
Explanation: The mosh
command is followed by the -p
flag, which allows you to specify the Mosh UDP port to use for the connection.
Example output:
Connecting to remote_host (IP_ADDRESS)...
Use case 6: Usage when mosh-server
binary is outside standard path
Code:
mosh --server=path/to/bin/mosh-server remote_host
Motivation: Use this command when the mosh-server
binary is not located in the standard path and you need to specify the full path to the binary.
Explanation: The mosh
command is followed by the --server
flag, which allows you to specify the path to the mosh-server
binary. This is useful when the binary is not in the standard search path.
Example output:
Connecting to remote_host (IP_ADDRESS)...
Conclusion
The mosh
command is a powerful tool for establishing secure shell connections to remote servers and maintaining persistent connections while roaming between networks. By using the various command options and arguments, users can customize their connections according to their specific requirements, such as specifying private keys, ports, or custom server paths.