Snippets

Here's some of my tips, tweaks and tuts, ranging from PHP, Linux, MySQL and more!

Snippets / Linux

Fix: Firewall is stopped (iptables) in linux

I came across this quick fix when trying to restart the firewall for my linux box.

$ service iptables status
Firewall is stopped.

Trying to start it?

The obvious solution was to just do service iptables start but this didnt do anything.

Solution

We just need to save the firewall rules before we start the service.

$ service iptables save
Flushing firewall rules:  [  OK  ]
Setting chains to policy ACCEPT: filter [ OK  ]
Unloading iptables modules: [  OK  ]

$ service iptables start
Applying iptables firewall rules: [  OK  ]

12 years ago / Read More

Snippets / Linux

Copy files from one linux machine to another

It's fairly easy to transfer files from one Linux server to another using the scp command.

An example

scp -pv user@123.456.78:source-file.tar /home/user/source-file.tar

Options

-p Preserves the modification and access times, as well as the permissions of the source-file in the destination-file

-q Do not display the progress bar

-r Recursive, so it copies the contents of the source-file (directory in this case) recursively

-v Displays debugging messages

Note

To use this command you need to have open-ssh installed in the hosts.

13 years ago / Read More

Snippets / Linux

Colour highlighting - grep search results

I always find it difficult scanning through hundreds of lines of search results trying to match the keyword you specified.

By default linux doesn't colour code grep search results, but there's an option to change this.

By saving some settings inside your .bash_profile (file that get's called upon your login, usually resides within /home/username/.bash_profile) the linux terminal will remember your settings.

# create red colour for grep search results
export GREP_OPTIONS='--color=auto' 

# or use a green highlight?
export GREP_COLOR='1;32'

13 years ago / Read More

Snippets / Linux

Securing Image Upload directories (777)

It is considered risky to leave directories in your website with the access permission 777, which allows anybody to upload files to this folder. However many PHP projects such as Wordpress require these folder permissions in order to function correctly.

I find it helpful to create a .htaccess file to go inside the image upload directory. This file will do a couple of things:
1.) Only allow certain file types to be published by the server
2.) Disable PHP from rendering pages inside that directory
3.) Disable CGI from executing any scripts inside that directory

.htaccess file

# only allow certain file types

order deny,allow
deny from all


# stop PHP from rendering anthing
RemoveHandler .php
RemoveType .php
php_flag engine off
Options -ExecCGI

How can I easily find directories with 777 permissions?

Using the linux command line find function
# find
find httpdocs/ -type d -perm 777

# results
httpdocs/images_folder/news
httpdocs/images_folder/avatars
httpdocs/images_folder/events
If your website becomes compromised it's probably worth scanning through your server for .php files which reside within directories with 777 permissions.
find httpdocs/ -type d -perm 777 -exec find {} -name "*.php" \;

13 years ago / Read More

Snippets / Linux

Send Email from Linux Command Line

Sending emails from the linux command line can be very useful when creating shell scripts to inform people of the completion of a scheduled crontab.

Using Linux's "mail" command

You can send emails by the following;

mail -s "Subject of Message" your@emailaddress.co.uk < /dev/null

This simplys sends a blank message to the email address of your choice.

Sending Emails with message body

In order to send emails with content in the body you can echo some output in order to pass it to the mail command

# setup the variables
$MSG="This is the body of the email address"
$SUBJET="Test Email Subject"
$ADDRESS="your@emailaddress.co.uk"

echo $MSG | mail -s $SUBJECT $ADDRESS

Alternatively if you wish to send an email, with the body text from a file you can use the following command

mail -s $SUBJECT $ADDRESS < /path/to/file.txt

13 years ago / Read More

Snippets / Linux

Turn on syntax highlighting VIM

If you want to turn on syntax highlighting for vim editor on Ubuntu such edit the main config file here

# open vim settings
vim /etc/vim/vimrc
Then uncomment " syntax on " around line 20

# leave setting
syntax on

If you just wanted to turn it on just while you were editing a file (not a permanent solution)

:syntax on

Then press "Enter"

13 years ago / Read More

Snippets / Linux

Remember Alias binds - Linux

Binding Aliases in linux is a very useful way shorten your workload and the amount you type!

In order to save your alias binds you need to save them in the .profile file which should be in the login root directory.

An Example

# open file to edit
vim .profile

# add alias bind
alias zf=/path/to/shell/script.sh

Remember to save the file and logout and login again.


13 years ago / Read More