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
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."
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
wget -O newname.txt http://example.com/file.txt
wget -c http://example.com/largefile.zip
wget -P /path/to/directory http://example.com/file.txt
- Create a file
urls.txt
with each URL on a new line:
http://example.com/file1.txt
http://example.com/file2.txt
urls.txt
:wget -i urls.txt
Advanced Options
- Download Files in Background
wget -b http://example.com/file.txt
wget --limit-rate=200k http://example.com/file.txt
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.com
wget --tries=10 http://example.com/file.txt
wget --user-agent="Mozilla/5.0" http://example.com/file.txt
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
wget -r http://example.com/directory/
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
wget --timeout=30 http://example.com/file.txt
wget --wait=5 http://example.com/slow_server/file.txt
Viewing and Logging
- Verbose Output
wget -v http://example.com/file.txt
wget -o download.log http://example.com/file.txt
wget -q http://example.com/file.txt
Example Scenarios
- Download a File with Progress Bar
wget --progress=bar http://example.com/file.txt
wget --server-response http://example.com/file.txt
wget --load-cookies cookies.txt http://example.com/file.txt
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
curl -o newname.txt http://example.com/file.txt
curl -L http://example.com
curl -C - -O http://example.com/file.txt
curl --limit-rate 200k http://example.com/file.txt
HTTP Requests
- GET Request
curl http://example.com
curl -X POST -d "param1=value1¶m2=value2" http://example.com
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://example.com
curl -X PUT -d "param1=value1¶m2=value2" http://example.com
curl -X DELETE http://example.com/resource
Headers and Authentication
- Include HTTP Header
curl -H "Authorization: Bearer token" http://example.com
curl -H "Content-Type: application/json" -H "Authorization: Bearer token" http://example.com
curl -u username:password http://example.com
curl -H "Authorization: Bearer token" http://example.com
Data Transfer
- Upload a File
curl -T file.txt http://example.com/upload
curl -F "[email protected]" -F "[email protected]" http://example.com/upload
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
curl --max-time 30 http://example.com
curl -A "Mozilla/5.0" http://example.com
Viewing and Logging
- Verbose Output
curl -v http://example.com
curl -s http://example.com
curl -o file.txt http://example.com
Example Scenarios
- Download a File with Progress Bar
curl --progress-bar -O http://example.com/file.txt
curl http://example.com -o output.html
curl -I http://example.com
curl --data-urlencode "param1=value1" --data-urlencode "param2=value2" http://example.com
GIT
Configuration
- Set user name
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "vim"
git config --list
Basic Commands
- Initialize a new repository
git init
git clone [URL]
git status
git add [file]
git add .
(Add all files)
git commit -m "Commit message"
git log
Branching and Merging
- List all branches
git branch
git branch [branch_name]
git checkout [branch_name]
git checkout -b [branch_name]
git merge [branch_name]
git branch -d [branch_name]
Remote Repositories
- Add a remote repository
git remote add origin [URL]
git remote -v
git fetch [remote]
git push [remote] [branch]
git pull [remote] [branch]
Stashing
- Save changes temporarily
git stash
git stash list
git stash apply
git stash pop
git stash drop [stash@{0}]
Viewing Changes
- Show changes in the working directory
git diff
git diff [commit1] [commit2]
git diff [file]
Undoing Changes
- Discard changes in the working directory
git checkout -- [file]
git reset HEAD [file]
git revert [commit]
Tags
- Create a new tag
git tag [tag_name]
git tag
git show [tag_name]
git push [remote] [tag_name]
NET-TOOLS
ifconfig
- Show network interfaces
ifconfig
ifconfig [interface]
ifconfig [interface] up
ifconfig [interface] down
ifconfig [interface] [IP_address]
ifconfig [interface] netmask [netmask]
ifconfig [interface] broadcast [broadcast_address]
netstat
- Show all network connections
netstat -a
netstat -l
netstat -s
netstat -r
netstat -p
netstat -i
netstat -s -p [protocol]
route
- Show routing table
route
route add default gw [IP_address]
route add -net [network_address] netmask [netmask] gw [gateway]
route del [target]
arp
- Show ARP table
arp -a
arp -s [IP_address] [MAC_address]
arp -d [IP_address]
hostname
- Show the current hostname
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
lsof -u [username]
lsof -p [PID]
lsof -c [command]
lsof +D /path/to/directory
Network Connections
- List all network connections
lsof -i
lsof -i :[port]
lsof -i [protocol]
lsof -i tcp
lsof -i udp
Files and Directories
- List open files for a specific file
lsof /path/to/file
lsof +D /path/to/directory
lsof -d [descriptor]
Process and User Information
- List files opened by a specific user
lsof -u [username]
lsof -u ^[username]
lsof -p [PID]
lsof -p [PID1],[PID2]
Advanced Options
- List network files with PID and user information
lsof -i -n -P
lsof -i 4
lsof -i 6
lsof -i @hostname
lsof -i tcp:80
lsof -i udp:53
lsof -k
Example Scenarios
- List all open files for a specific user
lsof -u john
lsof -p 1234
lsof -i
lsof -i :8080
lsof +D /var/log