Showing posts with label Howto Opensource. Show all posts
Showing posts with label Howto Opensource. Show all posts

Thursday, July 21, 2011

vsftpd กับ SElinux ไม่ยอมอ่าน home directory มี error cannot change directory

อันเนื่องมาจากงาน Implement ที่บริษัทแห่งหนึ่งแถวๆบางซื่อ

โดยได้รับมอบหมายให้ติดตั้ง ftp server ในองค์กร เพื่อให้ customer ดาวน์โหลดไฟล์ แทนการ attachfile ไปกับ e-mail
เพื่อความปลอดภัย ไม่ให้ไฟล์ของบริษัทต้องรั่วใหลไปไกล..

vsftpd เมื่อ login ด้วย local user มี error ว่า cannot change directory

หากปิด SElinux ก็จะเข้าได้ปกติ ... แต่จะมีประโยชน์อันใด ถ้าหากต้องหนีปัญหาด้วยการปิดความปลอดภัย
มิสู้เปิด option ให้ SElinux อนุญาติให้ vsftpd เขียน home directory ได้มิดีกว่าหรือ

ด้วยคำสั่ง

setsebool -P ftp_home_dir=1

-P เมื่อต้องการให้การตั้งค่ามีผลหลังจาก boot เครื่องแล้ว
ตามปกติถ้าไม่ใส่ option ใด ๆ จะเป็นการเปลี่ยนแปลงค่าแบบชั่วคราวเท่านั้น

1 คือ เปิดเป็น ON


นอกจากนี้ SELinux ยังมีคำสั่งที่เกี่ยวข้องกับ vsftpd อีกคือ

allow_ftpd_anon_write –> permits the writing of files to directories configured with the public_content_rw_t setting.
allow_ftpd_use_cifs –> permits the use of files that are shared via CIFS
allow_ftpd_use_nfs –> permits the use of files that are shared via NFS
ftp_is_daemon –> required for the standalone daemon
ftp_home_directory –> permits read and write access to user home directories


ถ้าสมมติว่าเราสร้าง “file” ไว้ที่ไดเรกทอรี /var/ftp/pub สิ่งที่มองเห็นว่าเป็น SELinux ด้วย # ls -Z /var/ftp/pub ก็คือ
-rw-r–r– root root system_u:object_r:public_content_t file

system_u is used because this is a default setting for the system.
system_u:

The system object shows the context for the role.
object_r

The type describes the nature of the data. In this case this is public read only data by default.
public_content_t
If you wanted to allow users to write to the pub directory you would need to change the context. You could do that with the chcon command.

chcon -R -u ser_u -t public_content_rw_t /var/pub
The default settings for the ftp directory are set in the file /etc/selinux/targeted/contexts/files/file_contexts .

Be very careful in making changes in this file. Here is the listing for ftp.
/var/ftp(/.*)? system_u:object_r:public_content_t:s0

ที่มา :
http://beginlinux.com/blog/2008/11/vsftpd-and-selinux-on-centos/

Wednesday, July 20, 2011

ใช้คำสั่ง linux ตรวจสอบ Tape Backup

อันเนื่องมาจากการดูแลเซิร์ฟเวอร์ Linux ของหน่วยงานราชการที่ใช้ Tape Backup สำหรับ admin อย่างเรา มีหน้าที่ตรวจสอบและทำรายงานว่า tape ยังทำงานได้ปกติสุขดีอยู่หรือไฉน....

ตรวจสอบสถานะของ Tape Backup

# mt -f /dev/st0 status

หมุนม้วนเทปกลับไปยังต้นม้วน

# mt -f /dev/st0 rewind

Backup ไดเรกทอรี /www และ /home ด้วยคำสั่ง

# tar -czf /dev/st0 /www /home

ถามเทปว่าตอนนี้กำลังอยู่ที่ block ไหน

# mt -f /dev/st0 tell

ขอดูไฟล์ต่าง ๆ ในมัวนเทปหน่อยซิ

# tar -tzf /dev/st0

Restore /www directory:
# cd /
# mt -f /dev/st0 rewind
# tar -xzf /dev/st0 www

Unload the tape:

# mt -f /dev/st0 offline

Erase the tape:

# mt -f /dev/st0 erase

You can go BACKWARD or FORWARD on tape with mt command itself:
(a) Go to end of data:
# mt -f /dev/nst0 eod(b)

Goto previous record:
# mt -f /dev/nst0 bsfm 1(c)

Forward record:
# mt -f /dev/nst0 fsf 1

Replace /dev/st0 with your actual tape drive name.

ลอกเขามาแหละครับที่นี่
http://www.cyberciti.biz/faq/linux-tape-backup-with-mt-and-tar-command-howto/

Tuesday, March 31, 2009

วิธีการ capture the output of “top” ไปแสดงที่ file?

คำสั่ง top เป็นคำสั่งแสดงโปรแกรมและ process ต่าง ๆ แบบ real time เหมือนกับ task list ในโปรแกรมวินโดสว์

การใช้งานก็แสนง่าย เพียงคีย์คำสั่ง top แล้วกด enter เท่านั้น โปรเซสต่าง ๆจะแสดงให้ได้รับชมกันทางหน้าจอ อย่างเป็นระเบียบเรียบร้อย ถ้าเราอยากจะเก็บหน้าจอ process ต่าง ๆ เหล่านี้ให้แสดงไปที่ไฟล์แทนเพื่อเก็บไปวิเคราะห์หรือหาเหตุผลอื่นก็ตามที คำสั่ง top ทำได้ดังนี้

top -b -n1 > /tmp/top.txt

ทุกอย่างที่เห็นบนหน้าจอเมื่อสักครู่นี้จะไปแสดงในไฟล์ top.txt แทน


นอกจากนี้ top ยังแสดงหลายๆ report เพื่อเทียบเคียงเป็นช่วงเวลาได้ เช่นตัวอย่างข้างล่างนี้ ให้รัน top ห้าครั้ง แต่ละครั้งให้รอห้าวินาทีก่อนที่จะรัน top ต่อไ

top -b -n5 -d5 > /tmp/top.txt

แหล่งข้อมูลอ้างอิง
http://magazine.redhat.com/category/tips-and-tricks/page/4/

Monday, March 30, 2009

File Sharing Management

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

ผู้เขียนเกริ่นไว้ในย่อหน้าแรกแบบคนมองโลกในแง่ลบ เพราะหลาย ๆ บริษัทที่ผู้เขียนได้ประสบมา มีการนำลีนุกซ์มาใช้ในองค์กร และต้องเปลี่ยนใจกลับไปใช้ windows 2003 เหมือนเดิม ส่วนใหญ่จะมีปัญหากับการทำ file sharing และเซตอัพตัวลีนุกซ์ให้ยืดหยุ่นกับลักษณะงานที่ต้องแชร์กันไม่ได้ อย่างเช่นงานโปรเจก ที่ไฟล์ๆหนึ่งต้องมีการเข้ามาใช้หลายคน จากหลายฝ่าย แต่ละคนที่เข้าถึงไฟล์นั้นต้องมีการแก้ไขบ้าง ปรับปรุงบ้าง บางคนให้เข้าถึงไฟล์นั้นได้ แต่ห้ามแก้ไข เป็นต้น หรืองานทางด้านการผลิตที่มีการผูกสูตรการผลิต (BOM) เป็นไฟล์ excel และให้ฝ่ายต่าง ๆที่เกี่ยวข้องกรอกข้อมูล พอ save ไฟล์แล้วอีกฝ่ายหนึ่งเปิดขึ้นมาเป็น read only และต้องแก้ค่า permission กันอยู่ตลอด ปัญหาต่าง ๆ เหล่านี้ เมื่อแอดมินพยายามแก้ปัญหาก็จะมองไปที่ configuration ของ samba เป็นหลัก โดยไม่มองให้ลึกลงไปถึงตัวลีนุกซ์ที่ samba ใช้เป็นเจ้าเรือนในการรัน ซึ่งมี permission ในการจัดการไฟล์ ก่อนที่ตัว samba จะเข้ามาจัดการในส่วนของ samba เอง (ด้วย option : create mask,directory mask,directory security mask เป็นต้น)

ปัญหาการแชร์ไฟล์ในลักษณะดังกล่าวข้างต้นนั้น ลีนุกซ์เอง มีวิธีการจัดการได้อย่างเหมะสมและยืดหยุ่น ด้วยคำสั่งต่าง ๆ ของลีนุกซ์อยู่แล้ว แทบจะไม่ต้องไปกำหนดค่า option เพิ่มเติมให้ samba เลย เรามาลอง implement file sharing ตามโจทก์ข้างต้นนั้นโดยยกตัวอย่างไฟล์สูตรการผลิตที่เป็น excel (BOM : Bill of Material) ดังนี้

1. สร้างไดเรกทอรีบนลีนุกซ์เพื่อเปิดแชร์ร่วมกันก่อน
#mkdir /home/bomproduct
2. สร้างกลุ่ม และสร้างรายชื่อผู้ใช้งานมีสังกัดกลุ่ม เช่น
#groupadd mktgrp
#groupadd productgrp
#useradd -g mktgrp tawich
#useradd -g mktfrp somporn
#useradd -g productgrp somjit
#useradd -g productgrp prakong
3. ไดเรกทอรี /home/bomproduct มีเจ้าภาพคือสังกัดกลุ่ม productgrp ในข้อหนึ่งเราสร้างขึ้นมาโดย root ให้เปลี่ยนเป็นกลุ่ม productgrp ด้วยคำสั่ง #chgrp -R productgrp /home/bomproduct
4. กำหนดให้ไดเรกทอรีหรือไฟล์ใดๆ ที่ถูกสร้างขึ้นมาเป็นกลุ่ม productgrp เสมอไม่ว่าผู้สร้างจะเป็นใครสังกัดกลุ่มไหน ด้วยคำสั่ง #chmod -R 2775 /home/bomproduct
5. เพิ่มผู้ใช้งานจากกลุ่มต่าง ๆ เข้าเป็นสมาชิกของกลุ่ม productgrp
#gpasswd -M tawich,somporn (ตัวอย่างนี้เป็นการเพิ่ม user 2 คนจากกลุ่ม mktgrp เข้าเป็นสมาชิกเพิ่ม)
6. กำหนด umask ในไฟล์ /etc/bashrc ให้เป็น 002 (ตัวอย่างนี้เป็น FC9)
7. สร้างแชร์ริ่งด้วย samba แก้ไขไฟล์ /etc/samba/smb.conf
[bomfiles]
comment = BOM for Product
path = /home/bomeproduct
browseable = yes
read only = no

นี่เป็นตัวอย่างจริง ๆ จากการ implement ให้บริษัทแห่งหนึ่ง และได้ใช้ samba โดยมีลีนุกซ์เป็นพลังขับเคลื่อนระบบ BOM มาจนถึงบัดนี้....
เอกสารการ implement โดยละเอียดพร้อมภาพประกอบ ผู้อ่านสามารถดาวน์โหลดได้ที่นี่


แหล่งข้อมูลเพิ่มเติม เกี่ยวกับ permission

http://www.redhat.com/docs/manuals/linux/RHL-8.0-Manual/ref-guide/s1-users-groups-private-groups.html
http://www.lockergnome.com/linux/2002/08/29/the-users-mask/
http://www.dartmouth.edu/~rc/help/faq/permissions.html
http://www.ubuntuclub.com/node/58

Tuesday, March 24, 2009

fcrontab เล็กๆน้อย กับ endian firewall

-x /bin/run-parts รันสคริปไฟล์ที่เจอในไดเรกทอรีนั้น เช่น
-x /bin/run-parts /etc/cron.daily -> รันสคริปทุกไฟล์ที่เจอในไดเรกทอรี cron.daily

DESCRIPTION
run-parts is a utility that will run scripts that are found in a direc-
tory. For example, it might be useful to create an /etc/cron.daily
directory and put scripts in there for daily cron jobs. Then run-parts
can be called once a day from root's crontab to run all the scripts
found in /etc/cron.daily:

40 4 * * * run-parts /etc/cron.daily

run-parts automatically skips files with certain suffixes that are gen-
erally associated with backup or extra files. Any file that ends in
one of these will be silently ignored: ~ ^ , .bak .new .rpmsave
.rpmorig .rpmnew .swp

AUTHOR
Patrick J. Volkerding , with ideas borrowed
from the Red Hat and Debian versions of this utility.

Twitter Delicious Facebook Digg Stumbleupon Favorites More