Skip to content

Linux Basics#

Resources#

Guides#

References#

Help/Manuals#

  • ManKier explain: concise pretty man pages with an API
  • explainshell: match command-line arguments to their help text
  • tldr pages: more concise man pages
  • manned: man page versions across a range of distributions
  • ubuntu: ubuntu man pages and documentation

Communities#

Tools#

Filesystem#

  • which: display executable's location
  • type: describe a command
  • rm: delete directory

    Bash
    rm -rf
    

  • find: find file/directory recursively

    Bash
    find . -iname _gsdata_ -exec rm {} +
    find . -type d -name _gsdata_ -print -exec rm -rfv {} +
    find . -type f \( -name .DS_Store -o -name Thumbs.db \) -print -exec rm {} +
    

  • tree: show directory structure as tree

    Bash
    tree -d [dir]   # list directories only
    tree -ah        # include hidden, human readable
    tree -fp -L <n> # include paths, permissions, limit up to N levels
    

  • df: display free disk space

    Bash
    df -lhT # show usage of all mounted filesystems
    

  • du: disk usage/size of directory

    Bash
    du -sh fooDir/
    

Package Management#

Command Description
apt list --installed list installed pkgs
sudo apt policy list installed `repositories'
apt show <pkg> show dependencies of pkgs
sudo apt install <pkg> install <pkg>
sudo apt remove <pkg> uninstall <pkg>
sudo apt autoremove uninstall unused dependencies e.g. <pkg> leftover deps that were auto installed but longer required
sudo apt purge <pkg> uninstall <pkg> configuration files e.g. located under /etc

Console Management#

  • env: print the environment variables

  • tail: displays the last part of a file

Bash
tail -f [file]      # outputs last lines in realtime
tail -n 100 [file]  # outputs last n lines
  • bind: configure keybinds for terminal

  • bind: query keybinds

    Bash
    bind -l      # List all bindable readline functions
    bind -v      # List variables and values
    bind -psX    # List keys bound to readline functions, macros, and shell executions
    bind -q <fn> # Query about which keys invoke the named function
    

  • bind: change keybinds

    Bash
    bind -u <fn>                 # Unbind all keys which are bound to the named function
    bind -r <keys>               # Remove the binding for <keys>
    bind -f <filename>           # Read key bindings from <filename>
    bind -x '"<keys>": "<cmds>"' # Execute <cmds> when <keys> is entered
    

  • less: console pager

Navigation Key Action
down/up,j/k move down/up one line
space bar/b move down/up one page
left/right scroll left/right horizontally
g/G,Home/End go to start/end line
/pattern/?pattern search forward/backward for pattern
n/N go to next/previous occurrence
q Quit less

Networking#

  • list connections
Bash
netstat --all | head -n 15
sudo netstat --tcp --udp --listening --program --numeric --extend
  • query interaces
Bash
ip a
netstat -ie

Permissions#

  • stat: pretty print stats about file
Bash
stat [file]
  • permission attributes
Text Only
.rwxrwxrwx  path/to/file
││││││││││ 
└│││││││││─ file type attribute
 │││││││││   -: file
 │││││││││   d: directory
 │││││││││   l: symbolic link
 │││││││││   c: character special file representing byte stream device e.g. /dev/null
 │││││││││   b: block special file representing block stream device e.g. hard drive
 │││││││││
 ├││││││││─ u: user owner
 └││││││││─  r: read
  └│││││││─  w: write
   └││││││─  x: execute
    ││││││
    ├│││││─ g: group owner
    └│││││─  r: read
     └││││─  w: write
      └│││─  x: execute
       │││
       ├││─ o: other
       └││─  r: read
        └│─  w: write
         └─  x: execute
  • id: display user identity

  • umask: set the default file permissions

  • chmod: change permissions to folders and files

Bash
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +
  • chmod recursively to 775/664
Bash
chmod -R a=,a+rX,u+w,g+w /some/path/here
          ^  ^    ^   ^ adds write to group
          |  |    | adds write to user
          |  | adds read to all and execute to all folders (which controls access)
          | sets all to `000`
  • chown: change ownership to user/group recursively
Bash
chown -R user:group /some/path/here

Process Management#

  • id: find UID/GID for user

    Bash
    id [username]
    

  • ps: list running processes

    Bash
    ps aux
    pstree
    

  • pkill: kill process by name

    Bash
    pkill chrome
    

Services#

  • systemd: initialization system and service manager

  • systemd quickstart guide

  • systemctl: manage services/daemons

  • systemctl quickstart guide

    • systemctl start [name.service]
    • systemctl stop [name.service]
    • systemctl restart [name.service]
    • systemctl reload [name.service]
    • systemctl status [name.service]
    • systemctl is-active [name.service]
    • systemctl list-units --type service --all
    • unit types
    Bash
    $systemctl list-unit --type=help
    service   # service
    target    # group of units
    mount     # filesystem mountpoint
    automount # filesystem auto-mountpoint
    device    # kernel device names, which you can see in sysfs and udev
    path      # file or directory
    scope     # external processes not started by systemd
    slice     # a management unit of processes
    snapshot  # systemd saved state
    socket    # IPC (inter-process communication) socket
    swap      # swap file
    timer     # systemd timer
    
  • list services

    Bash
    systemctl | grep running                                       # Loaded+Active
    systemctl --no-pager | grep running | column -t                # For more readable output
    systemctl list-units --no-pager --all                          # Loaded services: Active+Pending+Failed+Inactive+Dependents
    systemctl list-units --no-pager --all --state=inactive         # Loaded services: Inactive
    systemctl list-units --no-pager --type=service                 # Loaded services: Active+Pending+Failed
    systemctl list-units --no-pager --type=service --state=running # Loaded services: Running
    systemctl list-unit-files --no-pager --type=service            # Installed services: Loaded+Unloaded
    
  • start/stop/restart the specified service

    Bash
    systemctl start [service]
    systemctl stop [service]
    systemctl restart [service]
    
  • enable/disable service to auto start at boot time

    Bash
    systemctl enable [service]
    systemctl disable [service]
    
  • show/check service current status/runtime information

    Bash
    systemctl status [service]
    systemctl is-active [service]
    systemctl is-enabled [service]
    systemctl is-failed [service]
    systemctl list-dependencies [service]
    

Diagnostics#

  • syslog: a protocol for transferring log messages
  • dmesg: command that prints kernel logs

    Bash
    dmesg -H         # command that prints kernel logs
    

  • journalctl: service that collects logs from systemd

    Bash
    journalctl                              # all boot log entries
    journalctl -k                           # all boot kernel entries
    journalctl -b                           # current boot entries
    journalctl -b -k                        # current boot kernel entries
    journalctl -b --priority=err            # all error or higher logs since last boot
    journalctl -b --priority=err..warning   # all warning to notice since last boot
    
    # Log Levels
    #  emerg (0): system is unusable
    #  alert (1): action must be taken immediately
    #   crit (2): critical conditions
    #    err (3): error conditions
    #   warn (4): warning conditions
    # notice (5): normal but significant condition
    #   info (6): informational
    #  debug (7): debug-level messages