首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Linux基础之用户管理

今天给大家介绍的是linux基本的用户(usr)管理和组(group)管理。

在linux中,任何一个要使用系统资源的用户,都必须首先向系统管理员(root)申请一个账号,然后以这个账号的身份进入系统。

用户的账号一方面可以帮助系统管理员对使用系统的用户进行跟踪,并控制他们对系统资源的访问;另一方面也可以帮助用户组织文件,并为用户提供安全性保护。

在linux中拥有最高管理员权限的账户为root,root账号是其他所有账号的基础,首先,在有了root根账号的情况下,我们想要创建我们自己的个人账号就需要用到以下指令:

useradd 参数 用户名

关于参数,我们这边介绍一下,有哪些常用参数:

-c comment 指定一段注释性描述。

-d 目录 指定用户主目录。

-g 用户组 指定用户所属的用户组。

-u 用户号 指定用户的用户号。

useradd -d /usr/john -m john

创建了一个用户john,其中-d和-m选项用来为登录名john产生一个主目录/usr/john

这是添加账号,下面我们来看看删除账号,指令:

userdel 参数 用户名

我们一般会用到的参数是-r ,递归删除,把它所在的目录也删除掉。

试试手:

userdel -r john

此命令删除用户sam在系统文件中(主要是/etc/passwd, /etc/shadow, /etc/group等)的记录,同时删除用户的主目录

下面讲一下修改账号

修改用户账号就是根据实际情况更改用户的有关属性,如用户号、主目录、用户组、登录Shell等。

修改已有用户的信息使用usermod命令:

usermod 参数 用户名

常用的选项包括-c, -d, -m, -g, -G, -s, -u以及-o等,这些选项的意义与useradd命令中的选项一样,可以为用户指定新的资源值。

另外,有些系统可以使用选项:-l 新用户名

(有兴趣的都读一下,所有的参数都在这里了)

这个选项指定一个新的账号,即将原来的用户名改为新的用户名。

-c, --comment COMMENT new value of the GECOS field

-d, --home HOME_DIR new home directory for the user account

-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE

-f, --inactive INACTIVE set password inactive after expiration

to INACTIVE

-g, --gid GROUP force use GROUP as new primary group

-G, --groups GROUPS new list of supplementary GROUPS

-a, --append append the user to the supplemental GROUPS

mentioned by the -G option without removing

him/her from other groups

-h, --help display this help message and exit

-l, --login NEW_LOGIN new value of the login name

-L, --lock lock the user account

-m, --move-home move contents of the home directory to the

new location (use only with -d)

-o, --non-unique allow using duplicate (non-unique) UID

-p, --password PASSWORD use encrypted password for the new password

-R, --root CHROOT_DIR directory to chroot into

-s, --shell SHELL new login shell for the user account

-u, --uid UID new UID for the user account

-U, --unlock unlock the user account

-v, --add-subuids FIRST-LAST add range of subordinate uids

-V, --del-subuids FIRST-LAST remove range of subordinate uids

-w, --add-subgids FIRST-LAST add range of subordinate gids

-W, --del-subgids FIRST-LAST remove range of subordinate gids

-Z, --selinux-user SEUSER new SELinux user mapping for the user account

试试手:

usermod -d /home/z yuqing

把用户所在的主目录改为/home/z

usermod -g yuqingwu yuqing

把用户所在的组改成yuqingwu

对账户的增删改我们已经介绍完毕了,现在我们来看看,口令管理(这里的口令就是我们所指的账号密码,这边我们把它叫做口令)

我们刚刚创建用户的时候没有口令,会被系统锁定,无法使用,必须为其指定口令后才可以使用,即使是指定空口令。

指定和修改用户口令的Shell命令是passwd。超级用户可以为自己和其他用户指定口令,普通用户只能用它修改自己的口令。命令的格式为:

passwd 参数 用户名

可使用的参数:

-l 锁定口令,即禁用账号。

-u 口令解锁。

-d 使账号无口令。

-f 强迫用户下次登录时修改口令。

我们看看系统还提供了那些命令参数

(自行翻译,经常用的我已经列出来了,下面是系统提供的所有的参数,感兴趣的都了解一下)

Usage: passwd [options] [LOGIN]

Options:

-a, --all report password status on all accounts

-d, --delete delete the password for the named account

-e, --expire force expire the password for the named account

-h, --help display this help message and exit

-k, --keep-tokens change password only if expired

-i, --inactive INACTIVE set password inactive after expiration

to INACTIVE

-l, --lock lock the password of the named account

-n, --mindays MIN_DAYS set minimum number of days before password

change to MIN_DAYS

-q, --quiet quiet mode

-r, --repository REPOSITORY change password in REPOSITORY repository

-R, --root CHROOT_DIR directory to chroot into

-S, --status report password status on the named account

-u, --unlock unlock the password of the named account

-w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS

-x, --maxdays MAX_DAYS set maximum number of days before password change to MAX_DAYS

现在我们来改一下yuqing这个用户的口令(密码):

passwd yuqing

为yuqing这个用户设置口令(密码)

TIPS:在linux中,所有的输入密码都是看不到的,所以别人看不到你输入的密码有多少位,是多少,安全性比较强。

我现在是处在root用户下面,所以可以直接进行设置修改,如果是普通用户,

他们在修改自己的口令时,passwd命令会先询问原口令,验证后再要求用户输入两遍新口令,如果两次输入的口令一致,则将这个口令指定给用户;而超级用户(root)为用户指定口令时,就不需要知道原口令。

为用户指定空口令时,执行下列形式的命令:

passwd -d yuqing

其实严格来说,为了系统安全起见,用户应该选择比较复杂的口令,例如最好使用8位长的口令,口令中包含有大写、小写字母和数字。

下面,我们可以禁用yuqing这个账号

passwd -l yuqing

这样yuqing这个账号就被锁住的,我们就不可以对其进行访问了。

下面我们来看看组(group)的管理:

每个用户都有一个用户组,系统可以对一个用户组中的所有用户进行集中管理。不同Linux 系统对用户组的规定有所不同,如Linux下的用户属于与它同名的用户组,这个用户组在创建用户时同时创建。

用户组的管理涉及用户组的添加、删除和修改。组的增加、删除和修改实际上就是对/etc/group文件的更新。

先来添加组吧,需要用到的指令是:

groupadd 参数 组名

可以使用的参数有:

-g GID 指定新用户组的组标识号(GID)。

-o 一般与-g选项同时使用,表示新用户组的GID可以与系统已有用户组的GID相同。

我们来看看系统里面还有什么其他的参数:

(自行翻译,如果真的英语不行,请告诉我,我给你有翻译的网址,谢谢,不客气!)

Usage: groupadd [options] GROUP

Options:

-f, --force exit successfully if the group already exists,

and cancel -g if the GID is already used

-g, --gid GID use GID for the new group

-h, --help display this help message and exit

-K, --key KEY=VALUE override /etc/login.defs defaults

-o, --non-unique allow to create groups with duplicate

(non-unique) GID

-p, --password PASSWORD use this encrypted password for the new group

-r, --system create a system account

-R, --root CHROOT_DIR directory to chroot into

--extrausers Use the extra users database

试试:

groupadd yuqing2

向系统中增加了一个新组yuqing2

group -g 201 yuqing3

此命令向系统中增加了一个新组yuqing3,同时指定新组的组标识号是201。

下面我们再来看看删除一个组:

删除组其实很简单:

groupdel 组名

都不用加参数的,我们来试试:

这样就删除我们刚刚新建的group yuqing2和yuqing3了。

接下来我们看看修改组属性,这个时候我们用到的指令是:

groupmod 参数 用户组

常用的参数有:

-g GID 为用户组指定新的组标识号。

-o 与-g选项同时使用,用户组的新GID可以与系统已有用户组的GID相同。

-n新用户组 将用户组的名字改为新名字

下面我们来看看系统里还提供了什么参数:

(自行翻译)

Usage: groupmod [options] GROUP

Options:

-g, --gid GID change the group ID to GID

-h, --help display this help message and exit

-n, --new-name NEW_GROUP change the name to NEW_GROUP

-o, --non-unique allow to use a duplicate (non-unique) GID

-p, --password PASSWORD change the password to this (encrypted)

PASSWORD

-R, --root CHROOT_DIR directory to chroot into

现在我们要把group yuqing的组标识改成404:

groupmod -g 404 yuqing

同时修改组标识为202和重命名成yuqingjohn

groupmod -g 202 -n yuqingjohn yuqing

最后还有个切换问题:

如果一个用户同时属于多个用户组,那么用户可以在用户组之间切换,以便具有其他用户组的权限。

用户可以在登录后,使用命令newgrp切换到其他用户组,这个命令的参数就是目的用户组。例如:

newgrp root

这条命令将当前用户切换到root用户组,前提条件是root用户组确实是该用户的主组或附加组。

今天就讲到这里了。我们下次再见吧!

听说有气质的人都会关注这个公众号!

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180613G1NMQG00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券