SSH (Secure Shell) is used to securely connect to a remote machine.
- is program for logging into a remote machine and for executing commands on a remote machine. It is intended to provide secure encrypted communi‐ cations between two untrusted hosts over an insecure network. X11 connections, ar‐ bitrary TCP ports and UNIX-domain sockets can also be forwarded over the secure channel.
# Connect to a remote server
ssh username@hostname
# Connect using a specific port
ssh -p 2222 username@hostname
Changes the current directory.
# Change to the home directory
cd ~
# Change to the /etc directory
cd /etc
# Go up one directory level
cd ..
Lists directory contents.
- list information about the FILEs (the current directory by default). Sort en‐ tries alphabetically if none of -cftuvSUX nor --sort is specified.
# List files in the current directory
ls
# List all files, including hidden files
ls -a
# List files with detailed information
ls -l
Print the full filename of the current working directory.
pwd
Nano is a simple, easy-to-use text editor.
- is a small and friendly editor. It copies the look and feel of Pico, but is free software, and implements several features that Pico lacks, such as: opening multiple files, scrolling per line, undo/redo, syntax coloring, line numbering, and soft-wrapping overlong lines.
# Open a file in nano
nano filename.txt
# Save changes and exit
Ctrl + O, Enter, Ctrl + X
Vim is a powerful text editor.
- is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is especially useful for editing programs.
There are a lot of enhancements above Vi: multi level undo, multi windows and buffers, syntax highlighting, command line editing, filename completion, on-line help, visual selection, etc.. See ":help vi_diff.txt" for a summary of the dif‐ ferences between Vim and Vi
# Open a file in vim
vim filename.txt
# Enter insert mode
i
# Save changes and exit
:wq
Copies files and directories. Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
# Copy a file
cp source.txt destination.txt
# Copy a directory recursively
cp -r source_dir destination_dir
Moves or renames files and directories. Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
# Move a file
mv source.txt destination.txt
# Rename a file
mv oldname.txt newname.txt
Removes files or directories.
# Remove a file
rm filename.txt
# Remove a directory and its contents
rm -r directory_name
Changes file permissions. Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE.
# Make a file executable
chmod +x script.sh
# Set specific permissions
chmod 755 filename.txt
Changes file owner and group.
- changes the user and/or group ownership of each given file.
# Change the owner of a file
chown username filename.txt
# Change the owner and group of a directory recursively
chown -R username:group directory_name
Displays information about running processes.
- displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top instead
# Display all running processes
ps aux
# Display processes for a specific user
ps -u username
# To see every process running as root
ps -U root -u root u
Sends a signal to terminate a process.
- The default signal for kill is TERM. Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9, -SIGKILL or -KILL.
# Kill a process by PID
kill 1234
# Force kill a process
kill -9 1234
Displays real-time system information including processes.
- program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. The types of system summary information shown and the types, order and size of information displayed for processes are all user configurable and that configuration can be made persistent across restarts
top
An interactive process viewer.
- is a cross-platform ncurses-based process viewer. It is similar to top, but allows you to scroll vertically and horizontally, and interact using a pointing device (mouse). You can observe all processes running on the system, along with their command line arguments, as well as view them in a tree for‐ mat, select multiple processes and acting on them all at once.
htop
Reports disk space usage.
- displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown. Disk space is shown in 1K blocks by default, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
# Display disk usage in human-readable format
df -h
Estimates file space usage.
- Summarize disk usage of the set of FILEs, recursively for directories
# Display the size of the current directory and subdirectories
du -h
# Display the size of a specific directory
du -sh directory_name
Archives files.
- is an archiving program designed to store multiple files in a single file (anarchive), and to manipulate such archives. The archive can be either a regular file or a device (e.g. a tape drive, hence the name of the program, which stands for tapearchiver), which can be located either on the local or on a remote machine
# Create a tar archive
tar -cvf archive.tar file1 file2
# Extract a tar archive
tar -xvf archive.tar
# Create a gzipped tar archive
tar -czvf archive.tar.gz file1 file2
# Extract a gzipped tar archive
tar -xzvf archive.tar.gz
Compresses files.
- reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. (The default extension is z for MS‐ DOS, OS/2 FAT, Windows NT FAT and Atari.) If no files are specified, or if a file name is "-", the standard input is compressed to the standard output. Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links.
# Compress a file
gzip filename.txt
# Decompress a file
gunzip filename.txt.gz
Searches for files in a directory hierarchy.
- searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the out‐ come is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name. If no starting-point is specified, `.' is assumed
# Find a file by name
find /path/to/search -name filename.txt
# Find files modified in the last 7 days
find /path/to/search -mtime -7
Searches for patterns in files.
- searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.
# Search for a pattern in a file
grep 'pattern' filename.txt
# Search recursively in all files in a directory
grep -r 'pattern' /path/to/search
Concatenates and displays file content.
# Display file content
cat filename.txt
# Concatenate multiple files and display the output
cat file1.txt file2.txt
Views file content one page at a time.
- is a program similar to more(1), but it has many more features. Less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi(1). Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.)
# View a file
less filename.txt
# Scroll forward and backward
# Forward: Space or f
# Backward: b
Displays the last part of a file.
- Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name
# Display the last 10 lines of a file
tail filename.txt
# Display the last 50 lines of a file
tail -n 50 filename.txt
# Follow a file (useful for logs)
tail -f filename.txt
Displays the first part of a file.
# Display the first 10 lines of a file
head filename.txt
# Display the first 20 lines of a file
head -n 20 filename.txt
A powerful text processing language.
# Print the first column of a file
awk '{print $1}' filename.txt
# Print lines where the second column is greater than 100
awk '$2 > 100' filename.txt
A stream editor for filtering and transforming text.
# Replace 'old' with 'new' in a file
sed 's/old/new/g' filename.txt
# Delete lines containing a pattern
sed '/pattern/d' filename.txt
Securely copies files between hosts.
# Copy a file to a remote host
scp localfile.txt username@remotehost:/path/to/remote/directory
# Copy a file from a remote host
scp username@remotehost:/path/to/remotefile.txt localfile.txt
A fast and versatile file copying tool.
# Synchronize a local directory with a remote directory
rsync -avz /path/to/local/directory username@remotehost:/path/to/remote/directory
# Synchronize a remote directory with a local directory
rsync -avz username@remotehost:/path/to/remote/directory /path/to/local/directory
A non-interactive network downloader.
# Download a file from a URL
wget http://example.com/file.zip
# Download a file and save it with a different name
wget -O newfile.zip http://example.com/file.zip
A tool for transferring data from or to a server.
# Download a file from a URL
curl -O http://example.com/file.zip
# Download a file and save it with a different name
curl -o newfile.zip http://example.com/file.zip
A package handling utility for Debian-based systems.
# Update the package index
sudo apt-get update
# Install a package
sudo apt-get install package_name
# Remove a package
sudo apt-get remove package_name
A package manager for RPM-based distributions.
# Install a package
sudo yum install package_name
# Remove a package
sudo yum remove package_name
# Update all packages
sudo yum update
A package manager for Python packages.
# Install a package
pip install package_name
# Uninstall a package
pip uninstall package_name
# List installed packages
pip list
A platform for developing,
shipping, and running applications in containers.
# Pull an image from the Docker repository
docker pull image_name
# Run a container from an image
docker run image_name
# List running containers
docker ps
# Stop a running container
docker stop container_id
Controls the systemd system and service manager.
# Start a service
sudo systemctl start service_name
# Stop a service
sudo systemctl stop service_name
# Enable a service to start at boot
sudo systemctl enable service_name
# Disable a service from starting at boot
sudo systemctl disable service_name
# Check the status of a service
sudo systemctl status service_name
Queries and displays logs from journald.
# View all logs
sudo journalctl
# View logs for a specific service
sudo journalctl -u service_name
# Follow new log entries
sudo journalctl -f
Prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
# Display all network connections
netstat -a
# Display listening ports
netstat -l
# Display network statistics
netstat -s
Utility to investigate sockets.
# Display all open TCP connections
ss -t
# Display all open UDP connections
ss -u
# Display listening sockets
ss -l
Configures network interfaces.
# Display network interface information
ifconfig
# Assign an IP address to an interface
sudo ifconfig eth0 192.168.1.100
Shows/manipulates routing, devices, policy routing, and tunnels.
# Display network interfaces
ip a
# Assign an IP address to an interface
sudo ip addr add 192.168.1.100/24 dev eth0
# Display routing table
ip route
Sends ICMP ECHO_REQUEST packets to network hosts.
# Ping a host
ping hostname
# Ping a host with a specified number of packets
ping -c 4 hostname
Prints the route packets take to the network host.
# Trace the route to a host
traceroute hostname
Queries the Domain Name System (DNS) to obtain domain name or IP address mapping.
# Look up the IP address of a domain
nslookup domain.com
DNS lookup utility.
# Perform a DNS lookup
dig domain.com
# Perform a reverse DNS lookup
dig -x ip_address
Shows or sets the system's hostname.
# Display the hostname
hostname
# Set a new hostname
sudo hostname new_hostname
Displays the current username.
whoami
Executes a command as another user, typically the superuser.
# Run a command as the superuser
sudo command
# Edit a file with superuser privileges using nano
sudo nano filename.txt
Changes a user's password.
# Change your own password
passwd
# Change another user's password (superuser only)
sudo passwd username
Adds a user to the system.
# Add a new user
sudo adduser username
Modifies a user account.
# Add a user to a group
sudo usermod -aG groupname username
Removes a user from the system.
# Remove a user
sudo deluser username
Adds a new group.
# Add a new group
sudo groupadd groupname
Deletes a group.
# Delete a group
sudo groupdel groupname
Schedules periodic jobs.
# Edit the current user's crontab
crontab -e
# List the current user's scheduled jobs
crontab -l
Schedules a command to run at a specific time.
# Schedule a command to run at a specific time
echo "command" | at 10:00
Shows how long the system has been running.
uptime
Displays memory usage.
# Display memory usage in human-readable format
free -h
Reports virtual memory statistics.
# Display virtual memory statistics
vmstat
Reports CPU and I/O statistics.
# Display CPU and I/O statistics
iostat
Prints kernel ring buffer messages.
# Display all messages
dmesg
# Display messages and follow new ones
dmesg -w
Lists information about block devices.
# Display block devices
lsblk
Locates and prints block device attributes.
# Display block device attributes
blkid
Mounts a filesystem.
# Mount a filesystem
sudo mount /dev/sdX1 /mnt
Unmounts a filesystem.
# Unmount a filesystem
sudo umount /mnt
Manipulates disk partition table.
# View and edit disk partitions
sudo fdisk /dev/sdX
Builds a Linux filesystem.
# Create an ext4 filesystem
sudo mkfs.ext4 /dev/sdX1
Checks and repairs a Linux filesystem.
# Check and repair a filesystem
sudo fsck /dev/sdX1
Converts and copies a file.
# Create a bootable USB drive
sudo dd if=path/to/image.iso of=/dev/sdX bs=4M status=progress
A partition manipulation program.
# Start parted on a disk
sudo parted /dev/sdX
Provides fast, secure, and scalable system logging.
# Restart rsyslog service
sudo systemctl restart rsyslog
Rotates, compresses, and mails system logs.
# Manually rotate logs
sudo logrotate /etc/logrotate.conf
Controls the system hostname.
# Set the system hostname
sudo hostnamectl set-hostname new_hostname
Controls system time and date.
# Display current time settings
timedatectl
# Set the system timezone
sudo timedatectl set-timezone America/New_York
Accesses the hardware clock.
# Display the current hardware clock time
sudo hwclock
# Set the hardware clock to the current system time
sudo hwclock --systohc
Displays or sets the system date and time.
# Display the current date and time
date
# Set the date and time
sudo date MMDDhhmmYYYY
An arbitrary precision calculator language.
# Start bc interactive calculator
bc
Evaluates expressions.
# Perform arithmetic operations
expr 2 + 2
Builds and executes command lines from standard input.
# Use with find to delete files
find /path/to/search -name "*.tmp" | xargs rm
Creates an alias for a command.
# Create an alias
alias ll='ls -la'
# Remove an alias
unalias ll
Displays a line of text.
# Print a string
echo "Hello, World!"
# Print the value of a variable
echo $HOME
Displays or modifies the environment.
# Display all environment variables
env
# Run a command with modified environment variables
env VAR=value command
Sets environment variables.
# Set an environment variable
export VAR=value
# Export a variable to the environment
export PATH=$PATH:/new/path
Executes commands from a file in the current shell.
# Source a script
source script.sh
Displays the command history.
# Display command history
history
# Clear command history
history -c
Creates an alias for a command.
# Create an alias
alias ll='ls -la'
# Remove an alias
unalias ll
Displays the manual for a command.
# Display the manual for ls
man ls
Displays the information about commands.
# Display the info for ls
info ls
Displays a one-line description of a command.
# Display a one-line description of ls
whatis ls
Searches the manual pages for a keyword.
# Search for a keyword in the manual pages
apropos keyword
Locates the binary, source, and manual page files for a command.
# Locate the files for ls
whereis ls
Locates the executable file associated with a command.
# Locate the executable for ls
which ls
Describes a command.
# Describe the ls command
type ls
Executes commands in the Bourne shell.
# Run a shell script
sh script.sh
Executes commands in the Bourne-again shell.
# Run a shell script
bash script.sh
Executes commands in the Z shell.
# Run a shell script
zsh script.sh
Executes commands in the Korn shell.
# Run a shell script
ksh script.sh
Executes commands in the Debian Almquist shell.
# Run a shell script
dash script.sh
A terminal multiplexer.
# Start a new screen session
screen
# Reattach to a detached screen session
screen -r
A terminal multiplexer.
# Start a new tmux session
tmux
# Reattach to a detached tmux session
tmux attach