Cheatography
                https://cheatography.com
            
        
        
    
                   
                            
    
                    A cheat sheet of the commands and hints I use most for Linux
                    
                 
                    
        
        
            
    
        
                            
        
                
        
            
                                
            
                
                                                | Expansion
                        
                                                                                    
                                                                                            | ls $(which cp)
 ls `which cp`
 | Command expansion |  
                                                                                            | mkdir {2017...2020}-{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) |  Redirection & Commands
                        
                                                                                    
                                                                                            |  | Standard Out |  
                                                                                            |  | Standard Error |  
                                                                                            | ls > file.txt 2>&1
 ls &> file.txt
 | Standard Out & Error |  
                                                                                            |  | Read stdin & writes stdout |  
                                                                                            |  | Run cmd1 then cmd2 |  
                                                                                            |  | Run cmd2 if cmd1 is OK |  
                                                                                            |  | Run cmd2 if cmd1 is KO |  su / sudo
                        
                                                            
                            | Become another user,  ROOT
  by default.  If  -
  or  -l
 , load user envs and working dir. |  
                            | Execute command as user. Default  root
 |  
                            | Execute command as user. Default  root
 |  
                            | Which commands I'm allowed to execute |  
                            | Edit  /etc/sudoers
  file. Example   teralco ALL=(root) NOPASSWD: /etc/init.d/jboss
 teralco ALL=(jboss) NOPASSWD: /bin/kill
 |  UFW
                        
                                                            
                            | ufw status [verbose|numbered]
Show status and rules |  
                            | ufw default deny incoming
 ufw default allow outgoing
Deny all incoming traffic by defaultAllow all outgoing traffic by default
 |  
                            | ufw [allow|deny] from IP to [any|interface_name] [proto tcp|udp] port PORT
Full allow/deny rule |  
                            | ufw [allow|deny] service_name
Allow/deny a service ( ssh www ftp ...
 ) |  
                            |  |  
                            |  |  |  | Alt commands
                        
                                                            
                            | /var/log/messages & /var/log/syslog
System log files |  
                            | Record session commands in file |  
                            | Keep command running even after close session |  
                            | tar -Jxvf file.tar.xz [-C dest_folder]
Extract xz (higher compress ratio) |  
                            | zip -FF x.zip --out Y.zip && unzip Y.zip
Merge zip files (x.zip, x.z01, x.z02) |  
                            | Higher memory consumption processes |  
                            | List partition tables |  
                            | dd bs=4M if=input.iso of=/dev/sd? conv=fdatasync
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 |  
                            | Dir size |  Permissions
                        
                                                            
                            | chmod [u|g|o][+|-|=][rwx] dir_file
Change permissions |  
                            | Set uid. File is always exec as owner user |  
                            | Set gid. New files in folder are always owned by folder owner |  
                            | Sticky Bit. Files in dir can only be renamed or removed by owner or root |  
                            | Set default permissions to dirs |  |  | Enviroment
                        
                                                                                    
                                                                                            | /etc/profile/etc/bash.bashrc
 | Global env config files |  
                                                                                            |  | User env config file |  
                                                                                            |  | Print defined env vars |  
                                                                                            |  | Show defined aliases |  
                                                                                            |  | Make a VAR available to child process |  
                                                                                            |  | Load a file into current script or shell session |  Network
                        
                                                            
                            | Show ips |  
                            | ifup|ifdown interface_name
Up or down an interface |  
                            | /etc/network/interfaces
File interfaces are defined |  
                            | hostnamectl [set-hostname hostname]
Manage hostname ( /etc/hostname
 ) without restarting |  
                            | All tcp and udp listening ports |  
                            | All tcp and udp listening ports |  
                            | Scan port in host |  SSH
                        
                                                            
                            | ssh-keygen [-f /etc/ssh/ssh_host_rsa_key] -t rsa -b 4096
Generate ssh rsa key. By default  ~/.ssh/id_rsa
 |  
                            | ssh-copy-id [-i ~/.ssh/mykey] user@host
Copy ssh key in remote host ( /.ssh/authorized_keys
 ). By default  /.ssh/id_rsa
 . |  
                            | Exec command in host |  
                            | Connect to hostB through hostA (Jumping). Identification in localhost. Can use  ProxyJump
  in  ~/.ssh/config
 |  
                            | ssh -D port -fCqN user@host
Proxy socket (HTTP and HTTPS traffic) through port. |  
                            | ssh -nNT -L 9000:remote_server:80 user@host
Tunneling (Local port forwarding). Map  remote_server:80
  into  localhost:9000
  through host |  
                            | ssh -nNT -R 9000:localhost:3000 user@host
Tunneling (Remote port forwarding). Map  localhost:3000
  into  host:9000
 |  | 
            
                            
            
            
        
        
        
        
        
            
    
        
          
Created By
Metadata
Comments
Nice, I will use it.
Add a Comment
Related Cheat Sheets