,

My Linux Cheat Sheet

I frequently use Ubuntu or Debian to run my applications. Here is my cheat sheet of how to perform commonly required tasks on these Linux distributions.

Managing Packages

Get ‘add-apt-repository’ command back on Debian 8


sudo apt-get install -y software-properties-common

Search for installed packages


sudo dpkg -l | grep 

# Example of finding packages containing mysql in the name
sudo dpkg -l | grep mysql

Completely remove a package (with all configuration files)


sudo apt-get --purge remove 

# Example removing a package called mysql-core
sudo apt-get --purge remove mysql-score

Update packages


sudo apt-get update
sudo apt-get upgrade

Files and directories

Create directory including all sub directories in path


mkdir -p <you're directory path>

# Example:
mkdir -p /usr/some/directory/with/subdirectories

Configure vim editor to use 4 character tab spaces for Python development

Open /etc/vim/vimrc and add the following lines:


set tabstop=4
set shiftwidth=4
set expandtab

Locate files that are taking up disk space


# Install ncdu (an awesome tool)
sudo apt-get install -y ncdu

# Find all large files/directories
sudo ncdu /

Screenshot of NCDU

Create a symbolic link (especially useful for adding nginx enabled-sites)


ln -s
# Example adding a symbolid link in sites-enabled to a file to sites-available
ln -s /etc/nginx/sites-available/www-mysite /etc/nginx/sites/enabled/www-mysite

Delete all files with a specific extension


# First run the below to see what will be deleted
find . -name "*." -type f
# Then run this to delete
find . -name "*." -type f -delete

# Example for finding and deleting all files with .pyc extension
find . -name "*.pyc" -type f
find . -name "*.pyc" -type f -delete

Users, groups and security

Create a new user


sudo adduser
# Example of creating a new user called 'mark'
sudo adduser mark

Create a new group


sudo addgroup
# Example of adding a new group called 'kaizen'
sudo addgroup kaizen

Add a user to an existing group


usermod -a -G
# Example adding a user 'mark' to group 'sudo'
usermod -a -G sudo mark

Generate SSH DSA private key


# Create a DSA key
ssh-keygen -t dsa -c ""

Generate SSH RSA private key


# Create a RSA key
ssh-keygen -t rsa -c ""

General Admin/Control

Change the hostname


echo "<hostname>" > /etc/hostname
/etc/init.d/hostname restart

Check distribution and version installed


lsb_release -a

Shutdown the system


sudo shutdown -h now

Reboot the system


sudo shutdown -r now

Find a kill a process

Note, you find the pid using the first command, then kill it using the second.


# Find the process
ps axf | grep
kill -9 <pid>

Find open ports


sudo netstat -plnt

Activate a Python virtualenv (without virtualenvwrapper)


source /<environment name>/bin/activate
# Example - activate a virtual env called kaizen
source /usr/local/virtualenvs/kaizen/bin/activate

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: