Friday, 22 February 2013

CRON JOB IN LINUX

CRON JOB IN LINUX

The following line specifies that the Apache error log is to be cleared at one minute past midnight (00:01) of every day of the month, of every day of the week, assuming that the default shell for the cron user is Bourne Shell.
compliant:


1 0 * * *  printf > /var/log/apache/error_log
 

The following line causes the user program test.pl – ostensibly a Perl script – to be run every two hours, namely at midnight, 2am, 4am, 6am, 8am, and so on:


 
0 */2 * * *  /home/username/test.pl

Predefined scheduling definitions

There are several special predefined values which can be used to substitute the CRON expression.

Entry Description Equivalent To
@yearly (or @annually) Run once a year at midnight in the morning of January 1 0 0 1 1 *
@monthly Run once a month at midnight in the morning of the first of the month 0 0 1 * *
@weekly Run once a week at midnight in the morning of Sunday 0 0 * * 0
@daily Run once a day at midnight 0 0 * * *
@hourly Run once an hour at the beginning of the hour 0 * * * *
@reboot Run at startup @reboot


*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 7) (0 or 7 are Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
@reboot configures a job to run once when the daemon is started. Since cron is typically never restarted, this typically corresponds to the machine being booted. This behavior is enforced in some variations of cron, such as that 

provided in Debian so that simply restarting the daemon does not re-run @reboot jobs.
@reboot can be useful if there is a need to start up a server or daemon under a particular user, and the user does not have access to configure init to start the program.
There are sites where the cron expressions are described as containing also seconds setting.

cron permissions

The following two files play an important role:

  • /etc/cron.allow - If this file exists, then you must be listed therein (your username must be listed) in order to be allowed to use cron jobs.
  • /etc/cron.deny - If the cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use cron jobs.
Please note that if neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use cron jobs, or all users will be able to use cron jobs.


CRON expression

A CRON expression is a string comprising 5 or 6 fields separated by white space that represents a set of times, normally as a schedule to execute some routine.
Format
Field name
Mandatory?
Allowed values
Allowed special characters
Minutes
Yes
0-59
* / , -
Hours
Yes
0-23
* / , -
Day of month
Yes
1-31
* / , - ? L W
Month
Yes
1-12 or JAN-DEC
* / , -
Day of week
Yes
0-6 or SUN-SAT
* / , - ? L #
Year
No
1970–2099
* / , -
In some uses of the CRON format there is also a seconds field at the beginning of the pattern
Special characters
Support for each special character depends on specific distributions and versions of cron
Asterisk ( * )
The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 4th field (month) would indicate every month.
Slash ( / )
Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form "*/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field.
Percent ( % )
Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
Comma ( , )
Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
Hyphen ( - )
Hyphens are used to define ranges. For example, 2000-2010 would indicate every year between 2000 and 2010 CE inclusive.
L
'L' stands for "last". When used in the day-of-week field, it allows you to specify constructs such as "the last Friday" ("5L") of a given month. In the day-of-month field, it specifies the last day of the month.
W
The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can be specified only when the day-of-month is a single day, not a range or list of days.
Hash ( # )
'#' is allowed for the day-of-week field, and must be followed by a number between one and five. It allows you to specify constructs such as "the second Friday" of a given month.
Question mark ( ? )
Note: Question mark is a non-standard character and exists only in some cron implementations. It is used instead of '*' for leaving either day-of-month or day-of-week blank.

Sunday, 17 February 2013

IPTABLES TO BLOCK TORRENT

## IPTABLES TO BLOCK TORRENT (working correctly)
iptables -A INPUT -m string --string "BitTorrent" --algo bm --to 65535 -j DROP

iptables -A INPUT -m string --string "BitTorrent protocol" --algo bm --to 65535 -j DROP 

iptables -A INPUT -m string --string "peer_id=" --algo bm --to 65535 -j DROP 

iptables -A INPUT -m string --string ".torrent" --algo bm --to 65535 -j DROP

iptables -A INPUT -m string --string "announce.php?passkey=" --algo bm --to 65535 -j DROP

iptables -A INPUT -m string --string "torrent" --algo bm --to 65535 -j DROP

iptables -A INPUT -m string --string "announce" --algo bm --to 65535 -j DROP 

iptables -A INPUT -m string --string "info_hash" --algo bm --to 65535 -j DROP

iptables -A INPUT -m string --string "peer_id" --algo kmp --to 65535 -j DROP 

iptables -A INPUT -m string --string "BitTorrent" --algo kmp --to 65535 -j DROP 

iptables -A INPUT -m string --string "BitTorrent protocol" --algo kmp --to 65535 -j DROP

iptables -A INPUT -m string --string "bittorrent-announce" --algo kmp --to 65535 -j DROP 

iptables -A INPUT -m string --string "announce.php?passkey=" --algo kmp --to 65535 -j DROP

iptables -A INPUT -m string --string "find_node" --algo kmp --to 65535 -j DROP 

iptables -A INPUT -m string --string "info_hash" --algo kmp --to 65535 -j DROP 

iptables -A INPUT -m string --string "get_peers" --algo kmp --to 65535 -j DROP

iptables -A INPUT -m string --string "announce" --algo kmp --to 65535 -j DROP 

iptables -A INPUT -m string --string "announce_peers" --algo kmp --to 65535 -j DROP

Saturday, 16 February 2013

SQUID TRANSPARENT PROXY ON UBUNTU

SQUID TRANSPARENT PROXY 3.1.19 ON UBUNTU 12 STEP BY STEP

sudo su

apt-get install squid

#Key File Locations
## Squid configuration file
/etc/squid/squid.conf

## Squid access log file
/var/log/squid/access.log


cp /etc/squid3/squid.conf  /etc/squid3/squid.conf.orig

 

## To remove blank and commented lines 
cd /etc/squid3/ 

sed '/^$/d' squid.conf > squid.conf.ed && sed '/^\#/d' squid.conf.ed > squid.conf.edi && echo $?

mv squid.conf.edi squid.conf 

nano /etc/squid3/squid.conf 

## WELCOME TO SQUID 3.1.19
## -----------------------
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt 
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT

http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

## Insert your own rules here
acl bldownload url_regex -i "/etc/squid3/download.acl"
acl download method GET
acl officehours time MTWHF 9:00-13:00
acl officehours time MTWHF 14:00-17:00
acl blsites url_regex -i "/etc/squid3/officetime.acl"


http_access deny bldownload

http_access deny download
http_access deny blsites officehours

acl fblock dstdomain .facebook.com
http_reply_access deny  fblock officehours
http_access deny CONNECT fblock officehours

http_access allow localnet
http_access deny all

http_port 192.168.0.10:3128 intercept

## Cache Settings

cache_mem 51200 MB
memory_replacement_policy lru
cache_replacement_policy lru
cache_dir ufs /cache/squid3 51200 16 256



#cache_mem and cache_dir 51200 must be same otherwise it will be a warning to initialize cache

## Access and Cache logs
access_log /var/log/squid3/access.log squid
cache_log /var/log/squid3/cache.log

coredump_dir /proxycache/squid3

refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern (Release|Packages(.gz)*)$      0       20%     2880
refresh_pattern .               0       20%     4320

visible_hostname raabtaproxy



mkdir /cache && mkdir /cache/squid3 && chown proxy:proxy /cache/squid3 && chmod 777 /cache/squid3 && echo $? 

touch /etc/squid3/download.acl && touch /etc/squid3/officetime.acl && echo $? 


## To start restart or stop the squid
/etc/init.d/squid3 start|restart|stop  



## To reconfigure policies 
squid3 -k reconfigure 



## to initialize cache
squid3 -z

## To make transparent proxy
echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -j REDIRECT --to-ports 3128
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERAD

## To save iptables 
iptables-save > iptables-tproxy 

## To restore iptables
iptables-restore < iptables-tproxy

Friday, 15 February 2013

UBUNTU WIFI CONFIGURATION

## To add wifi network
apt-get install wpasupplicant wireless-tools

nano /etc/network/interfaces
auto wlan0
iface wlan0 inet dhcp
wpa-ssid  mywif
wpa-psk  password

Wednesday, 13 February 2013

UBUNTU QUOTA CONFIGURATION

## Ubutnu Quota configuration
sudo apt-get install quota quotatool

nano /etc/fstab
/dev/sda1     /               ext4    errors=remount-ro,usrquota,grpquota 0       1

root@ubuntu:/# mount -o remount /
root@ubuntu:/# quotacheck -avugm

root@ubuntu:/# quotaon -avug
/dev/disk/by-uuid/62905db6-a34e-42d0-a9da-feb18d548046 [/]: group quotas turned on
/dev/disk/by-uuid/62905db6-a34e-42d0-a9da-feb18d548046 [/]: user quotas turned on

quotatool -u asad -bq 5000Mb -l "6000 Mb" / -v

quotatool -u noreen -bq 5000Mb -l "6000 Mb" / -v

quotatool -u adnan -bq 5000Mb -l "6000 Mb" / -v

quotatool -u ibtisam -bq 5000Mb -l "6000 Mb" / -v

quotatool -u adil -bq 5000Mb -l "6000 Mb" / -v

quotatool -u zahoor -bq 5000Mb -l "6000 Mb" / -v

quotatool -u shahab -bq 5000Mb -l "6000 Mb" / -v

quotatool -u ibrar -bq 5000Mb -l "6000 Mb" / -v

Tuesday, 12 February 2013

UFW - UBUNTU FIREWALL

## UFW configuration files
/etc/ufw/

## Install if not installed
apt-get update
apt-get install ufw

## firewall enable or disable
ufw enable | disable

## firewall logging on or off
ufw logging on|off

## check firewall status
ufw status

## allow ssh service from everywhere
ufw allow 22   


ufw allow 22/tcp               

## Allow ssh from 172.168.1.100 to 172.16.10.10 on port 22 using tcp

ufw allow from 172.168.1.100 to 172.16.10.10 port 22 proto tcp    

## To delete the rule
ufw  delete allow from 172.168.1.100 to 172.16.10.10 port 22 proto tcp

## Allow all traffic from local network to an interface
ufw allow from 192.168.1.0/24 to 192.168.1.11
 
## allow ssh from ip and to server
ufw allow from 192.168.79.129 to 192.168.79.128 port 22      

## deny ssh
ufw deny 22

## firewall delete allow rule for port 80 tcp
ufw delete allow 80/tcp

## firewall delete  allow rule for port 53
ufw delete allow 53

## When all required services allowed then let’s shut everything else down
ufw default allow|deny