Show Menu
Cheatography

Linux Commands & Options Cheat Sheet (DRAFT) by

NOS-120 Commands & Command Options (but for CPCC this time)

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

Command Redire­ction

<
Gets input from somewhere other than stdin (keyboard)
>
Sends output somewhere other than stdout (monitor)
1>
Altern­ative to "­>" (not really used)
>>
Redirects stdout; appends it to existing content (instead of overwr­iting it)
2>
Redirects only error messages (stderr)
&>
Redirects both stdout and stderr
cmd1 | cmd2
Pipes cmd1 into cmd2
(uses the output of cmd1 as the input for cmd2)

Basic File/D­ire­ctory Commands

ls dir1
List contents of dir1
-l
additional info (owner, perms, etc.)
-a
lists all, including hidden files
-i
adds file inode to info displayed
-h
adds file size in human-­rea­dable format
(e.g. MB instead of bytes)
-d
info for directory itself (rather than its contents)
 
mkdir dir1
Create a new directory
-p
creates full path (multiple direct­ories if needed)
-v
verbose (output shows action taken)
cp dir1 dir2
 
Copy dir1 into dir2
-r
recurs­ively ("and its conten­ts")
 
rm dir1
Removes (deletes) an empty directory
-r
recurs­ively ("and it's conten­ts"), including special files
-i
intera­ctively delete files (ask before deleting)
mv file1 dir1
Move file1 into dir1
-v
verbose (output shows action taken)
 
ln target link
Creates a hard link from file "­lin­k" to file "­tar­get­"
-s
makes a symbolic link instead of a hard link

Locating Files

locate pattern
Search for file names with specified pattern (via "­loc­ate­" database)
updatedb
Update the "­loc­ate­" database
 
find /dir -how what
Recursive search starting at "/dir" for pattern ("wh­at") of specified type ("ho­w")
-name name
case-s­ens­itive search for file name
-iname name
case-insensitive search for file name
-user name
files owned by user with specified username
-uid 1002
files owned by user with UID 1002
-size 100M
files that are exactly 100 MB in size
-size [+/-]100M
files that are [larger/smaller] than 100 MB
-type f
specif­ically files (not direct­ories)
-perm /o=w
files where at least "other" has write perms
inum inode
file names associated with specified inode (i.e. including linked files)
-mmin -5
files modified in the last 5 minutes
-ctime
creation time
<
find /usr -size +100M -exec ls -lh {} \;
>
shows
ls -l
output AND actual file size of search results
(essen­tially "­pip­es" results of
find
into
ls -lh
)

Users & Groups

Command
Options
What It Does
id user1
Show UID, GID, & secondary groups
(current user if not specified)
getent passwd usr1
Find out if usr1 is known to the system
useradd user1
Add a new user
-u #
set a specific UID
 
-s /sbin/nologin
create a user without a login shell
usermod user1
Change properties of existing user
-c "text"
adds text to comment field
-g group1
changes primary group to group1
-G group1
replaces supple­mentary group with group1
-aG group1
appends group1 to supple­mentary groups
-L user1
locks user1's account (instead of deleting)
newgrp group1
Change current user's primary group
(temporary; current session only)
userdel user1
Deletes user1 but not their home directory
-r
deletes both user and home directory
groups user1
Displays simple list of groups user1 is a member of
passwd user1
Set password for user1
chage user1
Change password aging properties for user
-m days
minimum # of days between password changes
-M days
maximum # of days between password changes
-W days
warning period before password expires
-I days
inactivity period (password usable after expira­tion)
-d 0
require password change on next login
-E date
date when account expires
getent group grp1
Find out if grp1 is known to the system
groupadd group1
Create a new group
-g #
set a specific GID
-r
create a system group
groupmod group1
Change properties of existing group
-g #
change GID to specified number
To set the number of days from today when user's account expires:
chage -E $(date +days %Y-%m-%d)


To give full admin privileges to a user or group:
echo "[user1|%group1] ALL=(ALL) ALL" >> /etc/s­udo­ers.d/name

Ownership & Permis­sions

chown user:group
Change owning user and owning group
chown :group
Change only owning group
chgrp group file
Alternate way to change owning group
 
chmod [perms] file
Change file/d­ire­ctory permis­sions
-R
recursive; applies to directory and all contents
g+r
grants owning group the read permission
a-x
removes execute perms for all parties (without changing other perms)
-R a=rwX
recurs­ively set perms for all parties to rw, with x perms on directory only
770
octally set rwx perms for owning user & group, 0 for other
chmod [perms] file
(cont'd) Change special permis­sions
o+t dir
set Sticky bit (in other)
* For direct­ories only *
1xxx
set Sticky bit – octal form
(xxx = regular perms)
g+s dir/file
set SetGID bit (in group)
* For direct­ories and files *
2xxx
set SetGID bit – octal form
3xxx
set both Sticky & SetGID bits
(chmod "­1"+"2­")
u+s file
set SetUID bit (in user)
* For files only *
4xxx
set SetUID bit – octal form
 
umask
Displays currently set umask
(when used by itself)
077
removes default perms
(0 from user, 7 from group/­other)
0077
preserves any special perms when removing defaults
Octal Expression Values: r=4 | w=2 | x=1

Default permis­sions (on vanilla Linux):
direct­ories=777 | files=666

Jobs & Load Handling

w
Displays currently logged-in users, login method, time, & resource usage info
--from
shows users' IP addresses
(in addition to regular output)
-u username
limits output to specified username
command &
Starts a new job in the background
jobs
Displays jobs running in the background
("+" = default job)
fg %1
Brings job 1 to the foreground
bg %1
Sends job 1 to the background
pidof command
Returns the PID(s) of a currently running job
systemctl status cmd
Shows main PID of a process, among other things
(can use if
pidof
returns multiple)
 
--- CPU Load Handling ---
uptime
Displays the load average for the last 1, 5, and 15 mins
lscpu
Displays number of CPUs in the system, among other things
-p
simplified output
(each row of numbers = 1 CPU)
Process Priority Values: -20 to 19 (default = 0)
(negative = higher priority | positive = lower priority)

Viewing & Managing Processes

ps
Shows info about processes; pipe to
head
/
grep
/etc for less output
(Note: "
[output]
" = kernel thread)
--forest
shows output in visual "­tre­e" format
Shows processes in visual tree format
(may not always be installed)
-p
includes PIDs in tree output
Linux terminal equivalent of Task Manager
(Also shows load average)
Keyboard Controls:
 
k
send a signal to a process from within
top
 
Shift+M
sort by memory usage
 
Shift+P
return to sorting by CPU usage
 
l / t / m
toggle "­loa­d", "­thr­ead­s", & "­mem­ory­" header lines
kill PID
Sends signal 15 to specified process
* Must use process ID *
-signal
sends specified signal
(can use signal name or number)
-l
displays list of signal options
killall process
Kills processes by name instead of PID
pkill
Look up & manage processes by name or other attributes
(default is signal 15)
-signal
specify signal to send
-u username
sends signal to user's account
(forces logout & shuts down their processes)
-t pts/22
kills specified terminal session (TTY)
 
nice -n x command
Starts a new process with an adjusted priority value (x)
renice -n x -p PID
Change the priority value (x) of an existing process

Managing Services & Daemons

--- System Commands ---
systemctl reboot
reboots the system
systemctl daemon­-reload
tells systemd you've made changes to a unit file & it needs to reinit­ialize those unit files
systemctl get-de­fault
shows which target is the default target that the system summons whenever it starts up
systemctl set-de­fault 
graphi­cal.target
sets default target to "­gra­phi­cal.ta­rge­t"
 
--- Unit Commands ---
systemctl list-units
shows all the active & loaded units on the system
-t type
filters output by specified type of unit
-a
shows all units known to systemd
(including "­ina­cti­ve" & "­not­-fo­und­")
systemctl status unit
shows status & details of specified unit (including location of unit files)
systemctl is-enabled 
unit.s­ervice
shows whether the service is enabled
(i.e. will start automa­tically on system boot)
systemctl is-active 
unit.s­ervice
shows whether the service is active
systemctl enable --now 
unit.s­ervice
enables the service & start it immedi­ately (sets "­ena­ble­" persis­tently)
systemctl disable --now 
unit
stops service immedi­ately & sets to "­dis­abl­ed" persis­tently
systemctl reload unit
sends the "hang up" signal; drops config file loaded in memory & reload it from file system
systemctl restart unit
kills the process & starts it up again fresh
(main PID will change)
systemctl reload­-or­-re­start 
unit
reloads if possible, otherwise restart
(for when you don't know if unit supports reload)
systemctl list-dependencies 
unit.service
shows list of units required by unit.s­ervice for it to work
--reverse
shows units that call unit.s­ervice
systemctl mask unit
prevents unit from being started automa­tically or manually
(not even via
enable --now
command)
systemctl unmask unit
undoes masking on a service

File Systems & Block Devices

findmnt -s
Simplified output about mountp­oints & filesy­stems
lsblk
Lists block devices in tree format (incl. size, mountp­oint, etc.)
-f
/
--fs
adds info about filesy­stems
-p
shows full device paths
blkid /dev/file
Shows info about block device & filesy­stem, including partition UUID
(non-partition devices must have a filesystem to show output)
df location
Shows data (storage) utiliz­ation of filesystem, device, & mountpoint
(Shows info for whole filesy­stem, even if
location
is a subdir­ectory)
-h
human-­rea­dable format
(converts bytes to MB, GB, etc.)
 
du /dir
Shows data (storage) utilization of a specific directory
-s
summary version of output
-h
human-­rea­dable format
(converts bytes to MB, GB, etc.)
mkfs.type /dev/file
Creates filesystem of specified type on specified block device
(e.g.
mkfs.ext4 /dev/vdc1
)
mount /dev/file /dest
Takes filesystem on specified block device & mounts it in specified directory
umount mountpoint
Unmount the filesystem at the specified mountpoint
lsof mountpoint
List open files; shows which files are open in that filesystem
fuser mountpoint
Similar info as
lsof
, but less detailed
-m
shows only the PID associated with the open file

RPM Software Packages

rpm -qa
Query all; shows all software packages installed on the system
rpm -qf /file
Query file; asks RPM database which software package owns "/file"
rpm -qp
Query package; runs command against the .rpm file itself
-qpl
list of files in the .rpm package
-qpc
shows config files shipped with the package
-qpd
shows documen­tation files shipped with the package
-qpi
information about the package file (metadata, summary, software descri­ption, etc.)

Managing Software Packages (DNF)

Info Commands:
dnf info package
Shows package metadata, summary, descri­ption, etc.
(same as
rpm -qpi
, but package doesn't have to be installed)
dnf provides /file
Shows what software package provides the file
(same as
rpm -qf
, but package doesn't have to be installed)
dnf search 'gui'
Searches for "gui" based on package name & metadata
dnf search all 'gui'
Includes package descri­ption when searching for "gui"
dnf list 'x*'
Lists all available and/or installed software packages that begin with "x"
dnf group list
Shows list of available software groups
("Environment Groups­" = logical groupings of regular groups from "­Ava­ila­ble­" list)
dnf group info "Name"
Lists software packages included in specified group
dnf history
Displays history of DNF comman­ds/­actions
dnf history undo 3
Undoes number 3 from DNF history
(e.g. if it installed something, "­und­o" will uninstall it)
Downlo­ad/­Install Commands:
dnf download 
--resolve
package
Downloads software package & its depend­encies without installing them
dnf install package
Installs package & automa­tically resolves depend­encies
-y
auto-yes (for non-in­ter­active)
 
dnf remove package
Uninstalls package & any depend­encies (if they aren't being used elsewhere)
-y
auto-yes (for non-in­ter­active)
 
dnf update package
Redown­loads .rpm file & reinstalls newer version
-y
auto-yes (for non-in­ter­active)

Network Info Commands

ip -br addr
Shows current IP & netmask for all interfaces
ip link show
Link-level properties of all interfaces
(MAC addresses, etc.)
ip address show
Equivalent of
ipconfig

(same info as
link show
, plus IP addresses)
ip a s
abbrev­iated form
 
ip route show
Shows default route
ip r s
abbrev­iated form
 
ping
|
ping6
IPv4 ping | IPv6 ping
tracepath

tracepath6
Equivalent of
tracert
/
traceroute

IPv6 version
mtr address
"My tracer­out­e" program; gives extra info & continuous running stats
nmap -sS host
Scans ports on host; shows port number­/type, open state, associated service, etc.
ss
Sockets state inform­ation
-p
processes respon­sible for opening ports
-l
listening sockets
-u
UDP sockets
-n
translates names to numbers
(e.g. process name + PID)
-t
TCP sockets
 
nmcli connection show
Shows configured connection profiles
 
nmcli c s
abbrev­iated form
 
--active
only show currently active profiles
nmcli dev status
Status of interfaces (devices), incl. type & profile name
nmcli dev show int
Shows settings applied to the specified interface (device)

Network Config­uration Commands (NMCLI)

nmcli con add <specs>
[options]
Create a new connection profile
<sp­ecs> = required configs
[options] = other optional configs
<con-name "Name">
profile name
<type ethernet>
type of connection
<ifname eth0>
interface to associate with profile
[ipv4.method manual]
specifies manual IPv4 address config
(default=auto; only needed for manual)
[ipv4.a­ddr­esses 
addres­s/cidr]
manually set IPv4 address & subnet
[ipv4.dns x.x.x.x]
specify DNS server address
nmcli con mod "Name"
Modify properties of a connection profile
ipv4.g­ateway x.x.x.x
set IPv4 default gateway
+property.attribute 
value
add a value to an array of values
(e.g. add an IP address to list of addresses)
-property.attribute 
value
remove a value from an array of values
nmcli con up "Name"
Activates specified profile on whatever interface it's configured for
nmcli con reload 
"
Name"
Reloads profile after config changes
Note: Must re-
up
profile before new config will be applied to interface!

SSH

ssh user@ip.addr
Start ssh connection
-v
verbose; shows in detail what's happening while establ­ishing connection
-Y
enables graphical applic­ation support
-p port#
connect to ssh service not listening on default port 22
-i keyfile
reads private key from identity file for public key authentication
(not part of lecture; added from Google search when it came up in the quiz)
-o option dest_server


Example:
PreferredAuthenti
cations=password
configures options when connecting to specified destination
(Example: require a password; can use commas to specify multiple)
 
ssh-keygen
Generates a public­/pr­ivate key pair
-N '' -f filename
generates key pair without extra prompts
(
''
= no passph­rase;
-f
specifies non-de­fault file location)
 
ssh-co­py-id user@ip
Installs public key on destin­ation server
-i filepath
specifies which public key to install

Misc. Commands

whatis command
Displays a short, one-line summary of what the command does
file something
Tells you what kind of file "something" is
(More reliable than file extension)
echo
Creates output (
print
= Python equiva­lent)
date
Displaying, setting, calculating...
basically all things date-r­elated
+%F
display current date in intern­ational format (YYYY-­mm-dd)
+%R
display current time (24-hr clock)
+%s
display # of seconds since epoch
 
hostname
Shows the hostname of the current system. That is all.
host hostname
Shows IP address associated with specified hostname
hostna­mectl
Shows properties of current system (hostname, OS, kernel, etc.)
set-ho­stname 
new_name
sets hostname to "new_name"
 
sos report
Generates an SOS report
-l
lists available plugins for SOS
(recommended to pipe to
less
)
--upload
automa­tically associates generated report with your Red Hat account
(enables browsing the data via the web portal)
sleep seconds
Does nothing for specified time
(used for scripting)
tmux
Splits terminal window into multiple separate panes (I think)
gnome-­cha­racters
Opens window of gnome icons/­emotes
gnome-­cal­culator
Opens built-in calculator (like Windows)