Show Menu
Cheatography

Linux / Bash / System Administration Cheat Sheet by

For System Administration Class

SSH

ssh user@host
ssh user@host (port)

Help

man (command) = Shows the manpages for the current command
(Command) --help = Less detailed version of manpages
whatis <co­mma­nd> -Gets short one line manual page to describe

Searching

grep "­TOD­O" files = Searches for TODO in files
grep -r "­TOD­O" dir = Searches recurs­ively for TODO in the directory
locate (file) = Finds all instances of file
find (file) = Find all instances of file

IO Redire­ction

command < file (Read input of command from file)
command > file (Write output of command to file)
command > /dev/null (Discard output of command)
command >> file (Append output of file)
command1 | command2 (Pipe output of command1 to command2)

Touch

Create single file: touch example
Create multiple files: touch example1 example2
Set current time and date of file: touch -a example
Create file with specified time: touch -t YYMMDD­HHMM.SS example

User Additions

sudo useradd <us­ern­ame>
sudo smbpasswd -a <us­ern­ame>

DHCP Setup

sudo \cp /usr/s­har­e/d­oc/­dhc­p-s­erv­er/­dhc­pd.c­on­f.e­xample /etc/d­hcp­/dh­cpd.conf (Setup New Empty Config)
Setup Static IP: ip addr add 192.16­8.114.1 dev br0;
Setting up bridging: sudo dnf install bridge­-utils
sudo brctl addbr br0; sudo brctl addif enp1s0; sudo brctl addif eno1; sudo ifconfig br0 up

Check history of commands

history | less

Check if is a File, Directory, or Symbolic Link

if [ -d "­$1" ] (Direc­tory)
elif [ -h "­$1" ] (Symbolic Link)
elif [ -f "­$1" ] (File)

Executing Scripts

Make script execut­able: chmod +x <sc­rip­tna­me>
./<­scr­ipt­nam­e> - Execute script

Networking

netstat -rn -displays destin­ation network and hosts of routing table
ifconfig lo 127.0.0.1 . -assigns loopback to 127.0.0.1
ifconfig eth0 127.17.75.20 . -assigns ethernet device to IP
route add default gw 128.17.75.98 . -adds route to routing table

Clear

clear (clears command line)
 

Create­/Ad­d/R­emove Direct­ories

mkdir (dir) [Creating Directory]
pwd [Show current directory]
cp file1 file2 [Copies file1 to file 2]
cp -r dir1 dir2 [Copies dir1 to dir2]
rm file/ rm -r dir [Remove file or remove directory]

Permis­sions

chmod ugo=rwx file = Changes permis­sions of file to Read, write, and execute for the User,G­roup, and all Others. This command can have many iterat­ions.
chmod 777 = First digit is read(4­+2+1), second is write(­4+2+1), and the third is (4+2+1). Many combin­ations of numbers can be used.

Systemd

sudo systemctl start/­sto­p/r­estart 'nginx.se­rvice' . -start, stop, and restart service with systemctl
sudo systemctl enable­/di­sable 'nginx.se­rvice' -disables or enables service
systemctl list-units --all . -lists all units systemd has loaded into the system
journalctl . -view all log entries
sudo systemctl edit 'nginx.se­rvice' . -modify service directly

Perfor­mance Monitoring

Display and manage top processes: top
Display processor related statis­tics: mpstat 1
Display virtual memory statis­tics: vmstat 1
Display the last 100 syslog messages: tail 100 /var/l­og/­mes­sages
Monitor all traffic on port 80: tcpdump -i eth0 'port 80'
List files opened by user: lsof -u user
Display free and used memory ( -h for human readable, -m is MB, -g is GB): free -hmg

Network Diagno­stics

dig oreill­y.com -returns inform­ation help by domain

Display absolute path of files or links

which <fi­len­ame>

DHCP Config File

option domain­-name “sap.c­sla­b.m­ora­via­n.edu”;
option domain­-na­me-­servers sap.cs­lab.mo­rav­ian.edu;
author­ita­tive;
log-fa­cility local7;

Show users currently logged in to host

users

Arrays

Creating an array: array=­("Th­is" "­is" "­an" "­arr­ay")
Print element in array: echo ${arra­y[0]}, echo ${arra­y[1]}
Expand all elements of array: printf ${arra­y[@]}
Delete array value: unset ARRAY[1]

Exit command in Script

Exit 0 (Standard exit)
Exit (1,2,3­,etc.) - If you want to customize for your script

Direct­ories to Know

/ - Root
/bin – Essential User Binaries
/boot – Static Boot Files
/dev – Device Files
/etc – Config­uration Files
/home – Home Folders
/lib – Essential Shared Libraries
 

Navigating Files/­Dir­ect­ories

ls [list files in a directory]
ls -al [Formats files in a list and shows hidden files
cd (dir) [Change directory]
pwd [Show current directory]
more (file) [Outputs contents of a file]

Firewalld

firewa­ll-cmd --add-­ser­vic­e='­service name' -Adds system service to firewall protection

If-the­n-exit

if []; then echo "­" exit 1 . -if-th­en-exit commands for scripts

Kerberos Install

sudo dnf install ntp -y
sudo dnf install krb5-libs krb5-s­erver krb5-w­ork­station
Edit /var/k­erb­ero­s/k­rb5­kdc­/ka­dm5.acl Change /admin­@EX­AMPLE to /admin
sudo /usr/s­bin­/kd­b5_util create -s

Test

Example to test if directory was created: test -d /tmp/t­emp_dir

How long system has been running

uptime

Download files from the web

wget -c [URL]

Reading from Standard Input with xargs

find . -mtime -1 -type f -print | xargs tar --create --gzip --file­=$D­EST­DIR­$FI­L> : Reads items from the standard input

DNF

sudo dnf install <pa­cka­ge> -useful for installing new packages from the command line
sudo dnf upgrade <pa­cka­ge> -upgrades or updates given package
sudo dnf remove <pa­cka­ge> -removes the selected package

Crontab

cron -e (Find timed services)
crontab -l 2>/­dev­/null | grep -q "0 0 * crontab <co­mma­nd> (Ensure crontab is not outputting any else but the job to run)

Setting Up JupyterHub

jupyterhub --gene­rat­e-c­onfig (Generates jupyte­rhu­b_c­onf­ig.py)
jupyterhub -f /etc/j­upy­ter­hub­/ju­pyt­erh­ub_­con­fig.py (Start jupyterhub using config)
c.Jupy­ter­Hub.ip = '192.1­68.1.2' c.Jupy­ter­Hub.port = 443 (Change IP and Port in config FIle)

Dick Usage

df -h (Shows used disk space of certain file or directory)

Tar

tar -x (Exract files) -v (verbose) z (zipped file) -f (use tar archive for operation)

Reboot System Properly

reboot
 

Shortcuts

Ctrl+C = Ends current process
Ctrl+Z = Stops current process, but can be resumed
Ctrl+D = Log out of current session
exit = Log Out of current session
Up/Down Arrows = Navigate through previous commands

Check if argument is a variable

if [ -v "­$1" ]; then

Functions

Create function in command line example: bash$ home() { printf '%s\n' "­$HO­ME" ; }
-- in a function: The option terminator string
"­$@" expands to all of the arguments given to the function: printf '%s\n' "­$@"
Save defined fucntion in bash: declare -f home
When writing functions in scripts, it is best to instan­tiate them at the top of the script so no errors are produced.

Check if argument is a command

if command -v "­$1" >/d­ev/­null; then

Universal Home Directory to use in Scripts etc.

$HOME

Find

find -mtime (Files access within certain time) -type (Look for specific file type)

Shebang

#!/bin­/bash -normal shebang of commented file
echo "­Hello World!­" -display line of text
#comment -# is the standard way to command in text editor
variab­le_­nam­e="S­eba­sti­aan­" -standard way to establish variables

Alias

Create an alias example: alias 11='ls --colo­r=auto'
Chec k whether command is an alias: type -a 11

IpConfig

ipconfig -a -displays all networks and interfaces
ping host -send a ping signal to the host
ifconfig eth0 -displays eth0 address and details

Adding Google Authen­ticator to JupyterHub

Add this command to JupyterHub config: from oauthe­nti­cat­or.g­oogle import Google­OAu­the­nti­cator c.Jupy­ter­Hub.au­the­nti­cat­or_­class = Google­OAu­the­nti­cator
Customize domain name: c.Goog­leO­Aut­hen­tic­ato­r.h­ost­ed_­domain = ['jupy­ter­hub.com']
Installing oauth: dnf install oauthe­nti­cator

File Transfers

Secure copy exampl­e.txt to the /tmp folder on server: scp exampl­e.txt server­:/tmp
Copy .zip files from server to the local /tmp folder: scp server­:/v­ar/www/.zip /tmp
Copy all files and direct­ories recurs­ively from server: scp -r server­:/v­ar/www /tmp
Synchr­onize /home to /home/.ba­ckups: rsync -a /home /backups/
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.