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.txtwget -O newname.txt http://example.com/file.txtwget -c http://example.com/largefile.zipwget -P /path/to/directory http://example.com/file.txt- Create a file
urls.txtwith each URL on a new line:
http://example.com/file1.txt
http://example.com/file2.txturls.txt:wget -i urls.txtAdvanced Options
- Download Files in Background
wget -b http://example.com/file.txtwget --limit-rate=200k http://example.com/file.txtwget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.comwget --tries=10 http://example.com/file.txtwget --user-agent="Mozilla/5.0" http://example.com/file.txtwget --user=username --password=password http://example.com/securefile.txtHandling Directories and Files
- Create a Directory for Downloads
wget -P /path/to/download/directory http://example.com/file.txtwget -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.txtwget --timeout=30 http://example.com/file.txtwget --wait=5 http://example.com/slow_server/file.txtViewing and Logging
- Verbose Output
wget -v http://example.com/file.txtwget -o download.log http://example.com/file.txtwget -q http://example.com/file.txtExample Scenarios
- Download a File with Progress Bar
wget --progress=bar http://example.com/file.txtwget --server-response http://example.com/file.txtwget --load-cookies cookies.txt http://example.com/file.txtwget --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.txtcurl -o newname.txt http://example.com/file.txtcurl -L http://example.comcurl -C - -O http://example.com/file.txtcurl --limit-rate 200k http://example.com/file.txtHTTP Requests
- GET Request
curl http://example.comcurl -X POST -d "param1=value1¶m2=value2" http://example.comcurl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://example.comcurl -X PUT -d "param1=value1¶m2=value2" http://example.comcurl -X DELETE http://example.com/resourceHeaders and Authentication
- Include HTTP Header
curl -H "Authorization: Bearer token" http://example.comcurl -H "Content-Type: application/json" -H "Authorization: Bearer token" http://example.comcurl -u username:password http://example.comcurl -H "Authorization: Bearer token" http://example.comData Transfer
- Upload a File
curl -T file.txt http://example.com/uploadcurl -F "[email protected]" -F "[email protected]" http://example.com/uploadcurl -O http://example.com/file1.txt -O http://example.com/file2.jpgProxy and Network Options
- Download via Proxy
curl -x proxy_address:port http://example.comcurl --max-time 30 http://example.comcurl -A "Mozilla/5.0" http://example.comViewing and Logging
- Verbose Output
curl -v http://example.comcurl -s http://example.comcurl -o file.txt http://example.comExample Scenarios
- Download a File with Progress Bar
curl --progress-bar -O http://example.com/file.txtcurl http://example.com -o output.htmlcurl -I http://example.comcurl --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 --listBasic Commands
- Initialize a new repository
git initgit clone [URL]git statusgit add [file]git add . (Add all files)git commit -m "Commit message"git logBranching and Merging
- List all branches
git branchgit 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 -vgit fetch [remote]git push [remote] [branch]git pull [remote] [branch]Stashing
- Save changes temporarily
git stashgit stash listgit stash applygit stash popgit stash drop [stash@{0}]Viewing Changes
- Show changes in the working directory
git diffgit 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 taggit show [tag_name]git push [remote] [tag_name]
NET-TOOLS
ifconfig
- Show network interfaces
ifconfigifconfig [interface]ifconfig [interface] upifconfig [interface] downifconfig [interface] [IP_address]ifconfig [interface] netmask [netmask]ifconfig [interface] broadcast [broadcast_address]netstat
- Show all network connections
netstat -anetstat -lnetstat -snetstat -rnetstat -pnetstat -inetstat -s -p [protocol]route
- Show routing table
routeroute add default gw [IP_address]route add -net [network_address] netmask [netmask] gw [gateway]route del [target]arp
- Show ARP table
arp -aarp -s [IP_address] [MAC_address]arp -d [IP_address]hostname
- Show the current hostname
hostnamehostname [new_hostname]dnsdomainname
- Show the DNS domain name
dnsdomainname
LSOF
Top of Page | Cheat Sheets Navigation
Basic Usage
- List all open files
lsoflsof -u [username]lsof -p [PID]lsof -c [command]lsof +D /path/to/directoryNetwork Connections
- List all network connections
lsof -ilsof -i :[port]lsof -i [protocol]lsof -i tcplsof -i udpFiles and Directories
- List open files for a specific file
lsof /path/to/filelsof +D /path/to/directorylsof -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 -Plsof -i 4lsof -i 6lsof -i @hostnamelsof -i tcp:80lsof -i udp:53lsof -kExample Scenarios
- List all open files for a specific user
lsof -u johnlsof -p 1234lsof -ilsof -i :8080lsof +D /var/log