Useful hosting linux commands | Basics

If you are managing a server, you may need a bunch of commands to supervise the resources or manage and help its users.

Resources

Distro

cat /etc/*-release

Files Permissions

chown username.username -R .

chmod

chattr

Disk

Memory space

df -h

df -hT /home

The memory used by the current directories/files

du -h --max-depth=1 | sort -hr

Inodes

To get the total number of inodes in the current directory, use: wc -l

If you whish to display the distribution of inodes in the current subdirectorties, use:

find . -maxdepth 1 -type d | while read line; do echo "$(find $line | wc -l) $line"; done | sort -n

The indes of -maxdepth defines the level of subdirectories.

CPU/RAM

To display the CPU/RAM consumption, you may use ps aux or the dynamic top (or dbtop for database activities) . You can also display it for a specific username

top -u username

ps aux | egrep username

If you whish to kill all process of a user:

ps aux | egrep username | awk '{print $2}' | xargs kill -9

killall -s 9 -v -u username

The user can also kill his process if he has a correct acces to the terminal:

ps aux | awk '{print $2}' | xargs kill -9

or by filtering the processes, as if I wanted to only kill NodeJs based processes:
ps aux | grep -i node | awk '{print $2}' | xargs kill -9

Network

traceroute / tracert (win), mtr

Curl

Domain Records

    • whois
      • whois example.com | egrep ‘WHOIS Server|Creation|Updated|Expiry|Status’
  • nslookup
  • dig

Text manipulation

search a text in a file or directory

grep "word" file

or recursively in all the subdirectory grep -r "word"

replace a text

This is highly useful if you are migrating a content, and you need to change any mention of previous username/password:

grep -rl 'old' . | xargs sed -i 's/old/new/g'


MySQL

To change username’s password in MySQL (sometimes depending on the version), use:

SET PASSWORD FOR 'dbuser'@'localhost' = PASSWORD('XXXXXXXXXX');

OR

ALTER USER 'dbuser'@'localhost' IDENTIFIED BY 'XXXXXXXXXX';

To backup SQL

mysqldump username_dbname > export.sql

To import SQL to a db

mysql username_dbname < import.sql


Cron

crontab -e


cPanel

Using ssh you can create a temporary session to cPanel or WHM.

/scripts/whmlogin

List users & their domains

ls -la /etc/valiases

To package the user account including files and DB, mails, DNS Zone, etc…

/scripts/pkgacct username

To restore it, remove cpmove_ prefix from the filename of the compressed package.

/scripts/restorepkg /path/cpmove_file.tar.gz

Data transfer

  • SCP
  • wget
    • wget –ftp-user=XXXXX –ftp-password=XXXXX ftp://host.com/ -q -O

Others:

screen to keep your session in the background detached from the terminal.

  • Create a new session: screen -S session_name
  • List sessions: screen -ls
  • To resume one of the sessions: screen -r
  • Ctrl+D to end it.

Comments

Leave a Reply

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