Show Menu
Cheatography

Linux cheatsheet Cheat Sheet by

A cheat sheet of the commands and hints I use most for Linux

Expansion

ls $(­which cp)
ls `­which cp`
Command expansion
mkdir {2017...2­020­}-{­01-12}
Brace expansion
echo text ~/*.txt {a,b} $USER
Variable expansion
echo "text ~/*.txt {a,b} $USER"
" expansion
echo 'text ~/*.txt {a,b} $USER'
' expansion (NONE)

Redire­ction & Commands

ls > file.txt
Standard Out
ls 2> file.txt
Standard Error
ls > file.txt 2>&1
ls &> file.txt
Standard Out & Error
ls | tee file | wc -l
Read stdin & writes stdout
cmd1 ; cmd2
Run cmd1 then cmd2
cmd1 && cmd2
Run cmd2 if cmd1 is OK
cmd1 || cmd2
Run cmd2 if cmd1 is KO

su / sudo

su [-[l]] [user]
Become another user, ROOT by default. If - or -l, load user envs and working dir.
su [user] -c 'command'
Execute command as user. Default root
sudo [-u user] command
Execute command as user. Default root
sudo -ll
Which commands I'm allowed to execute
visudo
Edit /etc/­sud­oers file. Example
teralco ALL=(root) NOPASSWD: /etc/i­nit.d/­jboss
teralco ALL=(j­boss) NOPASSWD: /bin/kill

UFW

ufw status [verb­ose­|­num­ber­ed]
Show status and rules
ufw default deny incoming
ufw default allow outgoing
Deny all incoming traffic by default
Allow all outgoing traffic by default
ufw [allo­wdeny] from IP to [any|i­nte­rfa­ce_­name] [proto tcp|udp] port PORT
Full allow/deny rule
ufw [allo­wdeny] servi­ce_­name
Allow/deny a service (ssh www ftp ...)
ufw [enab­le­|d­isa­ble]
ufw delete [rule­|­num­ber]
 

Alt commands

/var/­log­/me­ssages & /var/l­og/­syslog
System log files
scr­ipt file
Record session commands in file
nohup command &
Keep command running even after close session
tar -Jxvf file.t­ar.xz [-C dest_f­older]
Extract xz (higher compress ratio)
zip -FF x.zip --out Y.zip && unzip Y.zip
Merge zip files (x.zip, x.z01, x.z02)
ps aux --sort -rss
Higher memory consum­ption processes
fdisk -l
List partition tables
dd bs=4M if=inp­ut.iso of=/de­v/sd? conv=f­dat­async
Burn iso in device

Searches

find path -name *.log -type f -mtime +5 -exec rm -rvf {} \;
Find and delete files older than 5 days
find path -type f -printf "­%s­\t%p­\n" | sort -rn | head -10
Find the 10 biggest files
grep -rlie 'pattern' path
Find files with pattern content
du -cks path/* | sort -rn | head -10
Find biggest dirs
du -sh path
Dir size

Permis­sions

chmod [u|g|o­][+­|-|­=][rwx] dir_file
Change permis­sions
chmod u[+|­-]s file
Set uid. File is always exec as owner user
chmod g[+|­-]s dir
Set gid. New files in folder are always owned by folder owner
chmod [+|-]t dir
Sticky Bit. Files in dir can only be renamed or removed by owner or root
umask xxxx
Set default permis­sions to dirs
 

Enviroment

/etc/profile
/etc/bash.bashrc
Global env config files
~/.bashrc
~/.profile
User env config file
printenv
Print defined env vars
alias
Show defined aliases
export VAR
Make a VAR available to child process
sou­rce script
. script
Load a file into current script or shell session

Network

ip addr
Show ips
ifu­p­|­ifd­own inter­fac­e_n­ame
Up or down an interface
/etc/­net­wor­k/i­nte­rfaces
File interfaces are defined
hos­tna­mectl [set-h­ostname hostn­ame]
Manage hostname (/etc­/ho­stn­ame) without restarting
net­stat -tuln
All tcp and udp listening ports
ss -tuln
All tcp and udp listening ports
nc -vz host port
Scan port in host

SSH

ssh­-ke­ygen [-f /etc/s­sh/­ssh­_ho­st_­rsa­_key] -t rsa -b 4096
Generate ssh rsa key. By default ~/.ss­h/i­d_rsa
ssh­-co­py-id [-i ~/.ssh­/mykey] user@host
Copy ssh key in remote host (~/.s­sh/­aut­hor­ize­d_k­eys). By default ~/.ss­h/i­d_rsa.
ssh user@host command
Exec command in host
ssh -J hostA hostB
Connect to hostB through hostA (Jumping). Identi­fic­ation in localhost. Can use Pro­xyJ­ump in ~/.ss­h/c­onfig
ssh -D port -fCqN user@host
Proxy socket (HTTP and HTTPS traffic) through port.
ssh -nNT -L 9000:r­emo­te_­ser­ver:80 user@host
Tunneling (Local port forwar­ding). Map remot­e_s­erv­er:80 into local­hos­t:9000 through host
ssh -nNT -R 9000:l­oca­lho­st:3000 user@host
Tunneling (Remote port forwar­ding). Map local­hos­t:3000 into host:­9000

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

Nice, I will use it.

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Linux Command Line Cheat Sheet
          mod_rewrite Cheat Sheet
          Vim NERDTree Cheat Sheet