百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

2、linux命令-用户管理(linux用户管理总结)

nanshan 2024-11-04 13:06 14 浏览 0 评论

linux命令-用户管理

用户切换

[root@eric ~]# su eric # 切换到用户eric

[eric@eric root]$

[eric@eric root]$ su # 切换到root

Password:

[root@eric ~]#

[root@eric ~]# exit # 退出当前用户

exit

[eric@eric root]$

usermod ye02 -G wheel # 用户组切换

用户信息存储位置

# /etc/passwd # 存储Linux中的?户信息

# /etc/shadow # 存储Linux中的?户密码信息

# /etc/group # 存储Linux中的?户组信息

# /etc/gshadow # 存储Linux中的?户组密码信息

passwd文件解读

useradd zhangsan

more /etc/passwd

zhangsan:x:1002:1002::/home/zhangsan:/bin/bash

[1]:[2]:[3]:[4]:[5]:[6]:[7]

[1] : ?户名

[2] : ?户密码 | X - 密码单独存储在/etc/shadow

[3] : ?户ID

[4] : 组ID group-id (GID) # 主组

[5] : ?户描述信息

[6] : 该?户的家?录

[7] : ?户所使?的默认shell壳-类型

shell类型

#查看当前系统中?持的linux shell类型

[root@centos7 ~]# cat /etc/shells

/bin/sh #最古?

/bin/bash #最常?/最常?

/sbin/nologin # ?户禁?登录

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/bin/tcsh

/bin/csh

#查看当前环境正在使?shell

[root@centos7 ~]# echo $SHELL

/bin/bash

#临时shell切换

[root@centos7 ~]# sh

sh-4.2#

sh-4.2# bash

#永久切换(账户退出重新登录后?效)

[root@centos7 tom]# chsh -s /bin/sh

Changing shell for root.

Shell changed.

用户操作

没有指定uid和gid,系统默认从1001开始,自动创建命令home/用户,自动使用bash

useradd zhangsan

useradd -u 2001 lisi # 指定uid

useradd -u 2002 -c "description" user02 #添加描述

useradd -u 2003 -s /bin/csh user03 #指定shell

useradd user04 -u 2004 -d /home/user04 # 指定家目录

useradd user05 -u 2005 -g 2004 # 指定用户组(用户组必须已经存在)

useradd user07 -u 2007 -g 2004 -G 2003 # 指定扩展用户组(扩展用户组必须已经存在)

[root@eric home]# cat /etc/passwd

user07:x:2007:2004::/home/user07:/bin/bash

[root@eric home]# cat /etc/group

user03:x:2003:user07

[root@eric home]# id user07

uid=2007(user07) gid=2004(user04) groups=2004(user04),2003(user03)

[user07@eric home]$ cd user07 #切换到用户07

[user07@eric ~]$ touch 128.txt

-rw-r--r--. 1 user07 user04 0 Jul 31 18:46 128.txt 默认创建文件在user04组

[user07@eric ~]$ newgrp user03 #切户用户组到03。(可以采用命令exit退出该用户组03)

[user07@eric ~]$ touch 456.txt

-rw-r--r--. 1 user07 user03 0 Jul 31 18:50 456.txt 创建文件的所属组在03

userdel user08 #删除用户,用户组没有其他成员的话,也会相应删除用户组。

userdel -r user05 # 删除用户及对应的信息

userdel -rf user05 # 强制删除用户及对应的信息

/etc/shadow文件解读

user01:$6$bfcEHLZS$rfpTnTE8vogY0cWFRJqks9qdwsB0vukkCiWL7Hj/Xwe0p0LjkpEYUIDGA1XW

QcwxA8BsLd5P2m2lG3L/avZJd/:20615:0:99999:7:::

user02:!!:20615:0:99999:7:::

[1]:[2]:[3]:[4]:[5]:[6]:[7]:[8]:[9]

[1] : ?户名

[2] : 密码 | “!!” 账号锁定 / $6$ 密码已经设置,并且密码本地加密。

/etc/logins.defs

# Use SHA512 to encrypt password.

ENCRYPT_METHOD SHA512 (哈希法)

[3] : 上次修改密码时间 (从1970-1-1 (unix)到 修改密码当天)

[4] : 密码的最短有效期 (3) | 0 【不设置最短有效期】

[5] : 密码的最?有效期 (30)| 99999

[6] : 密码到期前多久发送告警 - 7 提前7天发送警告

[7] : 密码过期后的宽限?期 - 8 再宽限8天

[8] : 账号的失效?期 ------优先级最?, ?旦当前参数到期,该账号直接?即?法使?。 (从

1970-1-1 (unix)到 修改密码当天)

[9] : 保留

用户密码管理

[root@eric ~]# passwd --help

Usage: passwd [OPTION...]

-k, --keep-tokens keep non-expired authentication tokens

-d, --delete delete the password for the named account (root only)

-l, --lock lock the password for the named account (root only)

-u, --unlock unlock the password for the named account (root only)

-e, --expire expire the password for the named account (root only)

-f, --force force operation

-x, --maximum=DAYS maximum password lifetime (root only)

-n, --minimum=DAYS minimum password lifetime (root only)

-w, --warning=DAYS number of days warning users receives before password

expiration (root only)

-i, --inactive=DAYS number of days after password expiration when an account

becomes disabled (root only)

-S, --status report password status on the named account (root only)

--stdin read new tokens from stdin (root only)

Help options:

-?, --help Show this help message

--usage Display brief usage message

root账号可直接修改密码。用户修改自己的账号需要提交旧密码,并有大小写等要求。

root@eric user07]# passwd user01

[root@eric ~]# passwd -S user01 # 查看用户密码情况

user01 PS 2023-07-31 0 99999 7 -1 (Password set, SHA512 crypt.)

[root@eric ~]# passwd -l user02 # 锁定用户user02

Locking password for user user02.

passwd: Success

[root@eric ~]# passwd -u user02 # 解除锁定用户user02

chage -E 2023-8-1 user01 #强制用户的实效时间。

chage -d 0 user01 # 下次登录时候强制修改密码

passwd -e user01 # 下次登录时候强制修改密码

passwd -d user02 # 删除用户密码

echo "123" | passwd --stdin user01 通过管道方式修改密码

passwd -n 3 -x 30 -w 5 -i 2 user01 #最短3天最长30天提前5天警告宽限2天

用户组管理

#?户组信息

cat /etc/group

user02:x:2002:user01

[1]:[2]:[3]:[4]

[1]: ?户组组名

[2]: ?户组密码信息

[3]: ?户组ID

[4]: 附加?户组成员

groupadd wangwu # 新增用户组

[root@eric ~]# cat /etc/group

wangwu:x:2009:

groupdel wangwu2 # 删除用户组

groupadd -g 3001 wangwu2 # 指定id创建组

usermod命令

[root@eric ~]# usermod --help

Usage: usermod [options] LOGIN

Options:

-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

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

usermod user01 -g 2003 # 已经创建的用户需要通过usermod修改组,-g修改默认值

usermod user01 -G 2004 # -G 覆盖模式,增加或者修改扩展组

usermod user01 -aG 2002 # -aG 追加模式,增加扩展组

gpasswd命令

[root@eric ~]# gpasswd --help

Usage: gpasswd [option] GROUP

Options:

-a, --add USER add USER to GROUP

-d, --delete USER remove USER from GROUP

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

-Q, --root CHROOT_DIR directory to chroot into

-r, --delete-password remove the GROUP's password

-R, --restrict restrict access to GROUP to its members

-M, --members USER,... set the list of members of GROUP

-A, --administrators ADMIN,...

set the list of administrators for GROUP

Except for the -A and -M options, the options cannot be combined.

gpasswd wangwu # 用户组密码设置,用于用户切换用户组时候输入密码

[root@eric ~]# gpasswd -d user04 user02 # user02组中删除user04用户

Removing user user04 from group user02

[root@eric ~]# gpasswd -a user04 user02 # user02用户组添加user04用户

Adding user user04 to group user02

/var/spool/mail 删除用户后,该目录下用户文件夹需要手工删除

[root@eric ~]# ls /var/spool/mail

eric ovswitch rpc user02 user04 user10

lisi root user01 user03 user07 zhangsan

[root@eric ~]# rm -rf /var/spool/mail/uer03 # user03用户删除后,该目录还在,需要手工删除。

[root@eric mail]# ls -a /home/user02 #创建用户时候,系统会自动生成隐藏文件。(不建议手动创建家目录文件夹)

. .. .bash_logout .bash_profile .bashrc .mozilla

[root@eric skel]# ls /etc/skel -a # 在该文件夹下创建文件,之后添加用户时候会自动添加到用户的家目录。

. .. .bash_logout .bash_profile .bashrc .mozilla

[root@eric ~]# chfn # 修改用户描述信息

Changing finger information for root.

Name [root]: user10

Office []: abc

Office Phone []: 123

Home Phone []: 123

Finger information changed.

向用户弹窗

[root@eric ~]# wall "this sever will be shutdown,please exit" # 向所有用户弹窗

Broadcast message from root@eric.centos7 (pts/0) (Tue Aug 1 17:46:49 2023):

this sever will be shutdown,please exit

禁止用户登录

[root@centos7 ~]# touch /etc/nologin #禁?所有普通?户登录

[root@centos7 ~]# rm -rf touch /etc/nologin #取消禁?所有普通?户登录

相关推荐

在 Ubuntu 上安装 Zabbix(以 Zabbix 6.4 LTS 版本为例)

Zabbix是一个流行的开源监控解决方案,能够监控各种网络参数和服务器健康状态。一、环境准备系统要求Ubuntu20.04/22.04LTS至少2GBRAM(生产环境建议4GB+)至少1...

如何在 Ubuntu 24.04 服务器上安装 Apache Solr

ApacheSolr是一个免费、开源的搜索平台,广泛应用于实时索引。其强大的可扩展性和容错能力使其在高流量互联网场景下表现优异。Solr基于Java开发,提供了分布式索引、复制、负载均衡及自...

如何在 Ubuntu 24.04 LTS 或 22.04/20.04 上安装 Apache Maven

Maven是由Apache托管的开源工具,用于管理Java项目。它包含一个项目对象模型(POM):一个配置文件(XML),其中包含项目的基本信息,包括配置、项目依赖项等。Maven可以处理...

Cursor的终极对手——Trae Pro最新系统提示词

前段时间,字节的AI编程神器Trae国际版,终于甩出了Pro订阅计划!很多对它又爱又恨的小伙伴,直呼:终于等到你。爱它,是因为Trae长期免费+体验真香;恨它?还不是那该死的排队等待,...

AI系统提示词:V0(ai代码提示)

以下是对V0系统提示词(SystemPrompt)的分部分讲解与解读,帮助你理解其核心内容和设计意图。V0系统提示词##CoreIdentity-Youarev0,Vercel&...

8岁男童失踪第13天,搜救人员发现可疑水库,更恶心的事情发生了

Lookingatyourrequest,Ineedtorewritethearticleaboutthe8-year-oldmissingboywhilemaking...

docker常用指令及安装rabbitMQ(docker安装zabbix)

一、docker常用指令启动docker:systemctlstartdocker停止docker:systemctlstopdocker重启docker:systemctlrestart...

三步教你用Elasticsearch+PyMuPDF实现PDF大文件秒搜!

面对100页以上的大型PDF文件时,阅读和搜索往往效率低下。传统关系型数据库在处理此类数据时容易遇到性能瓶颈,而Elasticsearch凭借其强大的全文检索和分布式架构,成为理想解决方案。通过...

ElasticSearch中文分词插件(IK)安装

坚持原创,共同进步!请关注我,后续分享更精彩!!!前言ElasticSearch默认的分词插件对中文支持很不友好。一段话按规则会以每个中文字符来拆解,再分别建立倒排索引。如"中华人民共和国国歌...

SpringBoot使用ElasticSearch做文档对象的持久化存储?

ElasticSearch是一个基于Lucene的开源搜索引擎,广泛应用于日志分析、全文搜索、复杂查询等领域,在有些场景中使用ElasticSearch进行文档对象的持久化存储是一个很不错的选择...

Elasticsearch数据迁移方案(elasticsearch copyto)

前言最近小编要去给客户部署一套系统涉及到了Mysql和ES数据的迁移,下面就给大家分享一下ES数据迁移的几套方案,根据具体的使用场景来选择不同的迁移方案能使你事倍功半,话多说下面就一一介绍。Elast...

Rancher部署单体ElasticSearch(rancher2.5部署)

Rancher是k8s图形管理界面,之前曾有写文章介绍如何安装。ElasticSearch是热门搜索引擎,很多地方都有用到,常规安装部署略显繁琐,本文介绍在k8s下用rancher简易部署ES。1.在...

Elasticsearch在Java项目的搜索实践:从零开始构建高效搜索系统

Elasticsearch在Java项目中的搜索实践:从零开始构建高效搜索系统在现代的Java项目中,数据量激增,传统的数据库查询方式已经无法满足快速检索的需求。这时,Elasticsearch(E...

小白入门-Kibana安装(kibana安装配置)

一Kibana基础1.1介绍Kibana是一款免费且开放的前端应用程序,其基础是ElasticStack,可以为Elasticsearch中索引的数据提供搜索和数据可视化功能。Kiban...

Docker上使用Elasticsearch,Logstash,Kibana

在对一个项目做性能测试时我需要处理我们web服务器的访问日志来分析当前用户的访问情况。因此,我想这是试用ELK的一个好机会。ELK栈首先要注意的是使用它是非常简单的。从决定使用ELK到在本机上搭一个...

取消回复欢迎 发表评论: