This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cheatsheet:ssh [2023/02/16 19:52] – [ssh command cheatsheet] kamaradski | cheatsheet:ssh [2023/02/21 12:24] (current) – kamaradski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== ssh command cheatsheet ====== | ====== ssh command cheatsheet ====== | ||
| - | Related folders: | + | ===== what are ssh and sshd ===== |
| - | /etc/ssh/ => OS level setup and configuration | + | |
| - | ~/ | + | SSH (Secure Shell) is a network protocol that provides secure communication between two computers, typically a client and a server. It is commonly used to enable remote access and management of a server or other networked device. The SSH protocol encrypts all traffic between the client and the server, making it resistant to eavesdropping, |
| + | |||
| + | To establish an SSH connection, a client program sends a request to the server to initiate the SSH protocol. The server runs the SSH daemon (sshd) that listens for incoming SSH connections on a designated port (usually 22). Once a connection is established, | ||
| + | |||
| + | The SSH protocol supports a variety of authentication methods, including password authentication and public key authentication. With password authentication, | ||
| + | |||
| + | The SSH and SSHD services provide a secure way to manage remote systems, transfer files securely, and execute remote commands securely over the network. These services are widely used in various environments, | ||
| + | |||
| + | |||
| + | ===== connect to a remote server ===== | ||
| + | <code bash> | ||
| + | $ ssh username@hostname | ||
| + | </code> | ||
| + | |||
| + | ===== connect using a non-default SSH port ===== | ||
| + | <code bash> | ||
| + | $ ssh -p < | ||
| + | </code> | ||
| + | |||
| + | ===== connect using a specific identity file ===== | ||
| + | <code bash> | ||
| + | $ ssh -i /path/ | ||
| + | </ | ||
| + | |||
| + | ===== run a remote command ===== | ||
| + | <code bash> | ||
| + | $ ssh username@hostname " | ||
| + | </ | ||
| + | |||
| + | ===== keys and certificates ===== | ||
| + | |||
| + | ==== create id_rsa ssh key ==== | ||
| + | <code bash> | ||
| + | $ ssh-keygen -t rsa | ||
| + | or | ||
| + | $ ssh-keygen -t rsa -b 4096 -C "< | ||
| + | </ | ||
| + | |||
| + | ==== copy local public key to remote " | ||
| + | <code bash> | ||
| + | $ ssh-copy-id -i ~/.ssh/id_rsa.pub -p 1234 user@host | ||
| + | </code> | ||
| + | |||
| + | ==== remove a specific | ||
| + | <code bash> | ||
| + | $ ssh-keygen -f "/ | ||
| + | |||
| + | # Host 10.0.0.10 found: line 39 | ||
| + | # Host 10.0.0.10 found: line 40 | ||
| + | / | ||
| + | Original contents retained as / | ||
| + | </ | ||
| ===== create " | ===== create " | ||
| Line 29: | Line 81: | ||
| $ scp / | $ scp / | ||
| </ | </ | ||
| + | |||
| + | ===== start/ | ||
| + | |||
| + | Ubuntu, Debian, CentOS, Fedora and other systems that use systemd | ||
| + | <code bash> | ||
| + | sudo systemctl start|restart|stop ssh|sshd | ||
| + | </ | ||
| + | |||
| + | Systems that use SysV init: | ||
| + | <code bash> | ||
| + | sudo service ssh|sshd start|restart|stop | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== important files ===== | ||
| + | |||
| + | ==== ~/ | ||
| + | |||
| + | Is a system file that contains a list of public keys for hosts that the user has previously connected to via SSH. When a user connects to a remote host using SSH for the first time, the remote host's public key is added to this file, and on subsequent connections, | ||
| + | |||
| + | ==== ~/ | ||
| + | A file that contains a list of public keys that are authorized to access an SSH user account. When a user wants to set up passwordless authentication to a remote host, they can add their public key to the authorized_keys file on the remote host. When the user attempts to connect to the remote host via SSH, the remote host checks the authorized_keys file to see if the user's public key is listed. If the key is found, the user is granted access to the account without needing to enter a password. This file is a critical part of SSH security as it allows for secure remote access to a system without the need for password-based authentication. Meaning: this keeps track of the inbound SSH calls where you login to your own user from another machine. | ||
| + | |||
| + | ==== ~/ | ||
| + | This is a configuration file that allows users to specify SSH options for various hosts, as well as create shortcuts for commonly used options. It provides a way to simplify the process of connecting to remote hosts by allowing users to set up SSH options once and reuse them for future connections. For example, users can specify options such as the hostname, port number, username, and identity file, as well as advanced options such as TCP keepalive and SSH agent forwarding. | ||
| + | |||
| + | Additionally, | ||
| + | |||
| + | ==== / | ||
| + | This is a system-wide configuration file that sets the default options for the SSH client. It applies to all SSH connections initiated from the local machine, regardless of the user account used to establish the connection. The options set in this file can be overridden by user-specific options in the " | ||
| + | |||
| + | The ssh_config file includes a variety of configuration options that allow administrators to specify various parameters for SSH connections. For example, it allows setting options such as the default port number for SSH connections, | ||
| + | |||
| + | This file also supports conditional settings, which enable administrators to specify different options based on the context of the connection. For example, options can be set based on the originating IP address, the hostname of the remote machine, or other criteria. Overall, the ssh_config file provides a powerful and flexible way to manage SSH connections and security settings across an entire system. | ||
| + | |||
| + | ==== / | ||
| + | sshd_config is the system-wide configuration file for the OpenSSH daemon (sshd) that listens for incoming SSH connections on a server. It sets the default options for the SSH server and applies to all incoming SSH connections, | ||
| + | |||
| + | The sshd_config file includes a wide range of configuration options that enable system administrators to specify various parameters for SSH server connections. For example, it allows setting options such as the default port number for SSH connections, | ||
| + | |||
| + | The file also supports various security-related settings, such as setting restrictions on the users who can connect to the server, limiting the number of concurrent connections, | ||
| + | |||
| + | Similar to the ssh_config file, the sshd_config file supports conditional settings that enable administrators to specify different options based on the context of the connection. For example, options can be set based on the originating IP address, the hostname of the remote machine, or other criteria. | ||
| + | |||
| + | Overall, the sshd_config file is an essential tool for system administrators to manage and configure SSH server connections and maintain a secure and reliable remote access environment. | ||
| + | |||
| + | |||
| + | ==== logfiles ==== | ||
| + | |||
| + | The related log files for SSH and SSHD can also vary depending on the operating system and version, but here are the common ones for several popular Linux distributions: | ||
| + | |||
| + | Ubuntu, Debian, and other systems that use systemd: | ||
| + | The SSH log file is typically located at ''/ | ||
| + | |||
| + | CentOS, Fedora, and other systems that use systemd: | ||
| + | The SSH log file is typically located at ''/ | ||
| + | |||
| + | Systems that use SysV init: | ||
| + | The SSH log file is typically located at ''/ | ||
| + | |||
| + | In general, the SSH log file records information about SSH client connections, | ||
| + | |||
| ===== tunneling ===== | ===== tunneling ===== | ||
| + | The SSH command with the -L and -D options allows you to create secure connections between two computers, which can be useful in many situations. | ||
| + | |||
| + | The -L option creates a secure tunnel that connects your computer to another computer through a third computer. This can be helpful when you need to access a computer that's behind a firewall or is not accessible from your computer directly. | ||
| + | |||
| + | For example, imagine you need to access a computer at work, but it's only accessible from your work network. By creating an SSH tunnel with the -L option, you can connect to a different computer that is accessible from your computer, and that computer will connect to the work computer for you. | ||
| + | |||
| + | The -D option creates a different type of tunnel that lets you use a remote computer as a proxy. This is useful if you want to access resources that are only available from a remote computer. | ||
| + | |||
| + | For example, imagine you need to access a website that is only available from a computer in a different country. By creating an SSH tunnel with the -D option, you can use the remote computer as a proxy to access the website from your computer. | ||
| + | |||
| + | Overall, the -L and -D options are powerful tools that allow you to securely connect to remote computers and access resources that might otherwise be unavailable to you. | ||
| + | |||
| + | |||
| + | ==== -D option (dynamic port forwarding) (SOCKS proxy) ==== | ||
| + | Let's say you're traveling abroad and need to access a website that's only accessible from your home country. By using the -D option, you can create a SOCKS proxy that routes all your web traffic through a remote computer in your home country. | ||
| - | ==== open SOCKS tunnel (used for browsing for example) ==== | + | The command to create the proxy would look like this: |
| <code bash> | <code bash> | ||
| - | $ ssh -D 6666 -vTCN username@sshserver.domain.com -p1234 | + | ssh -D 1080 user@home.com |
| # where: | # where: | ||
| - | # -D 6666 is the local port to use (pick open port at will) | + | # -D 1080 is the local port to use (pick any open port [above 1024] at will) |
| - | # -p 1234 is the remote port at which the remote ssh-server is listening for you | + | |
| </ | </ | ||
| - | ==== tunnel | + | In this example, you're connecting to the " |
| - | This is very useful is you want to tunnel into an ssh jumphost in a remote network, and then reach a service on another server | ||
| - | inside the target network. For example, you can tunnel a mail local mail client, via an ssh jumphost, to a private email server somewhere on the remote network. | ||
| + | ==== -L option (remote port forwarding) ==== | ||
| + | Let's say you have a server that's only accessible from your office network, but you need to access it from your home computer. By using the -L option, you can create a secure tunnel that connects your home computer to the server through another computer that's accessible from your home computer. | ||
| + | |||
| + | The command to create the tunnel would look like this: | ||
| <code bash> | <code bash> | ||
| - | ssh -L 6666:192.168.1.100:1234 usnername@ssh-jumphost.domain.com -p 8080 -vTN | + | ssh -L 8888:server.example.com:22 user@accessible.com |
| # where | # where | ||
| - | # 6666 is the local port to use (pick open port at will) | + | # 8888 is the local port to use (pick any open port [above 1024] at will) |
| - | # 192.168.1.100 is the ip-address of the remote | + | # server.example.com is the target |
| - | # 1234 is the remote port to use on the remote | + | # 22 is the remote port we want to reach on the target |
| - | # -p 1234 is the remote port at which the remote ssh-server is listening | + | # accessible.com |
| + | </ | ||
| - | ssh -L opens a local port. Everything that you send to that port is put through the ssh connection and leaves through | + | In this example, you're connecting to the " |
| - | If you do, e.g., ssh -L 4444:google.com:80, if you open http:// | + | |
| - | ssh -D opens a local port, but it doesn' | + | |
| - | If you open, e.g., ssh -D 7777, when you tell your browser to use localhost: | + | ===== SSHd settings ===== |
| - | requests goes through the ssh tunnel. To the public internet, it's as if you were browsing from your ssh server instead of from your computer. | + | |
| + | Related file: /etc/ssh/ | ||
| + | |||
| + | ==== disable the use of passwords to login ==== | ||
| + | Note: make sure you have a working key-based login in place before doing this (or local access via a real keyboard/ | ||
| + | |||
| + | < | ||
| + | ChallengeResponseAuthentication no | ||
| + | PasswordAuthentication no | ||
| + | UsePAM no | ||
| </ | </ | ||
| + | |||
| + | ==== disable the root account from logging in ==== | ||
| + | |||
| + | < | ||
| + | PermitRootLogin no | ||
| + | # | ||
| + | </ | ||
| + | |||