未加星标

linux筛选数据和用户基本操作

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
linux筛选数据的基本操作

列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。

[[email protected] script]# who
chenjianhang tty1 2016-10-08 07:51
root tty2 2016-10-08 07:51
root pts/1 2016-10-08 02:58 (192.168.17.1)
[[email protected] script]# who | grep -o "^[^[:space:]]*\>" | uniq
chenjianhang
root
[[email protected] script]#

取出最后登录到当前系统的用户的相关信息。

[[email protected] ~]# last -1
root pts/1 192.168.17.1 Sat Oct 15 22:39 still logged in
wtmp begins Thu Dec 3 15:53:02 2015
[[email protected] ~]#

取出当前系统上被用户当作其默认shell的最多的那个shell。

[[email protected] ~]# cat /etc/passwd | cut -d: -f7 | grep ".*sh" | uniq -c
1 /bin/bash
1 /sbin/shutdown
3 /bin/bash
1 /bin/tcsh
1 /bin/csh
7 /bin/bash
[[email protected] ~]# cat /etc/passwd | cut -d: -f7 | grep ".*sh" | uniq -c | tail -1
7 /bin/bash
[[email protected] ~]#

将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。

[[email protected] ~]# cat /etc/passwd | sort -nk 3 -t : | tail -10 | tr [a-z] [A-Z] >/tmp/maxusers.txt
[[email protected] ~]# cat /tmp/maxusers.txt
DOCKER:X:504:504::/HOME/DOCKER:/BIN/CSH
OPENSTACK:X:3000:3000:COMMENT IMFOTMATION:/HOME/OPENSTACK:/BIN/BASH
UBUNTE:X:3001:3001:COMMENT IMFOTMATION:/HOME/UBUNTE:/BIN/BASH
MARIADB:X:3002:162::/HOME/MARIADB:/SBIN/NOLOGIN
FEDORA:X:3003:3003::/USERS/FEDORA:/BIN/BASH
WWW:X:3004:3004::/USERS/WWW:/BIN/BASH
NEWUSERNAME:X:3005:3005::/HOME/NEWUSERNAME:/BIN/BASH
ROOTKIT:X:3006:3006::/HOME/ROOTKIT:/BIN/BASH
CHROOT:X:3007:3007::/BIN/CHROOT:/BIN/BASH
NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
[[email protected] ~]#

取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。

[[email protected] ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:32:B0:C8 inet addr:192.168.17.128 Bcast:192.168.17.255 Mask:255.255.255.0inet6 addr: fe80::20c:29ff:fe32:b0c8/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:4079 errors:0 dropped:0 overruns:0 frame:0TX packets:3539 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:449857 (439.3 KiB) TX bytes:520996 (508.7 KiB)Interrupt:67 Base address:0x2024
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:16436 Metric:1RX packets:1565 errors:0 dropped:0 overruns:0 frame:0TX packets:1565 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:3688919 (3.5 MiB) TX bytes:3688919 (3.5 MiB)
[[email protected] ~]# ifconfig | grep "inet addr" | cut -d":" -f 2 | cut -d" " -f 1
192.168.17.128
127.0.0.1
[[email protected] ~]#

列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。

[[email protected] ~]# ls /etc/*.conf | cut -d"/" -f 3 | tr [a-z] [A-Z] > /tmp/etc.test
[[email protected] ~]# cat /tmp/etc.test
AUTOFS_LDAP_AUTH.CONF
CDRECORD.CONF
CONMAN.CONF
DHCP6C.CONF
DNSMASQ.CONF
ESD.CONF
GPM-ROOT.CONF
GRUB.CONF
GSSAPI_MECH.CONF
HOST.CONF
IDMAPD.CONF
INITLOG.CONF
JWHOIS.CONF
KRB5.CONF
LDAP.CONF
LD.SO.CONF
LFTP.CONF
LIBAUDIT.CONF
LIBUSER.CONF
LOGROTATE.CONF
LTRACE.CONF
MKE2FS.CONF
MODPROBE.CONF
MTOOLS.CONF
MULTIPATH.CONF
NSCD.CONF
NSSWITCH.CONF
NTP.CONF
ODDJOBD.CONF
PAM_SMB.CONF
PRELINK.CONF
READER.CONF
RESOLV.CONF
SCROLLKEEPER.CONF
SESTATUS.CONF
SMARTD.CONF
SYSCTL.CONF
SYSLOG.CONF
TPVMLP.CONF
UPDATEDB.CONF
WARNQUOTA.CONF
YP.CONF
YUM.CONF
[[email protected] ~]#

显示/var目录下一级子目录或文件的总个数。

[[email protected] ~]# ll /var
total 180
-rw-r--r-- 1 root root 0 Sep 25 18:38 1text2x
-rw-r--r-- 1 root root 0 Sep 25 18:38 1text2x2
-rw-r--r-- 1 root root 0 Sep 25 18:38 1text2x-2
drwxr-xr-x 2 root root 4096 Dec 3 2015 account
drwxr-xr-x 10 root root 4096 Dec 3 2015 cache
drwxr-xr-x 2 root root 4096 Feb 23 2012 cvs
drwxr-xr-x 3 root root 4096 Dec 3 2015 db
drwxr-xr-x 3 root root 4096 Dec 3 2015 empty
drwxr-xr-x 2 root root 4096 May 11 2011 games
drwxrwx--T 2 root gdm 4096 Oct 15 22:24 gdm
drwxr-xr-x 23 root root 4096 Dec 3 2015 lib
drwxr-xr-x 2 root root 4096 May 11 2011 local
drwxrwxr-x 6 root lock 4096 Oct 16 08:23 lock
drwxr-xr-x 12 root root 4096 Oct 16 04:02 log
lrwxrwxrwx 1 root root 10 Dec 3 2015 mail -> spool/mail
drwxr-xr-x 2 root root 4096 May 11 2011 nis
drwxr-xr-x 2 root root 4096 May 11 2011 opt
-rw-r--r-- 1 root root 0 Sep 25 18:42 ppasswd
drwxr-xr-x 2 root root 4096 May 11 2011 preserve
drwxr-xr-x 2 root root 4096 Sep 11 2012 racoon
drwxr-xr-x 18 root root 4096 Oct 16 07:25 run
drwxr-xr-x 11 root root 4096 Dec 3 2015 spool
drwxrwxrwt 3 root root 4096 Sep 22 08:03 tmp
drwxr-xr-x 6 root root 4096 Dec 3 2015 www
drwxr-xr-x 3 root root 4096 Dec 3 2015 yp
[[email protected] ~]# ll /var | wc -l
26
[[email protected] ~]#

取出/etc/group文件中第三个字段数值最小的10个组的名字。

[[email protected] ~]# cat /etc/group | sort -n -t ":" -k 3 | head -10 | cut -d ":" -f 1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
[[email protected] ~]#

将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。

[[email protected] ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[[email protected] ~]# cat /tmp/etc/test
cat: /tmp/etc/test: No such file or directory
[[email protected] ~]# cat /tmp/etc.test
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
CentOS release 5.11 (Final)
Kernel \r on an \m
[[email protected] ~]# 请总结描述用户和组管理类命令的使用方法并完成以下练习

创建组distro,其GID为2016;

[[email protected] ~]# groupadd -g 2016 distro

创建用户mandriva, 其ID号为1005;基本组为distro;

[[email protected] ~]# adduser -u 1005 -g distro mandriva

创建用户mageia,其ID号为1100,家目录为/home/linux;

[[email protected] ~]# adduser -u 1100 -d /home/linux mageia

给用户mageia添加密码,密码为mageedu;

[[email protected] ~]# passwd mageia
Changing password for user mageia.
New UNIX password:
BAD PASSWORD: it is based on a dictionary word
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[[email protected] ~]#

删除mandriva,但保留其家目录;

[[email protected] ~]# userdel mandriva

创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

[[email protected] ~]# useradd -u 2002 -g distro -G peguin

修改slackware的默认shell为/bin/tcsh;

[[email protected] ~]# usermod --shell /bash/tcsh slackware

为用户slackware新增附加组admins;

[[email protected] ~]# usermod -G admins slackware

为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;

[[email protected] ~]# passwd -n 3 -x 180 -w 3 slackware
Adjusting aging data for user slackware.
passwd: Success
[[email protected] ~]#

添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;

[[email protected] ~]# useradd -u 3003 -g clouds -G peguin,nova openstack

添加系统用户mysql,要求其shell为/sbin/nologin;

[[email protected] ~]# adduser --shell /sbin/nologin mysql

使用echo命令,非交互式为openstack添加密码。

[[email protected] ~]# echo "openstack" | passwd --stdin openstack
Changing password for user openstack.
passwd: all authentication tokens updated successfully.
[[email protected] ~]#

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: RIA数据SAPAUTIUBAUTUTSBIKEE
分页:12
转载请注明
本文标题:linux筛选数据和用户基本操作
本站链接:http://www.codesec.net/view/483639.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(16)