Show Menu
Cheatography

Linux Command Sheet Cheat Sheet (DRAFT) by

Linux Command Frequently Used

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Inform­ation

pwd
| print working directory
cat
| print file [on screen]
user head or tail instead of cat
| shows the first few lines at beginning or end
hostname
| display hostname
-i [displays network address] -l [displays all local IPs]
date
| set/di­splay date & time
whois
uptime
| display how long the system has been running
cal
| displays calendar
uname -a
| displays Linux system info
uname -r
| displays kernel info
lsb_re­lease -s

Manage Users

su -
| switch to root user & move to that directory
adduser
| add new user
usermod -aG sudo username
| add user to sudo list
groups username
| list groups a user belongs too
su mfsmith
| switch to listed user mfsmith
passwd
| change password for current user name
passwd mfsmith
| change password for listed user
passwd -S sysadmin
| displays the status of the users password
 
[P] Unlocked
 
[L] locked
 
[NP] no password
adduser mfsmith -or- useradd mfsmith
| Creates a new user
usermod mfsmith
| modify username account
deluser mfsmith -or- userdel
| Deletes user
deluser mfsmith --remo­ve-­all­-files
| Deletes the user and removes all files in directory
getent powerusers | less
| print user groups
usermod -l newuse­rname olduse­rname
| change user name
sudo chfn mfsmith
| edit basic info i.e. full name, room #, phone #

Process Management

ps
| displays users current processes
ps -ef
| displays all processes
pmap
| displays memory usage
pstree
| display in a tree
ps -ef | grep wa
| display all info reference this process
ps kill 84679
| kill process id 84679
ps killall java
| kill all processes named java
top
| show all running proicesses

Disk/H­ardware Mgmt

df -h
| disk usage
ps
| running processes

System Monitoring

whoami
| display user
uptime
| system run time
cat /proc/­cpuinfo
|
free -h
| display free memory}
lshw
| print hardware config­uration
last reboot
| disaply date & time of last reboot
w
| display currently logged in users

General Mainte­nance

Shutdown [OPTIONS] TIME [MESSAGE]
| sudo shutdown =1 "­System scheduled for restar­t"
Update & Upgrade the Debian OS
 
sudo apt-get update && sudo apt-get upgrade

Package management | OS Mainte­nance

sudo apt-get update
| check for updates in current version
apt-get updater && apt-get upgrade
 
apt-cache search docker
| search for packages with the word "­Doc­ker­" in the file name
apt-get install docker
| install docker
 
apt-get remove
| removes a package
apt-get purge
| removes and deletes

Misc Need Organizing

ssh mfsmit­h@s­rv-­tower
log into machine
scp mfsmit­h@s­rv-­tower :srv-dc
scp<us­ern­ame.@<­hos­t:[­remote sourse] <local.
copies files from the host to the destin­ation

Working with Processes

pstree
| display a tree of processes
systemctl proces­sname
| control the systemd init system & service manager

SuperUser

su -
| change to superuser & stay at root directory
sudo -
| execute a command as root
- or -
sudo -u
| execute a command as a particular user

Installs - Frequently Used

vagran­t@d­ebi­an11:/ sudo apt-get install nano -y

Basic Network Mgmt

iwconfig
| wireless interface
ifconfig
| ethern­eti­nte­rface
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
-auto enp0s3
iface enp0s3 inet static
-address 192.16­8.1.97
-netmask 255.25­5.255.0
-gateway 192.16­8.1.1
-dns-d­omain exampl­e.com
-dns-n­ame­servers 1.1.1.1

sudo systemctl restart networking

Networking Comman­d/Q­ueries

ip a
| display IP Address, routing, devices and tunnels
netstat
| displays all active TCP connec­tions, ports
-nutlp [displays tcp/udp ports & applic­ation running on port]
-e [includes ethernet stats] -o [includes PIDs] -p [displays protocols] -r [displays routing table]
ping
| test commun­ication with machines
ping -c 4
| limits the ping response to 4 replies
traceroute srv-dc
print packet route
host
| DNS lookup utililty
hostname
| show or set system name
iptables
|utililty for packet filtering and NAT
mtr
| network diagnostic tool
telnet
| access to the telnet tool
ufw
| applic­ation to mange filters and firewalls
mtr
| diagnostic tool
nbtstat
| displays netbios over tcp
arp
| displays caches apr table
pathping
| disaplys info about network latenc­y/loss between hops
tcpdump

Other Commands

; [semi colon]
| allows a user to stack/­insert multiple command on one command line
&& [double ampersand]
| if the command on the left is suceesful then run the command on the right
| | [double pipe]
based on the results of the first command, skip the second command
 

Directory Structure

/
| root directory of operating system
/etc
| host system config files
/var
| storage for file which grow in size
/bin
| user binary files i.e. cat, grep, etc.
/proc
| system processes
/srv
| system services
/mnt
| ppermenet storage
/home
| home directory for logged in user
/lib
| shared library files and kernel modules
/usr
| user utilities and apps
/dev
| device files i.e. mouse, keyboard, harddrive, etc.
/sbin
| system binary files
/opt
| option software
/media
| temp storag­e/flash drives

Directory Colors

Blue | Directory
Yellow | Device
Green | Executable
Magenta | Graphic Image
Cyan | symbolic Link
Red | Archive File
 
Red & Black | Broken link
**On many Linux distri­but­ions, direct­ories may be displayed in blue, executable files may be displayed in green, and symbolic links may be displayed in cyan.
‌⁠​​
Colored output is not the default behavior for the ls command, but rather the effect of the --color option. The ls seems to perform this coloring automa­tically because there is an alias for the ls command, so it runs with the --color option.**

sysadm­in@­loc­alh­ost:~$ type ls
ls is aliased to `ls --colo­r=auto'

Working with Direct­ories

ls
| list contents of current directory
ls -r
| list files in subdir­ect­ories
ls -al
| list files with direct­ories, size, permis­sions etc.
ls-l
|list files and permis­sions
 
r [read] w[write] x [execute] -= [no permis­sions
ls -a
| list contents of current directory including hidden files
ls -lart
| long, include hidden, ,
ls -d*/
| list direct­ories
cd ~
| jump to users home
cd..
| jump to last directory
cd /
| jump to root home directory
cd -
| previous working directory
cd ../../
| back up two levels

Searching

locate [name of file]
| searches system & locates that directory
find . -name [94148­11_­497­94.pdf]
| search in current directory for file name
find /home -name *.jpg
| search file type in listed directory
Using GREP
| grep searches for patterns in files
grep -r
command | grep
| searches the output of a command

Searching & Sorting

which i.e. which ls
| which command identifies the stored location of a command
type -a
| displays directory location of command
-t
| sort by time
-r
| sort by reverse order
Regular Expres­sions
.
| Any one single character
[ ]
| Any one specified character
[^ ]
| Not the one specified character
*
| Zero or more of the previous character
^
| If first character in the pattern, then pattern must be at beginning of the line to match, otherwise just a literal ^
$
| If last character in the pattern, then pattern must be at the end of the line to match, otherwise just a literal $
Extended Expres­sions
+
| One or more of the previous pattern
?
| The preceding pattern is optional
{ }
| Specify minimum, maximum or exact matches of the previous pattern
|
| Altern­ation - a logical "­or"
( )
| Used to create groups
Anchor Characters
'search term'
| single quotes "­pro­tec­ts" the word
'^ search term'
| ^ means the first place
'search term $'
| $ means the last place
[ ]
| [ ] match the blocked in character
.
|
*
| within a search, this character acts as a wildcard
?
| in a search, this character indicates other letter­s/n­umbers
!
| in a search, this character = does not
echo [D-P]*
echo [!D-P]*

echo ?????*s
echo Dns

Troubl­esh­ooting

Issue:
usermod command not found
Resolu­tion:
use su - instead of su root
Issue:
wget command not found
Resolu­tion:
sudo apt-get install wget
Issue:
cannot access reposi­tories
Resolu­tion:
manually add site via nano /etc/a­pt/­sou­rce­s.list

Firewall

sudo apt-get install ufw
| install firewall applic­ation
sudo ufw allow 'Nginx HTTP'
| allow applic­ation to pass through firewall
sudo ufw status
| check firewall status
sudo ufw disable
| disable firewall
sudo ufw enable
| enable firewall
sudo ufw allow 22
| allow port number to pass
sudo ufw reset
| reset firewall

Redire­ction

Redirect the output of a command
| add "­>"
i.e.
cat food.txt > newfoo­dfi­le.txt
 
| use "­>>" to overwrite a file

Fucnctions

Functions -
are typically utilized in scripting
are utilized to execute multiple commands
functi­on_name ()
{
commands
}

Quotation Marks

There are [3] types of quotes
Section [5] Linus Essentials
" " | double quotes
tell the system to ignore special characters
' ' | single quotes
the system does not consider it a variable
` ` | backwards quotes
the system interp­rests the word in single quotes as an execution
 
each of these marks tells the system the text inside the quotes are to be handled differ­ently