Skip to content

Instantly share code, notes, and snippets.

@prograhammer
Last active May 13, 2018 04:41
Show Gist options
  • Save prograhammer/94a14434bdd8a1f6aba7 to your computer and use it in GitHub Desktop.
Save prograhammer/94a14434bdd8a1f6aba7 to your computer and use it in GitHub Desktop.
A cheat sheet for some common linux commands, CentOS 6.4+ commands, and some commands related to Media Temple servers.

General Linux & CentOS 6.4+ & Media Temple - Cheat Sheet

Add a user and setup SSH passwordless access
# adduser someuser
# passwd  somepass
// Run the visude, it will probably open up in an editor like Vi
# /usr/sbin/visudo

// Look for this comment: Allow root to run any commands anywhere
root     ALL=(ALL)       ALL
someuser ALL=(ALL)       ALL

// Now logout as root, and login as someuser, then create a .ssh directory in home
# mkdir .ssh
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys

// Then copy the public key into the authorized_keys file. This will allow passwordless entry.
Find a File
// Find a file
# find / -name some-file.*

// Find file by content
# find . -name "*.php" -print | xargs grep "About-us"
View and Set Date/Time
// Type this command to view the date/time
# date
Sun Jul 7 11:50:26 CDT 2014

// Type this to set the date/time
# date --set="7 Jul 2013 11:51:00"
Sun Jul 7 11:51:00 CDT 2013

// Or
# date -s "7 Jul 2013 11:51:00    
Sun Jul 7 11:51:00 CDT 2013
Composer Update (probably should add an alias for this)
# php -d memory_limit=512M -d allow_url_fopen=1 -d suhosin.executor.include.whitelist=phar composer.phar update --no-scripts
Create a self-signed SSL
// Install mod_ssl and openssl
# yum install mod_ssl openssl

// Generate private key 
# openssl genrsa -out ca.key 2048 

// Generate CSR 
# openssl req -new -key ca.key -out ca.csr

// Generate Self Signed Key
# openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt

// Copy the files to the correct locations
# cp ca.crt /etc/pki/tls/certs
# cp ca.key /etc/pki/tls/private/ca.key
# cp ca.csr /etc/pki/tls/private/ca.csr    

// Then we need to update the Apache SSL configuration file
# vi +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf

// Change the paths to match where the Key file is stored. If you've used the method above it will be
SSLCertificateFile /etc/pki/tls/certs/ca.crt    

// Then set the correct path for the Certificate Key File a few lines below. If you've followed the instructions above it is:
SSLCertificateKeyFile /etc/pki/tls/private/ca.key

// While we are in here, we can go ahead and uncomment and update these lines:
DocumentRoot "/var/www/vhosts/yoursite.com/httpdocs"
ServerName yoursite.com:443

// Quit and save the file and then restart Apache
# /etc/init.d/httpd restart   

// Then add another virtual host to httpd.conf
# vi /etc/httpd/conf/httpd.conf

NameVirtualHost *:443

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/ca.crt
    SSLCertificateKeyFile /etc/pki/tls/private/ca.key
#   <Directory /var/www/vhosts/yoursite.com/httpsdocs>
#   AllowOverride All
#   </Directory>
    DocumentRoot /var/www/vhosts/yoursite.com/httpsdocs
    ServerName yoursite.com
</VirtualHost>


// Now restart apache
# service httpd restart

Installing, Updating, Removing

Remove an installed package (and dependencies)
// Look for the package...
# rpm -q openssl
openssl-xxxxxxx.i686
openssl-xxxxxxx.x86_64

// Or
# rpm -qa | grep "openssl"

// Now remove
# yum remove openssl-xxxxxx.i686
Remove an installed package (without removing dependencies)
# rpm -e --nodeps "openssl-xxxxxx.i686"
Update an existing package
# yum update openssl

MySQL

Log into mysql as administrator
# mysql -u admin -p`cat /etc/psa/.psa.shadow`
Do a hot backup of MySQL database

Put "your-database-here" into a folder you've created "mysql-backups" at "your-domain-here.com"

// Hot backup
# mysqldump -u admin -p`cat /etc/psa/.psa.shadow` --single-transaction your-database-here | gzip -c > /var/www/vhosts/your-domain-here.com/mysql-backups/your-database-here.sql.gz

// Unzip backup
# gunzip your-database-here.sql.gz

// Run sql (restores data)
// start mysql console
# mysql
// In console...
mysql> use your-database
mysql> source /path/to/your-database-here.sql
my.cnf
innodb_buffer_pool_size = 3072M  // Most important setting, needs to increase along with database growing
innodb_log_buffer_size = 725M   // Typically 25% of innodb_buffer_pool_size

mysql> SHOW STATUS LIKE "qcache%";
// Qcache_free_memory = how much available to cache
// Qcache_lowmem_prunes = how many times mysql had to prune the cache to make space for more queries. Need low value here.

Memory

# free -m
                 total       used       free     shared    buffers     cached
    Mem:          8192       2516       5675          0          0        442
    -/+ buffers/cache:       2073       6118
    Swap:         2048        147       1900

DoS Attack

Show which IPs dropped/blocked:

# iptables --list

Show IPs on the site/server at the moment:

# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment