linux 普通账户绑定1024以下端口



一、昨天收到一个开放需求:

能不能在这台机器单独开启一个普通的账户,需要的权限是可以绑定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以下端口。