Difference between revisions of "Linux Remote Shell Notes"
m |
m |
||
Line 1: | Line 1: | ||
+ | == use SSH to execute a remote command == | ||
+ | You'll need to be able to ssh with automated password or enter the password each time you run the command. | ||
+ | |||
+ | Execute a remote command on a host over SSH: | ||
+ | ssh nicolep@192.168.100.10 'reboot' | ||
+ | The example above will reboot the remote computer. | ||
+ | |||
+ | Multiple commands | ||
+ | ssh nicolep@192.168.100.10 'uptime; df -h' | ||
+ | |||
+ | Show the kernel version, number of CPUs and the total RAM: | ||
+ | ssh root@192.168.100.10 << EOF | ||
+ | uname -a | ||
+ | lscpu | grep "^CPU(s)" | ||
+ | grep -i memtotal /proc/meminfo | ||
+ | EOF | ||
+ | |||
+ | Here is how Nicole can execute her script on the remote computer | ||
+ | ssh nicolep@192.168.100.10 'bash -s' < nicolejob.sh | ||
+ | Nicole's script is local on her machine, and she executed it on the remote host. | ||
== auto login ssh == | == auto login ssh == |
Revision as of 12:01, 5 March 2018
Contents
use SSH to execute a remote command
You'll need to be able to ssh with automated password or enter the password each time you run the command.
Execute a remote command on a host over SSH:
ssh nicolep@192.168.100.10 'reboot'
The example above will reboot the remote computer.
Multiple commands
ssh nicolep@192.168.100.10 'uptime; df -h'
Show the kernel version, number of CPUs and the total RAM:
ssh root@192.168.100.10 << EOF uname -a lscpu | grep "^CPU(s)" grep -i memtotal /proc/meminfo EOF
Here is how Nicole can execute her script on the remote computer
ssh nicolep@192.168.100.10 'bash -s' < nicolejob.sh
Nicole's script is local on her machine, and she executed it on the remote host.
auto login ssh
The OpenSSH ssh utility doesn't accept a password on the command line or on its standard input. The nanny mentality whereby the developer protects us from ourselves by preventing us from doing something that compromises security, even though the result may be worse.
OPTIONS FOR SSH AUTOMATED
- Use an SSH key for authentication, instead of a password.
- Use sshpass, expect, or a similar tool to automate responding to the password prompt.
- Use the SSH_ASKPASS feature to get ssh to get the password by running another program.
- Use the insecure host-based authentication, sometimes common on private networks.
- Use a custom or modified ssh client adapted from source code, or one that allows for stored password.
sshpass
Install the sshpass utility so that you can automate ssh login including password.
apt install sshpass
Now you can automate the login process
sshpass -p "mysecretpass" ssh -o StrictHostKeyChecking=no nicolep@192.168.100.10
Custom port example:
sshpass -p "mysecretpass" ssh -o StrictHostKeyChecking=no nicolep@192.168.100.10:9600
public key authentication
In the source host run this only once:
ssh-keygen -t rsa
now press ENTER to every field
ssh-copy-id nicolep@192.168.100.10
shells that don't die when connection is lost
The two most common solutions are "screen" and "tmux." Screen has been around longer, tmux has some additional capabilities, and both will keep your terminal session alive even if your connection is lost.
Screen - Virtual Terminals From Console
Multiple virtual terminals from one terminal (like a remote shell), with the ability to detach processes and leave them running even when you disconnect.
tmux, the shell that doesn't die
tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.
tmux may be detached from a screen and continue running in the background, then later reattached.
- ssh into the remote machine
- start tmux by typing tmux into the shell
- start the process you want inside the started tmux session
- leave/detach the tmux session by typing Ctrl+b and then d
The tmux and GNU screen utilities have many similarities. tmux is considered more modern.
Each session is persistent and will survive accidental disconnection (such as ssh(1) connection timeout) or intentional detaching (with the ‘C-b d’ key strokes). tmux may be reattached using:
tmux attach
Example tmux commands include:
refresh-client -t/dev/ttyp2 rename-session -tfirst newname set-window-option -t:0 monitor-activity on new-window ; split-window -d bind-key R source-file ~/.tmux.conf \; \ display-message "source-file done"
Or from sh(1):
tmux kill-window -t :1 tmux new-window \; split-window -d tmux new-session -d 'vi /etc/passwd' \; split-window -d \; attach