Graduate Program KB

LINUX NOTES


Getting Help

  • --help -> Shows the instructions and flags of a command / program
  • man -> manual pages

Command Line Interface (CLI) Navigation

  • pwd -> print working dir (see which directory you are in)
  • ls -> list (list anything in the directory)
  • cd -> Change Directory (move to a directory)

Flags

  • -l (long format)
  • -a (Show hidden files)
  • -s (Show size)
  • -h (human readable)
    Example: user$ ls -lash

Helpful Commands

  • echo -> outputs the result of something
  • which -> Tells you what the path is to the program you are running (which ls)
  • cat (Prints contents of a file to terminal)
  • head (First 10 lines)
  • head -n 50 (First 50 lines)
  • tail -> outputs last 10 lines of a file
  • !! -> runs last command you have used
  • yes -> program that types y in terminal until (ctrl + c)
  • test -> program that checks file types and compares values
  • touch .hidden-file (touch makes a file, the dot makes the file hidden, need -a to view the file)
  • less (Read a file, but not for file editing, using /hey lets you search for stuff)
  • ps aux (programs running)
  • ~ (tilda represents home directory)
  • / (root directory)
  • Use tab to find all programs with the name you give them
  • ctr + r (reverse search, searchs using keyword for the command used)

Bash History

  • ~/.bash_history (bash history of all commands written)
  • Note: bash history is buffered and is only written after you log out

Signals

  • CTRL + C (Sigint, signal interrupt)

  • CTRL + D (SigQuit, signal quit)

  • kill -l (List all possible kill commands)

  • kill -9 (Sigkill stops a program without any clean up)

VIM Fundamentals

  • i -> interactive mode
  • :q (quit)
  • :q! (force quit)
  • :d (delete the line I am on)
  • :d100 (delete line 100)
  • yy Yank a line
  • dd Delete a line
  • p paste a line

Create and Move Files / Directories

  • mkdir -> Create Directory

  • mkdir -p (Make multiple folders at the same time)

  • rm (removes a file)

  • rm -r (remove directory)

  • rm -rf (Force Deletion)

  • cp (Copy to destination file)

  • cp textfile.txt dest.txt

  • mv (Re-name File or Folder)

Tape Archiver (TAR Files)

  • tar (Create, Extract, and List Archive Contents)

  • Create tar -cfz archive.tar.gz textfile1.txt textfile2.txt folder1

  • Extract tar -xzf archive.tar.gz -C destination

Wild Cards and Replacements

  • touch file{1,2,3,4}.txt (Use curly brace is a replacement)
  • ls file-_ (Star _ finds all of something)
  • ls file*.txt
  • ls file?.txt (Question mark matches exactly one character)
  • touch file{1..30}.txt``` (In a range)
  • echo {a..z..5} (Spaces are every 5 indexes)
  • \ (Escape Character)

Output Streams

  • echo 'this is my text' 1> newfile.txt (Changes the output stream so that the echo'd text goes to a file)

  • 1> (redirects the output stream)

  • 1>> (appends the output stream)

  • Note: 1 is standard output, 2 is standard error

  • 2> (directs standard error message to something (file))

  • cat nonexistent-file.txt 2> error.txt

Note: > directs both error and standard out to both
Note: If you want to direct standard error to the black hole of nothing use this command 2> /dev/null

Input Streams

  • < (Takes from some input)
  • cat < newfile.txt (Takes from input stream ie the file a gives it to cat)
  • grep (Searches for something)
  • grep "ls-error.txt" < ls.txt (Searches for the text in the text file ls.txt)
$ grep "ls-error.txt" < ls.txt 1> grep.txt 2> /dev/null

Pipes

  • cat ls.txt | grep "ls-error.txt" (Connects cat with grep, the output of cat is going to grep)

  • ps aux | grep "ps aux" (Taking ps-aux results and searching for a text using grep in the output)

  • yes > /dev/null & (& means put process in the background, infinite running program running in the background)

  • ps aux | grep "yes" (Searches for the program)

  • kill -9 1224 (Kills the process via ID)

  • yes | rm -i file*.txt (Pipes yes program into the program asking for yes or no answers)


Principle Of Least Power

Users

  • whoami (user you are in)
  • cat /etc/passwd (shows all users)

Super User

  • sudo -s (Swap to super user)
  • sudo (Temporarily switch to super user)

Create User

  • sudo useradd brian (Makes user brian)
  • sudo passwd brian (Set password)
  • su brian (Switch to brian)

Groups and Permissions

  • sudo usermod -aG sudo brian (Adds brian to the sudo group, now brian can use sudo)
  • sudo usermod -aG ubuntu brian

Change Ownership Of a File / Folder

  • sudo chown ubuntu:ubuntu textfile.txt

  • sudo chown ubuntu:ubuntu /folder1

  • sudo chmod u=rw,g=rw,o=rw hello.txt

  • sudo chmod +x mynewprogram (Now everyone can execute the program)

Passwd File Format

  • Username : Password : UserID : GroupID : User ID Info : Home Dir : Command/Shell
  • oracle:x:1021:1020:Oracle user:/data/network/oracle/:/bin/bash

Using Enviroment

  • printenv (shows all environmental variables)

  • $USER (replaces with the environment variable)

  • echo $USER

  • /etc/environment (makes environment variables permanent)

  • BASHRC (Runs at the beginning of every session)

  • ~/.bashrc

  • source ~/.bashrc (Runs .bashrc again)

System Processes

  • ps (process status)

  • CTRL + Z (Stops the process)

  • bg 1 (Continues running the job in the background)

  • fg 1 (Puts job in the foreground)

  • jobs (Shows you what jobs are running)

  • jobs -l (Shows PID of the jobs, so you are able to kill them)

Operators (&&, ||)

  • touch status.txt && date >> status.txt && uptime >> status.txt

  • Means: Run touch and if it works run date, append to status.txt and if that works run uptime and append to status.txt

  • && Both commands including the first must return successfully

  • || One of the commands must return successfully (if both return successful it does not work)


Network and Internet

Making a VM

  • multipass launch --name secondary
  • multipass shell secondary

Adding a User to the VM

  • sudo useradd -s /bin/bash -m -g ubuntu brian
  • sudo passwd brian

SSH

  • Create Public and Private Key

  • ssh-keygen -t rsa

  • Secondary PC

  • make folder called .ssh and make file called authorized_keys

  • save key on secondary pc in the authorized_keys file

  • chmod the directory 700

  • chmod 600 keyfile

  • ifconfig (gives ip of the secondary pc)

  • Primary PC

  • ssh brian@192.168.64.5

SFTP

  • sftp brian@192.168.64.3

  • lpwd (local pwd)

  • lls (local list)

  • sftp> put status.txt (Sends file from local to secondary)

  • sftp> put file-to-put.txt putted-file.txt (sends and renames on secondary pc)

  • sftp> get status.txt (to get a file from the secondary PC)

WGET (Download something Online, HTTP Request)

wget https://raw.githubusercontent.com/btholt/bash2048.sh

CURL (Download something Online)

curl https://raw.github/2048.sh > game.sh
  • Start API Server

  • python3 -m http.server 8000 --bind 0.0.0.0

  • In Browser go to (192.168.64.2:8000 to visit the server)

curl http://192.168.64.2:8000/
curl -o res2.txt http://192.168.64.2:8000/output.txt

curl -X POST http://192.168.64.2:8000/
curl -d "this is the post body" PUT http://192.168.64.2:8000/
curl -X DELETE "anything" http://192.168.64.2:8000/
curl -b "name=brian"

Package Managment & APT COMMANDS (Advanced Packaging Tools)

  • Note: dpkg (debian)
  • apt install (newer tool)
  • apt search nodejs (search a tool)
  • sudo apt autoremove
  • apt update (Updates a package that already exists)
  • apt upgrade (Update gets a new version of the registry [list of packages])
  • apt full-upgrade (runs autoremove and upgrade)

Snaps

  • runs on multiple linux distros

  • Safer, sandboxed and cannot break out of home folder

  • Automatically updates

  • sudo snap install lolcat

  • sudo snap install --channel=14/stable --classic node


Shell Scripts

Writing Scripts

  • vi genfiles.sh
DESTINATION=~/temp                                 (This is a variable)
FILE_PREFIX=file

mkdir -p $DESTINATION                              (-p means create or dont create based on if the file exists or not)
cd $DESTINATION
touch file{1..10}.txt
touch ${FILE_PREFIX}{1..10}.txt
echo done
  • source genfiles.sh
  • . genfiles.sh
  • ./genfiles.sh

Hashbang

  • At the top of the file. This denotes how to run the file, file no longer needs the .sh extension
  • #! /bin/bash

Path and Variables

  • Path shows where linux searches for programs from left to right

  • $PATH

  • If we make a bash script and we want linux to automatically recognize it as a program, we need to add the directory the script is in to the $PATH.

  • In .bashrc Add

export PATH=~/folder_with_scripts:$PATH
  • Now folder with sctipts will be the first thing linux looks through for scripts you have written

Command Line Arguments

  • In the bash file, you want to use $number

  • $1 (first argument being provided)

  • $2 (second argument being provided)

User Input from Bash Script

  • use the read keyword for user input
  • read -p "enter a file prefix: " FILE_PREFIX

Conditionals

if [ -z $DESTINATION ]; then                                                       (-z means if string is empty)
    echo "no path provided, defaulting to ~/temp"
    DESTINATION=temp
fi
  • CHECK IF FILE EXISTS
  • test -e ~output.txt && echo is true

Else If Statements

if [ $1 -gt 10]; then
    echo "greater than 10"
elif [ $1 -lt 10]; then
    echo "less than 10"
else
    echo "equals 10"
fi

Case Statements

case $1 in
    "smile"
        echo"I am happy"
            ;;
    "sad"
        echo"I am sad"
            ;;
    "laugh"
        echo ":D"
            ;;
    echo "I don't know that one yet!"
        ;;
esac

Loops and Arrays

friends=(Kyle Marc jem "brian holt" Sarah)            (Array declare)
echo my second friend is ${friends[1]}

#FOR LOOP
for friends in ${friends[]}
do
     echo friend: $friend
done

echo "I have ${#friends[*]} friends"         (length of the array)

#WHILE LOOP
while [ $VAR1 -ne $VAR2 ]
do
    read -p "your guess: " $VAR2
done
echo "you got var 1"

Automation and Customization

Cron

  • runs tasks on a schedule (daily, weekly, hourly)

  • file put in cron folder must be executable file chmod +x

  • /etc/cron.

  • crontab -e

  • echo -e "this is how you make color \e[32mgreen]"