quickreference:ssh
Differences
This shows you the differences between two versions of the page.
| — | quickreference:ssh [2025/01/05 07:45] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== ssh Quick Reference ====== | ||
| + | This is just some common tricks to use for ssh | ||
| + | |||
| + | ===== Passwordless Logins ===== | ||
| + | |||
| + | Sometimes, you need to be able to have an automated process log in to a //Target// machine from a //Source// machine. In most cases, this is done as the root user. This decreases security somewhat since if your //Source// machine is compromised, | ||
| + | |||
| + | The best way to do it is to use the // | ||
| + | |||
| + | First, on //Target//, create a public key pair. First, look if you already have a public key pair as we might not want to overwrite it. | ||
| + | |||
| + | <code bash> | ||
| + | ls /root/.ssh | ||
| + | </ | ||
| + | |||
| + | If that does not have id_rsa and id_rsa.pub, or if you want to trash those, then we're good. If they exist, you can either use ' | ||
| + | |||
| + | <code bash> | ||
| + | ssh-keygen -t rsa -b 4096 | ||
| + | </ | ||
| + | |||
| + | This creates a file, / | ||
| + | |||
| + | On //Target//, do the following: | ||
| + | |||
| + | <code bash> | ||
| + | mkdir -p /root/.ssh | ||
| + | joe / | ||
| + | # place contents of id_rsa.pub on a separate line, then exit the editor | ||
| + | chown -fR root:root /root/.ssh | ||
| + | chmod 700 /root/.ssh | ||
| + | chmod 600 / | ||
| + | </ | ||
| + | |||
| + | From // | ||
| + | |||
| + | ==== Limit to a single program ==== | ||
| + | |||
| + | A more secure way to do this is to limit the command available. In this case, edit // | ||
| + | |||
| + | < | ||
| + | command="/ | ||
| + | </ | ||
| + | |||
| + | Upon login, / | ||
| + | |||
| + | <code perl access_control.pl> | ||
| + | #! / | ||
| + | use warnings; | ||
| + | use strict; | ||
| + | |||
| + | # get IP address | ||
| + | my $realIP = $ENV{' | ||
| + | $realIP =~ m/ | ||
| + | $realIP = $1; | ||
| + | |||
| + | # and hostname | ||
| + | my ($hostname, | ||
| + | # if $temp exists, it is the IP | ||
| + | $realIP = $temp if $temp; | ||
| + | |||
| + | die "You must send hostname with command\n" | ||
| + | |||
| + | # do whatever you want here. | ||
| + | # look through $ENV to see what you can access | ||
| + | |||
| + | my $OK = & | ||
| + | |||
| + | die unless $OK; | ||
| + | |||
| + | 1; # we made it here, so they can issue the command | ||
| + | </ | ||
| + | |||
| + | ===== Port Forwarding ===== | ||
| + | |||
| + | ssh has the ability to forward an IP:port //as seen by the local machine// to an IP:port //as seen by the remote machine//. The most common occurrence of this would be to be able to access an internal web site when you ssh into a remote machine that is on that internal network. | ||
| + | |||
| + | The syntax uses the **-L** parameter to ssh, in the form | ||
| + | < | ||
| + | Remember, the local port is as seen by the local machine, and the remote ip/port is as seen by the remote machine. Let's use an example where we want to hit an internal web site where we have remote ssh capabilities. We can log into the remote server as | ||
| + | < | ||
| + | ssh username@joe.example.org | ||
| + | </ | ||
| + | |||
| + | The internal IP of joe.example.org is 192.168.1.5, | ||
| + | |||
| + | <code bash> | ||
| + | ssh -L localhost: | ||
| + | # or, you can leave off the first IP and localhost is assumed | ||
| + | ssh -L 8080: | ||
| + | </ | ||
| + | |||
| + | When this connection is made, any traffic going to localhost: | ||
| + | |||
| + | < | ||
| + | https:// | ||
| + | </ | ||
| + | |||
| + | and see the normally inaccessible web site on the remote network. | ||
| + | |||
| + | **Note**: You should not try to use a port that is already being used on your machine. So, for example, if you have a web server running on your local machine at port 8080, ssh can get very confused. In that case, you would want to use another port. You can use any unused port between 1025 and 65535 (don't know about the first and last ones there). | ||
| + | |||
| + | ===== Relay Port Forwarding ===== | ||
| + | |||
| + | I don't know the actual term for this, but we can forward a port to some machine, then forward that port to still another one. In this case, we have jane.example1.org, | ||
| + | |||
| + | <code bash> | ||
| + | ssh -L localhost: | ||
| + | # we make the connection to jane and get a command prompt | ||
| + | ssh -L localhost: | ||
| + | # we are now on john, and 3389 from jane is forwarded to windows | ||
| + | # server at 192.168.1.10 | ||
| + | </ | ||
| + | In this case, we have said //any traffic for port 3389 on my local machine is forwarded to localhost port 3389 on jane// in the first command. | ||
| + | |||
| + | The second ssh command says //any traffic for port 3389 on my local machine (jane) is forwarded to port 3389 on the machine on my same subnet at 192.168.1.10 on port 3389// | ||
| + | |||
| + | You can now open an rdp client on your local machine to connect to localhost: | ||
| + | |||
| + | ===== Shared ssh configuration ===== | ||
| + | |||
| + | The simplest way to share ssh configurations with a group is to have a shared group of ssh configuration files. You might go so far as to have one file per client, with each file containing the config information to make an ssh connection to various machines within a client' | ||
| + | |||
| + | In this example, we will have multiple files with a .ssh suffix. A sample file would be | ||
| + | <code sshconfig> | ||
| + | # acme.ssh | ||
| + | # ssh configuration file for client Acme | ||
| + | Host | ||
| + | Hostname 192.168.100.3 | ||
| + | User tech | ||
| + | |||
| + | Host | ||
| + | Hostname 192.168.100.1 | ||
| + | User | ||
| + | Port 2222 | ||
| + | </ | ||
| + | A second file might be | ||
| + | <code sshconfig> | ||
| + | # hal.ssh | ||
| + | # ssh configuration file for client HAL | ||
| + | Host | ||
| + | Hostname 192.168.55.1 | ||
| + | User root | ||
| + | Port 22 | ||
| + | |||
| + | Host | ||
| + | Hostname fileserver.hal.local | ||
| + | Port 22 | ||
| + | </ | ||
| + | Save these two files in a location shared with the relevant technicians who require access. This can be done via an NFS share, a local directory on a terminal server, or even sync' | ||
| + | |||
| + | On each system, modify the system wide ssh configuration to include all files matching *.ssh. NOTE: This could also be achieved by adding the Include into individual users .ssh/config files. | ||
| + | <code bash> | ||
| + | echo ' | ||
| + | </ | ||
| + | |||
| + | ==== Discussion ==== | ||
| + | |||
| + | * Permissions should be fairly strict, though it is not as strictly enforced as some aspects of the ssh system. | ||
| + | * All directories and files should be owned by root | ||
| + | * files should be 644 (rw_rw_rw_) | ||
| + | * directories 755 (rwxr_xr_x). | ||
| + | * This allows all users to read the files but only root can modify them. | ||
| + | * The .ssh suffix is only one convention; not a requirement or any kind of magic. By using " | ||
| + | * Users can still have their own .ssh/config files for private configurations | ||
| + | * Adding a client prefix with a period in between is simply convenient. In the example, you see that both hal and acme have a connection named router. **Host** is what you type, **Hostname**, | ||
| + | * Host and Hostname are the only required fields. If no user is specified in the configuration, | ||
| + | * The example of having one file per client/ | ||
| + | |||
| + | ===== Shared authorized_keys file ===== | ||
| + | |||
| + | Similar and complementary to the shared configuration above is the ability to have the same set of authorized_keys files available across multiple machines. This is especially important in clusters of servers, where all members of the cluster need to have the same basic set of public keys which are allowed to access them without a password. | ||
| + | |||
| + | Again, share a single file across all machines with the public keys that are allowed to access all machines in the cluster. It does **not** hurt to have the public key of the current machine in this file, so no modification is necessary to ensure a machine does not have its own public key. | ||
| + | |||
| + | An NFS mount is likely the best way for to set this up. I'm going to assume it is mounted at / | ||
| + | |||
| + | Add/replace the AuthorizedKeysFile directive in / | ||
| + | <code bash> | ||
| + | # Debian style | ||
| + | echo ' | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | Reload the sshd daemon. | ||
| + | |||
| + | ==== Discussion ==== | ||
| + | * Permissions very important here. | ||
| + | * All files and directories must be owned by root | ||
| + | * Directories must be 755, and if possible, 700 | ||
| + | * Files must be 644, and if possible, 600 | ||
| + | * Users may still have a personal .ssh/ | ||
| + | * Some systems are set for .ssh/ | ||
| + | * This does **not** include the known_hosts file, so the initial connection must still be made. However, the documentation in sshd_config implies this can be accomplished somehow. | ||
| + | |||
| + | ===== Links ===== | ||
| + | |||
| + | * https:// | ||
quickreference/ssh.1564879264.txt.gz · Last modified: (external edit)
