Last active
April 19, 2018 12:42
-
-
Save robinmitra/9157055 to your computer and use it in GitHub Desktop.
Linux (specifically Ubuntu) commands and techniques
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Display current date and time (including timezone) | |
date | |
# Change timezone | |
sudo dpkg-reconfig tzdata | |
# or | |
sudo dpkg-reconfigure tzdata | |
# restart cron as otherwise the time will keep running on UTC | |
/etc/init.d/cron stop | |
/etc/init.d/cron start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## General commands | |
# Truncate a file - note: if superuser privilege is needed for truncating, enclose the below commands in single quotes and run as rooted bash, i.e. sudo bash -c '<command>' | |
> <file> | |
# or | |
cat > <file> | |
# or | |
cat /dev/null > <file> | |
# Append a file to another file | |
cat <content> >> <file> | |
# Convert Mac line endings (Ctrl-r) into Unix ones (Ctrl-n) | |
cat <input-file> | tr "\r" "\n" > <output-file> | |
# Print first/last 10 lines of a file | |
# first 10 lines | |
head <file> | |
# last 10 lines | |
tail <file> | |
# Print first/last n lines of a file | |
# first n lines | |
head -n <num> <file> | |
# last n lines | |
tail -n <num> <file> | |
# Describe an application (provided it has a man page) - e.g. nginx, apache2, mysql | |
whatis <service> | |
# Download a file and pass the output to a command | |
wget -O - <file-url> | <command> | |
# Download a file, following redirects if present (e.g. Sourceforge) | |
curl -L '<file-url>' | |
# Set a temporary environment variable | |
export <VARIABLE>=<value> | |
# Print all environment variables | |
printenv | |
#or | |
env | |
# Print the value of a particular environment variable | |
printevn <VARIABLE> | |
# or | |
echo $<VARIABLE> | |
# Nullify an environment variable | |
export <VARIABLE>= | |
# Unset an environment variable | |
unset <VARIABLE> | |
# Un-export an environment variable, thereby demoting it to become a shell variable while still retaining its value | |
export -n <VARIABLE> | |
# Copy a file's contents into clipboard (assuming xclip is installed) | |
xclip -sel clip < <file> | |
# Modify text in-place in a file | |
sed -i "s/<original>/<new>/g" <file> | |
# Copy a local file to a remote host via SSH, compressing the file during transfer and showing transfer progress | |
rsync -vz --progress -e ssh <source-file> <dest-host>:<dest-path> | |
# Copy a file from one remote host to another | |
rsync -vz --progress -e ssh <source-host>:<source-file> <dest-host>:<dest-path> | |
# Move the cursor to start of the current line of text in terminal | |
ctrl + a | |
# or to the end of the line | |
ctrl + e | |
# Clear the current line and place cursor at the start in terminal | |
ctrl + u | |
# Comment out the currently types line of text in the terminal | |
shift + 3 | |
# Add sudo before the currently entered statement | |
ctrl + 3, ctrl + 3 | |
# Remove all characters on the right of the cursor | |
ctrl + k | |
# Remove word before the cursor | |
ctrl + w | |
# Undo deleted characters | |
ctrl + y | |
# Switch to Bash shell | |
bash | |
# Switch to Zsh Shell | |
zsh | |
# Compute and show MD5 checksum | |
md5sum <file> | |
# Check whether MD5 checksum (in the checksum file) matches the actual file's checksum | |
md5sum -c <checksum-file> | |
# Compute and show SHA1 checksum | |
sha1sum <file> | |
# Check whether SHA1 checksum (in the checksum file) matches the actual file's checksum | |
sha1sum -c <checksum-file> | |
# Set default editor | |
sudo update-alternatives --config editor # if it still doesn't work, set the value of 'EDITOR' environment variable to 'vim' in the user profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show version | |
openssl version | |
# Show built date for the installed version | |
openssl version -b | |
# Create a self signed certificate and a private key | |
sudo openssl req -x509 -nodes -newkey rsa:2048 -days 365 -keyout <key-file>.key -out <certificate-file>.crt | |
# Create a certificate signing request (CSR) and a private key | |
sudo openssl req -new -nodes -newkey rsa:2048 -keyout <key-file>.key -out <csr-file>.csr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Package Management commands in Ubuntu | |
# Remove a package along with all of its configuration - note: this only removes system-wide configuration data, and not user-specific ones | |
sudo apt-get remove --purge <package> | |
# or simply | |
sudo apt-get purge <package> | |
# Check if a package is installed | |
dpkg -l <package> | |
# or, show similarly named packages | |
dpkg -l | grep <package> | |
# Show detailed package information | |
dpkg -s <package> | |
# Search for a package in the cached repository index (searches for the package text in the title as well as description text) | |
apt-cache search <package> | |
# Search for a package by title only | |
apt-cache search --names-only <package> | |
# Search for a package using Regex and by title only | |
apt-cache search --names-only '<package-regex>' | |
# Show package information | |
apt-cache show <package> | |
# Show extended package dependency information (e.g. reverse dependencies) | |
apt-cache showpkg <package> | |
# Show dependencies for a package | |
apt-cache depends <package> | |
# Show reverse dependencies for a package (i.e. list of packages that depend on a given package) | |
apt-cache rdepends <package> | |
# Show policy information regarding a package (basically shows installed and available versions) | |
apt-cache policy <package> | |
# Add an apt key (e.g. GPG) | |
sudo apt-key add <key> | |
# Download an apt key and add it | |
wget -0 - <key-url> | sudo apt-key add - | |
# List all keys | |
apt-key list | |
# Delete a key (the key ID is the idenfitier after the '/' and can be seen by listing all keys) | |
apt-key del <key-id> | |
# Remove a package's dependencies - note: this should only be used for dependencies of a meta package as it's got the potential to remove everything if used otherwise | |
$(apt-cache depends --no-recommends --no-suggests <package> | awk '{print "sudo apt-get remove "$NF}') | |
# optionally (but safer and, hence, recommended), save the output to a script which could be edited before running | |
apt-cache depends --no-recommends --no-suggests <package> | awk '{print "sudo apt-get remove "$NF}' > remove.sh | |
# Remove orphan packages no longer needed (e.g. as a result of removing a meta-package) | |
sudo apt-get autoremove | |
# or to also purge configuration data - note: this only removes system-wide configuration data, and not user-specific ones | |
sudo apt-get autoremove --purge | |
# List orphan packages no longer needed | |
deborphan | |
# Remove a package, purge its configuration data, along with any consequent orphan packages and their own respective configuration data | |
sudo apt-get autoremove --purge <package> | |
sudo apt-get autoremove --purge `deborphan` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check whether postfix is running or not | |
sudo postfix status | |
# If it is not running, start it | |
sudo postfix start | |
# Send a test email (note that '.' at the end to indicate end of message) | |
telnet localhost 25 | |
ehlo localhost | |
mail from: root@<sender-host> | |
rcpt to: <recipient-email> | |
data | |
Subject: <subject> | |
<multi-line-message> | |
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Process Management | |
# Show process ID based on name (e.g. httpd, nginx, mysql, etc.) | |
pgrep <process-name> | |
# Show process ID of a process (e.g. httpd, nginx, mysql, etc.) | |
pidof <process-name> | |
# Kill a process using its process ID | |
sudo kill <process-id> | |
# Edit crontab for the current user | |
crontab -e | |
# List crontab for the current user | |
crontab -l | |
# List crontab for a given user | |
sudo crontab -u <user> -l | |
# Show all running processes and files opened by them (e.g. this is useful for checking which processes depend on Openssl - sudo lsof | grep libssl) | |
sudo lsof | |
# or, for cleaner output | |
lsof | grep libssl | awk '{print $1}' | sort | uniq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show the number of processing units available | |
nproc | |
# Show processor information (e.g. architecture, cores, etc.) | |
lscpu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show all IP table rules | |
sudo iptables -L | |
# Show all firewall rules | |
sudo ufw status | |
# or, for more verbose output | |
sudo ufw status verbose | |
# Enable firewall | |
sudo ufw enable | |
# Disable firewall | |
sudo ufw disable | |
# Deny all connections by default | |
sudo ufw default deny | |
# Allow all connections on a port | |
sudo ufw allow <port> | |
# Allow connection from an IP address to any protocol on a certain port | |
sudo ufw allow from <IP/CIDR> to any port <port> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Specify private key to use | |
ssh -i <path-to-private-key> <remote-host> | |
# Show detailed connection process | |
ssh -vv <remote-host> | |
# Generate a pair of SSH keys | |
ssh-keyget -t rsa -C "<comments>" | |
# Copy the public key(s) to remote host, where <remote-host> is in SSH format | |
ssh-copy-id <remote-host> | |
# To copy only a specific public key | |
ssh-copy-id -i <path-to-public-key> <remote-host> | |
# or if ssh-copy-id command doesn't exist (e.g. on Mac OSX) | |
cat <path-to-public-key> | ssh <user>@<remote-host> "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys" | |
# Add an existing key to ssh-agent (for cases where the key was restored from backup) | |
ssh-add <path-to-private-key> | |
# List all keys being managed by the ssh-agent | |
ssh-add -l | |
# Restart SSH daemon | |
sudo service ssh restart | |
# Authorise the public key on the remote host | |
cat <path-to-public-key> | ssh <remote-host> "cat >> ~/.ssh/authorized_keys" | |
# Kill an active SSH session | |
sudo pkill -9 -t <tty> | |
# Check current session's TTY | |
tty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Print low level system information (e.g. OS, kernel, machine, processor) | |
uname -a | |
# or | |
cat /proc/version | |
# Print hight level system information (e.g. distro name, version, pretty name) | |
cat /etc/*-release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Restart Unity without logging out and in | |
unity # Note: Run it in dash (i.e. Alt + F2) instead of terminal so that it doesn't bind to the terminal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## User and group management commands in Ubuntu | |
# Show the name of the current user | |
whoami | |
# Create a new user | |
adduser | |
# Add an existing user to an existing group | |
sudo usermod -a -G <group> <user> | |
# Refresh user permissions without restart or logging out and in | |
su <user> | |
# Change to another user | |
sudo -i -u <user> | |
# or | |
su - <user> | |
# Simulate root login shell (this is the current best practice if root access is really needed; see https://help.ubuntu.com/community/RootSudo#Logging_in_as_another_user) | |
sudo -i | |
# Run a command as another user | |
sudo -u <user> <command> | |
# Edit user priviledges (e.g. giving a user sudo priviledges) | |
sudo visudo | |
# Delete a user, but keep its home directory | |
sudo deluser username | |
# Delete a user, including his home directory | |
sudo deluser --remove-home username | |
# Change user account password | |
passwd | |
# See a list of all logged in users | |
who | |
# or simply | |
w | |
# Show a list of all users | |
cat /etc/passwd | |
# or | |
cut -d : -f 1 /etc/passwd | |
# Show a list of all groups | |
cat /etc/groups | |
# or | |
cut -d : -f 1 /etc/groups |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment