linux用户管理相关 创建用户CentOS环境/usr/sbin/adduser是一个链接文件指向 useradduseraddnew_user_name两者都会在home下自动创建家目录,没有设置密码,需使用passwd修改密码-b#指定根目录, 默认是/home-d#指定家目录, 默认是/home/usernameUbuntu环境useradd如果不添加任何选项创建出来的用户是默认的三无用户且该用户无法登陆无家目录无密码无系统Shell用adduser时系统会提示输入创建用户所需的各种信息只要跟着系统的提示就能完成用户的创建相对来说过程稍微繁琐otherpasswdusername#为用户设置密码passwd是修改当前用户密码sudopasswd是修改root用户密码#By default,it prints the real user ID(用户id),real group ID(该用户的主要用户组的id),and supplemental group IDs.#如果euid不等于ruid,打印euid#如果egid不等于rgid,打印egid#如果SELinux是支持的,还会打印安全上下文idusernamewhoami#prints the user name associated with the current effective user ID.#当前登陆到系统中的用户#通过(远程sshd:rootnotty,su-),who不会进行显示#通过(远程sshd:rootpts/1,或者直接在虚拟机中登陆login -- root)是可以显示的whogroupsusername1username2#打印用户所在的用户组(1个用户可以处于多个用户组中)userdel用户名#只删除用户userdel-r用户名#连同用户主目录一块删除usermod-g[groupName][userName]#修改用户的组groupadd[groupName]#新增组groupdel[groupName]#删除组groupmod-n[newName][oldName]#修改组名vim/etc/sudoers NOPASSWORD#指的是sudo的时候不用输密码和用户相关的几个文件#file permission是644,文件所有者,文件的用户组中的用户,以及其他用户都可以读取#每一行是一个用户#name:password:UID:GID:GECOS:directory(用户的家目录):shell(登陆时使用的shell程序)/etc/passwd#ubuntu中file permission是640,文件所有者和文件的用户组(通常是用户组shadow)中的用户可以进行读取#centos中file permission是000,只有文件所有者(root用户)可以读取/etc/shadow#系统中所有的用户组,一个用户可以加入多个用户组,用户组就类似于群聊的概念#group_name(用户组名字):password:GID:user_list(用户组中所有的用户)/etc/group#有时候当前使用的用户信息居然在/etc/passwd中找不到,用户加入的用户组信息在/etc/group中也没有getentpasswdusername#会从多个数据源查找stracegetentpasswdusername21|grepopen#一般会查看/etc/nsswitch.conf文件/etc/login.defs和/etc/environment文件中定义了PATH变量的初始值grep-lpam_env /etc/pam.d/*#将文件内容包含pam_env字段的文件名列出来sudonl/etc/sudoers每个用户都有一个唯一的uid 下面两个东西是针对进程来讲(ruid和euid可以不同)real user ID(ruid):哪个用户创建的该进程 effective user ID(euid):进程使用的哪个用户的文件访问权限 getuid()#获取该进程的ruidgeteuid()#获取该进程的euidgetpwuid(uid)#根据uid获取用户名setuid()#只有当进程的euid为root时,才可以成功调用,会修改进程的ruid和euidseteuid()#只有当进程的euid为root时,才可以成功调用,只会修改进程的euidprintf(ruid1 is %d\n,getuid());printf(euid1 is %d\n,geteuid());intretsetuid(111);if(ret){perror(setuid(111) failed);}printf(ruid2 is %d\n,getuid());printf(euid2 is %d\n,geteuid());lcwhhxlcwhhx:~/workspace/code_space/cmake$ ll ./main -rwxrwxr-x1lcwhhx lcwhhx8480Jan1418:08 ./main* lcwhhxlcwhhx:~/workspace/code_space/cmake$sudochownroot:root ./main lcwhhxlcwhhx:~/workspace/code_space/cmake$sudochmodus ./main lcwhhxlcwhhx:~/workspace/code_space/cmake$ ll ./main -rwsrwxr-x1root root8480Jan1418:08 ./main* lcwhhxlcwhhx:~/workspace/code_space/cmake$ ./main ruid1 is1000euid1 is0#因为可执行文件设置了setuid_bit位,所以进程的euid为文件的owner_idruid2 is111euid2 is111lcwhhxlcwhhx:~/workspace/code_space/cmake$sudochmodu-s ./main lcwhhxlcwhhx:~/workspace/code_space/cmake$ ll ./main -rwxrwxr-x1root root8480Jan1418:08 ./main* lcwhhxlcwhhx:~/workspace/code_space/cmake$ ./main ruid1 is1000euid1 is1000setuid(111)failed: Operation not permitted ruid2 is1000euid2 is1000https://www.jianshu.com/p/46f852d5e23chttps://superuser.com/questions/547966/whats-the-difference-between-adduser-and-useraddhttps://askubuntu.com/questions/345974/what-is-the-difference-between-adduser-and-useraddhttps://man7.org/linux/man-pages/man8/useradd.8.htmlhttps://man7.org/linux/man-pages/man1/groups.1.htmlhttps://man7.org/linux/man-pages/man5/group.5.htmlhttps://man7.org/linux/man-pages/man5/shadow.5.htmlhttps://man7.org/linux/man-pages/man5/passwd.5.htmlhttps://www.gnu.org/software/coreutils/manual/html_node/User-information.htmlhttps://www.man7.org/linux/man-pages/man1/ps.1.htmlhttps://man7.org/linux/man-pages/man2/setuid.2.htmlhttps://man7.org/linux/man-pages/man7/credentials.7.htmlhttps://elixir.bootlin.com/linux/v6.5.8/source/include/linux/cred.h#L119