Archive for สิงหาคม 1st, 2007

Aug-1-07

การตั้งเวลาด้วย crontab บน ubuntu

posted by อดิศร ขาวสังข์

crontab คืออะไร
crontab เป็น simple text file ที่ประกอบด้วยรายการคำสั่งที่จะให้รันตามเวลาที่กำหนด โดยคำสั่งดังกล่าวจะสัมพันธ์กับเวลาในการรัน ที่ถูกควบคุมจาก cron daemon และถูก executed ใน system’s background ข้อมูลเพิ่มเติมเกี่ยวกับ crontab สามารถดูได้จาก crontab’s man page

crontab ทำงานอย่างไร
ระบบจะรักษา (maintain) crontab ของแต่ละ user ไว้ในระบบการแก้ไขหรือสร้าง crontab คุณจะต้องใช้ text editor ที่ระบบได้กำหนดไว้แล้วโดย nono text editor เป็น default text editor บนระบบ ubuntu ของผู้เีขียนที่ใช้ทดลอง ซึ่ง text editor ดังกล่าวนี้จะต้องเปิดขึ้นมาด้วยคำสั่ง crontab ที่ใช้ option เป็น -e (crontab -e)โดยการสร้าง crontab ให้ใ้ช้คำสั่งดังนี้ :

#crontab -e

เมื่อใช้คำสั่ง crontab -e ระบบจะเปิด nano text editor ขึ้นมาเป็น blank window เพื่อให้เราป้อนเวลาและคำสั่ง สำหรับการตั้งเวลาดังรูปข้างล่าง โดยแต่ละบรรทัดจะแทน separate crontab entry ที่รู้จักกันในชื่อ “cron job”ถ้าคุณไ่ม่คุ้นเคยกับ nono text editor คุณควรจะศึกษาข้อมูลเพิ่มเติมจากแหล่งข้อมูลนอกเหนือจากที่นี้

Crontab Sections
แต่ละ section จะถูกแยกโดยหนึ่งช่องว่าง (space) ซึ่งในส่วนของ section สุดท้าย (command section) จะมี space เป็น 1 space หรือมากกว่า ทั้งนี้จะไม่อนุญาตให้มี space มากกว่า 1 space ระหว่าง section 1-5 โดย section 1-5 จะถูกใช้เพื่อแสดงว่างาน (task) จะถูก executed เป็นเวลาบ่อยแค่ไหน โดยรูปแบบของ cron job จะเป็นดังนี้ :

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command

รุปแบบที่ 1
01 04 1 1 1 /usr/bin/somedirectory/somecommand
หมายถึงจะมีการรันคำสั่ง /usr/bin/somedirectory/somecommand ที่เวลา 4:01am ในวันที่ 1 ที่เป็นวันจันทร์ของเดือนมกราคม

เราสามารถใิ้้้้ช้เครื่องหมาย * เพื่อแทนความหมาย “ทุก ๆ” (ทุกนาที, ทุกชั่วโมง,ทุกวัน, ทุกเดือน, ทุกวันของสัปดาห์)

รูปแบบที่ 2
01 04 * * * /usr/bin/somedirectory/somecommand
หมายถึงจะมีการรันคำสั่ง /usr/bin/somedirectory/somecommand ที่เวลา 4:01am ทุก ๆ วันของทุก ๆ เดือน

เราสามารถใช้คอมม่า (,) เพื่อให้มีการรันกมากว่า 1 instance ของคำสั่งายในช่วงเวลา และใช้เครื่องหมาย dash (-) เพื่อแทนคำสั่งที่ต่อเนื่องได้

รูปแบบที่ 3
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
หมายถึงจะมีการรันคำสั่งที่นาทีที่ 1 และ 31 ถัดจากเวลา 4:00am และ 5:00am (4:01am, 4:31am, 5:01am, 5:31am) ในวันที่ 1-15 ของเดือนมกราคมและมิถุนายน

ข้อความ “/usr/bin/somedirectory/somecommand” จากที่กล่าวมาแสดงถึงงาน (task) ที่จะรัน ณ เวลาที่กำหนด ซึ่งมีการแนะนำว่าควรจะเป็น full path command ดังรูปแบบที่กล่าวมา โดย crontab จะเริ่มรันคำสั่งทันทีเมื่อการแ้ก้ไข (crontab -e) ถูกต้องและมีการบันทึกแล้ว

Crontab Options
crontab -l
: แสดง crontab ที่มีอยู่
crontab -r : เป็นการลบ crontab ที่มีอยู่
crontab -e : เป็นการแก้ไข crontab ที่มีอยู่ ผ่าน editor ที่ถูกระบุไว้ใน environment variables ซึ่งในที่นี้เป็น nano editor

หลังจากที่ exit ออกจาก editor แล้ว crontab ที่เราแก้ไขจะุูถูกตรวจสอบความถูกต้องจากระบบ และถ้าไม่มีความผิดพลาด ก็จะถูกติดตั้งโดยอัตโนมัติ

Crontab Example

ตัวอย่างที่ 1
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
หมายถึงเป็นกำหนดให้ crontab มีการรัน updatedb ซึ่งจะเป็นการอัพเดต slocate database ทุก ๆ เช้าเวลา 4:45am วิธีการทำก็คือให้พิมพ์คำว่า crontab -e และพิมพ์ข้อความหนึ่งบรรทัดดังกล่าวใต้บรรทัิดต่อไปนี้ :
# m h dom mon dow command

แล้วให้บันทึกการแก้ไขและออกจาก editor

ถ้าเรากำหนด crontab ไม่ถูกต้องตามรูปแบบจะมีข้อความฟ้องดังตัวอย่างดังนี้ :
“/tmp/crontab.P7vQuf/crontab”:2: bad day-of-month
errors in crontab file, can’t install.
Do you want to retry the same edit?

เราสามารถใช้ double-ampersand (&&am ;) ในส่วนของ command section เพื่อรันคำสั่งหลาย ๆ คำสั่งที่ติดต่ีอกัน (consecutively) ได้

ตัวอย่างที่ 2
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
หมายถึงเป็นการรัน chkrootkit และ updatedb ทีุ่เวลา 4:45am ของทุกวัน

ตัวอย่างที่ 3
10 3 * * * /usr/bin/foo
หมายถึงเป็น /usr/bin/foo ที่เวลา 2:10pm ของทุกวันจันทร์

จบครับ

Aug-1-07

Enabling and disabling services during start up in ubuntu

posted by อดิศร ขาวสังข์

อ้างอิง
http://linuxhelp.blogspot.com/2006/04/enabling-and-disabling-services-during_01.html

Debian Linux has its own script to enable and disable services across runlevels. It is called update-rc.d. Going by the above example, you can enable apache webserver as follows:

# update-rc.d apache2 defaults

… this will enable the apache webserver to start in the default run levels of 2,3,4 and 5. Of course, you can do it explicitly by giving the run levels instead of the “defaults” keyword as follows:

# update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .

The above command modifies the sym-links in the respective /etc/rcX.d directories to start or stop the service in the destined runlevels. Here X stands for a value of 0 to 6 depending on the runlevel. One thing to note here is the dot (.) which is used to terminate the set which is important. Also 20 and 80 are the sequence codes which decides in what order of precedence the scripts in the /etc/init.d/ directory should be started or stopped.

And to disable the service in all the run levels, you execute the command:

# update-rc.d -f apache2 remove

Here -f option which stands for force is mandatory.

But if you want to enable the service only in runlevel 5, you do this instead:

# update-rc.d apache2  start 20 5 . stop 80 0 1 2 3 4 6 .
Aug-1-07

ความแตกต่างระหว่าง VPN และ VPN pass-through

posted by อดิศร ขาวสังข์

You will see this feature mainly in home (or small office) Internet gateway devices. This is an excellent question because it confuses a lot of folks who don’t know much about the inner workings of VPNs.A SOHO network device that supports VPN will probably support either IPsec, PPTP, L2TP or SSL VPN technologies. This means that the device actually has an implementation of the protocol running on it and can be used to connect to a central server or VPN gateway; therefore, a VPN client would not be required

On the other hand, a SOHO network device that supports VPN pass-thru simply means that it can support “passing through” packets that originate from VPN clients (typically on laptops or PC’s) out through a VPN server on the Internet. A special feature like this is needed because: 1) these SOHO devices are involved with NAT and PAT, 2) VPN protocols like IPsec (the data path is called ESP) doesn’t have a specific port number for the device to multiplex the port address translation back to your laptop or PC 3) that’s why this feature enables some special processing of packets that are IPsec ESP data packets and allows the device to keep a table of active connected VPN tunnels.

Aug-1-07

การติดตั้ง Web based VPN ด้วย SSL-Explorer บน Ubuntu

posted by อดิศร ขาวสังข์

บทนำ
SSL-Explorer เป็น user-friendly VPN app ที่น่าสนใจซึ่งต้องการเพียงแต่ Java และ Web browser เ่ท่านั้นสำหรับการรันใช้งานบน clientโดยผู้ใช้งานสามารถใช้งานได้ง่ายไ่ม่่จำเป็นต้องมีการคอนฟิกซอฟต์แวร์ สิ่งที่ต้องมีคือต้ิองมีการติดตั้ง jdk เวอร์ชันล่าสุดเท่านั้น ในบทความนี้จะอธิบายวิธีการติดตั้งบน Ubuntu 6.06 LTS และ 7.04 (Server version)

สิ่งที่ดีเกี่ยวกับ road warriors แบบนี้คือสามารถเชื่อมต่อจากที่ไหนก็ได้ ซึ่งโดยทั่วไปแล้วตามโรงแรมหรือ Internet cafe จะไม่มีการบล็อค https ดังนั้นจึงดีกว่า VPN แบบอื่นที่อาจจะถูกบล็อคพอร์ตได้

ขั้นตอนการติดตั้ง

  1. ติดตั้ง sun-java5-jdk ant ด้วยคำสั่ง
    apt-get install sun-java5-jdk ant
  2. ดาวน์โหลด SSL-explorer จาก http://www.sshtools.com/showSslExplorerCommunity.do อาจจะใช้คำสั่ง wget ดังนี้
    wget http://downloads.sourceforge.net/sslexplorer/sslexplorer-0.2.14_01-src.tar.gz?modtime=1181645280&big_mirror=0
    ทั้งนี้ hyperlink ข้างบนนี้อาจต้องเปลี่ยนแปลงให้ตรงตามความเป็นจริงนะครับ แล้วแตกไฟล์ (untar) ด้วยคำสั่ง
    tar xvfz sslexplorer-0.2.14_01-src.tar.gz
  3. เข้าไปยังไดเร็คทอรี่และติดตั้งโปรแกรมด้วยคำสั่ง :
    cd sslexplorer-0.2.14_01
    sudo ant install

    คำสั่งข้างบนนี้จะเป็นการคอมไพล์และมีการถามให้เปิด browser ไปที่้ http://yourhost:28080 โดยให้ทำตามขั้นตอนเพื่อคอนฟิก Server ของคุณ การติดตั้งสามารถเลือกติดตั้งเป็น Enterprise Edition ซึ่งจะมีคุณสมบัติที่หลากหลายกว่า เื่มื่อติดตั้งเสร็จแล้วให้กลับไปยังหน้าต่าง console โดยจะต้องมีข้อความที่ว่า “BUILD SUCCESSFUL”
  4. ทำการ start up server อีกครั้งด้วยการพิมพ์คำสั่ง
    sudo ant run
    แล้วรอจนกว่ามีคำว่า BUILD SUCCESSFUL
  5. ให้เปิด browser ไปที่ https://yourhost ซึ่งจะต้องใช้เวลาพอสมควรสำหรับการstart up ตรงนี้อาจจะทำให้บางคนสับสนและคิดว่ามีความผิดพลาดเกิดขึ้น จนต้องติดตั้งโปรแกรมใหม่ จริง ๆ แล้วไม่้ต้อง
  6. สามารถให้มีการรัน SSL VPN นี้ทุกครั้งเมื่อมีการ boot เครื่องด้วยการใช้คำสั่ง :
    sudo ant install-service
    ซึ่งจะเป็นการสร้าง rc file ในตำแหน่งที่เหมาะสม และตอนนี้คุณสามารถควบคุมการทำงานโดยใช้คำสั่งต่อไปนี้ได้ :
    sudo /etc/init.d/sslexplorer {start|stop|restart}
  7. ในกรณีต้องการมี CA signed certificate สามารถใช้บริการจาก CACert วิีธีการที่ดีอันหนึ่งคือการ Enable HTTP redirect ซึ่งสามารถทำได้ด้วยการคอนฟิกค่าของ Web Server ซึ่งถ้าไม่ทำ Enable HTTP redirect ผู้ใช้อาจจะร้องเรียนว่า VPN ไม่ work เนื่องจากใ้ช้ http แทน https เ้ว้นเสียแต่คุณจะต้องอธิบายเกี่ยวกับ https ให้กับผู้ใช้งานจำนวนมาก
  8. ในการเรียกใ้ช้งานจากเครื่อง client เครื่อง client จะต้องติดตั้ง Java runtime environment (JRE)

จบครับ

Aug-1-07

Web based VPN with SSL-Explorer on Ubuntu

posted by อดิศร ขาวสังข์

SSL-Explorer is a nice user-friendly VPN app that only needs java and a web browser on the client to run. It is thus a LOT easier to get users onto it, as it requires no software to configure and run, the only thing they may need to do is install the latest jdk version. This howto describes how to install on Ubuntu 6.0.6 LTS (server version), but will work on [insert your distro here] with a couple of modifications (most likely just package management and package names).

The really nifty thing about this is that road warriors will be able to connect from just about anywhere, I dont know of any hotels or internet cafes that block https! This beats a lot of other vpn solutions because there are more and more places blocking ports. Here is how to install ssl explorer on Ubuntu (6.0.6 Dapper)

Prepare the server

Do a standard install of ubuntu 6.0.6 LTS from the server cd. We need jdk on the server, you can now do this with apt if you have the multiverse repo enabled (edit /etc/apt/sources.list and uncomment the multiverse line)

apt-get install sun-java5-jdk ant

Download SSL-Explorer from http://www.sshtools.com/showSslExplorerCommunity.do and untar

tar zxvf  sslexplorer-0.2.7_02-src.tar.gz
#(Your version may be newer of course) cd  sslexplorer-0.2.7_02 sudo ant install

This will compile the whole lot and eventually ask you to point your browser to http://yourhost:28080. Go there and follow the steps to configure your server. You can also choose to install the enterprise edition, which offers some more advanced features. Once you have finished, go back to the console and it should say “BUILD SUCCESSFUL”.

Start up the server again by typing

sudo ant run

in the sslexplorer directory,w ait a bit, then connect to https://yourhost - remember it may take a bit of time to start up, this confused me, and I thought there were errors and ended up reinstalling. Once you are happy, you can make it run on each boot by running

sudo ant install-service

This chucks rc files in the right places, now you can control it with sudo /etc/init.d/sslexplorer {start|stop|restart}

The rest of the configuration is fairly basic, you may want to get a CA signed certificate, I just used one from CACert - another good thing to do is “Enable HTTP redirect”, this is in the “server” section when you log in as admin. If you dont do this, users will complain that the vpn is not working because they are using http:// instead of https:// - unless you feel like explaining https to hundreds of users, just enable this (you may not be able to because of a shortage of external ips etc, but do it if you can)

Clients need to have a Java runtime environment installed on the computers they are connecting from, again I used the Sun JRE because the app was developed with this, but you may find another that works OK.

AD Authentication

SSL Explorer can also do AD authentication, and its pretty easy. You will need to tell it to use AD during setup, theres not a way to change after setup. The steps are pretty simple:

  • Create an AD service account, like sslexplorer
  • Give the setup all the details it asks for, they are pretty self explanatory (Domain, DC hostname etc)
  • Choose an admin user

Upgrading

Upgrading ssl explorer from an earlier version is reasonably painless as well, from the SSL-Explorer knowledge base: http://3sp.com/kb/idx/87/245/article/How_can_I_upgrade_to_a_newer_release_of_the_Community_Edition_source_code.html . I found I had to stop the sslexplorer service, remove the service links, copy the old db and conf directories over to the new location, then run from there. After doing that, install the service links again, pointing to the new location:

cd ssl-explorer-0.2.7 sudo /etc/init.d/sslexplorer stop sudo ant
uninstall-service cd .. tar zxvf ssl-explorer-0.2.9
cd ssl-explorer-0.2.9_4 cp /opt/ssl-explorer-0.2.7/sslexplorer/db /
opt/sslexplorer-0.2.9_4/sslexplorer/ cp /
opt/ssl-explorer-0.2.7/sslexplorer/conf /
opt/sslexplorer-0.2.9_4/sslexplorer/ sudo ant start
#Make sure it runs ok sudo ant install-service