Bash Script

Admin Tools

Packages | Script | Cheat sheets

Welcome

This Bash script automates the installation and configuration of essential system administration tools on various Linux distributions. It ensures that administrators have a consistent and useful environment across different systems by installing common tools and configuring syntax highlighting for Vim and Nano. The script supports RHEL 8 and 9, Rocky Linux 8 and 9, Arch Linux, and Ubuntu 22.04 and 24.04.

It isn't uncommon for some of the packages to be installed by default.

Packages

Common Packages

HTOP

HTOP

htop: Interactive process viewer.

WGET

WGET

wget: Network downloader.

CURL

CURL

curl: Command-line tool for transferring data with URLs.

GIT

GIT

git: Version control system.

NET-TOOLS

NET-TOOLS

net-tools: Network configuration tools (including ifconfig).

LSOF

LSOF

lsof: Lists open files.

Operating System Specific Packages

Debian Specific

Debian Specific

software-properties-common: Provides add-apt-repository command

RHEL Specific

RHEL Specific

epel-release: Extra Packages for Enterprise Linux repository.

Script:

#!/bin/bash

# This script installs common and necessary Linux applications for system administration.
# It includes Vim and Nano syntax highlighting for all users.
# The script detects the OS and installs applicable packages for RHEL 8 and 9, Rocky Linux 8 and 9, Arch Linux, and Ubuntu 22.04 and 24.04.
# Results of the script execution are logged to /var/tmp/install_admin_tools.log.
# Common admin tools I install on linux systems to assist with administration
# Infrastructure Bash Script - JW 4.12.24

LOG_FILE="/var/tmp/install_admin_tools.log"
exec > >(tee -a $LOG_FILE) 2>&1

echo "Starting system administration tools installation..."

# Define common packages for installation
COMMON_PACKAGES="htop wget curl git net-tools lsof"
DEBIAN_PACKAGES="software-properties-common"
RHEL_PACKAGES="epel-release"
ARCH_PACKAGES=""

# Function to install packages on Debian-based systems (Ubuntu)
install_debian_packages() {
    echo "Updating package list..."
    apt update

    echo "Installing common packages..."
    apt install -y $COMMON_PACKAGES $DEBIAN_PACKAGES vim nano

    echo "Installing syntax highlighting for Vim and Nano..."
    apt install -y vim-nox vim-scripts
    echo "syntax on" >> /etc/vim/vimrc
    echo "include \"/usr/share/nano/*.nanorc\"" >> /etc/nanorc
}

# Function to install packages on RHEL-based systems (RHEL, Rocky)
install_rhel_packages() {
    echo "Updating package list..."
    yum update -y

    echo "Installing EPEL repository..."
    yum install -y $RHEL_PACKAGES

    echo "Installing common packages..."
    yum install -y $COMMON_PACKAGES vim-enhanced nano

    echo "Installing syntax highlighting for Vim and Nano..."
    echo "syntax on" >> /etc/vimrc
    echo "include \"/usr/share/nano/*.nanorc\"" >> /etc/nanorc
}

# Function to install packages on Arch Linux
install_arch_packages() {
    echo "Updating package list..."
    pacman -Syu --noconfirm

    echo "Installing common packages..."
    pacman -S --noconfirm $COMMON_PACKAGES vim nano

    echo "Installing syntax highlighting for Vim and Nano..."
    pacman -S --noconfirm vim-syntastic nano-syntax-highlighting
    echo "syntax on" >> /etc/vimrc
    echo "include \"/usr/share/nano/*.nanorc\"" >> /etc/nanorc
}

# Detect OS and version
if [[ -f /etc/os-release ]]; then
    . /etc/os-release
    OS=$ID
    VERSION=$VERSION_ID

    case $OS in
        ubuntu)
            echo "Detected OS: Ubuntu $VERSION"
            case $VERSION in
                22.04|24.04)
                    echo "Applying package installation for Ubuntu $VERSION..."
                    install_debian_packages
                    ;;
                *)
                    echo "Unsupported Ubuntu version. Exiting."
                    exit 1
                    ;;
            esac
            ;;
        rhel|rocky)
            echo "Detected OS: $PRETTY_NAME"
            case $VERSION in
                8|9)
                    echo "Applying package installation for $PRETTY_NAME..."
                    install_rhel_packages
                    ;;
                *)
                    echo "Unsupported RHEL/Rocky Linux version. Exiting."
                    exit 1
                    ;;
            esac
            ;;
        arch)
            echo "Detected OS: Arch Linux"
            echo "Applying package installation for Arch Linux..."
            install_arch_packages
            ;;
        *)
            echo "Unsupported OS. Exiting."
            exit 1
            ;;
    esac
else
    echo "Unable to detect OS. Exiting."
    exit 1
fi

echo "System administration tools installation completed successfully."

View on GitHub

Cheat Sheets

HTOP | WGET | CURL | GIT | NET-TOOLS | LSOF

HTOP

Navigation

  • Up/Down Arrow Keys: Scroll through the list of processes.
  • Left/Right Arrow Keys: Scroll horizontally through the process list.
  • Page Up/Page Down: Scroll up and down one full screen at a time.
  • Home/End: Scroll to the top or bottom of the process list.

Basic Commands

  • F1 or ?: Help - Display help screen with a summary of commands.
  • F2 or S: Setup - Enter the setup menu to configure htop.
  • F3 or /: Search - Search for a process by name.
  • F4 or *: Filter - Filter processes by name.
  • F5 or t: Tree - Toggle the display of the process tree.
  • F6 or >: Sort by - Choose a column to sort the processes by.
  • F7 or [: Nice - Increase the nice value (lower priority) of a process.
  • F8 or ]: Renice - Decrease the nice value (higher priority) of a process.
  • F9 or k: Kill - Kill a process.
  • F10 or q: Quit - Exit htop.

Additional Commands

  • Space: Tag/untag a process.
  • u: Show only processes owned by a specific user.
  • U: Remove user filter, showing all processes.
  • s: Trace system calls (strace) for a process.
  • l: Display the log file for a process.
  • F: Follow a process - keep the selection on a specific process.
  • +: Expand all processes in a tree.
  • -: Collapse all processes in a tree.
  • a: Display affinity of a process.
  • i: Invert the sort order.
  • h: Toggle the display of user threads.
  • H: Toggle the display of kernel threads.

Columns and Sorting

  • CPU%: Percentage of CPU usage.
  • MEM%: Percentage of memory usage.
  • TIME+: Total CPU time used by the process.
  • PID: Process ID.
  • USER: User who owns the process.
  • PRI: Priority of the process.
  • NI: Nice value of the process.
  • VIRT: Virtual memory used by the process.
  • RES: Resident memory used by the process.
  • SHR: Shared memory used by the process.
  • S: Process status (running, sleeping, etc.).
  • Command: Command line of the process.

Customizing htop

F2 (Setup Menu)

  • Display Options: Customize the visual representation of htop.
  • Columns: Choose which columns to display.
  • Meters: Configure the meters displayed at the top of the screen.
  • Colors: Change the color scheme of htop.

Filtering and Searching

  • F3 (/): Enter a search term to find specific processes.
  • F4 (*): Enter a filter term to display only matching processes.

Example Usage

  • Search for a process by name: Press F3, type the name, and press Enter.
  • Sort processes by memory usage: Press F6, then select MEM%.
  • Kill a process: Use the arrow keys to select the process, then press F9.

WGET

Basic Usage

wget [options] [URL]

Common Options

  • Download a Single File
  • wget http://example.com/file.txt
  • Download a File and Save with a Different Name
  • wget -O newname.txt http://example.com/file.txt
  • Continue a Previous Download
  • wget -c http://example.com/largefile.zip
  • Download a File to a Specific Directory
  • wget -P /path/to/directory http://example.com/file.txt
  • Download Multiple Files
    • Create a file urls.txt with each URL on a new line:
    • http://example.com/file1.txt
      http://example.com/file2.txt
    • Use wget to download all files listed in urls.txt:
    • wget -i urls.txt

Advanced Options

  • Download Files in Background
  • wget -b http://example.com/file.txt
  • Limit Download Speed
  • wget --limit-rate=200k http://example.com/file.txt
  • Mirror an Entire Website
  • wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.com
  • Set Number of Retries
  • wget --tries=10 http://example.com/file.txt
  • Specify User-Agent
  • wget --user-agent="Mozilla/5.0" http://example.com/file.txt
  • Download with HTTP Authentication
  • wget --user=username --password=password http://example.com/securefile.txt

Handling Directories and Files

  • Create a Directory for Downloads
  • wget -P /path/to/download/directory http://example.com/file.txt
  • Recursive Download
  • wget -r http://example.com/directory/
  • Exclude Certain File Types
  • wget -r --reject jpg,png http://example.com/directory/

Proxy and Network Options

  • Download via Proxy
  • wget -e use_proxy=yes -e http_proxy=proxy_address:port http://example.com/file.txt
  • Set Timeout for Download
  • wget --timeout=30 http://example.com/file.txt
  • Wait Between Downloads
  • wget --wait=5 http://example.com/slow_server/file.txt

Viewing and Logging

  • Verbose Output
  • wget -v http://example.com/file.txt
  • Log Output to a File
  • wget -o download.log http://example.com/file.txt
  • Quiet Mode
  • wget -q http://example.com/file.txt

Example Scenarios

  • Download a File with Progress Bar
  • wget --progress=bar http://example.com/file.txt
  • Download a File and Display Headers
  • wget --server-response http://example.com/file.txt
  • Download Using Cookies
  • wget --load-cookies cookies.txt http://example.com/file.txt
  • Download with Referrer
  • wget --referer=http://example.com http://example.com/file.txt

CURL

Basic Usage

curl [options] [URL]

Common Options

  • Download a File
  • curl -O http://example.com/file.txt
  • Save File with a Different Name
  • curl -o newname.txt http://example.com/file.txt
  • Follow Redirects
  • curl -L http://example.com
  • Resume a Previous Download
  • curl -C - -O http://example.com/file.txt
  • Limit Download Speed
  • curl --limit-rate 200k http://example.com/file.txt

HTTP Requests

  • GET Request
  • curl http://example.com
  • POST Request
  • curl -X POST -d "param1=value1¶m2=value2" http://example.com
  • POST Request with JSON Data
  • curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://example.com
  • PUT Request
  • curl -X PUT -d "param1=value1¶m2=value2" http://example.com
  • DELETE Request
  • curl -X DELETE http://example.com/resource

Headers and Authentication

  • Include HTTP Header
  • curl -H "Authorization: Bearer token" http://example.com
  • Include Multiple Headers
  • curl -H "Content-Type: application/json" -H "Authorization: Bearer token" http://example.com
  • Basic Authentication
  • curl -u username:password http://example.com
  • Bearer Token Authentication
  • curl -H "Authorization: Bearer token" http://example.com

Data Transfer

  • Upload a File
  • curl -T file.txt http://example.com/upload
  • Upload Multiple Files
  • curl -F "[email protected]" -F "[email protected]" http://example.com/upload
  • Download Multiple Files
  • curl -O http://example.com/file1.txt -O http://example.com/file2.jpg

Proxy and Network Options

  • Download via Proxy
  • curl -x proxy_address:port http://example.com
  • Set Timeout for Download
  • curl --max-time 30 http://example.com
  • Specify User-Agent
  • curl -A "Mozilla/5.0" http://example.com

Viewing and Logging

  • Verbose Output
  • curl -v http://example.com
  • Silent Mode (No Output)
  • curl -s http://example.com
  • Log Output to a File
  • curl -o file.txt http://example.com

Example Scenarios

  • Download a File with Progress Bar
  • curl --progress-bar -O http://example.com/file.txt
  • Download and Save Output to a File
  • curl http://example.com -o output.html
  • Get HTTP Headers Only
  • curl -I http://example.com
  • Send Data with URL Encoding
  • curl --data-urlencode "param1=value1" --data-urlencode "param2=value2" http://example.com

GIT

Configuration

  • Set user name
  • git config --global user.name "Your Name"
  • Set user email
  • git config --global user.email "[email protected]"
  • Set default text editor
  • git config --global core.editor "vim"
  • Check configuration settings
  • git config --list

Basic Commands

  • Initialize a new repository
  • git init
  • Clone an existing repository
  • git clone [URL]
  • Check repository status
  • git status
  • Add files to staging area
  • git add [file]
    git add . (Add all files)
  • Commit changes
  • git commit -m "Commit message"
  • Show commit history
  • git log

Branching and Merging

  • List all branches
  • git branch
  • Create a new branch
  • git branch [branch_name]
  • Switch to a branch
  • git checkout [branch_name]
  • Create and switch to a new branch
  • git checkout -b [branch_name]
  • Merge a branch into the current branch
  • git merge [branch_name]
  • Delete a branch
  • git branch -d [branch_name]

Remote Repositories

  • Add a remote repository
  • git remote add origin [URL]
  • Show remote repositories
  • git remote -v
  • Fetch changes from a remote repository
  • git fetch [remote]
  • Push changes to a remote repository
  • git push [remote] [branch]
  • Pull changes from a remote repository
  • git pull [remote] [branch]

Stashing

  • Save changes temporarily
  • git stash
  • List stashed changes
  • git stash list
  • Apply stashed changes
  • git stash apply
  • Apply and remove stashed changes
  • git stash pop
  • Drop a specific stash
  • git stash drop [stash@{0}]

Viewing Changes

  • Show changes in the working directory
  • git diff
  • Show changes between commits
  • git diff [commit1] [commit2]
  • Show changes for a specific file
  • git diff [file]

Undoing Changes

  • Discard changes in the working directory
  • git checkout -- [file]
  • Unstage a file
  • git reset HEAD [file]
  • Revert a commit
  • git revert [commit]

Tags

  • Create a new tag
  • git tag [tag_name]
  • List all tags
  • git tag
  • Show details of a tag
  • git show [tag_name]
  • Push a tag to a remote repository
  • git push [remote] [tag_name]

NET-TOOLS

ifconfig

  • Show network interfaces
  • ifconfig
  • Show a specific interface
  • ifconfig [interface]
  • Enable an interface
  • ifconfig [interface] up
  • Disable an interface
  • ifconfig [interface] down
  • Assign an IP address to an interface
  • ifconfig [interface] [IP_address]
  • Assign a netmask to an interface
  • ifconfig [interface] netmask [netmask]
  • Assign a broadcast address to an interface
  • ifconfig [interface] broadcast [broadcast_address]

netstat

  • Show all network connections
  • netstat -a
  • Show listening ports
  • netstat -l
  • Show network statistics
  • netstat -s
  • Show routing table
  • netstat -r
  • Show PID and program name for each connection
  • netstat -p
  • Show network interfaces
  • netstat -i
  • Show statistics for a specific protocol
  • netstat -s -p [protocol]

route

  • Show routing table
  • route
  • Add a default gateway
  • route add default gw [IP_address]
  • Add a static route
  • route add -net [network_address] netmask [netmask] gw [gateway]
  • Delete a route
  • route del [target]

arp

  • Show ARP table
  • arp -a
  • Add a static ARP entry
  • arp -s [IP_address] [MAC_address]
  • Delete an ARP entry
  • arp -d [IP_address]

hostname

  • Show the current hostname
  • hostname
  • Set a new hostname
  • hostname [new_hostname]

dnsdomainname

  • Show the DNS domain name
  • dnsdomainname

LSOF

Top of Page | Cheat Sheets Navigation

Basic Usage

  • List all open files
  • lsof
  • List open files by a specific user
  • lsof -u [username]
  • List open files for a specific process
  • lsof -p [PID]
  • List open files for a specific command
  • lsof -c [command]
  • List open files in a directory
  • lsof +D /path/to/directory

Network Connections

  • List all network connections
  • lsof -i
  • List network connections on a specific port
  • lsof -i :[port]
  • List network connections by protocol
  • lsof -i [protocol]
    lsof -i tcp
    lsof -i udp

Files and Directories

  • List open files for a specific file
  • lsof /path/to/file
  • List open files in a directory recursively
  • lsof +D /path/to/directory
  • List open files with a specific file descriptor
  • lsof -d [descriptor]

Process and User Information

  • List files opened by a specific user
  • lsof -u [username]
  • Exclude files opened by a specific user
  • lsof -u ^[username]
  • List files opened by a specific process
  • lsof -p [PID]
  • List files opened by multiple processes
  • lsof -p [PID1],[PID2]

Advanced Options

  • List network files with PID and user information
  • lsof -i -n -P
  • List all IPv4 network files
  • lsof -i 4
  • List all IPv6 network files
  • lsof -i 6
  • Find files opened by a specific network connection
  • lsof -i @hostname
  • Find files using a specific port and protocol
  • lsof -i tcp:80
    lsof -i udp:53
  • Display files in use by kernel
  • lsof -k

Example Scenarios

  • List all open files for a specific user
  • lsof -u john
  • List all open files for a specific process ID
  • lsof -p 1234
  • List all open network connections
  • lsof -i
  • Find processes listening on port 8080
  • lsof -i :8080
  • List all open files for a specific directory
  • lsof +D /var/log