歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux 普通賬戶綁定1024以下端口

linux 普通賬戶綁定1024以下端口

日期:2017/3/3 16:59:53   编辑:關於Linux

一、昨天收到一個開放需求:

能不能在這台機器單獨開啟一個普通的賬戶,需要的權限是可以綁定udp 53端口

這個帳號是給開發人員使用的

乍看這個需求,貌似很簡單,但其實裡面涉及一些技術問題(在Linux上普通用戶無法綁定1024以下的端口),

當時為了不耽誤開放工作,給了一台測試機的root,現在回過頭來解決改問題。

二、google得知,基本有兩種解決方法:

1、常用的就是使用sudo給予普通用戶一定的權限,不過這跟給root有什麼區別,還得維護sudo。

2、debian系統下有一個小程序authbind,允許程序不使用root權限來綁定系統1024以下的特權端口,

在程序啟動時必須調用authbind,authbind會調用一些環境變量,來允許你的程序綁定在特權端口。

Ubuntu 12.04安裝authbind

apt-get install authbind

怎樣使用authbind呢?通過配置文件區域來使用了,默認的配置文件區域在/etc/authbind目錄下,裡面有三個目錄:byport、byaddr、byuid。

假如我們有個test賬號,想運行一個程序綁定80端口

在byport目錄下建立80文件:/etc/authbind/byport/80,設置test賬戶有80文件的使用權限,如果80文件可以被test訪問,則綁定就是成功的,否則綁定就是失敗的。

具體操作:

chmod 755 /etc/authbind/port/80

chown test.test /etc/authbind/port/80

在你要啟動的命令前加上authbind --deep命令即可。

我們也可以直接在地址上綁定端口,在byaddr下建立ip:port文件,測試方法如上。

也可以在byuid目錄下建立uid文件,只要你的test賬號可以訪問,否則綁定失敗。

三、centos實現

由於authbind是基於debian的,所以在yum上找不到源,google也沒有找到對應的rpm;

從github中發現:https://github.com/tootedom/authbind-centos-rpm

down下來,按照指示rpmbuild -v -bb --clean SPECS/authbind.spec出現兩個問題:

1、路徑錯誤

[root@stat authbind]# rpmbuild -v -bb --clean SPECS/authbind.spec

error: File /root/authbind/SOURCES/authbind_2.1.1.tar.gz: No such file or directory

2、沒能生成build目錄

[root@stat authbind]# rpmbuild -v -bb --clean SPECS/authbind.spec

Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.6tbsn7

+ umask 022

+ cd /root/authbind/authbind/BUILD

/var/tmp/rpm-tmp.6tbsn7: line 26: cd: /root/authbind/authbind/BUILD: No such file or directory

error: Bad exit status from /var/tmp/rpm-tmp.6tbsn7 (%prep)

RPM build errors:

Bad exit status from /var/tmp/rpm-tmp.6tbsn7 (%prep)

對rpmbuild不熟,但發現SOURCES/authbind_2.1.1.tar.gz,解壓後發現Makefile,直接安裝成功!

[root@stat authbind-2.1.1]# make

cc -g -O2 -Wall -Wwrite-strings -Wpointer-arith -Wimplicit -Wnested-externs -Wmissing-prototypes -Wstrict-prototypes -DMAJOR_VER='"1"' -DMINOR_VER='"0"' -DLIBAUTHBIND='"/usr/local/lib/authbind/libauthbind.so.1"' -DHELPER='"/usr/local/lib/authbind/helper"' -DCONFIGDIR='"/etc/authbind"' -D_GNU_SOURCE -c -o authbind.o authbind.c

cc -g authbind.o -o authbind

cc -g -O2 -Wall -Wwrite-strings -Wpointer-arith -Wimplicit -Wnested-externs -Wmissing-prototypes -Wstrict-prototypes -DMAJOR_VER='"1"' -DMINOR_VER='"0"' -DLIBAUTHBIND='"/usr/local/lib/authbind/libauthbind.so.1"' -DHELPER='"/usr/local/lib/authbind/helper"' -DCONFIGDIR='"/etc/authbind"' -D_GNU_SOURCE -c -o helper.o helper.c

cc -g helper.o -o helper

cc -D_REENTRANT -g -O2 -Wall -Wwrite-strings -Wpointer-arith -Wimplicit -Wnested-externs -Wmissing-prototypes -Wstrict-prototypes -DMAJOR_VER='"1"' -DMINOR_VER='"0"' -DLIBAUTHBIND='"/usr/local/lib/authbind/libauthbind.so.1"' -DHELPER='"/usr/local/lib/authbind/helper"' -DCONFIGDIR='"/etc/authbind"' -D_GNU_SOURCE -c -o libauthbind.o -fPIC libauthbind.c

ld -shared -soname libauthbind.so.1 -o libauthbind.so.1.0 libauthbind.o -ldl -lc

[root@stat authbind-2.1.1]#

[root@stat authbind-2.1.1]#

[root@stat authbind-2.1.1]# make install

install -o root -g root -m 755 -d /usr/local/lib/authbind /usr/local/share/man/man1 /usr/local/share/man/man8

install -o root -g root -m 755 -s authbind /usr/local/bin/.

install -o root -g root -m 644 libauthbind.so.1.0 /usr/local/lib/authbind/.

strip --strip-unneeded /usr/local/lib/authbind/libauthbind.so.1.0

ln -sf libauthbind.so.1.0 /usr/local/lib/authbind/libauthbind.so.1

install -o root -g root -m 755 -s helper /usr/local/lib/authbind/.

chmod u+s /usr/local/lib/authbind/helper

install -o root -g root -m 755 -d /etc/authbind \

/etc/authbind/byport /etc/authbind/byaddr /etc/authbind/byuid

[root@stat authbind-2.1.1]# cd /etc/authbind/

[root@stat authbind]# ls

byaddr byport byuid

之後按照authbind --deep實現linux 普通賬戶綁定1024以下端口。

本文出自 “麥麥的運維之路” 博客,請務必保留此出處http://xiaomaimai.blog.51cto.com/1182965/1437027

Copyright © Linux教程網 All Rights Reserved