Skip to content

Linux Command Line Quick Guide

Commands with sudo

Sometimes when running a command with sudo you'll encounter an error, for example

bash
$ sudo nmap -sn 192.168.1.0/24
sudo: nmap: command not found
$ sudo nmap -sn 192.168.1.0/24
sudo: nmap: command not found

This is because the root user entered when using sudo doesn't share the existing PATH. To resolve this, do one of the following

  1. Use which, for example sudo $(which nmap)
  2. Issue visudo and comment out the line the begins with Defaults secure_path.

find

  • -type X indicate the type of object trying to be found, options include f for file, d for directory

fd

  • Rust-based alternative to find, not installed by default.

Working with Compilers & Linkers

ld --verbose -l<library>

When you program in C or C++, your compiler takes your program and turns them into binary executables. Sometimes it's useful to look inside a binary. To view a binary you can use the bless hex editor.

strings <file>

  • Prints the strings in the binary <file> to stdout

readelf --symbols <file>

  • Prints all of the symbols in the binary, including what they are (functions, objects, etc.), their sizes, and their memory addresses.

objdump -t <file>

  • Displays very similar information to readelf --symbols

objdump -s <file>

  • Displays information about the various sections in your binary. The most relevant are usually the text segment (where most of your code goes) and rodata segment (contains most of your static strings). Segments are segments of memotry, sections are logical sub-peices

objdump -d <file>

  • Dissassembles the binary into assembly

objdump -p <file>

  • Displayed object format specific headers. Useful for viewing the dependencies that are given to your program at compile time.

Disks and Block Devices

losetup: Setup loop devices

df

  • Short for disk free, use this command to display free disk space and other information on mounted block devices -h: Uses a human readable format. -i: Shows information about inodes. <path_to_directory_or_file>: Shows the file system and usage statistics for a given file or directory.

Networks and File Transfers

scp

  • Secure file copy over SSH Usage:

rsync

  • Syncs directories over a network connection. Often a better alternative to scp when working with more than a few files. -v: Verbose mode, show details in console -a: Archive mode (alias for -rlptgoD), shortcut to recurse and preserve file state -r: Recursive mode, also copy all subdirectories and files -l: Preserve symlinks -p: Preserve permissions -t: Preserve modification times -g: Preserve the group of the copied items -o: Preserve the owner of the copied items --progress: Display progress during transfer --exclude: Exclude the specified directory from being copied

[! Example]

sh
rsync -va -e 'ssh -p 2999' \
      --progress \
      --exclude 'build' --exclude '.git' \
      your_project user@remote_ip:~
rsync -va -e 'ssh -p 2999' \
      --progress \
      --exclude 'build' --exclude '.git' \
      your_project user@remote_ip:~

This will sync the folder your_project to the home directory of user@remote_ip over SSH non-default port 2999.

traceroute

telnet

netcat

socat

tcpdump

netstat

ss: Socket statistics

tc

tshark: Network analyzer that can capture packets from a live network.

Monitoring System Processes

Every program on your system is running in a process. Each process has an associated process ID (PID). The PID is required when issing a command such as kill or nice. Processes are also run with a controlling terminal (TTY). If a process doesn't have a controlling terminal (e.g. they were initialized at boot time) are shown with ?. Processes also have a CPU time (TIME), which denotes the amount of CPU time used by the process (different that run time). Each processes is initialized with a specific command (CMD).

ps

  • This command gives a user access to view relevant information about processes running on a system.     x: View all processes associated with the current user.     e: View all processes in the system.     H: Shows process heirarchy.     aux: Combines the a, u, and x commands. Useful for majority of                              troubleshooting tasks.

top or htop

  • This command provides much of the same information ps aux provides, but it dynamically updates and provides a quasi-graphical interface for a user to interact with to filter/manage system processes. It also shows the same information as uptime

kill: to be filled

nice: to be filled.

ln

  • Use this command to create symbol/soft links

Example

To create a symbolic link use the command ln -s <link-target> <symbolic-link>. To see your symbolic links you can use the ls -lah command. This will show the name of the link (<symbolic-link>) and the object its linked to (<link-target>). To remove a symbolic link you can either use the rm or unlink command, though I prefer to use unlink since there's a lower chance of making unrecoverable errors, especially when working in below the /home directory.

sh
$ touch dummy.sh
$ ln -s dummy.sh dummy-link.sh
$ ls -lah | grep dummy
  rwxrwxrwx   1  braden  braden  8 B  Fri Jan 21 12:56:12 2022  dummy-link.sh  dummy.sh
  rw-rw-r--   1  braden  braden  0 B  Fri Jan 21 12:54:42 2022  dummy.sh
$ unlink dummy-link.sh
$ ls -lah | grep dummy
  rw-rw-r--   1  braden  braden  0 B  Fri Jan 21 12:54:42 2022  dummy.sh
$ touch dummy.sh
$ ln -s dummy.sh dummy-link.sh
$ ls -lah | grep dummy
  rwxrwxrwx   1  braden  braden  8 B  Fri Jan 21 12:56:12 2022  dummy-link.sh  dummy.sh
  rw-rw-r--   1  braden  braden  0 B  Fri Jan 21 12:54:42 2022  dummy.sh
$ unlink dummy-link.sh
$ ls -lah | grep dummy
  rw-rw-r--   1  braden  braden  0 B  Fri Jan 21 12:54:42 2022  dummy.sh

Working with the Filesystem

tee

  • Write to both stdout and specified files

stat

  • Provides details information about a specified file/inode.

The ls command reads from a fundamental filesystem data strucuture called an inode. There is an associated inode for each file, directory, and socket on a system, which holds metadata about file, directory, or socket. There is a ceiling on the number of allowable inodes on a computer.

Working with Compression and Archives

ar

  • Creates, modifies, and extracts archives vx: Verbosely extracts contents of the archive

tar

  • Manages tape archives (TAR) x: Extract the archive c: Compress a folder v: Use verbose mode t: List contents of the archive (without extracting) f: Specify target file Note the following two options can be used but are inferred by the file extension z: Filter through gzip J: Filter through xz Examples
sh
tar xvf <archive>.tar.gz  # <- Uncompress GZ and extract TAR
tar xvf <archive>.tar.xz  # <- Uncompress XZ and extract TAR
tar cvf ARCHIVE.tar.gz path/to/dir # <- Compress and archive dir into TAR.GZ
tar cvf ARCHIVE.tar.xz path/to/dir # <- Compress and archive dir into TAR.XZ
tar xvf <archive>.tar.gz  # <- Uncompress GZ and extract TAR
tar xvf <archive>.tar.xz  # <- Uncompress XZ and extract TAR
tar cvf ARCHIVE.tar.gz path/to/dir # <- Compress and archive dir into TAR.GZ
tar cvf ARCHIVE.tar.xz path/to/dir # <- Compress and archive dir into TAR.XZ

dpkg

  • Manages DEB-type archives -l: Lists install DEB packages -L/--listfiles <package_name>: Shows all files in specified installed DEB archive -c <package_name>.deb: Shows structure of specified DEB archive -b <package_name>: Packages specified folder into a DEB archive (see making-deb-packages)

zip

  • Manages ZIP compressed archives -r: Recursive Examples
sh
zip -r package.zip path/to/dir # <- Compress and archive into ZIP
zip -r package.zip path/to/dir # <- Compress and archive into ZIP

7z

  • Manages 7z compressed archives a: Add files or directories to archive x: Extract files from archive Examples
sh
7z a package.7z path/to/dir # <- Compress and archive into 7Z
7z x package.7z # <- Uncompress and extract 7Z
7z a package.7z path/to/dir # <- Compress and archive into 7Z
7z x package.7z # <- Uncompress and extract 7Z

Working with Strings

cut

  • Extract lines or items within a line from a file or standard input. -d: Delimiter used to 'cut' the input string -f: Select field(s) cut by delimiter
sh
$ python3 --version
Python 3.8.10
$ python --version | cut -d ' ' -f 2
3.8.10
$ python3 --version
Python 3.8.10
$ python --version | cut -d ' ' -f 2
3.8.10

sed

  • String manipulation tool

Working with Serial Devices

setserial

Working with Users and Groups

cat /etc/passwd:

  • lists all users on the computer and their information, passwords are encrypted in the /etc/shadow file.

Misc

trap: https://www.linuxjournal.com/content/bash-trap-command

wc -l counts the number of 'words' or with -l number of lines in the terminal output

Tip

Use watch "ss -t | grep 127.0.0.1 | wc -l" to see the number of sockets connected to localhost 127.0.0.1