General Info

  • uname -a – Print operating system name
  • lsb_release -a – print distribution specific information
  • printenv – print enviroment variables
  • w – display who is logged in and what they are doing
  • pwd – display Present Working Directory
  • which <CMD> – print the location of the CMD executable

Hardware Info

Permissions

  • chmod – modify permissions of a file.
    • example: chmod 600 file.txt
    • OGA (Owner, Group and All) O, G and A are numbers from 0 to 7.
    • Each number can be seen as the sum of these three numbers:
      • 4 - Read (100 in binary)
      • 2 - Write (010 in binary)
      • 1 - eXecute (001 in binary)
    • You can also add Read, Write or eXecution permission for all users: chmod +[r|w|x] file.txt
  • chown – change owner of a file.
    • example: chown user:group /dir

Basic Functions

  • shutdown – shutdown the computer

  • date
    • get current time:
      date +%s
    • get time from a specific moment:
      date +%s -d "2016-08-17 20:15:03"
    • Convert and print number of seconds to “day hour minute second” format:
      echo "Duration: $(($d/86400))d $(($d/3600%24))h $(($d/60%60))m $(($d%60))s"
  • find – walk a file hierarchy
    • parameters
      • -name [<NAME>|"<NAME>"] – True if last component of the pathname being examined matches pattern.
      • -path [<PATH>|"<PATH>"] – True if the pathname being examined matches pattern.

Processes

Detach scripts/commands from shell:

Reference: nohup Execute Commands After You Exit From a Shell Prompt

Run commands in the background:
You can append & at the end of a command to run in the background.
This is useful when you have only one terminal and you want to run a command that takes too long to end or you want it to run it indefinitely, e.g.: tcpdump and you also want to execute another command in parallel.
Reference: Unix job control command list

Also:

  • jobs – List background processes
  • pgrep <process_name> – get pid of a process
  • kill PID – kill the process with this PID. Without parameters equals to pressing Ctrl+C when the process is in the foreground.

Filesystem

Common directories:

  • /home – Linux User Accounts Home Directory and FTP server home directory (some distros)
  • /etc – Linux Server’s and Applications System’s Configuration Files
  • /usr – Linux System Files (Shareable, Read-only Data)
  • /bin – Binary Files for user applications
  • /sbin – Binary Files for system programs

SSH Filesystem:

# Install SSHFS
sudo apt-get install sshfs
#Create a directory to mount the filesystem
mkdir /mnt/remote_sshfs
#Mount the filesystem
sshfs user@host:dir /mnt/remote_sshfs
#Unmount the filesystem
umount /mnt/remote_sshfs

Reference: How To Use SSHFS to Mount Remote File Systems Over SSH

Disks

partition tables:

info about disks:

  • lshw – is a small tool to provide detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc.
  • lshw -class disk – get info about disks
    Reference: Retrieve Disk Info from the Command Line
  • lsblk – view mounted devices
  • du – disk usage
    useful parameters:
    • -h : for human readable sizes
    • -d NUMBER : the maximum depth in the output

    If you want to order the result by size you can send the output to sort like this: du -h | sort -h
    Reference: Size of a directory & Free disk space

modify labels:

  • e2label – change the label of an ext2,3,4 partition
  • ntfslabel – change the label of a ntfs partition (from ntfs-3g package)

clean disks:

Text Editor

User Management

  • adduser <username> – asking for password, will create home directory and asking lots of information about the user.
  • useradd <username> – not asking for password, no home directory just only add new user.

Package Manager

APT

  • apt-get install --only-upgrade <package> – to only upgrade a specific package
  • apt show [package] – info about a package
  • sudo rm /var/lib/apt/lists/lock – unlock apt-get when it get locked (sometimes this happens when you Ctrl+C apt-get when it’s running)

logs:

All actions with apt (apt-get) are logged. These files are available in /var/log/apt/. To view the most recent history log, execute: less /var/log/apt/history.log.
These logs gets rotated (every month I guess), old files will be suffixed with a number and compressed. So to view the next history log, use: zless /var/log/apt/history.log.1.gz.

RPM

SSH

  • ssh-add -K ~/.ssh/<private_key_file> – to add a private key to the keychain

Debug

view shared/dynamic libraries used by a process:

  • ldd (Linux)
  • otool -L (MacOS)

Multiple terminals

  • tmux – terminal multiplexer
    • Ctrl+b % – Split pane horizontally
    • Ctrl+b " – Split pane vertically
    • Ctrl+b <arrow-key> – Switch pane
    • Ctrl+d – Close pane
    • Ctrl+b d – Detach current session
    • Ctrl+b D – Detach specific session (selected from list)
    • tmux ls – view running session
    • tmux attach -t 0 – attach to session 0
    • tmux new -s database – create a session with name
    • tmux rename-session -t 0 database – rename existing session
    • tmux attach -t database – attach to a named session
    • Ctrl+b ? – list of available commands
    • Ctrl+b z – go fullscreen (press again to go restore)
    • Ctrl+b Ctrl+<arrow-key> – Resize pane in direction of key
      Reference: A quick and easy guide to tmux

Network

Tags: linux tools