首先在管理员权限下创建用户,具体创建命令有两种创建用户有两条命令:adduer和useradd,对应着两条删除用户的命令:deluser和userdel。其区别具体见博客:地址1,地址2
个人建议使用adduser命令创建。(删除的时候用 sudo userdel -r username,可以连主目录一起删除)
sudo adduser username
如果创建的是管理员账户,参考地址3,创建的用户可以使用管理员权限。加上 --home 可指定用户主目录位置,例如 --home /home/test/username,当然如果用户已经创建了,则可以在目标位置创建好位置,复制当前主目录所有内容到目标位置,然后修改系统设置的主目录位置。
每个用户在创建的时候,都会至少存在于一个组,
Linux中每个文件有所有者(u)、所在组(g)、其它组(o)的概念,不同的组对于文件处理的权限有三种,读(r),写(w),执行(x),具体可看地址4
这里我贴一下
所以,如果我们创建的不是超级管理员账户,而是普通用户的话,设置用户只能看见自己home目录下的文件,可以使用命令(管理员账户下执行)
sudo chmod 700 /home/username
如果我们组内用户之间可见,那么首先我们创建一个组。这里先列举一些查看组的命令,
列出所有组
cat /etc/group
创建组,-g 是用来指定group id 的
sudo addgroup -g 1006 groupname
删除组命令,如果报错,参考解决方案
sudo groupdel groupname
用户添加到组内
sudo adduser username groupname
用户从组内移除
sudo gpasswd -d userName groupName
在Linux系统中,文件或者文件目录一般是只有一个组,和文件所有者,但是一个用户可以在多个组里。
首先,我们通过以下命令,查看文件的所有者和所在组以 /home/admin 为例子
ls -l /home/admin
我的输出结果某一行是
drwxr-xr-x 2 admin admin 4096 5月 31 18:55 Desktop
解释一下,第一个d是代表文件目录的意思,然后rwxr-xr-x,9个字符,每三个一组,为rwx ,r-x,r-x,通常要r代表读取权限,w代表写入权限,x代表可执行权限,-代表没有权限,第一组为为文件所有者,权限为rwx,即可读可写可执行,第二组为同组用户对该文件的权限为r-x,可读不可写(不可修改)可执行,第三组为其他用户,权限为r-x,即可读不可写可执行。2代表的是Desktop是admin文件的二级目录,第一个admin是Desktop 文件的所有者,第二个admin是文件所在的组,4096代表文件大小,单位为字节,即4kb,后面的是时间,还有文件名字。具体看下图,原文链接(感谢原作者)
现在我们需要考虑的是吧文件的所有者和用户组换掉,把/home/admin 这个文件夹的所有者和所在组替换成 admin2 这个用户和用户组。
更换文件所有者
sudo chown -R admin2 /home/admin
### chown 可以一次性修改用户和组
### chown -R username:group /home/admin
sudo chown -R admin2:admin2 /home/admin
更换文件所在的组
sudo chgrp -R admin2 /home/admin
至此,我们完成了把文件的所有者和所在组进行了更换,不同用户之间就可以通过设置文件所在组,和访问权限,用户之间实现部分文件共享,读写执行等功能。PS查看用户id 和group id
id username
其他
chmod a+x和chmod u+x区别
用户删除:
(1)deluser ——对应创建命令 adduser
只删除用户:
sudo deluser username
连同用户的主目录和邮箱一起删除:
sudo deluser --remove-home username (个人推荐使用这个命令,同时可以删除创建的组)
连同用户拥有的所有文件删除:
sudo deluser --remove-all-files username
(2)userdel ——对应创建命令useradd
只删除用户:sudo userdel username
连同用户主目录一起删除:sudo userdel -r username
如果创建时主目录已经存在,即主目录不属于当前要删除的用户,则无法删除主目录。
其他命令及解释:
/etc/passwd - 使 用 者 帐 号 资 讯,可以查看用户信息
/etc/shadow - 使 用 者 帐 号 资 讯 加 密
/etc/group - 群 组 资 讯
/etc/default/useradd - 定 义 资 讯
/etc/login.defs - 系 统 广 义 设 定
/etc/skel - 内 含 定 义 档 的 目 录
ubuntu 本身关于deluser 和adduser命令 的详细解释
(a) deluser
deluser USER
remove a normal user from the system
example: deluser mike
--remove-home remove the users home directory and mail spool
--remove-all-files remove all files owned by user
--backup backup files before removing.
--backup-to <DIR> target directory for the backups.
Default is the current directory.
--system only remove if system user
delgroup GROUP
deluser --group GROUP
remove a group from the system
example: deluser --group students
--system only remove if system group
--only-if-empty only remove if no members left
deluser USER GROUP
remove the user from a group
example: deluser mike students
general options:
--quiet | -q don't give process information to stdout
--help | -h usage message
--version | -v version number and copyright
--conf | -c FILE use FILE as configuration file
(b) userdel
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files,
even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-R, --root CHROOT_DIR directory to chroot into
-Z, --selinux-user remove any SELinux user mapping for the user