Essential linux commands for web administrators (What I use daily)

User in front of PC reviewingcode

Files

Search for a phrase in files

$ sudo grep -R "what_to_find" /where/to/look

A simple command that creates a neat summary of each file and folder in the current directory:

$ du -cksh *

Find and Change all files recursively to 644

sudo find /var/www/html/ -type f -exec chmod 644 {} \;

Find and Change all directories recursively to 755

sudo find /var/www/html/ -type d -exec chmod 755 {} \;

Replace spaces in the filename of all the mp3s in a directory with hyphens:

find . -name "* *mp3" -exec rename 's/\ /-/g' {} \;

Move files between servers

scp remote_user@remote_host:/path/to/remote/file /path/to/local/file
scp /path/to/local/file remote_user@remote_host:/home/path_to_user_name

Logs

tail -f /path/thefile.log
tail -f /var/log/syslog
tail -f /var/log/mysql/error.log
tail -f /var/log/apache2/error.log
tail -f /var/log/apache2/access.log

Use tail to display the last 200 entries.

sudo tail /var/log/apache2/error.log -n 200
sudo tail /var/log/apache2/access.log -n 200

Users

Check which group a particular user belongs to:

$ groups [username]

Change shell as www-data user

sudo -i -u www-data

Get the right permissions for apache to write to symfony’s app/logs and app/cache folders but the same can be applied to any folder

$ APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd' | grep -v root | head -1 | cut -d\ -f1`
$ sudo chmod +a "$APACHEUSER allow delete,write,append,file_inherit,directory_inherit" apachelogs/
$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" apachelogs/

Networking

Traceroute equivalent

mtr [ip/domain.com]
tracepath [ip/domain.com]

BASH

CREATE COMMAND BASH ALIASES

1. A simple safe way would be to use an alias. Place this into ~/.bashrc or ~/.bash_aliases file:

alias python=python3

2. After adding the above in the file, run

source ~/.bashrc 

or

source ~/.bash_aliases.

APACHE2 WWW-DATA /var/www/ permissions

https://askubuntu.com#/questions/46331/how-to-avoid-using-sudo-when-working-in-var-www/46371#46371

Add yourself to the www-data group and set the setgid bit on the /var/www directory such that all newly created files inherit this group as well.

sudo gpasswd -a "$USER" www-data

Correct previously created files (assuming you to be the only user of /var/www):

sudo chown -R "$USER":www-data /var/www
find /var/www -type f -exec chmod 0660 {} \;
sudo find /var/www -type d -exec chmod 2770 {} \;

(even safer: use 640 or 2750 and manually chmod g+w file-or-dir that needs to be writable by the webserver)

APACHE2 SETTINGS
https://devanswers.co/install-apache-virtual-hosts-ubuntu-20-04/

sudo nano /etc/apache2/sites-available/mydomain.com.conf
sudo nano /etc/apache2/apache2.conf # see file comments for more

Services

KILL SERVICE

ps -ax | grep application name
sudo kill <pid>
sudo killall <process-name>

Encryption

Create a dictionary with MBD5 hashes
The -n option removes the new line added to the end of “Password.” This is important as we don’t want the new line characters to be hashed with our password. The part “tr –d ‘ -‘ “ removes any characters that are a space or hyphen from the output.

echo -n “Password” | md5sum | tr -d ” -” >> target_hashes.txt

WordPress and Red Hat

When WordPress doesn’t update even though the permissions are correct:

Disable SELinux

Leave a Comment

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

Shopping Cart
  • Your cart is empty.
TOC
Scroll to Top