Useful Commands
🛠

Useful Commands

Linux

Curl

Show HTTP version
Bash
curl -k -sI https://127.0.0.1:8000

HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 10
Date: Sat, 01 Feb 2020 09:45:54 GMT
 
Bash
- Download the contents of a URL to a file:
    curl http://example.com --output path/to/file

- Download a file, saving the output under the filename indicated by the URL:
    curl --remote-name http://example.com/filename

- Download a file, following location redirects, and automatically continuing (resuming) a previous file transfer and return an error on server error:
    curl --fail --remote-name --location --continue-at - http://example.com/filename

- Send form-encoded data (POST request of type `application/x-www-form-urlencoded`). Use `--data @file_name` or `--data @'-'` to read from STDIN:
    curl --data 'name=bob' http://example.com/form

- Send a request with an extra header, using a custom HTTP method:
    curl --header 'X-My-Header: 123' --request PUT http://example.com

- Send data in JSON format, specifying the appropriate content-type header:
    curl --data '{"name":"bob"}' --header 'Content-Type: application/json' http://example.com/users/1234

- Pass a username and password for server authentication:
    curl --user myusername:mypassword http://example.com

- Pass client certificate and key for a resource, skipping certificate validation:
    curl --cert client.pem --key key.pem --insecure https://example.com

- Request the first 100 bytes
		curl -r 0-99 http://exmaple.com

- Send delete request
		curl -X DELETE https://www.baidu.com
 
Advanced usage
Output the entire process of a request communication
Bash
curl -v https://www.google.com
 

systemctl

The systemctl command is used for to init system and service manager.
Starting and Stopping Services
Start/Stop a Service immediately.
Shell
systemctl start/stop [service_name]
Enabling and Disabling Services
Enable/Dsiable a Service to start on boot.
Shell
systemctl enable [service_name]
Checking Service Status
Check Status: Check the status of a service.
Shell
systemctl status [service_name]
Managing the Systemd System
  1. Reload Systemd: Reload the systemd manager configuration.
    1. Shell
      systemctl daemon-reload
  1. Reboot the System: Use systemctl to reboot the system.
    1. Shell
      systemctl reboot
Listing Services
List Active Services: List all active services.
Shell
systemctl list-units --type=service
 
Other Useful Commands
Mask/Unmask a Service: Prevent a service from being started manually or automatically (mask) or reverse this action (unmask).
Shell
systemctl mask [service_name]
systemctl unmask [service_name]

netcat

Basic Data Transfer
Server Side (Receiving Data):
Shell
nc -l 1234 > outputfile.txt
Client Side (Sending Data):
Shell
nc server_address 1234 < inputfile.txt
Setting Up a Simple Chat Server
Server Side:
Shell
nc -l 1234
This command sets Netcat to listen on port 1234 for incoming connections.
Client Side:
Shell
nc server_address 1234
Connecting to the server's IP address (replace server_address) on port 1234 starts the chat session.
 

Create command alias

Bash
# git aliases
alias ginit="git init ."

SSH

Build ssh connect by using the specific key
Bash
ssh -i keyfile target_machine
Local Port Forwarding
Visit 8080 port on local machine will looks like visit the web server
Bash
ssh -o StrictHostKeyChecking=no -f -N -L 8080:localhost:80 root@${SERVER_IP}
notion image
 
Local Port Forwarding with a Bastion Host
Bash
ssh -o StrictHostKeyChecking=no -f -N -L 8080:${SERVER_IP}:80 root@${BASTION_IP}
notion image
 
Remote Port Forwarding
Bash
ssh -o StrictHostKeyChecking=no -f -N -R 0.0.0.0:8080:localhost:80 root@${GATEWAY_IP}
notion image
 
Remote Port Forwarding from a Home/Private Network
Bash
ssh -o StrictHostKeyChecking=no -f -N -R 0.0.0.0:8080:${SERVER_IP}:80 root@${GATEWAY_IP}
notion image

Add environment variables on MAC

Bash
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/XXX/.profile
eval "$(/opt/homebrew/bin/brew shellenv)"

Search texts

Bash
$ teststr="Jeff and the pet Lucky. Gregg and the dog Fido. Chris has 1 bird named Tweety."
$ echo $teststr | grep -Poi 'g{1,2}' 

dig

DNS lookup utility.
Bash
- Lookup the IP(s) associated with a hostname (A records):
    dig +short example.com

- Get a detailed answer for a given domain (A records):
    dig +noall +answer example.com

- Query a specific DNS record type associated with a given domain name:
    dig +short example.com A|MX|TXT|CNAME|NS

- Get all types of records for a given domain name:
    dig example.com ANY

- Specify an alternate DNS server to query:
    dig @8.8.8.8 example.com

- Perform a reverse DNS lookup on an IP address (PTR record):
    dig -x 8.8.8.8

- Find authoritative name servers for the zone and display SOA records:
    dig +nssearch example.com

- Perform iterative queries and display the entire trace path to resolve a domain name:
    dig +trace example.com

NC

A networking tool used for reading from and writing to network connections using TCP or UDP protocols. It's often described as the "Swiss army knife" of networking due to its wide range of capabilities. Here are some common uses and examples of the nc command:
Common Uses
  1. Creating a TCP Connection:
      • Connect to a server: nc [server] [port]
      • For example, to connect to a web server on port 80: nc example.com 80
  1. Listening for Incoming Connections:
      • Listen on a specific port: nc -l [port]
      • For example, to listen on port 1234: nc -l 1234
  1. Transferring Files:
      • On the receiving end: nc -l [port] > filename.ext
      • On the sending end: nc [destination] [port] < filename.ext
  1. Chat Server and Client:
      • Server: nc -l [port]
      • Client: nc [server address] [port]
  1. Port Scanning:
      • Scan a range of ports: nc -z [destination] [port-range]
      • For example, to scan ports 20-30: nc -z example.com 20-30
  1. Testing Network Throughput:
      • On the server: nc -l [port] > /dev/null
      • On the client: nc [server address] [port] < /dev/zero

Export local ports to public

Bash
brew install cloudflared
cloudflared tunnel --url http://localhost:3000

Sync files over SHH or locally

Shell
rsync -avz /path/to/local/file user@remote_host:/path/to/remote/directory

AWS

Copy from EC2

Bash
scp -i abc.pem abc.pdf [email protected]:/home/ec2-user
 

Create HTTPS certificates

Step

  1. Create a root certificate
  1. Generate a certificate signed by the root certificate
Bash
openssl genrsa -des3 -out mykey.key 2048  #generate a private key
# create a certificate file, mypem.pem is the root certificate
# to install it on your local machine
openssl req -x509 -new -nodes -key mykey.key -sha256 -days 2048 -out mypem.pem

openssl req -new -sha256 -nodes -out myprivate.csr -newkey rsa:2048 \
    -keyout myprivate.key \
    -subj "/C=US/ST=Massachusetts/L=Cambridge/O=O'Reilly \
    Media/OU=Harmless scribes/CN=Local/[email protected]"
 
Create file: extfile.txt
Bash
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment
subjectAltName=DNS:localhost
 
Bash
openssl x509 -req -in myprivate.csr -CA mypem.pem -CAkey mykey.key \
    -CAcreateserial -out \
    myprivate.crt -days 500 -sha256 -extfile ./extfile.txt
 
  • The myprivate.crt file is a certificate signed by the root certificate, which is the file that reassures the browser that it can trust our application
  • The myprivate.key file will be used to encrypt connections between the development server and the browser.
 
If you created your application with create-react-app, you could enable HTTPS by putting this in a .env file in your application directory:
Bash
HTTPS=true
SSL_CRT_FILE=myprivate.crt
SSL_KEY_FILE=myprivate.key
 

Check the existing codes of the last running program

Bash
echo $?