apache linux权限,apache配置访问目录权限
老铁们,大家好,相信还有很多朋友对于apache linux权限和apache配置访问目录权限的相关问题不太懂,没关系,今天就由我来为大家分享分享apache linux权限以及apache配置访问目录权限的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
SELinux权限
在了解SELinux之前,我们先来了解一下Linux的两种访问控制策略:DAC和MAC
DAC,自主访问控制(Discretionary Access control)。系统只提供基本的验证,完整的访问控制由开发者自己控制。
DAC将资源访问者分成三类:Owner、Group、Other。
将访问权限也分成三类:read、write、execute
资源针对资源访问者设置不同的访问权限。访问者通常是各个用户的进程,有自己的uid/gid,通过uid/gid和文件权限匹配,来确定是否可以访问。DAC机制下,每一个用户进程默认都拥有该用户的所有权限。
DAC有两个严重问题:
问题一:
因为Root用户是拥有所有权限的,所以DAC对Root用户的限制是无效的。并且在Linux Kernel 2.1以后,Linux将Root权限根据不同的应用场景划分成许多的Root Capabilities,普通用户也可以被设置某个Root Capability。普通用户如果被设置了CAP_DAC_OVERRIDE,也可以绕过 DAC限制。
问题二:
用户进程拥有该用户的所有权限,可以修改/删除该用户的所有文件资源,难以防止恶意软件。
可见,DAC有明显的缺陷,一旦被入侵,取得Root权限的用户进程就可以无法无天,胡作非为,早期android版本就深受其害。
MAC,强制性访问控制(Mandatory Access control)。系统针对每一项访问都进行严格的限制,具体的限制策略由开发者给出。
Linux MAC针对DAC的不足,要求系统对每一项访问,每访问一个文件资源都需要根据已经定义好了的策略进行针对性的验证。系统可以针对特定的进程与特定的文件资源来进行权限的控制。即使是root用户,它所属的不同的进程,并不一定能取得root权限,而得要看事先为该进程定义的访问限制策略。如果不能通过MAC验证,一样无法执行相关的操作。
与DAC相比,MAC访问控制的“主体”变成了“进程”而不是用户。这样可以限制了Root权限的滥用,另外要求对每一项权限进行了更加完整的细化,可以限制用户对资源的访问行为。
SELinux就是目前最好的MAC机制,也是目前的行业标准。
SELinux,安全增强Linux(Security-Enhanced Linux),是由美国国家安全局(NSA)发起,多个非营利组织和高校参与开发的强制性安全审查机制(Mandatory Access control,简称MAC)。SELinux最早于2000年12月采用GPL许可发布。目前,Linux Kernel 2.6及以上的版本都已经集成了SELinux。
SELinux分成三种模式:
Android 5.x及以上强制开启,因此,disabled(关闭)模式并没有什么用了。通常在调试时,我们会启用Permissve(宽容模式),以便尽可能的发现多的问题,然后一次修正。在量产时启用Enfocing mode(强制模式)来保护系统。
查看SELinux模式:adb shell getenforce
设置SELinux模式:adb shell setenforce 1//0是Permissve,1是Enfocing
SELinux的访问控制示意图:
通常我们开发的过程中,就是配置Subject、Object、Security Policy。
SELinux给Linux的所有对象都分配一个安全上下文(Security Context),描述成一个标准的字符串。
安全上下文的标准格式: user:role:type[:range]
Security Label用来绑定被访问资源和安全上下文,描述它们的对应关系。标准格式为:resource security_context。即:res user:role:type[:range]。这里也可以使用通配符,例如 net.就可以绑定所有以net.开头的属性,除此之外,还有类似正则表达式的*、?等等通配符。Security Label都定义在type_contexts当中,例如file的定义在file_contexts中,service定义在service_contexts中,property定义在property_contexts中。
举例:
file_contexts:
service_contexts:
查看进程安全上下文: ps-AZ。例如,查看Settings进程的安全上下文,ps-AZ| grep settings:
u:r:system_app:s0 system 1381 585 4234504 201072 0 0 S com.android.settings
查看文件安全上下文: ls-Z。例如,查看文件build.prop的安全上下文:
u:object_r:system_file:s0 build.prop
Type Enforcement(TE)是根据Security Context中的 type进行权限审查,审查 subject type对 object type的某个class类型中某种permission是否具有访问权限,是目前使用最为广泛的MAC审查机制,简单易用。
TE控制语句格式: rule_name source_type target_type: class perm_set
Type Enforcement规则说明:
举个例子,logd.te、tombstoned.te中定义的TE规则:
allow logd runtime_event_log_tags_file:file rw_file_perms;
dontaudit domain runtime_event_log_tags_file:file{ open read};
auditallow tombstoned anr_data_file:file{ append write};
neverallow logd{ app_data_file system_data_file}:dir_file_class_set write;
SELinux中每一个进程或者文件都对应一个type,而每一个type都对应有一个或几个attribute。所有常见的attribute定义在以下文件中:
system/sepolicy/public/attributes
system/sepolicy/prebuilts/api/[build version]/public/attributes
system/sepolicy/prebuilts/api/[build version]/private/attributes
其中的[build version]即为android版本号,例如android O为28.0。常见的attribute定义:
Type对应一个或者几个attribute,Type的定义格式:
type type_name, attribute1, attribute2;
Type的定义通常分散在各个te文件中。例如,常用普通文件的type定义在file.te中:
SEAndroid对于不同的资源类型,定义了不同的Class。比如普通的file、socket等等,比如SELinux使用的security,比如针对每个process参数的process等定义相关的class。这些class,每一个class都有相对应的permissions。比如file就有 read, write, create, getattr, setattr, lock, ioctl等等.比如process就有fork, sigchld, sigkill, ptrace, getpgid, setpgid等等。这些相关的class,以及他们具有那些Permissions都定义在以下文件中:
system/sepolicy/private/access_vectors
system/sepolicy/reqd_mask/access_vectors
system/sepolicy/prebuilts/api/版本号/private/access_vectors
例如:
定义完之后,在以下对应的security_classes文件中声明定义的classes。
system/sepolicy/private/security_classes
system/sepolicy/reqd_mask/security_classes
system/sepolicy/prebuilts/api/版本号/private/security_classes
例如:
注意,Classes和Permissions的定义与Kernel中相关API是强相关的,普通用户严禁修改。
在SELinux中,我们通常称一个进程是一个domain,一个进程fork另外一个进程并执行(exec)一个执行档时,我们往往会涉及到domain的切换.比如init进程, SELinux给予了它很大的权限,而它拉起的服务,我们要限制这个服务的权限,于是就涉及到从一个domain切换到另外一个domain,不然默认就使用init进程的domain.
在SELinux里面有专门的一条语法: type_transition statement.
在准备切换前我们先要确保有相关的权限操作:
如下面的demo, init拉起apache并且切换到 apache的domain.
(1).首先,你得让init_t域中的进程能够执行type为apache_exec_t的文件
allow init_t apache_exec_t: file{read getattr execute};
(2).然后,你还得告诉SELinux,允许init_t做DT切换以进入apache_t域
allow init_t apache_t: process transition;
(3).然后,你还得告诉SELinux,切换入口(对应为entrypoint权限)为执行apache_exec_t类型的文件
allow apache_t apache_exec_t: file entrypoint;
(4).最后,Domain Transition
type_transition init_t apache_exec_t: process apache_t;
可以看到,整个domain切换过程写起来非常麻烦。因此,Google为了使用方便,在system/sepolicy/public/te_macros文件中定义了宏:
我们可以使用这些宏来完成domain切换。
举例:
kernel启动init进程切换domain:
domain_auto_trans(kernel, init_exec, init)
init启动netd、vold、zygote、installd切换domain:
init_daemon_domain(netd)
init_daemon_domain(vold)
init_daemon_domain(zygote)
init_daemon_domain(installd)
一个进程创建在一个目录下创建文件时,默认是沿用父目录的Security Context,如果要设置成特定的Label,就必须进行Object Transitions.
同样是使用:type_transition statement.
对应的必须有两个前提条件:
下面是一个demo, ext_gateway_t这个domain在类型为in_queue_t的目录下,创建类型为 in_file_t的文件.
(1).首先,你得让ext_gateway_t对in_queue_t目录具备访问权限
allow ext_gateway_t in_queue_t: dir{ write search add_name};
(2).然后,你还得告诉SELinux,允许ext_gateway_t访问in_file_t的文件
allow ext_gateway_t in_file_t: file{ write create getattr};
(3).最后,Object Transition
type_transition ext_gateway_t in_queue_t: file in_file_t;
同样的,为了方便使用,Google也在system/sepolicy/public/te_macros文件中定义了宏:
使用举例:
file_type_auto_trans(factory, system_data_file, factory_data_file)
android O以前sepolicy集中放在boot image。前面提到SELinux在Android的主要变更历史时,有提到android O开始,Google将system image和 vendor image分离。因此,sepolicy也相应的被分离存放到system image以及 vendor image。与system相关的sepolicy就存放system image,与SoC vendor相关的sepolicy就存放在vendor image。
对于原生AOSP,Google设定了不同的存放目录,以便进行分离,以Google默认的sepolicy为例,sepolicy主目录为/system/sepolicy,我们主要关注三个子目录:
对于不同的平台,不同平台厂商也设定了不同的存放目录,以MTK平台为例:
首先,根据不同的platform共用sepolicy、platform独有、project独有,分为:
对应的,不同版本会导入不同目录下的sepolicy配置
以mt6763平台为例,导入时:
[common]路径为:/device/mediatek/sepolicy
[platfrom]路径为:/device/mediatek/mt6763/sepolicy/
具体的定义在BoardConfig.mk文件中:
然后,basic、bsp、full又可以主要细分为:
Google在system/sepolicy中定义了相关的neverallow规则,对SELinux Policy的更新进行了限制,以防止开发者过度开放权限,从而引发安全问题。并且还会通过CTS测试检测开发者是否有违法相关的规则。
因此,我们需要注意以下几点:
出现SELinux Policy Exception时常见的两种解决方案:
(1).修改对应节点的SELinux Security Label,为特定的Subject,如system_app、platform_app、priv_app,例如Settings,SystemUI等内置APP开启权限,但严禁为untrsted app开启权限。
(2).通过system server service或者 init启动的service读写操作,然后app通过binder/socket等方式连接访问.此类安全可靠,并且可以在service中做相关的安全审查,推荐这种方法.
情景:定义由 init进程启动的service, factory,其对应的执行档是/vendor/bin/factory。
(1).在device/mediatek/mt6763/sepolicy/basic/non_plat目录下创建一个factory.te,然后将te文件加入编译,如放到这种指定目录下不需要额外配置,sytem/sepolicy/Android.mk中定义的build_policy函数会遍历指定目录导入te文件。
(2).在factory.te中定义factory类型,init启动service时类型转换,
type factory, domain;
type factory_exec, exec_type, file_type, vendor_file_type;
init_daemon_domain(factory)
(3).在file_contexts中绑定执行档
/(system/vendor|vendor)/bin/factory u:object_r:factory_exec:s0
(4).根据factory需要访问的文件以及设备,定义其它的权限在factory.te中.
#Purpose: For key and touch event
allow factory input_device:chr_file r_file_perms;
allow factory input_device:dir rw_dir_perms;
情景:添加一个自定义的system property: persist.demo,并为platform_app设置读写权限
(1).在property.te中定义system property类型
type demo_prop, property_type
(2).在property_contexts中绑定system property的安全上下文。
persist.demo u:object_r:demo_prop:s0
(3).在platform_app.te中新增写权限,可以使用set_prop宏。
set_prop(platform_app, demo_prop)
(4).在platform_app.te中新增读权限,可以get_prop宏。
get_prop(platform_app, demo_prop)
情景:有一个设备节点/dev/demo,有一个platform_app进程需要读写这个设备节点。
(1).在device.te中定义device类型
type demo_device dev_type;
(2).在 file_contexts中绑定demo_device
/dev/demo u:object_r:demo_device:s0
(3).在platform_app.te中,允许platform_app使用demo device的权限
allow platform_app demo_device:chr_file rw_file_perms;
情景:有一个扩展的系统服务demo_service供APP调用。
(1).在service.te中定义service类型
type demo_service, app_api_service, system_server_service, service_manager_type;
(2).在service_contexts中绑定service
demo u:object_r:demo_service:s0
(3).在frameworks/base/core/java/android/content/Context.java中定义服务常量
public static final String DEMO_SERVICE="demo";
(4).在frameworks/base/core/java/android/app/SystemServiceRegistry.java中,参照其它系统服务注册demo_service
(5).在frameworks/base/services/java/com/android/server/SystemServer.java中,启动DemoService,添加到service_manager进行管理。
(6).最后一步,参考其它系统服务,实现DemoManager、DemoService,并定义如IDemoService等等的AIDL接口。
情景:一个native service通过init创建一个socket并绑定在/dev/socket/demo,并且允许某些process访问.
(1).在file.te中定义socket的类型
type demo_socket, file_type;
(2).在file_contexts中绑定socket的类型
/dev/socket/demo_socket u:object_r:demo_socket:s0
(3).允许所有的process访问,使用宏unix_socket_connect(clientdomain, socket, serverdomain)
unix_socket_connect(appdomain, demo, demo)
(1).在device/mediatek/mt6763/sepolicy/basic/non_plat目录下创建一个demo.te。
(2).在demo.te中定义demo类型,init启动service时类型转换。并可以根据demo需要访问的文件以及设备,定义其它的权限在demo.te中。
type demo, domain;
type demo_exec, exec_type, file_type;
init_daemon_domain(demo)
(3).绑定执行档 file_context类型
/vendor/bin/demo u:object_r:demo_exec:s0
(4).创建demo的入口执行档demo_exec、并配置相应的权限。
(1).将SELinux调整到Permissive模式复测
使用eng/userdebug版本,adb shell setenforce 0将SELinux模式调整到Permissive模式,然后复测。如果还能复现问题,则与SELinux无关;如果原本很容易复现,而Permissive mode不能再复现,那么就可能与SELinux相关。
(2).查看LOG中是否有标准的SELinux Policy Exception.
在Kernel LOG/ Main Log中查询关键字"avc: denied"看看是否有与目标进程相关的SELinux Policy Exception,并进一步确认这个异常是否与当时的逻辑相关。
一般情况我们在符合Google sepolicy策略及neverallow策略的前提下,根据LOG中的内容,需要什么权限就加什么权限。例如LOG:
2020-03-27 14:11:02.596 1228-1228/com.android.systemui W/FaceIdThread: type=1400 audit(0.0:481): avc: denied{ read} for name="als_ps" dev="tmpfs" ino=10279 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:als_ps_device:s0 tclass=chr_file permissive=0
LOG说明如下:
一般我们需要重点关注的是四个:permission、source type、target type、target class
根据这四个就可以配置出的所需要的selinux权限:
allow [source type] [target type]: [target class] [permission]
例1:
03-27 03:45:22.632 2958 2958 W Camera: type=1400 audit(0.0:314): avc: denied{ read} for name="u:object_r:graphics_debug_prop:s0" dev="tmpfs" ino=2649 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:graphics_debug_prop:s0 tclass=file permissive=0
解决方案:
按正常的套公式,应该是这样修改platform_app.te,增加:
allow platform_app graphics_debug_prop:file r_file_perms;
这里我们利用system/sepolicy/te_macros中定义的宏get_prop:
更多相关的宏定义请参考:system/sepolicy/public/te_macros。
所以最终简化后,修改platform_app.te,增加:
get_prop(platform_app, graphics_debug_prop)
例2:
03-27 14:11:02.596 1228-1228/com.android.systemui W/BackThread: type=1400 audit(0.0:481): avc: denied{ read} for name="als_ps" dev="tmpfs" ino=10279 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:als_ps_device:s0 tclass=chr_file permissive=0
解决方案:
修改platform_app.te增加:
allow platform_app als_ps_device:chr_file r_file_perms;
(1).不符合neverallow规则或者修改了neverallow规则
编译报错:
neverallow check failed at xxx
CTS测试项failed:
android.cts.security.SELinuxNeverallowRulesTest#testNeverallowRulesXXX
这类问题在android O vendor和system分离之后,尤其容易出现。基本上这类问题都是因为修改或者增加的te配置不符合neverallow规则,导致编译报错。而为了解决编译报错,又修改了neverallow规则,最终在跑CTS时,没法通过相关的测试项。
解决思路:
(2). init进程fork新进程没有做domain切换
CTS测试项failed:
android.security.cts.SELinuxDomainTest# testInitDomain
解决思路:
fork进程时,参考3.4节中做domain切换。
本文主要参考了MTK-Online的Quick-start中《SELinux问题快速分析》的内容,感谢原作者们的辛勤付出。另外,结合源码和自身开发实践,增加了一些自身理解和实践内容。
如何在Linux下搭建apache服务器
准备篇:
1、配置防火墙,开启80端口、3306端口
vi/etc/sysconfig/iptables
-A INPUT-m state--state NEW-m tcp-p tcp--dport 80-j ACCEPT#允许80端口通过防火墙
-A INPUT-m state--state NEW-m tcp-p tcp--dport 3306-j ACCEPT#允许3306端口通过防火墙
备注:很多网友把这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,
正确的应该是添加到默认的22端口这条规则的下面
如下所示:
##############################添加好之后防火墙规则如下所示##############################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT-m state--state ESTABLISHED,RELATED-j ACCEPT
-A INPUT-p icmp-j ACCEPT
-A INPUT-i lo-j ACCEPT
-A INPUT-m state--state NEW-m tcp-p tcp--dport 22-j ACCEPT
-A INPUT-m state--state NEW-m tcp-p tcp--dport 80-j ACCEPT
-A INPUT-m state--state NEW-m tcp-p tcp--dport 3306-j ACCEPT
-A INPUT-j REJECT--reject-with icmp-host-prohibited
-A FORWARD-j REJECT--reject-with icmp-host-prohibited
COMMIT
##################################################################################################
/etc/init.d/iptables restart#最后重启防火墙使配置生效
2、关闭SELINUX
vi/etc/selinux/config
#SELINUX=enforcing#注释掉
#SELINUXTYPE=targeted#注释掉
SELINUX=disabled#增加
:wq#保存,关闭
shutdown-r now#重启系统
安装篇:
一、安装Apache
yum install httpd#根据提示,输入Y安装即可成功安装
/etc/init.d/httpd start#启动Apache
备注:Apache启动之后会提示错误:
正在启动 httpd:httpd: Could not reliably determine the server's fully qualif domain name, using::1 for ServerName
解决办法:
vi/etc/httpd/conf/httpd.conf#编辑
找到#ServerName www.example.com:80
修改为 ServerName www.osyunwei.com:80#这里设置为你自己的域名,如果没有域名,可以设置为localhost
:wq!#保存退出
chkconfig httpd on#设为开机启动
/etc/init.d/httpd restart#重启Apache
二、安装MySQL
1、安装MySQL
yum install mysql mysql-server#询问是否要安装,输入Y即可自动安装,直到安装完成
/etc/init.d/mysqld start#启动MySQL
chkconfig mysqld on#设为开机启动
cp/usr/share/mysql/my-medium.cnf/etc/my.cnf#拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
2、为root账户设置密码
mysql_secure_installation
回车,根据提示输入Y
输入2次密码,回车
根据提示一路输入Y
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:
/etc/init.d/mysqld restart#重启
/etc/init.d/mysqld stop#停止
/etc/init.d/mysqld start#启动
三、安装PHP5
1、安装PHP5
yum install php
根据提示输入Y直到安装完成
2、安装PHP组件,使 PHP5支持 MySQL
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt
这里选择以上安装包进行安装
根据提示输入Y回车
/etc/init.d/mysqld restart#重启MySql
/etc/init.d/httpd restart#重启Apche
配置篇
一、Apache配置
vi/etc/httpd/conf/httpd.conf#编辑文件
ServerTokens OS在44行修改为:ServerTokens Prod(在出现错误页的时候不显示服务器操作系统的名称)
ServerSignature On在536行修改为:ServerSignature Off(在错误页中不显示Apache的版本)
Options Indexes FollowSymLinks在331行修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)
#AddHandler cgi-script.cgi在796行修改为:AddHandler cgi-script.cgi.pl(允许扩展名为.pl的CGI脚本运行)
AllowOverride None在338行修改为:AllowOverride All(允许.htaccess)
AddDefaultCharset UTF-8在759行修改为:AddDefaultCharset GB2312(添加GB2312为默认编码)
Options Indexes MultiViews FollowSymLinks在554行修改为 Options MultiViews FollowSymLinks(不在浏览器上显示树状目录结构)
DirectoryIndex index.html index.html.var在402行修改为:DirectoryIndex index.html index.htm Default.html Default.htm
index.php Default.php index.html.var(设置默认首页文件,增加index.php)
KeepAlive Off在76行修改为:KeepAlive On(允许程序性联机)
MaxKeepAliveRequests 100在83行修改为:MaxKeepAliveRequests 1000(增加同时连接数)
:wq!#保存退出
/etc/init.d/httpd restart#重启
rm-f/etc/httpd/conf.d/welcome.conf/var/www/error/noindex.html#删除默认测试页
二、php配置
vi/etc/php.ini#编辑
date.timezone= PRC#在946行把前面的分号去掉,改为date.timezone= PRC
disable_functions= passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#在386行列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
expose_php= Off#在432行禁止显示php版本的信息
magic_quotes_gpc= On#在745行打开magic_quotes_gpc来防止SQL注入
short_open_tag= ON#在229行支持php短标签
open_basedir=.:/tmp/#在380行设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
:wq!#保存退出
/etc/init.d/mysqld restart#重启MySql
/etc/init.d/httpd restart#重启Apche
测试篇
cd/var/www/html
vi index.php#输入下面内容
<?php
phpinfo();
?>
:wq!#保存退出
在客户端浏览器输入服务器IP地址,可以看到如下图所示相关的配置信息!
注意:apache默认的程序目录是/var/www/html
权限设置:chown apache.apache-R/var/www/html
至此,CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL)教程完成!
linux怎么配置samba服务器
1.samba服务器目录:
企业数据目录:/companydata
公共目录:/companydata/share
销售部目录:/companydata/sales
技术部:/companydata/tech
2.企业员工情况:
总经理:gm
销售部:销售部经理 redking、员工 sky、员工 jane
技术部:技术部经理 michael、员工 bill、员工 joy
3.搭建samba文件服务器,建立公共共享目录,允许所有人访问,权限为只读,为销售部和技术部分别建立单独的目录,只可以总经理和相应部门员工访问,并且公司员工禁止访问非本部门的共享目录。
案例解析:
1.需求分析
对于建立公共目录public字段就可以实现匿名访问,员工只能访问本部门的共享目录,禁止访问非本部门的共享目录,可以通过设置目录共享字段“browseable= no”及字段“valid users”来实现其隐藏功能和相应的访问权限。这样设置不能很好得解决同一目录多种需求的权限设置,所以需要建立独立配置文件,为每个部门建立一个组后并为每个组建立配置文件来实现隔离用户权限会比较灵活。
2.解决方案
1)建立各部门专用共享目录
使用mkdir建立需求的共享目录以便分门别类的存储相应资料。
[root@RHEL~]#mkdir/companydata
[root@RHEL~]#mkdir/companydata/share
[root@RHEL~]#mkdir/companydata/sales
[root@RHEL~]#mkdir/companydata/tech
同时设置/companydata共享目录的用户权限
[root@RHEL~]#chmod–R 777/companydata
2)添加samba服务器描述及设置smbpasswd文件
默认/etc/samba/目录下没有smbpasswd文件,要先关闭samba的tdbsam验证。
解决方法:在smb.conf文件中注释掉passdb backend= tdbsam一行,为了指定samba用户的验证我们加上这一条smb passwd file=/etc/samba/smbpasswd,然后保存退出。
3)添加用户和组
先建立销售部组sales,技术部组tech,然后使用useradd命令添加总经理帐号gm及各个员工的帐号并加入相应的用户组。
[root@RHEL~]#groupadd sales
[root@RHEL~]#groupadd tech
[root@RHEL~]#useradd gm
[root@RHEL~]#useradd–g sales redking
[root@RHEL~]#useradd–g sales sky
[root@RHEL~]#useradd–g sales jane
[root@RHEL~]#useradd–g tech michael
[root@RHEL~]#useradd–g tech bill
[root@RHEL~]#useradd–g tech joy
接着使用smbpasswd命令添加samba用户:
[root@RHEL~]#smbpasswd–a gm
New SMB password:
Retype new SMB password:
Added user gm.
[root@RHEL~]#smbpasswd–a redking
New SMB password:
Retype new SMB password:
Added user reding.
[root@RHEL~]#smbpasswd–a sky
New SMB password:
Retype new SMB password:
Added user syk.
[root@RHEL~]#smbpasswd–a jane
New SMB password:
Retype new SMB password:
Added user jane.
[root@RHEL~]#smbpasswd–a michael
New SMB password:
Retype new SMB password:
Added user michael.
[root@RHEL~]#smbpasswd–a bill
New SMB password:
Retype new SMB password:
Added user bill.
[root@RHEL~]#smbpasswd–a joy
New SMB password:
Retype new SMB password:
Added user joy.
4)配置smb.conf文件
(1)建立单独配置文件
用户配置文件使用用户名命令,组配置文件使用组名命令。
[root@RHEL~]#cp/etc/samba/smb.conf/etc/samba/gm.smb.conf
[root@RHEL~]#cp/etc/samba/smb.conf/etc/samba/sales.smb.conf
[root@RHEL~]#cp/etc/samba/smb.conf/etc/samba/tech.smb.conf
(2)设置主配置文件smb.conf
配置smb.conf主配置文件,在global节点中添加相应字段,确保samba服务器的主配置文件可以调用独立的用户配置文件和组配置文件。
include=/etc/samba/%U.smb.conf表示使samba服务器加载/etc/samba目录下格式为“用户名.smb.conf”的配置文件。
include=/etc/samba/%G.smb.conf表示使samba服务器加载/etc/samba目录下格式为“组名.smb.conf”的配置文件。
设置共享目录/companydata/share
添加如下内容:
[public]
comment=public data
path=/companydata/share
public=yes
5)设置总经理gm配置文件
[root@RHEL~]#vim/etc/samba/gm.smb.conf
添加如下内容:
[sales]
comment=sales data
path=/companydata/sales
writable=yes
valid users=gm
[tech]
comment=tech data
path=/companydata/tech
writable=yes
valid users=gm
6)设置销售部组sales配置文件
[root@RHEL~]#vim/etc/samba/sales.smb.conf
添加如下内容:
[sales]
comment=sales data
path=/companydata/sales
writable=yes
valid users=@sales,gm
7)设置技术部组tech配置文件
[root@RHEL~]#vim/etc/samba/tech.smb.conf
添加如下内容:
[tech]
comment=tech data
path=/companydata/tech
writable=yes
valid users=@tech,gm
8)开启samba服务
[root@RHEL~]#service smb start
9)测试
分别用所设置账号进行测试。