Linux commands every Web developer needs to know.

Getting oriented

Determining the installed version of GNU / Linux OS, Apache, PHP & MySQL

uname -a 
apache2 -v
php -v
mysql -p -e 'select version()'

Determining if Apache, .. is running .

									  ps aux | grep http
									  service apache2 status
									  systemctl status apache2

Updating the MySQL root password.

sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
mysql -u root
USE mysql;
UPDATE user SET password=PASSWORD("newpassword") WHERE user='root';
flush privileges;
sudo /etc/init.d/mysqld start
mysql -u root -p

Keys & Permissions

Creating encryption keys

ssh-keygen -t rsa

Uploading your public encryption key to the web server (two ways)

ssh-copy-id [email protected]
cat ~/.ssh/ | ssh [email protected] "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Logging into a server using SSH

ssh -p port-number  [email protected]

Adding "super" permissions to a user

adduser new-ssh-user
usermod -aG sudo  your-ssh-user-name

Allowing access to a service (e.g apache) through the firewall

sudo ufw app list
sudo ufw app info "Apache Full"
sudo ufw allow in "Apache Full"


Connecting to a MySQL database

mysql -h your-database-host-name -u your-database-user-name -p

Exporting from a MySQL database

mysqldump -h your-database-host-name -u your-database-user-name  -p your-database-name | gzip -9 > your-export-name.sql.gz

Importing to a database

gunzip < your-export-name.sql.gz | mysql -u your-database-user-name -p your-database-name
mysql -u your-database-user-name -p your-database-name <  your-export-name.sql

Files & Directories

Taking a backup of the web root directory

tar  --exclude='/path/to/excluded/directory' -cf - /path/to/web/root/directory | gzip > backup.tar.gz

Downloading files from another web server

scp -r [email protected]:/path/to/web/root/directory .

Synchronizing your files directory on the development server with the files directory on the production server

rsync -azP [email protected]:/path/to/web/files/directory  /path/to/local/directory/on/development/server
rsync -a --delete [email protected]:/path/to/web/files/directory  /path/to/local/directory/on/development/server

Free disk space ?

df -ah .

Size of a directory

du -sh

Largest files and directories (under a directory)

du -a /path/to/directory/ | sort -n -r | head -n 10

Searching for files (e.g tar.gz) with sizes

find / -name *.tar.gz -exec du -sh {} \;

List all files modified in the last 7 days

find ./ -type f -mtime -7

Number of lines that match a given search query

grep -r something /path/to/directory | grep something-else | wc -l
zgrep something  /path/to/gzip/file | wc -l


Monitoring log files

tail -f /var/log/syslog /var/log/messages
tail -n 10 /var/log/syslog
tail -f /var/log/apache2/access.log /var/log/apache2/error.log
egrep -i "denied|error|warn" /var/log/httpd/error_log
grep "user/register" /var/log/nginx/access.log.1 | wc -l

IP addresses assigned to a web server

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

How many HTTP connections are currently open.

netstat -ap | grep 'http'


Cloning a git repository

git clone git+ssh://[email protected]~/directory .

Show all modified files.

git status

Reversing a git commit that has not been pushed.

git reset --soft HEAD~1

Reversing a git commit that has been pushed.

git reset --hard HEAD~1
git push --force origin master


Applying security updates to an Ubuntu server

sudo apt-get update
sudo apt-get upgrade
sudo shutdown -r now

If you need website support: please send me an email [email protected], and let me know what services you need.