Networking and the Internet
SSH
Allows you to run commands on other machines, remotely. To do this we need to:
Generate a SSH key, it's essentially an authorisation key that acts as a handshake between devices.
dempsey$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dempsey/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dempsey/.ssh/id_rsa
Your public key has been saved in /home/dempsey/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:gACJxGl/ywpzZwlXnoeiwTs/GCmoTkbkeHP6O94PxiA dempsey
The key's randomart image is:
+---[RSA 3072]----+
|=+o |
|o+ . . . |
|..o . + o |
|+ = + = . |
|ooE % + S |
|o= % O |
|.o* O + |
|+ +.= . |
|.. .+oo.. |
+----[SHA256]-----+
The keys live in ~/.ssh/
the public key is in the id_rsa.pub file and the private key is in id_rsa.
Never give your private key to anyone
Useful info in ifconfig
Your IP address for a specific network is found at inet
.
netmask informs you which numbers locked in for other IP addresses on the network.
For example if netmask is 255.255.0.0.
Then every other IP on the network will have the same first 2 numbers.
If our inet was 128.34.5.2, then broadcast could be 128.34.23.1.
If you place your public key in the authorized_keys
(will have to make this file in ~/.ssh) file on the computer you are trying to connect to,
you will be able to connect to it as you have the private the key for that pair.
SFTP
SFTP => Secure File Transfer Protocol
You connect the same as SSH, you just need to have set up SSH already.
Have your public key in the authorized_keys
file on the remote machine.
Then you can connect to the remote machine using the following command.
stfp remote-machine@192.168.29.0
Useful commands
lls
- list local files
lpwd
- print local working directory
lcd
- change local directory
put
- upload file from local to remote
get
- download file from remote to local
You can run local commands on remote by prefixing with !
.
wget
A way of getting a remote file (e.g. from github) and downloading it to your local machine.
wget <URL>
It's like cp
for the internet
- is capabable of recursive downloads whereas curl is not
curl
Curl can do everything wget does (other than recursive downloads)
curl <URL>
Will output the contents of the file to stdout.
So what we can do is, say we are downloading a python file with curl.
curl <URL> > file.py
curl -o file.py <URL>
curl -O <URL>
This pipes the stdout from curl into a file... essentially doing what wget was doing. - The -o
flag allows you to specify the name of the file you want to save the output to. - The -O
flag will save the file with the same name as the remote file.
Curl is also capable of sending data to a server, and can be used to test APIs.
-d
'body' will implicitely send a POST request with the body specified. WARNING: When using curl be careful when ever using another persons command, especially if it pipes intobash
.
curl -sSL https://randomsite.com/install.sh | bash