centos sudoers file,centos图形界面安装

大家好,感谢邀请,今天来为大家分享一下centos sudoers file的问题,以及和centos图形界面安装的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!

Linux操作系统文件访问权限详解

一直以root登陆使用linux的人来说很少有权限被拒这种概念,但某些时候又深受权限拒绝困扰。

知道为什么很多程序中需要使用getuid(),setuid()?为什么以普通权限登陆的用户不能进入/root,为什么在/目录下执行ls-l后可以显示root的信息,但ls/root-al却是权限不够?为什么有些文件夹可以继续创建文件,但就是不能ls?等等,相信看了此文就能明白。

主要是学习笔记,不足之处请指正。

CentOS 5.4 [testc@xxx opt]$ uname-a Linux xxx 2.6.18-164.el5xen#1 SMP Thu Sep 3 04:47:32 EDT 2009 i686 i686 i386 GNU/Linux

一、口令文件1,格式存储文件/etc/passwd,格式如下:root:x:0:0:root:/root:/bin/bash aaa:x:501:501:bj, bj, 8111111,136000111:/home/aaa:/bin/bash用户名:加密密码:用户ID:组ID:注释:工作目录:shell:

默认情况是第一行的格式;注释字段可以自行修改,用逗号隔开,如第二行格式,这主要是给finger命令使用时可解析。

可以vi/etc/passwd修改,但为了保证其格式的正确性,请用vipw命令编译此文件。

sh-3.2# finger aaa Login: aaa Name: bj Directory:/home/aaa Shell:/bin/bash Office: bj, 8111111 Home Phone: 136000111 Never logged in. No mail. No Plan.

2,编程实例

/*getpwnam_pwuid.c*/#include#include#include

int main(void)

{//struct passwd*pwd= getpwnam("aaa");struct passwd*pwd= getpwuid(501);if(pwd== NULL)

{ printf("err.\n");return 1;}

printf("name:%s\n", pwd->pw_name);printf("passwd:%s\n", pwd->pw_passwd);printf("description:%s\n", pwd->pw_gecos);printf("uid:%d\n", pwd->pw_uid);printf("gid:%d\n", pwd->pw_gid);printf("dir:%s\n", pwd->pw_dir);printf("shell:%s\n", pwd->pw_shell);

return 0;}

sh-3.2# gcc getpwnam_pwuid.c-o app sh-3.2#./app name:aaa passwd:x description:bj, bj, 8111111,136000111 uid:501 gid:501 dir:/home/aaa shell:/bin/bash

二、组文件1,格式存储文件/etc/group,格式如下root:x:0:root bin:x:1:root,bin,daemon aaa:x:501:组名:加密密码:组ID:指向的各用户名

2,改变文件uid和gid.

sh-3.2# pwd/root/study sh-3.2# ls-al-rw-r——r—— 1 root root 397 10-11 03:23 test.c

chgrp改变所属组ID,当然只有root权限才可以修改。

sh-3.2# chgrp aaa test.c sh-3.2# ls-al-rw-r——r—— 1 root aaa 397 10-11 03:23 test.c

这个aaa就是新组名,其在/etc/group中,可以通过adduser aaa自行添加sh-3.2# cat/etc/group root:x:0:root bin:x:1:root,bin,daemon daemon:x:2:root,bin,daemon.

gdm:x:42:sabayon:x:86:plmtest:x:500:aaa:x:501:

chown改变用户ID或组ID sh-3.2# chown aaa:aaa test.c sh-3.2# ls-al-rw-r——r—— 1 aaa aaa 397 10-11 03:23 test.c

3,编程实例

/*getgrnam.c*/#include#include

int main(int argc, char*argv[])

{ if(argv[1]== NULL)

{ printf("input error.\n");return 1;}

struct group*gp= getgrnam(argv[1]);if(gp== NULL)

{ printf("err.\n");return 1;}

printf("name:%s\n", gp->gr_name);printf("psswd:%s\n", gp->gr_passwd);printf("gid:%d\n", gp->gr_gid);

int i;for(i= 0; gp->gr_mem[i]!= NULL; i++)

{ printf("group name:%s\n", gp->gr_mem[i]);}

return 0;}

sh-3.2# gcc getgrnam.c-o app sh-3.2#./app bin name:bin psswd:x gid:1 group name:root group name:bin group name:daemon 4,文件权限不细讲了sh-3.2# ls-al总计 483984 drwxr-x—— 13 root root 4096 02-22 00:01. drwxr-xr-x 32 root root 4096 02-21 21:15……

-rw-r——r—— 1 root root 464023491 10-25 22:33 3.3.005-080425.tgz-rw—— 1 root root 9346 02-21 23:16.bash_history-rw-r——r—— 1 root root 24 2007-01-06.bash_logout-rw-r——r—— 1 root root 191 2007-01-06.bash_profile-rw-r——r—— 1 root root 176 2007-01-06.bashrc drwxrwxrwx 10 1000 users 4096 08-23 20:16 cflow-1.3-rw-r——r—— 1 root root 759691 08-23 20:13 cflow.tar.gz-rw-r——r—— 1 root root 100 2007-01-06.cshrc-rwxr-xr-x 1 root root 582 11-11 21:48 delete_M.sh-rw-r——r—— 1 root root 2518 11-11 20:25.dir_colors

主要是最左边一列:drwxr-x——10个字符,最左边是文件类型,-默认为普通文件;d:目录文件;l符号链接……

后面9个,3个一组共三组,分别表示所属用户uid的权限;所属组或者附属组gid的权限;其它权限。

三个字符分别是读、写、执行权限读4,写2,执行1

所以chmod 777 test.c,提升到读、写、执行权限。

5,组权限操作实例此节演示相同组的成员之间共享资源,即不同uid但相同gid的用户共享同一组的资源。

为了方便起见,我同时开了两个终端。

"sh-3.2#"以root权限登陆的shell/bin/sh"[testa@xxx root]"以testa用户登陆的shell

注:下文提到的“用户”是指/etc/passwd里定义的通过终端登陆的用户(此文即以下增加的三个账号名)。

sh-3.2# useradd testa sh-3.2# useradd testb sh-3.2# useradd testc

sh-3.2# tail-f/etc/passwd-n 4 sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin testa:x:500:500::/home/testa:/bin/bash testb:x:501:501::/home/testb:/bin/bash testc:x:502:502::/home/testc:/bin/bash

再开一个终端登陆testa,之前那个终端保持。

sh-3.2# su testa [testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa)

[testa@xxx home]$ ls-al总计 28 drwxr-xr-x 5 root root 4096 02-21 22:52. drwxr-xr-x 32 root root 4096 02-21 21:15……

drwx—— 3 testa testa 4096 02-21 22:56 testa drwx—— 3 testb testb 4096 02-21 22:48 testb drwx—— 3 testc testc 4096 02-21 22:52 testc

[testa@xxx home]$ cd testb bash: cd: testb:权限不够

通过root修改testb目录权限为770,即当前uid或者gid相同的用户均有读写执行权限。

sh-3.2# cd/home/ sh-3.2# chmod 770 testb

[testa@xxx home]$ ls-al总计 28 drwxr-xr-x 5 root root 4096 02-21 22:52. drwxr-xr-x 32 root root 4096 02-21 21:15……

drwx—— 3 testa testa 4096 02-21 22:56 testa drwxrwx—— 3 testb testb 4096 02-21 22:48 testb(here modify)

drwx—— 3 testc testc 4096 02-21 22:52 testc

[testa@xxx home]$ cd testb bash: cd: testb:权限不够[testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa)

此时虽然开放了testb的所属组权限,但用户testa的gid=500(testa) groups=500(testa),它还不属于testb组。

下面修改testa的gid为testb(或者增加其附属组groups值为testb)

sh-3.2# usermod-G testb testa(增加用户testa的附属组testb)

sh-3.2# id testa uid=500(testa) gid=500(testa) groups=500(testa),501(testb)

此时testa终端需要重新登下,使刚才更改生效[testa@xxx root]$ exit exit [root@xxx~]# su testa [testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa),501(testb)

[testa@xxx root]$ cd/home/ [testa@xxx home]$ ls-al总计 28 drwxr-xr-x 5 root root 4096 02-21 22:52. drwxr-xr-x 32 root root 4096 02-21 21:15……

drwx—— 3 testa testa 4096 02-21 22:56 testa drwxrwx—— 3 testb testb 4096 02-21 22:48 testb drwx—— 3 testc testc 4096 02-21 22:52 testc [testa@xxx home]$ cd testb [testa@xxx testb]$ pwd/home/testb

以上是增加了用户testa的附属组testb,使其对于属于testb组的资源有了访问权限。

下面再使用newgrp切换用户testa的gid.

[testa@xxx testb]$ id uid=500(testa) gid=500(testa) groups=500(testa),501(testb)

[testa@xxx testb]$ newgrp testb [testa@xxx testb]$ id uid=500(testa) gid=501(testb) groups=500(testa),501(testb)

此时testa用户的gid已改为501(testb)。

组之前的关系在文件/etc/group sh-3.2# tail-f/etc/group-n 4 sabayon:x:86:testa:x:500:testb:x:501:testa(最后一列:组内用户列表。即组testb里包含testa,testa属于testb组,大概就这意思吧……)

testc:x:502:

虽然知道控制组关系的文件,但不能直接修改些文件,否则执行newgrp时会出现"抱歉"错误提示。

当然root用户权限是无限制的,它访问文件时不需要进行权限检查。

三、相关系统调用getuid();getgid();int setuid(uid_t uid);int setgid(gid_t gid);

只有超级用户或者需要设置的uid和当前用户的uid一致才可以设置,否则返回-1,置errno= EPERM, errno可以通过strerror()翻译。

其它:[testa@xxx home]$ su testa [testa@xxx home]$ sudo touch aa

testa is not in the sudoers file. This incident will be reported.

以root权限vim/etc/sudoers增加testa ALL=(ALL) ALL

参考:APUE2E,1.8, 4.4, 8.11

如何在CentOS 7.2下安装 Emacs

打开终端提交回答

sudo yum install emacs提交回答

输入你的root密码然后回车确定

但是问题不是这么简单,出现 XXX is not in the sudoers file的问题。。。

解决方案:

首需要切换到root身份

$su-

(注意有-,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用"su-"命令将环境变量也一起带过去,就象和root登录一样)

然后

$visudo//切记,此处没有vi和sudo之间没有空格

1、移动光标,到最后一行(最好是找到root ALL=(ALL)

ALL,在下面添加一行)

2、按a,进入append模式

3、输入

your_user_name ALL=(ALL) ALL

4、按Esc

5、输入“:w”(保存文件)

6、输入“:q”(退出)

这样就把自己加入了sudo组,可以使用sudo命令了。

问题是这个问题又衍生另一个问题,就是:

[root@VM_207_245_centos~]# visudo

visudo:/etc/sudoers busy, try again later

。。。

没退出vi,退出一次就好

[Linux] admin is not in the sudoers file 问题解决办法

在安装好 centos后,在普通账户下使用 sudo命令后会出现 admin is not in the sudoers file……的提示(PS:此处 admin值的是你当前账户的账户名,不是必须为 admin),这是因为当前用户账号没有加入用户组,需要修改 sudoers文件解决。

具体的解决办法如下:

1.在命令界面输入 su,切换到 root用户

2.因为 sudoers文件不可写,所以为 sudoers添加写权限

输入命令 chmod u+w/etc/sudoers

3.修改 sudoers文件

输入命令 vim/etc/audoers

如果没有安装 vim可以使用 vi/etc/sudoers

打开后添加如下一行:

4.去掉 sudoers的写权限

输入命令 chmod u-w/etc/sudoers

完成后切换到普通用户,测试 sudo命令是否可用

如上图所示,就是成功了。

阅读剩余
THE END