Find Large Files on Linux
Need to find all the large files on your system?
This will find all files larger than 500mb
cd/
find . -type f -size +500000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
Clear Memory Cache on Linux Server
The Linux OS has a very efficient memory management process that should be freeing any cached memory on the machine that it is being run on. However when it comes to Cached memory the Linux OS may at times decide that the Cached memory is being used and is needed which can lead to memory related issues and ultimately rob your server of any potentially free memory. To combat this you can force the Linux OS to free up and stored Cached memory. This is very helpful on a web (httpd) server
- Connect via shell using a program such as Putty
- At the shell prompt type crontab -e <enter> as this will allow you to edit cron jobs for the root user.
- If you are not familiar with vi (linux editor) you press "i" to insert text and once done hit "esc" and type ":wq" to save the file.
- Scroll to the bottom of the cron file using the arrows key and enter the following line:
1 1 * * * /usr/local/clearcache.sh
- Create a file in '/usr/local' called 'clearcache.sh' with the following content:
- Once you have saved this file, the job is complete! (chmod 755)
#!/bin/sh sync; echo 3 > /proc/sys/vm/drop_caches
Every morning at 1am your memory cache will be cleared
Apache Performance Tuning with MPM module prefork.c
Apache MPM prefork
MPM : Multi-Processing Modules (MPMs)
Module Identifier: mpm_prefork_module
Source File: prefork.c
Open the apache configuration file
/../httpd.conf
And defined the values as per your server capability
<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 10000
</IfModule>
Timeout 300
Usually this value doesn’t require editing and a default of 300 is sufficient.
Lowering the ‘Timeout’ value will cause a long running script to terminate earlier than expected.
On virtualized servers like VPS servers, lowering this value to 100 can help improve performance.
KeepAlive On
This setting should be “On” unless the server is getting requests from hundreds of IPs at once.
High volume and/or load balanced servers should have this setting disabled (Off) to increase connection throughput.
MaxKeepAliveRequests 100
This setting limits the number of requests allowed per persistent connection when KeepAlive is on. If it is set to 0, unlimited requests will be allowed.
It is recommended to keep this value at 100 for virtualized accounts like VPS accounts.
On dedicated servers it is recommended that this value be modified to 200.
KeepAliveTimeout 5
The number of seconds Apache will wait for another request before closing the connection. Setting this to a high value may cause performance
problems in heavily loaded servers. The higher the timeout, the more server processes will be kept occupied waiting on connections with idle clients.
It is recommended that this value be lowered to 5 on all servers.
MinSpareServers 5
This directive sets the desired minimum number of idle child server processes. An idle process is one which is not handling a request.
If there are fewer spareservers idle then specified by this value, then the parent process creates new children at a maximum rate of 1 per second.
Setting this parameter to a large number is almost always a bad idea.
recommends adjusting the value for this setting to the following:
Virtualized server, ie VPS 5
Dedicated server with 1-2GB RAM 10
Dedicated server with 2-4GB RAM 20
Dedicated server with 4+ GB RAM 25
MaxSpareServers 10
The MaxSpareServers directive sets the desired maximum number of idle child server processes. An idle process is one which is not handling a request.
If there are more than MaxSpareServers idle, then the parent process will kill off the excess processes.
The MaxSpareServers value should be set as double the value that is set in MinSpareServers.
StartServers 5
This directivesets the number of child server processes created on startup.
This value should mirror what is set in MinSpareServers.
MaxClients 150
This directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the specified limit will be queued.
Once a process is freed at the end of a different request, the queued connection will then be served.
For virtualized servers such as VPS accounts, it is recommended to keep this value at 150.
For all dedicated servers the recommended value for this setting is 250.
MaxRequestsPerChild 0
This directive sets the limit on the number of requests that an individual child server process will handle.
After the number of requests reaches the value specified, the child process will die. When this value is set at 0, then the process will never expire.
recommends adjusting the value for this setting to the following:
Virtualized server, ie VPS 300
Dedicated server with 1-4GB RAM 500
Dedicated server with 4+GB RAM 1000
Thats it, Save the httpd.conf and restart apache
service httpd restart
My conf is the following on a dedicated box:
Timeout 45 KeepAlive On MaxKeepAliveRequests 200 KeepAliveTimeout 5 # prefork MPM <IfModule prefork.c> StartServers 5 MinSpareServers 5 MaxSpareServers 10 ServerLimit 150 MaxClients 150 MaxRequestsPerChild 1000 </IfModule>
Create MySQL User Accounts from the Command Line
mysql commands to setup a new db with a new user/pass
create database dbname
CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password_for_new_username';
GRANT ALL ON dbname.* TO 'new_username'@'localhost';
Transmission Daemon on CentOS
Download and install all 5 rpms
libevent-2.0.10-1geekery.i386.rpm
transmission-2.22-1geekery.i386.rpm
transmission-cli-2.22-1geekery.i386.rpm
transmission-common-2.22-1geekery.i386.rpm
transmission-daemon-2.22-1geekery.i386.rpm
Configure transmission-daemon
If you are reading this how-to, you must be wanting to run transmission-daemon remotely, through transmisson-remote-gui so we must make the necessary adjustments.
transmission-daemon -f -t -u your_username -v your_password -w /path/to/downloaded/torrents
Now you can click Ctrl + C to cancel (close transmission-daemon)
Further configuring (required for remote usage!)
Setting the allowed hosts to "*" does not work through the transmission-daemon -a option for some reason, so we must edit the newly created config file. You can, of course, set the allowed hosts to just your IP, but I for instance have a dynamic IP address so I have to set it to "*". Let's proceed:
Firstly, make sure transmission-daemon is not running, or else, the changes to it's settings file will not be saved
nano /user/.config/transmission-daemon/settings.json
Look for this line:
"rpc-whitelist":
and set it to your remote IP or to "*"
Thats all, just start it up and connect with the transmisson-remote-gui
/etc/init.d/transmission-daemon start
Auto Start Daemon
nano /etc/rc.local add this line :: transmission-daemon
Hacker Typer
Ever wanted to type like a hacker?
Just go bang away on your keyboard then >> hackertyper.net
Command Line to Delete all Tables from MySQL Database
This command will remove / delete all tables in a specific mysql database.
mysql -u uname --password=pass dbname -e "show tables" | grep -v Tables_in | grep -v "+" | \gawk '{print "drop table " $1 ";"}' | mysql -u uname --password=pass dbname
Test MySQL PHP connection
you want to test the mysql database connection before you install a php script. to test the mysql database connections, if the connections fails, then the user is prompted to enter the database details again, if its successful, then it continues with the next step in the instalation process
testmysql.php
Mirror Your Web Site With rsync
1 Install rsync
First we have to install rsync on both server1.example.com and mirror.example.com. For Debian systems, this looks like this:
server1/mirror:
(We do this as root!)
apt-get install rsync
On other Linux distributions you would use yum (Fedora/CentOS) or yast (SuSE) to install rsync.
2 Create An Unprivileged User On server1.example.com
Now we create an unprivileged user called someuser on server1.example.com that will be used by rsync on mirror.example.com to mirror the directory /var/www (of course, someuser must have read permissions on /var/www on server1.example.com).
server1:
(We do this as root!)
useradd -d /home/someuser -m -s /bin/bash someuser
This will create the user someuser with the home directory /home/someuser and the login shell /bin/bash (it is important that someuser has a valid login shell - something like /bin/false does not work!). Now give someuser a password:
passwd someuser
3 Test rsync
Next we test rsync on mirror.example.com. As root we do this:
mirror:
rsync -avz -e ssh someuser@server1.example.com:/var/www/ /var/www/
You should see something like this. Answer with yes:
The authenticity of host 'server1.example.com (192.168.0.100)' can't be established. RSA key fingerprint is 32:e5:79:8e:5f:5a:25:a9:f1:0d:ef:be:5b:a6:a6:23. Are you sure you want to continue connecting (yes/no)? |
<-- yes
Then enter someuser's password, and you should see that server1.example.com's /var/www directory is mirrored to /var/www on mirror.example.com.
You can check that like this on both servers:
server1/mirror:
ls -la /var/www
You should see that all files and directories have been mirrored to mirror.example.com, and the files and directories should have the same permissions/ownerships as on server1.example.com.
4 Create The Keys On mirror.example.com
Now we create the private/public key pair on mirror.example.com:
mirror:
(We do this as root!)
mkdir /root/rsync
ssh-keygen -t dsa -b 1024 -f /root/rsync/mirror-rsync-key
You will see something like this:
Generating public/private dsa key pair. Enter passphrase (empty for no passphrase): [press enter here] Enter same passphrase again: [press enter here] Your identification has been saved in /root/cron/mirror-rsync-key. Your public key has been saved in /root/cron/mirror-rsync-key.pub. The key fingerprint is: 68:95:35:44:91:f1:45:a4:af:3f:69:2a:ea:c5:4e:d7 root@mirror |
It is important that you do not enter a passphrase otherwise the mirroring will not work without human interaction so simply hit enter!
Next, we copy our public key to server1.example.com:
mirror:
(Still, we do this as root.)
scp /root/rsync/mirror-rsync-key.pub someuser@server1.example.com:/home/someuser/
The public key mirror-rsync-key.pub should now be available in /home/someuser on server1.example.com.
5 Configure server1.example.com
Now log in through SSH on server1.example.com as someuser (not root!) and do this:
server1:
(Please do this as someuser!)
mkdir ~/.ssh
chmod 700 ~/.ssh
mv ~/mirror-rsync-key.pub ~/.ssh/
cd ~/.ssh
touch authorized_keys
chmod 600 authorized_keys
cat mirror-rsync-key.pub >> authorized_keys
By doing this, we have appended the contents of mirror-rsync-key.pub to the file /home/someuser/.ssh/authorized_keys. /home/someuser/.ssh/authorized_keys should look similar to this:
server1:
(Still as someuser!)
vi /home/someuser/.ssh/authorized_keys
ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@ mirror |
Now we want to allow connections only from mirror.example.com, and the connecting user should be allowed to use only rsync, so we add
command="/home/someuser/rsync/checkrsync",from="mirror.example.com",no-port-forwarding,no-X11-forwarding,no-pty |
right at the beginning of /home/someuser/.ssh/authorized_keys:
server1:
(Still as someuser!)
vi /home/someuser/.ssh/authorized_keys
command="/home/someuser/rsync/checkrsync",from="mirror.example.com",no-port-forwarding,no-X11-forwarding,no-pty ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@ mirror |
It is important that you use a FQDN like mirror.example.com instead of an IP address after from=, otherwise the automated mirroring will not work!
Now we create the script /home/someuser/rsync/checkrsync that rejects all commands except rsync.
server1:
(We still do this as someuser!)
mkdir ~/rsync
vi ~/rsync/checkrsync
#!/bin/sh
case "$SSH_ORIGINAL_COMMAND" in
*\&*)
echo "Rejected"
;;
*\(*)
echo "Rejected"
;;
*\{*)
echo "Rejected"
;;
*\;*)
echo "Rejected"
;;
*\<*)
echo "Rejected"
;;
*\`*)
echo "Rejected"
;;
rsync\ --server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo "Rejected"
;;
esac
|
chmod 700 ~/rsync/checkrsync
6 Test rsync On mirror.example.com
Now we must test on mirror.example.com if we can mirror server1.example.com without being prompted for someuser's password. We do this:
mirror:
(We do this as root!)
rsync -avz --delete --exclude=**/stats --exclude=**/error --exclude=**/files/pictures -e "ssh -i /root/rsync/mirror-rsync-key" someuser@server1.example.com:/var/www/ /var/www/
(The --delete option means that files that have been deleted on server1.example.com should also be deleted on mirror.example.com. The --exclude option means that these files/directories should not be mirrored; e.g. --exclude=**/error means "do not mirror /var/www/error". You can use multiple --exclude options. I have listed these options as examples; you can adjust the command to your needs. Have a look at
man rsync
for more information.)
You should now see that the mirroring takes place:
receiving file list ... done sent 71 bytes received 643 bytes 476.00 bytes/sec total size is 64657 speedup is 90.56 |
without being prompted for a password! This is what we wanted.
7 Create A Cron Job
We want to automate the mirroring, that is why we create a cron job for it on mirror.example.com. Run crontab -e as root:
mirror:
(We do this as root!)
crontab -e
and create a cron job like this:
*/5 * * * * /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --exclude=**/files/pictures -e "ssh -i /root/rsync/mirror-rsync-key" someuser@server1.example.com:/var/www/ /var/www/ |
This would run rsync every 5 minutes; adjust it to your needs (see
man 5 crontab
). I use the full path to rsync here (/usr/bin/rsync) just to go sure that cron knows where to find rsync. Your rsync location might differ. Run
mirror:
(We do this as root!)
which rsync
to find out where yours is.