New FAMILUG

The PyMiers

Wednesday 9 December 2015

Chạy service bằng port < 1024 - không cần quyền root

Trên các hệ điều hành nhân Linux, để một process có thể nghe (bind) ở port < 1024, process ấy phải có quyền root.

Các port < 1024 còn được gọi là các "privileged port".
Có nhiều cách để workaround giới hạn này, như:
- dùng iptables để chuyển hướng các kết nối tới privileged port đến port mà service đang listen
- dùng authbind

Linux từ bản 2.6.24 trở lên hỗ trợ thêm tính năng set "capability" để cho phép 1 file có thể thực hiện 1 khả năng nào đó mà không cần quyền root.

Để cho phép 1service listen ở privileged port mà không cần quyền root, chạy lệnh sau với quyền root:
# setcap 'cap_net_bind_service=+ep' FILE_PROGRAM
Trong đó dấu + nghĩa là tăng, dấu - là giảm, 
Có 3 bộ (set) file capabilities:
  • e đại diện cho từ Effective
  • p đại diện cho từ Permitted
  • i đại diện cho từ Inheritable

Chú ý FILE_PROGRAM PHẢI là một file binary, không được là 1 script.
Sau khi set xong, có thể xem capability đã set:
root@vagrant-hvn-trusty:~# getcap FILE_PROGRAM
FILE_PROGRAM = cap_net_bind_service+ep

Tham khảo:
http://man7.org/linux/man-pages/man7/capabilities.7.html
ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.2/capfaq-0.2.txt
http://stackoverflow.com/questions/413807/is-there-a-way-for-non-root-processes-to-bind-to-privileged-ports-1024-on-l
http://man7.org/linux/man-pages/man8/setcap.8.html

No comments:

Post a Comment