### SSH (Secure Shell Protocol) - A method for secure remote login from one computer to another > \# Add information about local and target to define which machines * Find out your username `$ whoami` * Check ip address > NOTE: - lo usually stands for the loopback interface (localhost) - wlan usually stands for a wireless networking interface - rmnet interfaces are usually associated with cellular connections and usb tethering * Linux `$ ip addr show | grep "inet "` `$ ifconfig` `$ nmcli -p device show` `$ ip route get 1.2.3.4 | awk '{print $7}'` `$ curl ifconfig.me` `$ ifconfig 2> /dev/null | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '[0-9.]*'` * Windows `$ ipconfig` > NOTE: The IP Address we use will usually be the one not similar to 127.0.0.1 (localhost) but rather a different string of numbers, ignore any forward slash and the numbers after it > Example: > `$ ip addr show | grep "inet "` ```bash inet 127.0.0.1/8 scope host lo inet 10.1.1.5/27 brd 10.1.1.31 [...] ``` > The `10.1.1.5` is what we want in this example * See if any current ssh is running `$ ps aux | grep sshd` * Terminate sshd - See all sshd connections, if non-zero number then there are ssh processes running `$ pgrep -c sshd` `$ pkill --signal HUP sshd` - Check connections again to make sure, if still not 0 then use this `$ pkill --signal KILL` * Disconnect specific ssh clients off from server 1. First see who's logged into the the machine `$ who` or `$ w`# if there is anything besides tty1, example `pts/3` then there is an active connection > NOTE will not work for Termux since it does not support `utmp / wtmp / btmp` files 2. Look for specific process ID `PID` `$ ps t` 3. (Optional) Laugh at their impending disconnection `$ echo "HAHAHAHAHAHAHAHA" | write pts/` 4. Kill the process `$ kill -9 30737` * If `sshd` on remote server produces the following error message: `sshd re-exec requires execution with an absolute path` then use this `/usr/sbin/sshd` * If `sshd` on remote server produces the following error message: `sshd: no hostkeys available -- exiting` then use this `ssh-keygen -A` * For Termux users: > NOTE: ip addr will not work unless phone is rooted, but `ifconfig` will from `net-tools` since the `proc_net` and `proc_net_tcp_udp` files are not accessible to `untrusted_app` domain but are allowed for `shell` (`adb`). * Termux install openssh `$ pkg in openssh` * Termux make sure user/root password is set `$ passwd # root does not need to be specified` * Generate your ssh key pair on remote host machine, not on server machine `$ ssh-keygen` * Accessing termux user environment from other consoles `$ scp your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS:~/.ssh/id_rsa.pub ~/.ssh/authorized_keys` * Second Option How do I add SSH Keys to authorized_keys file? `$ cat ~/.ssh/id_rsa.pub | ssh your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"` * Start server in machine that will ssh into `$ sshd` * Send authorization to the remote target so we can ssh into the machine `$ ssh-copy-id -p 8022 -i ~/.ssh/id_rsa.pub user@ip.address` * Android remote host into desktop server, assuming default port 8022 `$ ssh your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS -p 8022` * Desktop remote host into android server, assuming default port 8022 `$ ssh your_android_ssh_user@YOUR.ANDROID.IP.ADDRESS` * To view the log output from the server process. `$ logcat -s 'syslog:*'` #### Copying files ##### (SCP Secure Copy Protocol) - Syntax: ```bash scp [OPTIONS] [[user@]src_host:]file1 [[user@]dest_host:]file2 ``` ```bash scp file @: ``` How to SSH File Transfer from Remote to Local 1. Copy single file from local to remote using scp. `$ scp myfile.txt remoteuser@remoteserver:/remote/folder/` 2. scp from remote to local on local using a single file. `$ scp -P 22 remoteserver:/remote/folder/remotefile ~/localdir/localfile` * Copy multiple files from remote to local on locall: `$ scp your_username@remote.edu:/some/remote/directory/\{a,b,c\} ./` * Copy multiple files from local to remote: `$ scp foo.txt bar.txt your_username@remotehost.edu:~` `$ scp {foo,bar}.txt your_username@remotehost.edu:~` `$ scp *.txt your_username@remotehost.edu:~` * Copy multiple files from remote to remote: `$ scp your_username@remote1.edu:/some/remote/directory/foobar.txt \` 3. Move 'r' using scp from remote to local on local using a single file. `$ scp -r -P 22 remoteserver:/remote/folder/remotefile ~/localdir/localfile`