แสดงกระทู้

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - golfreeze

Pages: 1 ... 71 72 [73]
1081
About the script: The script below is used to make an effective and strong firewall for the Gateway
machines. The script is commented to make it self explanatory. This script is completely done using
Iptables.

#!/bin/bash
# iptables, by SysAdmin,
# description: Script for setting IPTABLES rules for Gateway
# processname: iptables

The below options will make us adjust the script parameters in one shot. If there requires a
situation like if we want to swap the eth0 and eth1 for some reason, we can adjust that in the below
option rather than adjusting that for the whole script manually. I am using my network configuration
in here. So modify this according to your requirements.

INTERNALIF="eth1" # Internal Ethernet card identifier
INTERNALNET="192.168.1.0/24" # Internet network address range
INTERNALBCAST="192.168.1.255" # Internal Broadcast address
EXTERNALIF="eth0" # External Ethernet card identifier

# Pathnames
DMESG="/bin/dmesg" # Location of Dmesg
IPTABLES="/sbin/iptables" # Location of Iptables

The section below is used to control the script using “Start” or
“Stop” or “Restart” functions just like controlling services
of a Linux machine. The script has to be called like in example below

# sh /firewall.sh start

if [ "$1" = "" ] # this will make the script to exit if no argument
is present
then
echo "Usage: sh /fire.sh (start|stop|restart)"
exit
else
case "$1" in
stop)
action "Shutting down firewall:" echo
$IPTABLES -F
$IPTABLES -P FORWARD DROP
exit 0
;;
status)
echo "The status command is not supported for iptables"
exit 0
;;
restart|reload)
$0 stop
exec $0 start
;;
start)
echo "Starting Firewall:"
;;
*)
echo "Usage: firewall (start|stop|restart)"
exit 1
esac
fi

To avoid any conflicts that may arise in the implementation of the Firewall, its better to flush
every rules that were actually present in the chains and then to start over.

$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD

The below are some tweaks that can be done to SysCtl to avoid make kinds of attacks. They are pretty
simple yet effective.

Disabling IP Spoofing attacks:

IP spoofing is one of the most common forms of on-line camouflage. In IP spoofing, an attacker gains
unauthorized access to a computer or a network by making it appear that a malicious message has come
from a trusted machine by “spoofing” the IP address of that machine.

echo 2 > /proc/sys/net/ipv4/conf/all/rp_filter

The below change will stop the Gateway from responding to broadcast pings.

echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

Block Source Routing:

Source routing allows the sender of the packet to specify the route that a packet must take in
traveling to the destination. If the selected route is not available for any reason, the packet
would not be delivered. If the recipient replied to the packets, the response would follow the same
route. it can be used to discover the IP addresses of routers within a network. However, it also has
the potential for misuse. A malicious user could use source routing to learn more about a network
that he or she is targeting for attack. Data packets contain information about where they have been
and what machines they have transited. A malicious user might send data into a network in order to
collect information about the network's topology. Its better to block source routing from the
Gateway itself

echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route

Kill timestamps.

echo 0 > /proc/sys/net/ipv4/tcp_timestamps

Trying to prevent SYN flood attacks:

A SYN flood is a form of denial-of-service attack in which an attacker sends a succession of SYN
requests to a target's system. One of most important steps is to enable the operating system's
built-in protection mechanisms like SYN cookies. In Linux, we can set the SYN Cookies in the
following manner.

echo 1 > /proc/sys/net/ipv4/tcp_syncookies

Disable all redirection requests in Gateway machine:

echo 0 >/proc/sys/net/ipv4/conf/all/accept_redirects

The below command enables bad error message protection

echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses

#Allow dynamic ip addresses

echo "1" > /proc/sys/net/ipv4/ip_dynaddr

Its recommend to log packets with impossible addresses. This can be done by giving a positive
parameter to the following sysctl parameter.

echo 1 > /proc/sys/net/ipv4/conf/all/log_martians

Set a local port range:

By default, the local port range is 32768 to 61000 for all new 2.6 kernel based systems. Its
explicitly mentioned in the Firewall script because if we need to increase that further, we can
adjust the port range throug the Firewall script itself.

echo "32768 61000" > /proc/sys/net/ipv4/ip_local_port_range

Denial of Service (DoS) attacks: Denial of Service attack is an attempt by a malicious (or
unwitting) user, process, or system to prevent legitimate users from accessing a resource (usually a
network service) by exploiting a weakness or design limitation in an information system. Examples of
DoS attacks include flooding network connections, filling disk storage, disabling ports, or removing
power. This can be limited by setting timeouts.

echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_time
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
echo 0 > /proc/sys/net/ipv4/tcp_sack
echo 1280 > /proc/sys/net/ipv4/tcp_max_syn_backlog

Basic Rules of a typical Firewall:

Kill INVALID packets with illegal combination flags.

$IPTABLES -A INPUT -m state --state INVALID -j DROP
$IPTABLES -A FORWARD -m state --state INVALID -j DROP

No restrictions to connections from localhost

$IPTABLES -A INPUT -i lo -j ACCEPT

Reject connections from Outside world to Internal loop back device.

$IPTABLES -A INPUT -d 127.0.0.0/8 -j REJECT

No restrictions for traffic generating from legit internal addresses

$IPTABLES -A INPUT -i $INTERNALIF -s $INTERNALNET -j ACCEPT

Incase we have to use IPv6 addresses in your environment uncomment the below line:

#$IPTABLES -A INPUT -p ipv6 -j ACCEPT

Kill all packets from Outside world claiming to be packets generated from Internal network.

$IPTABLES -A INPUT -i $EXTERNALIF -s $INTERNALNET -j REJECT

Block ICMP requests.

$IPTABLES -A FORWARD -p icmp --icmp-type echo-request -o $INTERNALIF -j REJECT

Prevent Ping flood attacks:

$IPTABLES -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -j DROP

Deny pings to local broadcast address:

$IPTABLES -A INPUT -p icmp -d $INTERNALBCAST -j DROP

#Allow all other icmp

$IPTABLES -A INPUT -p icmp -j ACCEPT

No restrictions to established connections:

$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Dont forward SMB related traffic. Samba Services are one of the most aimed targets by hackers.

$IPTABLES -A FORWARD -o $EXTERNALIF -p tcp --dport 137 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p tcp --dport 138 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p tcp --dport 139 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p udp --dport 137 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p udp --dport 138 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p udp --dport 139 -j REJECT
$IPTABLES -A INPUT -i $EXTERNALIF -p udp --dport 137 -j REJECT

1. Disable Samba Share

#$IPTABLES -A INPUT -p tcp --dport 137 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 137 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 138 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 138 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 139 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 139 -j ACCEPT

Allow all other connections to be forwarded

$IPTABLES -A FORWARD -o $EXTERNALIF -i $INTERNALIF -j ACCEPT

Allow replies from established connections :

$IPTABLES -A FORWARD -i $EXTERNALIF -m state --state ESTABLISHED,RELATED -j ACCEPT

#Allow yourself to be a DHCP server for your inside network
#Necessary because the default rule allowing valid addresses ignores broadcast

$IPTABLES -A INPUT -i $INTERNALIF -p tcp --sport 68 --dport 67 -j ACCEPT
$IPTABLES -A INPUT -i $INTERNALIF -p udp --sport 68 --dport 67 -j ACCEPT

1. This section is to allow nameserver packets.

cat /etc/resolv.conf | \
awk '/^nameserver/ {print $2}' | \
xargs -n1 $IPTABLES -A INPUT -p udp --sport 53 -j ACCEPT -s

#From here on, we are dealing with connection attempts. These services are not needed for our
Gateway. But just incase we happen to have them up and running, just uncomment and restart the
firewall script.

# ftp-data
#$IPTABLES -A INPUT -p tcp --dport 20 -j ACCEPT

# ftp
#$IPTABLES -A INPUT -p tcp --dport 21 -j ACCEPT
# ssh

#$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT
#telnet

#$IPTABLES -A INPUT -p tcp --dport 23 -j ACCEPT

# smtp One per second limt -burst rate of ten
#$IPTABLES -A INPUT -p tcp --dport 25 --syn -m limit --limit 1/s \
# --limit-burst 10 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 25 --syn -j DROP
#$IPTABLES -A INPUT -p tcp --dport 25 -j ACCEPT

# DNS
#$IPTABLES -A INPUT -p tcp --dport 53 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 53 -j ACCEPT

# http
$IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT

# POP-3
#$IPTABLES -A INPUT -p tcp --dport 110 -j ACCEPT

# identd
#$IPTABLES -A INPUT -p tcp --dport 113 -j ACCEPT

# https
#$IPTABLES -A INPUT -p tcp --dport 443 -j ACCEPT

#VNC Server
#$IPTABLES -A INPUT -p tcp --dport 5801 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 5901 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 6001 -j ACCEPT

##Some ports should be denied and logged.

$IPTABLES -A INPUT -p tcp --dport 1433 -m limit -j LOG --log-prefix "Firewalled packet: MSSQL
"
$IPTABLES -A INPUT -p tcp --dport 1433 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6670 -m limit -j LOG --log-prefix "Firewalled packet:
Deepthrt "
$IPTABLES -A INPUT -p tcp --dport 6670 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6711 -m limit -j LOG --log-prefix "Firewalled packet: Sub7
"
$IPTABLES -A INPUT -p tcp --dport 6711 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6712 -m limit -j LOG --log-prefix "Firewalled packet: Sub7
"
$IPTABLES -A INPUT -p tcp --dport 6712 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6713 -m limit -j LOG --log-prefix "Firewalled packet: Sub7
"
$IPTABLES -A INPUT -p tcp --dport 6713 -j DROP
$IPTABLES -A INPUT -p tcp --dport 12345 -m limit -j LOG --log-prefix "Firewalled packet:
Netbus "
$IPTABLES -A INPUT -p tcp --dport 12345 -j DROP
$IPTABLES -A INPUT -p tcp --dport 12346 -m limit -j LOG --log-prefix "Firewalled packet:
Netbus "
$IPTABLES -A INPUT -p tcp --dport 12346 -j DROP
$IPTABLES -A INPUT -p tcp --dport 20034 -m limit -j LOG --log-prefix "Firewalled packet:
Netbus "
$IPTABLES -A INPUT -p tcp --dport 20034 -j DROP
$IPTABLES -A INPUT -p tcp --dport 31337 -m limit -j LOG --log-prefix "Firewalled packet: BO
"
$IPTABLES -A INPUT -p tcp --dport 31337 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6000 -m limit -j LOG --log-prefix "Firewalled packet: XWin
"
$IPTABLES -A INPUT -p tcp --dport 6000 -j DROP

#Traceroutes depend on finding a rejected port. DROP the ones it uses

$IPTABLES -A INPUT -p udp --dport 33434:33523 -j DROP

#Don't log web or ssl because people surfing for long times lose connection tracking and cause
the system to create a new one, flooding logs.

$IPTABLES -A INPUT -p tcp --dport 80 -j REJECT
$IPTABLES -A INPUT -p tcp --dport 443 -j REJECT

The below rules will be effective it it doesnt match any of the above rules. A log for all
connections will be “noise” and will give us a hard time to find something
relevant from the flooded logs. So the rules below just logs connection requests.

$IPTABLES -A INPUT -p tcp --syn -m limit --limit 5/minute -j LOG --log-prefix "Firewalled
packet:"
$IPTABLES -A FORWARD -p tcp --syn -m limit --limit 5/minute -j LOG --log-prefix "Firewalled
packet:"

#Reject

$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
$IPTABLES -A INPUT -p all -j DROP

$IPTABLES -A FORWARD -p tcp -j REJECT --reject-with tcp-reset
$IPTABLES -A FORWARD -p all -j DROP

#Accept it anyway if it's only output
$IPTABLES -A OUTPUT -j ACCEPT

EXPLICITLY BLOCKING SERVICE PORTS FOR GATEWAY FROM OUTSIDE WORLD

/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 21 # ftp
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 22 # ssh
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 23 # telnet
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 25 # smtp
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 53 # domain
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 79 # finger
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 80 # httpd
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 110 # pop3
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 111 # sunrpc
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 137 # netbios-ns
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 138 # netbios-dgm
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 139 # netbios
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 143 # imap
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 161 # snmp
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 162 # snmp trap
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 443 # https
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 515 # printer
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 1080 # socks
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 2049 # nfs
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 3128 # squid
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 3306 # mysql
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 5432 # postgres

1. EXPLICITLY BLOCKING SERVICE PORTS FOR UDP CONNECTIONS

/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 21 # ftp
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 23 # telnet
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 25 # smtp
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 53 # domain
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 79 # finger
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 110 # pop3
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 111 # sunrpc
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 137 # netbios-ns
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 138 # netbios-dgm
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 139 # netbios
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 143 # imap
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 161 # snmp
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 162 # snmp trap
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 443 # https
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 515 # printer
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 1080 # socks
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 2049 # nfs
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 3128 # squid
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 3306 # mysql
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 5432 # postgres

1. this line will prevent people on the internet (or you) from pinging the gateway

/sbin/iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j DROP

exit 0

1082
#ipvs + Heartbeat + Debian4.0 R1 (NAT Mode) ต้องทำ static nat ที่ loadbalance ด้วย

LB1 = install Debian 4.0 R1
LB2 = install Debian 4.0 R1

##Add this in /etc/apt/sources.list
deb http://www.ultramonkey.org/download/3/sargemain
deb-src http://www.ultramonkey.org/download/3sarge main
deb http://ftp.coe.psu.ac.th/debianstablemain
deb-src http://ftp.coe.psu.ac.th/debianstable main

#apt-get update
#apt-get install ultramonkey

ปกติถ้า ทำ HA จะมี config หลักๆ 3 ตัวคือ
#/etc/ha.d/ha.cf , haresources , authkeys
ส่วนของ ipvs จะให้อ่านจากไฟล์ config /etc/ha.cf/ldirectord.cf

#Diagram ก็ประมาณนี้ครับ




####LB1

#vi /etc/ha.d/ldirectord.conf
# Global Directives
checktimeout=10
checkinterval=2
#fallback=127.0.0.1:80
autoreload=no
logfile=\"/var/log/ldirectord.log\" #เก็บไฟล์log ของ ldirector
logfile=\"local0\"
quiescent=yes

# Virtual Server for HTTP
virtual=10.0.1.152:80
real=192.168.0.4:80 masq 2
real=192.168.0.5:80 masq 1
service=http
request=\"test.html\"
receive=\"Test Page\"
scheduler=wrr
#persistent=600
protocol=tcp
checktype=negotiate


#vi haresources

lb1 ldirectord::ldirectord.cf
lb1 LVSSyncDaemonSwap::master
lb1 IPaddr2::10.x.x.152/23/eth0
lb1 IPaddr2::192.z.x.254/24/eth1
-----------------------------------------------------

---------------------------------------
lb1:/etc/ha.d# more authkeys
auth3
#1 crc
3 sha1 ultramonkey


lb1:/etc/ha.d# more ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
bcast eth1 # Linux
mcast eth1 225.0.0.1 694 1 0
auto_failback on
node lb1
node lb2
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster


--------------------------------------------------


####LB2
#vi /etc/ha.d/ldirectord.conf
# Global Directives
checktimeout=10
checkinterval=2
#fallback=127.0.0.1:80
autoreload=no
logfile=\"/var/log/ldirectord.log\"
logfile=\"local0\"
quiescent=yes

# Virtual Server for HTTP
virtual=10.0.1.152:80
real=192.168.0.4:80 masq 2
real=192.168.0.5:80 masq 1
service=http
request=\"test.html\"
receive=\"Test Page\"
scheduler=rr
#persistent=600
protocol=tcp
checktype=negotiate


haresource จะเอาไว้สร้างตัว vip ให้เราน่ะครับ ก็ไม่มีไรมาก 2 ตัวต้องเหมือนกันน่ะครับทั้ง ตัว
master(lb1) กับ slave(lb2)
เพราะว่า
#vi haresources

lb1 ldirectord::ldirectord.cf
lb1 LVSSyncDaemonSwap::master
lb1 IPaddr2::10.x.x.152/23/eth0
lb1 IPaddr2::192.z.x.254/24/eth1

-----------------------------------------------------

Lb2:/etc/ha.d# more authkeys
auth3
#1 crc
3 sha1 ultramonkey


Lb2:/etc/ha.d# more ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
bcast eth1 # Linux
mcast eth1 225.0.0.1 694 1 0
auto_failback on
node lb1
node lb2
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster





##Set static nat in loadbalance1 and loadbalance2

#vi /etc/rc.local
iptables -t nat -F
iptables -t nat -A POSTROUTING -j SNAT -o eth1 -s 192.168.0.4 --to 10.x.x.x
iptables -t nat -A POSTROUTING -j SNAT -o eth1 -s 192.168.0.5 --to 10.x.x.x

สั่งให้ อ่าน rc.local ก็
#/etc/rc.local

#เสร็จแล้ว ก็มาทำ ตัวroute สำหรับผมมาติดปัญหาตรงจุดนี้ครับ ซึ่งลองใช้ tcpdump ช่วยก็เห็นว่า packet
มันไม่ส่งจากเครื่อง Rip(192.168.0.4) กับ จุด 5 ไปยังเครื่อง loadbalance
ตรงจุดนี้สำคัญมากน่ะครับคือต้องเรียนรู้เรื่อง network routing table ให้พอเข้าใจก่อน
#netstat -nr
lb1:/home/golf# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
10.0.0.0 0.0.0.0 255.255.254.0 U 0 0 0 eth1
0.0.0.0 10.x.x.x 0.0.0.0 UG 0 0 0 eth1
0.0.0.0 192.168.0.254 0.0.0.0 UG 0 0 0 eth2

***#route add -net 0.0.0.0/0 gw 10.0.0.254 dev eth1
***#route add -net 0.0.0.0/0 gw 192.168.0.254 dev eth2
***#route add -net 192.168.0.0 netmask 255.255.255.0 dev eth2


แล้วก็มา set ให้ เครื่อง loadbalance forward packet มาให้เครื่อง Rip น่ะครับ
#sysctl -a | grep forward
#vi /etc/sysctl.conf
Add
net.ipv4.ip_forward = 1
แล้วสั่งให้ show ค่าออกมาดูน่ะครับ
#sysctl -p

ส่วนเครื่อง web1 / web2
ก็ลงเป็น
#web1 os linux(debian4)
#ip 192.168.0.4
เครื่องฝั่ง internal ก็ไม่มีไรมากครับ ลง app ที่เราจะต้องใช้ แล้วก็จูน ให้เสร็จ ก็เปิดรับ service
ให้ใช้งานได้ก็จบครับ

#set ip
#vi /etc/network/interface
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.4
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.254


#web2 os Freebsd(6.2)
#ip 192.168.0.5

#vi /etc/rc.conf
ifconfig_dc0=\"inet 192.168.0.5 netmask 255.255.255.0\"

ให้มัน restart network 1 ดอก
#/etc/rc.d/netif restart

คราวนี้ลองใช้ telnet เข้า vip ที่เรา set ไว้ดูน่ะครับ
ดังนี้
#telnet x.x.x.x 80
ลอง GET ดูน่ะครับ ถ้าได้ค่าว่า Test Page ก็แสดงว่า work แระ


คำสั่งที่จะใช้งานตรวจสอบว่า มี packet เข้ามาในเครื่องผ่าน nat เปล่าก็
#watch -n 1 "iptables -L -n -t nat -xv"

คำสั่งนี้เอาไว้ track ปัญหาน่ะครับ แล้วให้เขียน output ไปไว้ที่ xx.txt แล้วก็ค่อยไปไล่ๆ ดู
#tcpdump -n -i any port 80 > xx.txt

ดูว่า load balance ทำงานแบบ real time ก็
#watch -n 1 "ipvsadm -L -n --stats"
ได้ครับ

----------------------------------------------------------------------------------------------------


---
ในงานที่ต้องรับ connection ที่เยอะๆ นั้นการทำ load balance ก็สามารถช่วยแก้ปัญหาได้ครับ
ทำให้ประสิทธิภาพของงานทำได้ดีมากขึ้น แต่บางครั้งในส่วนของ software ก็อาจจะยังมีบางจุดที่ยังไม่ค่อย
stable พอหรือตาม config ที่อาจจะมีบางจุดที่นักพัฒนายัง implement อยู่
ซึ่งถ้ามองแล้วจะทำงานได้ไม่เท่ากับพวก hardware จริงๆ ซึ่งตัว hareware loadbalance
ค่อยข้างจะมีมาตรฐานพอสมควร ซึ่งพวกองค์กรที่ต้องการ performance แล้วก็แนะนำเป็น hareware (F5)
ดีกว่าครับ แพงดีแต่ก็คุ้มครับ

Lab By
http://www.packetlove.com
golfreeze@packetlove.com
ขอบคุณพี่อัท http://www.siambox.com ที่แนะนำเรื่อง route ครับ และคำแนะนำ อิอิ

1083
อิอิ วันก่อนไปแข่งงาน AirRaid Hacking Challenge BKK 2008 มาครับ
มี trick ที่ใช้คือ

Packet ที่ใช้เป็น aircrack-ng
ลง packet พื้นฐานสำหรับ ตัว aircrack-ng ก่อนโดย
#apt-get build-dep aircrack-ng

ต่อจากนั้นก็หา source file ตัวใหม่ของ aircrack-ng มาลงครับ
#tar zxf aircrack-ng-1-beta.tar.gz
#cd aircrack-ng
#make
#make install

เมื่อลง driver ของ interface DLink แล้วก็
เสร็จแล้วก็ ใช้วิธี search หา SSID ได้คือ
#airodump rausb0
------------------------------------------------------

ต่อมาเป็นวิธีการ set WIfi แบบ command line ครับ

ปลอม mac address โดยเปลี่ยนจากตัวเดิมเป็น 00:00:00:00:00:00 โดย
#ifconfig rausb0 hw ether 00:00:00:00:00:00 -arp

ค่า mac นี้เมื่อ restart เครื่องก็จะกลับเป็นค่าเดิมครับ

#ifconfig rausb0 10.x.x.x netmask 255.x.x.x up
#iwconfig rausb0 essid "XXX"
#iwconfig rausb0 key "xxdddffff"

แค่นี้ก็ปิง เครื่อง อื่นๆ ใน nw นั้นได้แล้วครับ

#route add default gw 10.x.x.y

อ่านเพิ่มเติมที่ http://golfreeze.packetlove.com/guyblog/?p=25

golfreeze[at]packetlove.com

1084
You can use -mtime option. It returns list of file if the file was last accessed N*24 hours ago. For
example to find file in last 2 months (60 days) you need to use -mtime +60 option.

* -mtime +60 means you are looking for a file modified 60 days ago.
* -mtime -60 means less than 60 days.
* -mtime 60 If you skip + or - it means exactly 60 days.

So to find text files that were last modified 60 days ago, use
$ find /home/you -iname "*.txt" -mtime -60 -print

Display content of file on screen that were last modified 60 days ago, use
$ find /home/you -iname "*.txt" -mtime -60 -exec cat {} \;

Count total number of files using wc command
$ find /home/you -iname "*.txt" -mtime -60 | wc -l

You can also use access time to find out pdf files. Following command will print the list of all pdf
file that were accessed in last 60 days:
$ find /home/you -iname "*.pdf" -atime -60 -type -f

List all mp3s that were accessed exactly 10 days ago:
$ find /home/you -iname "*.mp3" -atime 10 -type -f

There is also an option called -daystart. It measure times from the beginning of today rather than
from 24 hours ago. So, to list the all mp3s in your home directory that were accessed yesterday,
type the command
$ find /home/you -iname "*.mp3" -daystart -type f -mtime 1

1085
RAID ที่นิยมกันก็มี 0 , 1 , 5 , 0+1 , 1+0 เท่านั้นเอง บางคนอาจสงสัยว่า มีด้วยเหรอ RAID 1+0
อ่านต่อกันนะครับ

คำอธิบาย Logic Drive หมายถึง Drive ที่สร้างขึ้นมาจาก RAID นะครับ

RAID 0
RAID 0 เป็นแบบที่ง่ายและเป็นพื้นฐานที่สุดเลย คือ ทำการรวม HDD 2 ตัว (หรือมากกว่า) ให้กลายเป็น HDD
ตัวเดียวกัน (เรียกว่า Logic Drive) เช่น ถ้ามี HDD 40G 2 ตัว ตัวแรกจะเป็น C อีกตัวก็จะเป็น D
เมื่อเราแปลงเป็น RAID 0 เนื้อที่ของทั้ง 2 ตัวก็จะรวมกันเป็น Drive เดียวกัน ชื่อ C มีขนาด 80G
โดยเมื่อแปลงเป็น RAID 0 แล้ว OS และโปรแกรมต่างๆ ก็จะมอง Drive ใหม่นี่ เสมือนเป็น Drive
เดีวยวกันที่สามารถอ่านเขียนข้อมูลต่างๆ ได้ตามปกติ
ข้อดี คือ ทำให้การอ่านหรือเขียนข้อมูลเร็วขึ้นมาก เพราะมีหัวอ่าน/เขียนข้อมูลเพิ่มมากขึ้น (ในระบบ
SCSI
นะครับ ถ้า IDE หัวอ่านเพิ่มขึ้น ก็ไม่เร็วขึ้นหรอก ไปอ่านที่ อันแรกก็ได้ครับ)
และมันยังขยายเนื้อที่สำหรับเก็บข้อมูลได้ง่ายขึ้นด้วย
ข้อเสีย เนื่องจากมันไม่ได้ทำการสำรองข้อมูลเลย หรือแม้แต่ Parity Bit มันก็ไม่ได้ทำ (แม้แต่นิดเดียว)
ถ้า HDD ตัวไหนเสีย มันจะทำให้ Logic Drive ที่เราสร้างขึ้น เสียไปทั้งหมด ข้อมูลของเราก็จะหมดไปด้วย
(เศร้า)

RAID 1
RAID 1 ต่างจาก RAID 0 แบบว่า หน้ามือเป็นหลังมือเลย เนื่องจาก RAID 1 จะเก็บข้อมูลทั้งหมดลง HDD
ตัวแรก
เหมือนการใช้งานทั่วๆ ไป แต่จะมี HDD ตัวที่สองเพิ่มเข้ามา เราเรียก HDD ตัวนี้ว่า MIRRORING หรือ
DUPLEXED โดยที่ HDD ตัวทั่สองนี้ จะทำการสำรองข้อมูลจากตัวแรก เพื่อป้องกันการสูญหายของข้อมูล
โดยมีข้อกำหนดว่า HDD ทั้ง 2 ตัว ต้องขนาดเท่ากันพอดี ถ้าเป็นไปได้ ควรจะเป็นยี่ห้อ
และรุ่นเดียวกันด้วย
ข้อดี อย่างที่เห็นล่ะครับ ข้อมูลจะถูกสำรองไว้ตลอดเวลา ไม่ต้องกลัวว่าจะหายเลย ถ้า HDD ตัวไหนเสีย
อีกตัวก็จะขึ้นมาทำงานแทนทันที ถ้าเป็น Server ที่มีระบบ Hot Swap เราสามารถถอด HDD ตัวที่เสีย
ไปเปลี่ยน แล้วเอาตัวใหม่มาใส่ได้ทันที โดยไม่ต้องปิดเครื่องเลย เมื่อเราเอา HDD ตัวใหม่มาใส่
ระบบก็จะทำการสำรองข้อมูลไปที่ HDD ตัวใหม่เอง โดยอัตโนมัติ และข้อดีอีกอย่างคือ
มันสามารถเลือกที่จะอ่านข้อมูลจาก HDD ตัวไหนก็ได้ ทำให้มันอ่านข้อมูลได้เร็วขึ้น
ข้อเสีย เนื่องจากมันต้องเขียนข้อมูลลง HDD ถึง 2 ตัวต่อข้อมูล 1 ชุด
ทำให้ภาระในการเรียนข้อมูลมีมากขึ้นเป็น 2 เท่าเลย ทำให้เวลาในการเขียนข้อมูลมากกว่าปกติ
และระบบนี้ใช้เงินเยอะพอสมควร เพราะต้องใช้ HDD 2 ชุด ต่อข้อมูล 1 ชุด

RAID 0 + 1
RAID 0+1 คือการนำข้อดีของ RAID 0 และ RAID 1 มารวมกัน ทำให้มีการรวมเนื้อที่จาก HDD หลายๆ
ตัวเข้าด้วยกัน ละมีการทำสำเนาไปพร้อมกันด้วย โดยต้องนำ HDD มาแปลงเป็น RAID 0 จำนวน 2 ชุดก่อน
ทำให้ได้
Logic Drive ที่มีเนื้อที่ของ HDD มารวมกัน แล้วจึงสร้าง RAID 1 ขึ้นมาอีกทีจาก Logic Drive ทั้ง 2 ชุด
ข้อเสีย เปลือง HDD อย่างมาก และถ้า HDD ตัวไหนเสียไป อาจทำให้ Logic Drive เสีย และเจ้งทั้งระบบ

RAID 1 + 0
RAID 1+0 มีการทำงานเหมือนกับ RAID 0+1 เพียงแต่จะเริ่มสร้าง RAID 1 มาก่อน 2 ชุด
เพื่อทำการสำรองข้อมูลกันก่อน แล้วค่อยสร้าง RAID 0 ขึ้นมาอีกที เพื่อรวมข้อมูลทั้งหมดเข้าด้วยกัน
วิธีนี้เป็นที่นิยมมากกว่า RAID 0+1 อีกนะ (ผมก็ใช้ตัวนี้)
ข้อดี ถ้า HDD ตัวไหนเสีย จะไม่ทำให้ Logic Drive เสียไปด้วย
ข้อเสีย เปลือง HDD มากกว่า RAID 0+1 อีก ;-(

RAID 5
RAID 5 ได้นำข้อดีของ RAID แบบต่างๆ มารวมไว้ในตัวเอง คือ ราคา ประสิทธิภาพ
และความสามารถในการป้องกันข้อมูลสูญหาย เพราะ RAID แบบต่างๆ จะมีข้อดีไม่ครบทั้งหมด
คืออย่างมากก็ได้แค่
2 ใน 3 อย่าง RAID 1+0 ประสิทธิภาพดี ป้องกันข้อมูลได้ แต่แพงโคตร RAID 5 ต้องการ HDD 3 ตัวในการทำงาน
โดยนำเนื้อที่ของ HDD แต่ละตัวมาเก็บรวมกันเป็น 1 Logic Drive เหมือนการทำงานของ RAID 0 แล้วสร้าง
Parity Bit เพื่อใช้xxx้ข้อมูลของแต่ละ Drive ขึ้นมาโดยแยกออกไปเขียนใน Drive อื่นๆ เช่น Parity Bit
ของ
HddA จะนำไปไว้ที่ HddC ของ HddC ก็จะนำไปไว้ที่ HddB ส่วนของ HddB ก็จะนำไปไว้ที่ HddA วนกันไป 555
ข้อดี ข้อมูลไม่หายแน่นอน เพราะมีการเก็บ Parity ไว้ใน HDD แต่ละตัว แล้วความเร็วในการอ่านข้อมูล
ก็เยอะมากๆ ด้วย เนื่องจากมี HDD ถึง 3 ตัวนี่
ข้อเสีย เขียนข้อมูลได้ช้ามากๆ เนื่องจากต้องเขียนข้อมูลแล้ว ยังต้องไปเขีนยน Parity อีก
แล้วยังต้องใช้
HDD ถึง 3 ตัว ซึ่งเปลืองมาก (แต่ก็น้อยกว่า RAID 1+0) และอัตราความเร็วในการเขียนข้อมูลก็ช้ามากๆ

1087
ใช้ OS Debian 3.1
1.สั่งโหลด จาก mirror ในไทยครับ
โดยเพิ่มไปที่ /etc/apt/sources.list
แล้วสั่ง apt-get update แล้ว

#aptitude install snmpd
#aptitude install mrtg

แล้วมา config

#/etc/snmp/snmpd.conf

ให้เอา # ใส่หน้า คำสั่ง

com2sec paranoid default public

แล้วเอาเครื่องหมาย # ออกจากคำสั่ง

com2sec readonly default public

แล้วสั่ง daemon start

#/etc/init.d/snmpd restart

แล้วต่อไปมา config ไฟส์ mrtg บ้าง

แล้วพิมพ์ ตามนี้เพื่อทำการ สั่งให้สร้าง config


#cfgmaker --global 'options[_]: growright,bits' --global 'WorkDir: /home/golf/xxx' --output=/etc/mrtg.cfg public@127.0.0.1

การเพิ่มกราฟ นั้นไม่จำต้องลบไฟส์ในห้อง DocumentRoot ออกก็ได้น่ะครับแต่เพิ่มกราฟ establish กับ tcp
connection ดังนี้ในไฟส์ดังนี้ (ผมใช้เป็น OS Debian 3.1 ครับ)

#vi /etc/mrtg,cfg

โดยเพิ่ม

### Load Average ###
Target[cpu]: .1.3.6.1.4.1.2021.10.1.5.1&.1.3.6.1.4.1.2021.10.1.5.2: public@127.0.0.1
MaxBytes[cpu]: 100
Unscaled[cpu]: dwmy
Options[cpu]: gauge, absolute, growright, noinfo, nopercent
YLegend[cpu]: CPU Load(%)
ShortLegend[cpu]: (%)
LegendI[cpu]:   CPU System
LegendO[cpu]:   CPU User
Title[cpu]: CPU Analysis
PageTop[cpu]: <H1>CPU Analysis</H1>

### Memory Free ###
Target[mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.4.0: public@127.0.0.1
MaxBytes1[mem]: 1030288
MaxBytes2[mem]: 1052216
Unscaled[mem]: dwmy
Options[mem]: gauge, absolute, growright, noinfo
YLegend[mem]: Memory Free
ShortLegend[mem]: Bytes
kilo[mem]: 1024
kMG[mem]: k,M,G,T,P
LegendI[mem]: Real
LegendO[mem]: Swap
Legend1[mem]: (MBytes)
Legend2[mem]: (MBytes)
Title[mem]: Memory Analysis
PageTop[mem]: <H1>Memory Analysis</H1>

### New TCP Connection Monitoring (per minute) ###
Target[server.newconns]: tcpPassiveOpens.0&tcpActiveOpens.0: public@127.0.0.1
Title[server.newconns]: Newly Created TCP Connections
PageTop[server.newconns]: <H1>New TCP Connections</H1>
MaxBytes[server.newconns]: 10000000000
ShortLegend[server.newconns]: c/s
YLegend[server.newconns]: Conns / Min
LegendI[server.newconns]: In
LegendO[server.newconns]: Out
Legend1[server.newconns]: New inbound connections
Legend2[server.newconns]: New outbound connections
Options[server.newconns]: growright,nopercent,perminute

### Established TCP Connections ###
Target[server.estabcons]: tcpCurrEstab.0&tcpCurrEstab.0: public@127.0.0.1
Title[server.estabcons]: Currently Established TCP Connections
PageTop[server.estabcons]: <H1>Established TCP Connections</H1>
MaxBytes[server.estabcons]: 10000000000
ShortLegend[server.estabcons]:
YLegend[server.estabcons]: Connections
LegendI[server.estabcons]: In
LegendO[server.estabcons]:
Legend1[server.estabcons]: Established connections
Legend2[server.estabcons]:
Options[server.estabcons]: growright,nopercent,gauge

และสุดท้ายก็สั่ง คำสั่งข้างล่างนี้เพื่อให้แสดง output ออกมาที่ไฟส์ไหนถ้ารูปไม่ออกอาจเกิดจากการระบุ
output ผิดครับ

#indexmaker /etc/mrtg.cfg --columns=1 --output /home/golf/xxx/index.html

แล้วสั่ง mrtg 3 ครั้งสำหรับ Debian ครับ

#mrtg
#mrtg
#mrtg

แต่ถ้าเป็น ubuntu 6.06ให้สั่ง

#env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
#env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
#env LANG=C /usr/bin/mrtg /etc/mrtg.cfg

3 ครั้งน่ะครับ

จะได้กราฟตามนี้ครับที่ ===> http://www.saiyaithai.org/mrtg

1088
สำหรับ hard link ที่ใช้กัน ส่วนใหญ่ จะเป็น

เป็นการสร้างเพื่อ ชี้มาที่ inode เดียวกัน

ไฟล์ A---->inode 0001
สร้างhard link ไปที่ไฟล์ B และ ไฟล์ C ชี้ไปที่ ไฟล์ A ซึ่งมี inode = 0001
ไฟล์ B ก็ชี้ไปที่ inode 0001
ไฟล์ C ก็ชี้ไปที่ inode 0001

ดังนั้น
-ถ้าเราทำการแก้ไข ไฟล์ต้นฉบับ (A) ไปก็จะส่งผลกระทบต่อไฟล์ที่เชื่อมโยง (B) และ (C) ด้วย เพราะมี inode
เดียวกัน
-ถ้าเราทำการลบ ไฟล์ A ไป ไฟล์ C ก็ยังเข้าได้ผ่านไฟล์ B ได้ เพราะชี้ไปที่ inode เดียวกัน
-แต่ถ้าเราลบไฟล์ B ไปอีก ไฟล์ C จะเข้าไม่ได้ เพราะมันไม่ไฟล์ให้เข้าถึงอีก

แต่ข้อเสีย ของ hard link คือใช้งานกับ directory ไม่ได้ หรือข้าม partition ไม่ได้
เหมาะกับการเอาไปเป็นข้อมูล backup
ที่เป็นไฟล์สำคัญๆและป้องกันข้อมูลสูญหายมากกว่า
----------------------------------------------------------------------------------------------

Soft link (symlink) เหมือนการทำ short cut ซึ่งจะทำการสร้าง inode ออกมาจาก ตัว inode หลักนั้นๆ

เมื่อมีการสร้างไฟล์ A และสร้าง soft link ไฟล์ B และ C
ก็จะทำการสร้าง inode ของไฟล์นั้นเข้ามาใหม่ ทำให้ inode ไม่ซ้ำกับไฟล์ต้นฉบับ

ซึ่งถ้า มีไฟล์ symlink_file อยู่ต้องการสร้าง soft link ไปที่ไฟล์หลัก
soource_file จะทำได้โดย
#ln -s source_file symlink_file
ถ้าเราทำการลบไฟล์ source_file ไปก็จะทำให้เข้าไปใช้งานไฟล์ symlink_file ไม่ได้
เพราะว่า inode หลักที่อ้างถูกลบไปแล้ว

แต่ข้อดีของ soft link คือสามารถทำผ่าน partition ได้ครับ

ถ้ามีปัญหาสงสัย ก็โฟสไว้ได้น่ะครับ
golfreeze[at]packetlove.com

1089
ฟอร์แมตแรกที่จะแนะนำก็คือ gzip ย่อมาจาก GNU zip คำสั่งก็ตรงไปตรงมาครับ ก็คือ

gzip filename.txt

หลังจากกด enter ไปไฟล์ filename.txt ของเราจะหายไป แล้วกลายร่างเป็น filename.txt.gz เสียแล้ว
แต่ใครยังอยากเก็บไฟล์ต้นฉบับไว้อยู่ก็ให้ใส่ -c เข้าไปซึ่งเป็นการบอกให้ gzip แสดงเข้ามาออกทาง
standard output จากนั้นเราก็ทำการเปลี่ยนทิศทางไปยังไฟล์ที่เราต้องการ

gzip -c filename.txt > filename.txt.gz

ผลลัพธ์ที่ได้ก็จะเหมือนกับคำสั่งแรกต่างกันที่ต้นฉบับเรายังอยู่ ไม่ไปไหน
และถ้าเราอยากให้ไฟล์ที่ได้บีบกันแบบสุดๆ ก็ให้ใส่ -9 แต่ถ้าอยากได้ความเร็วก็ใส่ -1
จริงแล้วเราสามารถใช้ค่าได้ตั้งแต่ -1 ถึง -9 อย่างเช่น

gzip -9 filename.txt

แต่ถ้าใครอยากใช้ -9 ตลอดแต่ไม่อยากพิมพ์ทุกครั้งก็ให้เซ็น environment variable ดังนี้

export GZIP="-9"

อะพอเราบีบเสร็จ ก็ต้องทำการแตกไฟล์ ก็ทำโดย

gunzip filename.txt.gz

และเหมือนเดิมไฟล์ .gz จะหายไป ถ้าไม่อยากให้หายก็ต้องทำอย่างนี้

gzip -c filename.txt.gz > filename.txt

เป็นยังไงสำหรับฟอร์แมตแรก ใช้ไม่อยากเลย อะฟอร์แมตที่สองที่จะแนะนำก็คงเป็น bzip2 นั่นเอง
ซึ่งอันนี้มาทีหลัง gzip แต่บีบได้เล็กกว่า รูปแบบคำสั่งก็จะคล้ายกัน ดังนี้

bzip2 filename.txt

เราก็จะได้ filename.txt.bz2 มา แต่ไฟล์ต้นฉบับก็จะหายไป
ถ้าไม่อยากให้หายไม่ต้องทำการเปลี่ยนทิศทางให้เมื่อยเหมือน gzip เพราะมี option -k มาให้ k ย่อมาจาก
keep ก็ใช้ง่ายๆ ดังนี้

bzip2 -k filename.txt

และเหมือนกันกับ gzip ซึ่งมีระดับการบีบอัด ตั้งแต่ 1 ถึง 9 ซึ่ง 1 จะใช้เวลาน้อยที่สุด ส่วน 9
จะบีบอัดได้สูงสุด

bzip2 -9 filename.txt

ไหนๆ ก็เหมือนกันมาเยอะแล้ว ก็ต้องกำหนดค่า default ได้โดย

export BZIP2="-9"

และถ้าเราอยากแตกไฟล์ออกก็ใช้คำสั่ง

bunzip2 filename.txt.bz2

ซึ่งไฟล์ .bz2 ก็จะหายไป ถ้าไม่อยากให้หายก็ใส่ -k ไปด้วยน้า

มาถึงคำสั่งสำคัญอีกคำสั่ง คือ คำสั่ง tar คำสั่งนี้ไม่ได้มีการบีบอัดใดๆ แต่จะเป็นการรวมไฟล์หลายๆ
ไฟล์ไว้เป็นไฟล์เดียวกัน รูปแบบคำสั่งก็จะเป็นดังนี้

tar -cvf test.tar filename.txt mydir

คำสั่งขั้นบนจะเป็นการนำไฟล์ filename และไดเร็กทอรี่ mydir มา tar แล้วเก็บไว้ในชื่อไฟล์ text.tar โดย
option c หมายถึงเป็นการสร้างไฟล์ tar -v เป็นการแสดงรายละเอียดขณะทำงาน และ -f
หมายถึงระบุชื่อไฟล์ที่เราต้องซึ่งก็คือ test.tar

อะและถ้าเราต้องการ extract ไฟล์ก็ทำได้โดย

tar -xvf test.tar

ซึ่ง -x หมายถึงการ extract นั่นเอง ขั้นตอนต่อไปจะเป็นการรวมพลังกันระหว่างการบีบอัดและการ tar
ถ้าเราต้องการรวมพลัง tar กับ gzip ก็ใช้คำสั่ง

tar -cvzf test.tar.gz filename.txt mydir

ซึ่งเราจะ tar ไฟล์ filename.txt และไดเร็กทอรี่ก่อน แล้วจึงทำการบีบอัดด้วย gzip แต่ถ้าใครชอบ bzip2
ก็ให้ใช้คำสั่

tar -cvjf test.tar.bz2 filename.txt mydir

จะเห็นว่าถ้าเป็น gzip เราจะใช้ option -z แต่ถ้าเป็น bzip2 จะเป็น -j อะดังนั้นถ้าเราต้องการ extract
ก็ให้ใช้คำสั่ง สำหรับ gzip

tar -xvzf test.tar.gz

และ bzip2 ก็จะเป็น

tar -xvjf test.tar.gz

เรียบร้อยครับ เป็นยังไงใช้กันไม่อยากครับ ตรงไปตรงมา ไปและ



Special Thank = http://www.logicdream.com

1090
ปกติแล้ว เราสามารถตั้ง crontab ในลักษณะ นี้ ได้นะครับผม

@reboot = run at boot and reboot only
@yearly = run at midnight Jan 1 each year (equiv to 0 0 1 1 *)
@annually = run at midnight Jan 1 each year (equiv to 0 0 1 1 *)
@monthly = run at midnight on the first day of each month (equiv to 0 0 1 * *)
@weekly = run at midnight each Sunday (equiv to 0 0 * * 0)
@daily = run at midnight each day (equiv to 0 0 * * *)
@midnight = run at midnight each day (equiv to 0 0 * * *)
@hourly = run on the first second of every hour (equiv to 0 * * * *)
- or -
1 2 3 4 5 = specific time tags
- where -
1 = Minute (of hour) to activate [0-59]
2 = Hour (of day) to activate [0-23]
3 = Day (of month) to activate [1-31 ... 29,30,31 may not activate during all months]
4 = Month (of year) to activate [1-12 or 3-letter names "Jan,Feb,Mar"]
5 = Weekday to activate [0-6 or 3-letter names "Mon,Tue,Wed"]  (0=Sunday)

1091
วันนี้ พอดีมีเครื่อง R200 มาให้เล่นก็เลย ลองลงเป็น debian 4.0 R3 64 bit
กับ vhcs2 ครับ

ลองมาดูขั้นตอนกันเด้อ ซู

ก่อนอื่นลง OS ตั้ง ip เสร็จ ก็มาแก้ไข sources.list ให้โหลด package ในไทยตามนี้เด้อ

#vi /etc/sources.list

deb http://ftp.thaios.net/debian/etch main
deb-src http://ftp.thaios.net/debian/etch main

ทำการ update และ upgrade package
#apt-get update
#apt-get upgrade
แล้วทำการลง ssh ก่อนโดย

#apt-get install ssh

แล้วทำการลง package ต่างๆ ที่จำเป็นที่จะใช้ดังนี้

ลง MySQL server (MySQL5):

#apt-get install mysql-server mysql-client libmysqlclient15-dev phpmyadmin

แก้ไขให้ client connect ไปที่ mysql server ได้ทุกแบบไม่ใช่แค่ "localhost"
#vi /etc/mysql/my.cnf

หาบันทัด bind-address = 127.0.0.1 แล้วใส่ # ไปซะ
# bind-address = 127.0.0.1

ทำการ restart MySQL:
#/etc/init.d/mysql restart

ใส่ค่า password ของ mysql
#mysqladmin -u root password yourpassword

ต่อไปทำการ install Apache 2(.2) with PHP5

#apt-get install apache2 apache2-doc apache2-mpm-prefork apache2.2-common apache2-utils libexpat1 ssl-cert

และต่อไป install php5

#apt-get install libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd php5-idn php-pear
php5-imagick php5-imap php5-json php5-mcrypt php5-memcache php5-mhash php5-ming php5-mysql php5-ps
php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

จะขึ้น Pop-up ถามว่า

Continue installing libc-client without Maildir support?

ตอบไปว่า yes

แก้ไข config apache ดังนี้

DirectoryIndex index.html index.htm index.shtml index.cgi index.php index.php3 index.pl index.xhtml

แก้ไขไฟล์ /etc/apache2/ports.conf

โดยเพิ่ม Listen 443:

#vi /etc/apache2/ports.conf

Listen 80
Listen 443

ทำการเปิด module ต่างๆ ที่จะใช้ใน apache
#a2enmod ssl
#a2enmod rewrite
#a2enmod suexec
#a2enmod include

ทำการ reload config apache
#/etc/init.d/apache2 force-reload

ต่อไปทำการลง mail server
#apt-get install postfix postfix-tls

จะถามค่า hostname
ใส่ให้ตรงกับ ตอนที่ install os
เช่น server1.example.com

#apt-get install proftpd proftpd-mysql

เลือกเป็นแบบ "Stand alone"

#apt-get install courier-authdaemon courier-base courier-imap courier-maildrop courier-pop
libberkeleydb-perl libcrypt-blowfish-perl libcrypt-cbc-perl libcrypt-passwdmd5-perl libdate-calc-perl libdate-manip-perl libdbd-mysql-perl libdbi-perl libio-stringy-perl libmail-sendmail-perl libmailtools-perl libmd5-perl libmime-base64-perl libmime-perl libnet-dns-perl
libnet-netmask-perl libnet-perl libnet-smtp-server-perl
libperl5.8 libsnmp-session-perl libterm-readkey-perl libtimedate-perl perl perl-base perl-modules
bind9 diff gzip iptables libmcrypt4 patch procmail tar original-awk libterm-readpassword-perl
libsasl2-modules libsasl2 sasl2-bin bzip2 gcc make libc6-dev

แก้ไขไฟล์ /etc/proftpd/proftp.conf ดังนี้

#vi /etc/proftpd/proftpd.conf

UseIPv6 off

เพิ่ม config นี้ลงไป

DefaultRoot ~
IdentLookups off
ServerIdent on "FTP Server Ready"

ทำการ save แล้วออกไป.

ต่อไปทำการ download package vhcs2 มาคับตัวนี้ สามารถ support ได้ทั้งแบบ 64 bit กับ 32 bit น่ะครับ

#cd /usr/src
#wget http://downloads.sourceforge.net/vhcs/vhcs2-2.4.7.1.tar.bz2

แตกไฟล์:
#tar -xjf vhcs2-2.4.7.1.tar.bz2

เข้าไปยัง
#cd vhcs2-2.4.7.1

สั่งรัน เพิ่มให้สร้างตัว install :
#make install

หลังจากนั้นเข้าไปที่
#cd /tmp/vhcs-2.4.7.1

ทำการ copy file ไปยังปลายทาง:
#cp -R * /

##แก้ path fix error ftp ผ่านเว็บครับ

แก้ bug เรื่อง filemanager

Edit files:
/etc/vhcs2/apache/parts/als_entry.tpl
/etc/vhcs2/apache/parts/als_php2_entry.tpl
/etc/vhcs2/apache/parts/dmn_entry.tpl
/etc/vhcs2/apache/parts/dmn_php2_entry.tpl
/etc/vhcs2/apache/parts/sub_entry.tpl
/etc/vhcs2/apache/parts/sub_php2_entry.tpl

แล้วแก้ไขไฟล์ โดยลบ / หลัง /tmp/ ออกครับให้เหลือแค่

/tmp ในไฟล์เหล่านี้

เช่น
php_admin_value open_basedir "{GUI_ROOT_DIR}/:/etc/vhcs2/:/proc/:{WWW_DIR}/:/tmp/"
แก้เป็น
php_admin_value open_basedir "{GUI_ROOT_DIR}/:/etc/vhcs2/:/proc/:{WWW_DIR}/:/tmp"

แล้วเข้าไปที่ :
#cd /var/www/vhcs2/engine/

ทำการแก้ไข file vhcs2_common_code.pl
#vi vhcs2_common_code.pl

ใช้ vi เข้าไปแก้ บันทัดที่ "1408" หาคำว่า:
'key'=>$main::db_pass_key,

เพิ่มบันทัดนี้เข้าไปก่อนหน้า นั้น
'keysize'=>32,

ระวังเครื่องหมายด้วยน่ะครับ
แก้ที่บันทัด ประมาณ 1446 ให้ใส่ตามข้างบนนั้น


save แล้วออกมา.

เริ่มทำการ install vhcs ครับ
#cd /var/www/vhcs2/engine/setup
#./vhcs2-setup

1092
#พอดี เครื่องที่ได้ดูแล มีปัญหาเรื่อง perl script น่ะครับ ลักษณะคือมีการรัน perl5.7 อยู่ซึ่งเป็น
user apache สำหรับวิธีการ tracking หาต้นตอ นั้น ขอให้มีสติ ให้นิ่งก่อน แล้วเริ่มใช้วิธีง่ายๆ ดังนี้
ครับ
1.find command

#find / -user apache -name "*.pl"
#find / -user apache -name "*.txt"

ดูเจอไฟล์ไหนแปลกๆ ก็เข้าไปดูครับ

2.จะเข้าไปดูตาม log ของ access กับ error_log ใน apache ครับ
#less /var/log/httpd/access_log
#less /var/log/httpd/error_log

3.เข้าไปใน ไฟล์พวก log ของ ไฟล์ ftp log
#less /var/log/xferlog

4.อีกวิธีที่ผมชอบใช้ คือ ใช้ตัว application มาช่วย คือตัว lsof

ตัวนี้ เก๋าพอสมควร ซึ่งมันจะ หา ip ที่ connection "ESTABLISH" อยู่

และบางทีอาจปรากฏเป็น path ที่รันไฟล์อยู่ด้วย เราก็เข้าไปจัดการได้เลย

#lsof -i -n -P | more
ถ้าเจอ ip ที่เชื่อมต่อก็ block เพื่อตัดปัญหาที่ปลายทางไปก่อนก็ได้ครับ

5.ลองดู crontab ของ user นั้นๆเช่น
#crontab -u apache -l
ถ้าเจอ ก็ตามไปดู ไฟล์นั้น

6.ทำการ ตรวจไวรัสโดยใช้ tools clamscan ช่วย
#clamscan /

ึ7.ถ้าไม่เจอ จริงๆ แล้วล่ะก็ทำการ Killall process นั้นทิ้งซะ เช่น
#killall -9 perl5.7.8


ซึ่งปัญหาที่เกิดขึ้น แอดมินแต่ล่ะคนก็ แก้ปัญหาตามสไตส์ที่ตนเองถนัด สำหรับผมก็จะใช้ประมาณนี้
เลยอยากมาแชร์ให้เพื่อนๆ ได้ทราบ เผื่ออาจจะช่วยท่านได้บ้าง

แต่สุดท้าย ทุกอย่างก็ล้วนไม่มีอะไรสมบูรณ์ มันเป็น อนิจจัง ครับ

golfreeze[at]packetlove.com

1093
#คำศัพท์ เกี่ยวกับ DNS

ความหมาย/หน้าที่
Forward Master Zone
แปลงชื่อ Domain เป็น IP Adress

Reverse Master Zone
แปลง IP Adress เป็นชื่อ Domain

BIND(Berkeley Internet Name Domain)
โปรแกรมให้บริการดีเอ็นเอส

Quote
bind-utils-9.2.4-2
bind-9.2.4-2
bind-libs-9.2.4-2
ypbind-1.17.2-3
bind-chroot-9.2.4-2

Master Name Sever

Slave Name Server Name Server มีสองแบบคือ Master Name Sever และ
Slave Name Server ซึ่งจะต้องอยู่ต่างเครื่องกัน

Master Name Server จะอ่านข้อมูลประจำโซนจากแฟ้มใน Disk และมีสิทธิหน้าที่ในโซนนั้นๆ
Slave Name Server สำเนาข้อมูลจาก Master Name Server

named โปรแกรมบริการชื่อ Domain บน Linux

/usr/sbin/named -u named -t /var/named/chroot
Host
เครื่องคอมพิวเตอร์ศูนย์กลางที่ต่อเข้ากับ Network,Internet เพื่อบริการใดๆ

Domain
ชื่อ web address/computer
ทำให้เข้าใจ/จำง่าย/มีความหมาย กว่า IP Address

ถ้าเรียก โดเมน เหมือนเรียก IP Address

http://www.icann.org/

Sub Domain
ส่วนย่อยของ Domain เช่น mail.dns.linuxstep.com
mail คือ Sub Domain
Localhost = เครื่องเราเอง จะตั้งค่า ip address อะไร แต่เมื่อเรียก
Localhost ก็จะเรียกเครื่องเราเอง

IP Address 127.0.0.1 = Localhost
IP(Internet Protocal)
IP หรือ Internet Protocal
IP Address คือรหัสตัวเลขที่กำหนดขึ้น เพื่อใช้อ้างถึงที่อยู่ของ Host และ Network
ตัวเลขเหล่านี้จะไม่ซ้ำกัน (unique numeric) ซึ่งมีมาตรฐานในการกำหนด และเป็นที่รู้จักทั่วโลก IPV4,IPV6
Port ช่องทาง ที่ใช้ติดต่อกับ computer/อุปกรณ์ ใน network

(Address Localhost)
สำหรับ window กำหนด loopback IP=127.0.0.1 เมื่อส่งสัญญาณออกจะไม่ส่งออกนอกเครือข่าย แต่จะกลับมา
ที่ต้นทาง ด้วย loop back driver

http://www.certiguide.com/netplus/
cg_np_IVLoopbackAddressingandConnections.htm

Root Domain ตอนนี้มีอยู่ 13 ตัวทั่วโลก
ลำดับบนสุดของระบบ DNS โดยมีการกำหนดดังนี้
ลำดับที่ 1 root
ลำดับที่ 2 .com, .net, .edu, .gov, .org, .th, ...
ลำดับที่ 3 subdomain ตัวอย่าง mail3.linuxstep.com
mail3 =subdomain

ตัวอย่างลำดับ DNS
www2.dns.linuxstep.com

Name Resolution

การที่ DNS Server เก็บหมายเลข IP Address คู่กับชื่อเครื่องแบบ Domain Name
เพื่อทำการค้นหาชื่อตามการร้องขอของเครื่อง DNS client
DNS Server
เครื่องที่คอยจัดการ/ดูแล/แก้ไข domain subdomain
Client
เครื่องลูกข่าย
Resolver
กลไกการแปลง domain เป็น ip address
Reverse File
/etc/resolve.conf

การ resolve หมายถึง
DNS Zone
การจัด domain เป็นพื้นที่ย่อย
TCP,UDP,ARP

เป็น protocol ที่ DNS ใช้ในการติดต่อผ่าน port 53

TCP - ปรับปรุงข้อมูล
UDP - ส่งคำขอข้อมูล
ARP - แปลงค่า IP address เป็น Hardware

#Files ที่ใช้ในการ Config
Files Config

ความหมาย / หน้าที่
/etc/named.conf
กำหนดการทำงานของ Name Server สำคัญมาก
/var/named/named.ca เก็บข้อมูลชื่อ Server ซื่งเป็น Root Name Server
ไม่ควรแก้ไขข้อมูลใน file นี้เป็นอันขาด
/var/named/named.local เก็บข้อมูลการ map ชื่อ กับ IP Address แบบ Reverse Lookup
/var/named/dm.com.zone ระบุชื่อ Server,Zone, ทำ Forward Lookup
/var/named/ 203.203.203.203.zone

Reward Lookup
/etc/hosts
เก็บชื่อ host
/etc/resolve.conf
เก็บชื่อ domain และ ip ที่ทำงานแบบ Forward Lookup
/etc/sysconfig/network เก็บชื่อ host ของเครื่อง
/etc/sysconfig/named กำหนดตำแหน่งของ named ที่เป็น file config

#ความหมายของ SOA Resource Record
SOA (Start to authority)
เริ่มให้สิทธิ/อำนาจ

ความหมาย / หน้าที่

Domain Name ที่ได้จาก /etc/named.comf
IN Internet
serial = เป็นหมายเลขที่ใช้แสดงการอัพเดทข้อมูลระหว่าง Primary master และ Secondary master
refresh = ระยะเวลา refresh ข้อมูล
retry = ระยะเวลา ตรวจสอบข้อมูลกับ Primary DNS Server
expire = ระยะเวลา หมดอายุ กรณีติดต่อกับ Primary DNS Server ไม่ได้

#ความหมายข้อมูลใน Fields Type DNS Record
Type

ความหมาย / หน้าที่
A = แปลงชื่อ Domain เป็น IP Address แบบ ipv4
NS = Name Server record กำหนดว่า server ใดรับผิดชอบ domain
PTR = Pointer record ใช้ Map IP Address เป็น Host

MX = Mail Exchanger
ใช้ระบุชื่อเครื่อง Mail Server ของ Domain
เมื่อเราต้องกำหนด MX เราจะพบว่าต้องกำหนด
MX 10 mail1.example.com
MX 20 mail2.newdomain.com

ค่าเลข 10 และ20 นั้นหมายถึง Priority ของ Mail โดยค่าน้อยสุดจะสำคัญสุด

ชื่อหลัง MX ต้องเป็น Domain เท่านั้นและไม่ใช่ CName
CNAME = Cononical name ใช้เป็น Alias name ให้เป็น host จริง
เช่น ns,mail,www,www2
;
Remark


1094
ลง mailgraph + syslog-ng ใน freebsd

#cd /usr/ports/mail/mailgraph
#make install clean

เพิ่มใน /etc/rc.conf ตามนี้
mailgraph_enable="YES"
mailgraph_pidfile="/var/db/mailgraph/mailgraph.pid"
mailgraph_flags=""
logfile /var/log/maillog
"daemon-rrd=/var/db/mailgraph "ignore-localhost "daemon
"daemon-pid=${mailgraph_pidfile}"
mailgraph_user="root"
mailgraph_group="wheel"
สั่งให้ service ทำงาน
/usr/local/etc/rc.d/mailgraph start
ถ้าทำงานแล้วสั่ง
#/usr/local/etc/rc.d/mailgraph status
จะเป็น
mailgraph is running as pid 4406.

SYSLOG ธรรมดา
กรณีให้ส่ง log maillog มาจาก เครื่อง client (smtp0 , smtp1)
ฝั่ง client ที่ต้องการให้ ส่ง log มายังเครื่อง server ก็
ให้เข้าไปแก้ไข ใน
#vi /etc/syslog.conf

ยกตัวอย่างเช่น
ให้ maillog ส่ง log มาที่เครื่อง log server(xxx.yyy.zzz.100) ก็เพียงแค่

# ให้เก็บที่เครื่องตัวเอง กับส่งมาที่ ip xxx.yyy.zzz.100
mail.* -/var/log/maillog
mail.* @xxx.yyy.zzz.100

แล้วจัดการ save
แล้วสั่งให้ syslog ที่เครื่อง client restart 1 ครั้ง

#ตระกูล RH
/etc/init.d/syslog restart

แค่นั้นแล้วไปรอดูที่ cacti_syslog ครับ


แล้วไป config ให้ mailgraph.cgi ให้แสดงรูปให้ถูกที่

แล้วทำการสั่งให้ mailgraph ทำงานโดยเข้าไปเรียก

#/usr/local/sbin/mailgraph.pl --logfile /var/log/customer-maillog/maillog-total
--daemon-pid=/var/db/mailgraph/mailgraph.pid --daemon

ดูว่า service ทำงาน
#/usr/local/etc/rc.d/mailgraph status
จะเป็น
mailgraph is running as pid 4406.
ประมาณนั้น


ส่วนของ syslog-ng ( ฝั่ง server จะเป็น ) นั้น จะอยู่ที่

/usr/local/syslog-ng/syslog-ng.conf

สำหรับ config หลักๆ เป็น

source s_maillog_smtp0 { tcp(ip(116.68.146.13) port(21245)); }; #ควรตรงกับฝั่ง client
destination d_maillog_smtp0 { file("/var/log/customer-maillog/maillog-smtp0"); };
log { source(s_maillog_smtp0); destination(d_maillog_smtp0); }; #สามารถใส่ค่า filter ต่างได้ที่
ในส่วนของ "log" น่ะครับ
ข้อแนะนำ ส่วนของ filter สามารถใช้งานได้ดี เมื่อ log ที่ถูกส่งเข้ามาที่ server มีปริมาณไม่มากเกินไป
จะทำงานได้ ดี
แต่ ถ้ามีปริมาณเข้ามาเร็ว แนะนำ ให้ ส่วนของ client ทำการ ส่งเข้ามาเป็น socket แยก เลย ลองมาดูฝั่ง
client บ้างน่ะครั บ

options {
use_fqdn(no);
sync(0);
};

source s_maillog { unix-stream("/dev/log" max_connections(1000));
pipe("/proc/km
sg"); internal(); };
source s_file { unix-stream("/dev/log"); pipe("/proc/kmsg"); internal();
};

destination d_maillog { file("/var/log/maillog"); };
destination d_backup { tcp("10.20.0.252" port(20245)); }; # socket ควรส่งไปให้ ตรงกับ
ฝั่ง server น่ะครับ
destination d_tools { tcp("116.68.146.13" port(20245)); };# socket ควรส่งไปให้ ตรงกับ
ฝั่ง server น่ะครับ

destination d_tools_1 { tcp("116.68.146.13" port(21245)); };


log { source(s_maillog); destination(d_backup); };
log { source(s_maillog); destination(d_tools); };
log { source(s_maillog); destination(d_tools_1); };
log { source(s_maillog); destination(d_maillog); };

เมื่อ config เสร็จทั้งสองฝั่ง สั่งให้ restart service syslog-ng โดย

#killall syslog-ng
#/usr/local/sbin/syslog-ng

ครับ

ทำการปรับ permission maillog เป็น 644
#chmod 644 /var/log/customer_log/maillog*
แล้วสั่ง รันคำสั่ง ดังนี้
/usr/bin/perl /usr/local/sbin/mailgraph.pl --logfile /var/log/customer-maillog/maillog-smtp0
--daemon-rrd=/var/db/mailgraph/ --ignore-localhost --daemon
--daemon-pid=/var/db/mailgraph/mailgraph2.pid

1095
all application on unix knowledges by golfreeze / set mod rewrite ใน file .htaccess
« on: มกราคม 23, 2011, 12:17:25 am »
ก่อนอื่น ต้องทำความเข้าใจเกี่ยวกับ mod rewrite ก่อน

แต่เด๋วผมมาอัฟเดท อีกที วันนี้ ง่วงๆ เพลียๆ เขียนตัวแปรผิด ตลอด

เอาเป้นว่า config ดังนี้

เปิดไฟล์ httpd.conf ของ apache มาครับ
ถ้าจะใช้ .htaccess ใน dir ก็ config ใน dir นั้นอย่างเดียวน่ะครับ
เช่นจะใช้ ใน document root ที่เป็น /usr/local/www/apache22/data/xxx

ก็ set แบบนี้
<Directory \"/usr/local/www/apache22/data/xxx\">
Options FollowSymLinks
#AllowOverride None จะเลี้ยงใช้ htaccess ไม่ได้ เปลี่ยนเป็น All ครับ
#AllowOveride None
AllowOverride All
Order allow,deny
Allow from all
</Directory>

แล้วไปเขียนไฟล์ test ดังนี้
#vi /usr/local/www/apache22/data/xxx/.htaccess

RewriteEngine on
RewriteBase /
RewriteRules ^(.*)$ index.php?id=$1

-----------------------------------------------------------
#vi index.php
<?php
session_start();
$id = $_GET=[\"id\"];

echo \"$id\";

if (empty($id)) {
echo \"can not use\"; }
else {
echo \"can use\";
}
echo php_uname(\"n\");

?>

แล้วลองเรียกไฟล์ index.php ดูน่ะครับ ถ้าใช้ได้ จะขึ้นเป็น ชื่อ node + can use


golfreeze[at]packetlove.com

Pages: 1 ... 71 72 [73]