Linux——/etc/passwd文件含义,grep,cut

/etc/passwd文件含义

作用

  • 记录用户账户信息:共分为7段,使用冒号分割

含义

  • 文件内容意义:账户名:密码代号x:UID:GID:注释:家目录:SHELL

  • 第7列/sbin/nologin:不允许登录

示例

```shell

在passwd中检索包含root的行信息

root@server \~\]# grep -n "root" /etc/passwd # 在passwd检索不能登录的信息 \[root@server \~\]# grep -n "/sbin/nologin" /etc/passwd # 在passwd中检索可以登录的账户信息 \[root@server \~\]# grep -nv "/sbin/nologin" /etc/passwd (反向) # 检索包含"halt"的行及下3行信息 \[root@server \~\]# grep -nA3 "halt" /etc/passwd # 检索包含"sshd"的行及上5行信息 \[root@server \~\]# grep -nB5 "sshd" /etc/passwd # 检索包含"ftp"的行及上下2行 \[root@server \~\]# grep -nC2 "ftp" /etc/passwd ![](https://img-blog.csdnimg.cn/direct/dfe43cafdf684b69893043ccaddcb91d.png) # 检索不能登录的账户个数 \[root@server \~\]# grep -c "/sbin/nologin" /etc/passwd # 相当于 grep -n "/sbin/nologin" /etc/passwd \| wc -l # 检索包含大小写字母d的行信息 \[root@server \~\]# grep -ni "d" /etc/passwd \`\`\`![](https://img-blog.csdnimg.cn/direct/9df744c0dd9c4d4ea68094e2045b1458.png)

使用特殊符号匹配

匹配模式 说明

grep h 文件名 查找文件里有字符h的行

grep ^[q] 文件名 匹配以q开始的行

grep ^[qf] 文件名 匹配以q或者f开头的行

grep ^[ ^qf ] 文件名 不匹配以q或者f开头的行

grep ^[0-9] 文件名 匹配以数字开头的行

grep q$ 文件名 匹配以q结束的行

grep ^$ 过滤空白行

grep -r h ./* 如果要明确搜索当前目录中的子目录有h的行

grep -d s kip h ./* 忽略当前目录下的子目录下的普通文件

bash

root@server \~\]# grep \^a /etc/passwd ![](https://img-blog.csdnimg.cn/direct/173b7d2d967748bea3624a42d0f65430.png) \[root@server \~\]# grep -n "\^root" /etc/passwd \[root@server \~\]# grep -n "\^#" /etc/sos/sos.conf 找注释行 ![](https://img-blog.csdnimg.cn/direct/b33c330364324d0e8b2dc8f056550cc8.png) \[root@server \~\]# grep -n \[0-9\]$ /etc/sos/sos.conf \[root@server \~\]# grep -n \^$ /etc/sos/sos.conf

切割显示-cut

作用

  • cut命令用于按列提取文本内容

格式

```bash

cut -d "分隔符" -f列数字 文件名

```

示例

  • /etc/passwd文件在保存用户数据信息时,每一项值之间是采用冒号来间隔的,如:

```bash

root@server \~\]# head 2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin \`\`\` - 基于"行"的方式来提取数据是比较简单的,只需要设置好要搜索的关键词即可,但如果按列搜索,不仅要使用-f参数来设置需要看的列数,还需要使用-d参数来设置间隔符号。 \`\`\`shell \[root@kongd \~\]# cut -d : -f1 /etc/passwd root bin daemon adm lp \`\`\` - 例:提取内核版本号 \`\`\`bash \[root@server \~\]# uname -a \| cut -d " " -f3 \`\`\` - 例:提取IP地址 \`\`\`bash \[root@server \~\]# ip a \| grep ens160 \| grep inet \| cut -d "/" -f1 \| cut -d " " -f6 # 注意空格 \`\`\` - 例:提取mac地址 \`\`\`bash \[root@server \~\]# ip a \| grep link/ether \| cut -d " " -f6 \`\`\`

相关推荐
@CLoudbays_Martin112 分钟前
CDN是否能有效检测并且同时防御Ddos 和 CC 攻击?
java·服务器·网络·数据库·git·数据库开发·时序数据库
德迅云安全杨德俊10 分钟前
游戏盾:构筑网络安全防线,抵御DDoS攻击的解决方案
网络·安全·游戏·ddos
阿杆11 分钟前
文心快码 3.5S 发布!实测插件开发,Architect 模式令人惊艳
前端·后端·文心快码
文心快码BaiduComate12 分钟前
我用Comate搭建「公园找搭子」神器,再也不孤单啦~
前端·后端·微信小程序
河南博为智能科技有限公司19 分钟前
单串口服务器-工业级串口联网解决方案
运维·服务器·物联网·边缘计算
全栈技术负责人22 分钟前
前端全链路质量监控体系建设与实践分享
前端·系统架构·前端框架
爱喝水的鱼丶24 分钟前
SAP-MM:SAP采购组织全面学习指南:从概念到实战配置图解
运维·开发语言·数据库·学习·sap·mm模块·采购组织
Rum_0M27 分钟前
服务器内部信息获取
运维·服务器·web安全·网络安全·信息安全·职业技能大赛
Wy_编程31 分钟前
高并发服务器-多路IO转接-select
服务器·select·高并发
sorryhc37 分钟前
0~1构建一个mini blot.new(无AI版本)
前端·前端框架·openai