Linux基本命令

绝对路径:以/为起点的路径。
相对路径:以当前目录为参照物的路径。
[root@localhost ~]# pwd   //查看当前目录
/root
[root@localhost ~]# cd /   //切换目录
[root@localhost /]# cd /root
[root@localhost /]# cd /dev
[root@localhost dev]# ls   //查看当前文件
---------
[root@localhost ~]# cd /etc         //绝对路径进入etc目录
[root@localhost etc]# cd lvm        //以当前文件夹为参照物,相对路径进入lvm
[root@localhost lvm]# cd ..         //退到父目录,及上一级目录
[root@localhost etc]# cd /etc/lvm   //绝对路径
[root@localhost lvm]# cd /etc      //退到上一级目录
[root@localhost etc]# cd lvm        //相对路径
[root@localhost lvm]# cd ..         //退到上一级目录
[root@localhost etc]#
---------
[root@localhost ~]# cd /
[root@localhost /]# cd /etc/lvm
[root@localhost lvm]# ls           //当前目录下查看
archive  backup  cache  lvm.conf  lvmlocal.conf  profile
[root@localhost lvm]# cd /
[root@localhost /]# ls /etc/lvm         //绝对路径查看
archive  backup  cache  lvm.conf  lvmlocal.conf  profile
---------
[root@localhost /]# cd /etc
[root@localhost etc]# ls
[root@localhost etc]# cat redhat-release        //当前目录查看系统版本
CentOS Linux release 7.5.1804 (Core) 
[root@localhost etc]# cd ..
[root@localhost /]# cat /etc/redhat-release        //绝对路径查看系统版本
CentOS Linux release 7.5.1804 (Core) 
[root@localhost /]#

[root@localhost /]# lscpu          //查看CPU信息
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
座:                 1
NUMA 节点:         1
厂商 ID:           AuthenticAMD
CPU 系列:          23
型号:              96
型号名称:        AMD Ryzen 7 4700U with Radeon Graphics


[root@localhost /]# cat /proc/meminfo    //查看内容总大小和空闲情况
MemTotal:        1865284 kB
MemFree:          380436 kB

命令提示符的含义:

~             当前工作文件目录
Root          当前用户
Localhost      主机名
#             当前登录超级管理员登
@            分隔符

临时配置,重启后无配置:
[root@localhost ~]# hostname         //查看主机的全名
localhost.localdomain
[root@localhost ~]# hostname jin          //给主机临时更改名字
[root@localhost ~]# hostname
jin
[root@localhost ~]# cd /root
[root@localhost ~]#
退出终端,重新打开命令行终端
[root@jin ~]# hostname         //退出中端,重新打开命令行,临时名字才生效,命令提示符显示的名字是全名的第一个点的前面的内容
jin
[root@jin ~]# hostname localhost.localdomain
[root@jin ~]#
---------
[root@localhost ~]# ifconfig ens33  192.168.10.10             //给网卡配置临时地址
[root@localhost ~]# ifconfig                //查看当前激活网卡的信息

Less  查询大文件内容,可以分屏显示,按回车可以一行一行查看,

[root@heyulan ~]# less /etc/passwd    //查看Passwd的内容

如:/sbin

[root@heyulan ~]#

前:

[root@heyulan etc]# head -2 /etc/passwd  //查看文档的前两行

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@heyulan etc]# head -n 2 /etc/passwd   //查看文档的前两行

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@heyulan etc]# head /etc/passwd   //默认查看前10行

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

[root@heyulan etc]#

后:

[root@heyulan etc]# tail -n 2 /etc/passwd    //查看文档的后两行

tcpdump:x:72:72::/:/sbin/nologin

shisi:x:1000:1000:shisi:/home/shisi:/bin/bash

[root@heyulan etc]# tail /etc/passwd     //默认查看后10行

radvd:x:75:75:radvd user:/:/sbin/nologin

pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

gnome-initial-setup:x:990:984::/run/gnome-initial-setup/:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

shisi:x:1000:1000:shisi:/home/shisi:/bin/bash

[root@heyulan etc]#

[root@heyulan ~]# ls

anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面

[root@heyulan ~]# mkdir /root/hyl   //用绝对路径创建root目录下hyl目录

[root@heyulan ~]# ls

anaconda-ks.cfg  hyl  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面

[root@heyulan ~]# mkdir jyh    //用相对路径创建root目录下jyh目录

[root@heyulan ~]# ls

anaconda-ks.cfg  hyl  initial-setup-ks.cfg  jyh  公共  模板  视频  图片  文档  下载  音乐  桌面

[root@heyulan ~]#

[root@heyulan ~]# mkdir /root/xhyl /root/xjyh  //批量创建目录

[root@heyulan ~]# ls

anaconda-ks.cfg  hyl  initial-setup-ks.cfg  jyh  xhyl  xjyh  公共  模板  视频  图片  文档  下载  音乐  桌面

[root@heyulan ~]#

[root@heyulan ~]# touch /root/yl.txt  //创建空文档

[root@heyulan ~]# ls

anaconda-ks.cfg  hyl  initial-setup-ks.cfg  jyh  xhyl  xjyh  yl.txt  公共  模板  视频  图片  文档  下载  音乐  桌面

[root@heyulan ~]# touch /root/el.txt /root/xk.txt   //批量创建空文档

[root@heyulan ~]# ls

anaconda-ks.cfg  hyl                   jyh   xjyh    yl.txt  模板  图片  下载  桌面

el.txt           initial-setup-ks.cfg  xhyl  xk.txt  公共    视频  文档  音乐

[root@heyulan ~]#

[root@localhost ~]# nmtui   //配置网络,图形化界面

[root@localhost ~]# ifconfig   //查看配置

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.10.11  netmask 255.255.255.0  broadcast 192.168.10.255

[root@localhost ~]# proweroff    //关机

[root@localhost ~]# reboot      //重启

定时关机:

  Shutdown -h 0   //立马关机

  Shutdown -h 10   //10分钟后关机

  Shutdown -h 22:00   //22点关机

定时重启:

  Shutdown -r 0   //立马重启

  Shutdown -r  10   //10分钟后重启

  Shutdown -r  22:00   //22点重启

[root@localhost ~]# type ls     //确定内部命令和外部命令

ls 是 `ls --color=auto' 的别名

[root@localhost ~]# type cd

cd 是 shell 内嵌

[root@localhost ~]#

[root@localhost ~]# ls -l   //详细显示参数

命令字 [选项]....  [参数1][参数2].....

Ctrl+l:清空

Ctrl+c:删除当前命令行

Ctrl+u:删除至行首

Ctrl+w:往回删除一个单词

[root@localhost ~]# cd ~shisi   //直接打开用户shisi的家目录

[root@localhost shisi]# cd ..

[root@localhost home]# cd ..

[root@localhost /]# cd /root

[root@localhost ~]#

通配符:

-*:任意多个任意字符

-?:单个字符

[root@localhost ~]# ls -d /m*    //以m开头的命令

/media  /mnt

[root@localhost ~]# ls -d /m??   //知道以m开头且该命令有几个字母

/mnt

[root@localhost ~]# ls -d /m????  

/media

[root@localhost ~]#

[root@localhost ~]# cd /dev

[root@localhost dev]# ls /dev/tty0 /dev/tty2   //单个字符,若无则忽略

/dev/tty0  /dev/tty2

[root@localhost dev]# ls /dev/tty[0-5]    //多个字符或连续范围内的一个,若无则忽略

/dev/tty0  /dev/tty1  /dev/tty2  /dev/tty3  /dev/tty4  /dev/tty5

[root@localhost dev]# ls /dev/tty{1,3,5,7,9}  //不连续的多个字符

/dev/tty1  /dev/tty3  /dev/tty5  /dev/tty7  /dev/tty9

[root@localhost dev]#

[root@localhost ~]# hostname

localhost.localdomain

[root@localhost ~]# alias hh='hostname'   //别名:将hostname加别名hh

[root@localhost ~]#

[root@localhost ~]# hh   //hh = hostname

localhost.localdomain

[root@localhost ~]# unalias hh   //删除别名

[root@localhost /]# ls -R /qy   //递归显示

/qy:

ld

/qy/ld:

sx

/qy/ld/sx:

[root@localhost /]# ls -R /qyy

/qyy:

ldd

/qyy/ldd:

sxx

/qyy/ldd/sxx:

[root@localhost /]#

[root@localhost /]# ls

bin   dev  home  lib64  mnt  proc  qyy   run   srv  tmp  var

boot  etc  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# mkdir hyl jyh   //创建hyl,jyh目录

[root@localhost /]# ls

bin   dev  home  jyh  lib64  mnt  proc  qyy   run   srv  tmp  var

boot  etc  hyl   lib  media  opt  qy    root  sbin  sys  usr

[root@localhost /]# mv jyh hyll   //将jyh改名为hyll

[root@localhost /]# ls

bin   dev  home  hyll  lib64  mnt  proc  qyy   run   srv  tmp  var

boot  etc  hyl   lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# mv /hyl /hyll   //将hyl加入hyll目录

[root@localhost /]# ls

bin   dev  home  lib    media  opt   qy   root  sbin  sys  usr

boot  etc  hyll  lib64  mnt    proc  qyy  run   srv   tmp  var

[root@localhost /]# cd hyll

[root@localhost hyll]# ls

hyl

[root@localhost /]# mkdir bak   //创建bak目录

[root@localhost /]# ls

bak  boot  etc   hyll  lib64  mnt  proc  qyy   run   srv  tmp  var

bin  dev   home  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# rm -r hyll   //删除hyll目录(-r 删除时有提示信息)

[root@localhost /]# rm -rf  bak  //直接删除

[root@localhost /]# ls

bin   dev  home  lib64  mnt  proc  qyy   run   srv  tmp  var

boot  etc  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]#

[root@localhost /]# mkdir bak

[root@localhost /]# mkdir bakk

[root@localhost /]# ls

bak   bin   dev  home  lib64  mnt  proc  qyy   run   srv  tmp  var

bakk  boot  etc  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# cd bakk

[root@localhost bakk]# touch yl.txt   //创建yl文件夹

[root@localhost bakk]# cd /

[root@localhost /]# cp /bakk/yl.txt /bak  //将bakk中yl复制到bak

[root@localhost /]# ls

bak   bin   dev  home  lib64  mnt  proc  qyy   run   srv  tmp  var

bakk  boot  etc  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# cd bak

[root@localhost bak]# ls

yl.txt

[root@localhost bak]# cd /

[root@localhost /]# cp -r /bakk /bak  //复制目录,使用-r,最后一个目标

[root@localhost /]# ls

bak   bin   dev  home  lib64  mnt  proc  qyy   run   srv  tmp  var

bakk  boot  etc  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# cd bak

[root@localhost bak]# ls

bakk  yl.txt

[root@localhost bak]#

[root@localhost /]# ls

bak   bin   dev  home  lib64  mnt  proc  qyy   run   srv  tmp  var

bakk  boot  etc  lib   media  opt  qy    root  sbin  sys  usr

[root@localhost /]# cd qy

[root@localhost qy]# touch hyl.txt  //创建文件hyl

[root@localhost qy]# cd /

[root@localhost /]# cp -r /qy/hyl.txt /qyy/yll.txt  //将文件复制到目标后改名

[root@localhost /]# cd qyy

[root@localhost qyy]# ls

ldd  yll.txt

[root@localhost qyy]#

Vim 编辑器有三种模式:命令行模式、插入模式、末行模式

打开文档时,看到的是命令行模式,想编辑文档按“i”键,进入插入模式,进行编辑,编辑完毕,按Esc键,“:wq”保存退出、“q!”强制退出

[root@localhost ~]#

[root@localhost ~]# touch hyl.txt  //创建空文本文档

[root@localhost ~]# cat hyl.txt   

[root@localhost ~]# vim hyl.txt   //使用vim对已存在的文档进行编辑

[root@localhost ~]# cat hyl.txt    //查看hyl文档内容

[root@localhost ~]# vim hh.txt   //创建新文档,并编辑

[root@localhost ~]# cat hh.txt   //查看文档内容

[root@localhost ~]# ls -l /dev/cdrom  //查看cdrom光驱的实际接口类型为sr0 及物理接口为scsi 接口

lrwxrwxrwx. 1 root root 3 9月  25 16:03 /dev/cdrom -> sr0

[root@localhost ~]# mkdir /vcd  //创建挂载点及访问点

[root@localhost ~]# ls

anaconda-ks.cfg  hh.txt  hyl.txt               jyh   模板  图片  下载  桌面

grub2            hyl     initial-setup-ks.cfg  公共  视频  文档  音乐

[root@localhost ~]# ls /vcd  //查看vcd文档

[root@localhost ~]# mount /dev/cdrom /vcd  //访问点设置为vcd

mount: /dev/sr0 写保护,将以只读方式挂载

[root@localhost ~]# umount /vcd  //卸载

[root@localhost ~]# mount /dev/cdrom /vcd

mount: /dev/sr0 写保护,将以只读方式挂载

[root@localhost ~]# ls /vcd

CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7

EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

[root@localhost ~]# cd /vcd

[root@localhost vcd]# umount /vcd  //不能卸载正在使用的文档

umount: /vcd:目标忙。

        (有些情况下通过 lsof(8) 或 fuser(1) 可以

         找到有关使用该设备的进程的有用信息)

[root@localhost vcd]# mount /dev/cdrom /kkk  //同一文件可以挂载到多个文件目录中

mount: /dev/sr0 写保护,将以只读方式挂载

[root@localhost vcd]# ls /kkk

CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7

EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

[root@localhost vcd]#

[root@localhost vcd]# ls /boot  //boot文件原有内容

config-3.10.0-862.el7.x86_64

efi

grub

grub2

initramfs-0-rescue-aacfee034fad43f097b297d3ccdbc3d0.img

initramfs-3.10.0-862.el7.x86_64.img

initramfs-3.10.0-862.el7.x86_64kdump.img

symvers-3.10.0-862.el7.x86_64.gz

System.map-3.10.0-862.el7.x86_64

vmlinuz-0-rescue-aacfee034fad43f097b297d3ccdbc3d0

vmlinuz-3.10.0-862.el7.x86_64

[root@localhost vcd]# mount /dev/cdrom /boot   //挂载

mount: /dev/sr0 写保护,将以只读方式挂载

[root@localhost vcd]# ls /boot   //boot文件原有内容消失,成为挂载内容

CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7

EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

[root@localhost vcd]# umount /boot   //卸载挂载

[root@localhost vcd]# ls /boot   //原有内容恢复

config-3.10.0-862.el7.x86_64

efi

grub

grub2

initramfs-0-rescue-aacfee034fad43f097b297d3ccdbc3d0.img

initramfs-3.10.0-862.el7.x86_64.img

initramfs-3.10.0-862.el7.x86_64kdump.img

symvers-3.10.0-862.el7.x86_64.gz

System.map-3.10.0-862.el7.x86_64

vmlinuz-0-rescue-aacfee034fad43f097b297d3ccdbc3d0

vmlinuz-3.10.0-862.el7.x86_64

[root@localhost vcd]#

重定向:

[root@localhost ~]# ls --help > /ls.txt  //ls--help 输出的内容写入ls.txt 文件中

[root@localhost ~]# cat /ls.txt  //查看

[root@localhost ~]#

[root@localhost ~]# ls --help >> /ls.txt  //ls--help输出的内容追加到ls.txt文件中

[root@localhost ~]# cat /ls.txt  //查看

[root@localhost ~]# echo lll  //重定向内容录入

lll

[root@localhost ~]# echo hyl

hyl

[root@localhost ~]# echo heyulanwomenyiqiquchifanfanba > /hyl.txt

[root@localhost ~]# cat /hyl.txt

heyulanwomenyiqiquchifanfanba

[root@localhost ~]#

[root@localhost ~]# touch /mmm.txt  //与echo同理,录入内容

[root@localhost ~]# vim /mmm.txt

[root@localhost ~]# cat /mmm.txt

womenyiqiquchidongxi

woxihuannihuguoyun

[root@localhost ~]#

管道操作“|”:将前面命令的输出,交到后面的命令处理,作为后面命令的参数

[root@localhost ~]#

[root@localhost ~]# head -1 /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# tail -1 /etc/passwd

shisi:x:1000:1000:shisi:/home/shisi:/bin/bash

[root@localhost ~]# cat -n /etc/passwd  //显示全部内容加序号

     1 root:x:0:0:root:/root:/bin/bash

     2 bin:x:1:1:bin:/bin:/sbin/nologin

     3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

     4 adm:x:3:4:adm:/var/adm:/sbin/nologin

     5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

     6 sync:x:5:0:sync:/sbin:/bin/sync

     7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

     8 halt:x:7:0:halt:/sbin:/sbin/halt

     9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

    10 operator:x:11:0:operator:/root:/sbin/nologin

    11 games:x:12:100:games:/usr/games:/sbin/nologin

    12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

    13 nobody:x:99:99:Nobody:/:/sbin/nologin

    14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

    15 dbus:x:81:81:System message bus:/:/sbin/nologin

    16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin

    17 libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin

    18 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin

    19 colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin

    20 saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin

    21 abrt:x:173:173::/etc/abrt:/sbin/nologin

    22 setroubleshoot:x:995:992::/var/lib/setroubleshoot:/sbin/nologin

    23 rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin

    24 chrony:x:994:991::/var/lib/chrony:/sbin/nologin

    25 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

    26 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

    27 qemu:x:107:107:qemu user:/:/sbin/nologin

    28 unbound:x:993:990:Unbound DNS resolver:/etc/unbound:/sbin/nologin

    29 gluster:x:992:989:GlusterFS daemons:/var/run/gluster:/sbin/nologin

    30 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

    31 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

    32 geoclue:x:991:987:User for geoclue:/var/lib/geoclue:/sbin/nologin

    33 radvd:x:75:75:radvd user:/:/sbin/nologin

    34 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

    35 gdm:x:42:42::/var/lib/gdm:/sbin/nologin

         36gnome-initial-setup:x:990:984::/run/gnome-initial-setup/:/sbin/nologin

    37 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

    38 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin

    39 postfix:x:89:89::/var/spool/postfix:/sbin/nologin

    40 ntp:x:38:38::/etc/ntp:/sbin/nologin

    41 tcpdump:x:72:72::/:/sbin/nologin

    42 shisi:x:1000:1000:shisi:/home/shisi:/bin/bash

[root@localhost ~]# cat -n /etc/passwd | head -12  //前12行加序号

     1 root:x:0:0:root:/root:/bin/bash

     2 bin:x:1:1:bin:/bin:/sbin/nologin

     3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

     4 adm:x:3:4:adm:/var/adm:/sbin/nologin

     5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

     6 sync:x:5:0:sync:/sbin:/bin/sync

     7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

     8 halt:x:7:0:halt:/sbin:/sbin/halt

     9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

    10 operator:x:11:0:operator:/root:/sbin/nologin

    11 games:x:12:100:games:/usr/games:/sbin/nologin

    12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]#

[root@localhost ~]# cat -n /etc/passwd | head -12 |tail -5  //将前12行作为tail -5 的参数,从而显示8-12行

     8 halt:x:7:0:halt:/sbin:/sbin/halt

     9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

    10 operator:x:11:0:operator:/root:/sbin/nologin

    11 games:x:12:100:games:/usr/games:/sbin/nologin

    12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]#

[root@localhost ~]# cat -n /etc/passwd | head -12 |tail -5 |head -2 //显示8-12中的前两行

     8 halt:x:7:0:halt:/sbin:/sbin/halt

     9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

[root@localhost ~]# cat -n /etc/passwd | head -12 |tail -5 |tail -3

    10 operator:x:11:0:operator:/root:/sbin/nologin

    11 games:x:12:100:games:/usr/games:/sbin/nologin

    12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]# cat -n /etc/passwd | head -12 |tail -5 |tail -3 |tail -1

    12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]# cat -n /etc/passwd | head -12 |tail -5 |tail -3 |tail -1 |cat -n

     1     12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]# cat -n /etc/passwd | head -12 |tail -5 |tail -3|cat -n  //排序

     1     10 operator:x:11:0:operator:/root:/sbin/nologin

     2     11 games:x:12:100:games:/usr/games:/sbin/nologin

     3     12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]#

过滤及用户和组

[root@localhost ~]# grep root /etc/passwd  //过滤带root的内容

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# grep ROOT /etc/passwd   //区分大小写

[root@localhost ~]# grep -i ROOT /etc/passwd  //-i 忽略大小写

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# grep -v sbin /etc/passwd   //-v 取反,除sbin的内容

root:x:0:0:root:/root:/bin/bash

shisi:x:1000:1000:shisi:/home/shisi:/bin/bash

[root@localhost ~]#

[root@localhost etc]# grep  ^$ /etc/login.defs  //过滤空格

[root@localhost etc]# grep  ^# /etc/login.defs  //过滤#注释内容

[root@localhost etc]# grep -v  ^# /etc/login.defs  //过滤除#注释内容

[root@localhost etc]# grep -v  ^$ /etc/login.defs  //过滤除空格外内容

[root@localhost etc]# grep -v  ^# /etc/login.defs | grep -v  ^$  

[root@localhost etc]# grep -v  ^$ /etc/login.defs | grep -v  ^#  //过滤出无空格也无注释的内容(“|”管道符号)

[root@localhost etc]# grep -v ^# /etc/login.defs | grep -v ^$ > /logg.txt  //将>前面的内容写入logg.txt文件中(“>”重载)

[root@localhost etc]# cat /logg.txt  //查看logg.txt文件

MAIL_DIR /var/spool/mail

PASS_MAX_DAYS 99999

PASS_MIN_DAYS 0

PASS_MIN_LEN 5

PASS_WARN_AGE 7

UID_MIN                  1000

UID_MAX                 60000

SYS_UID_MIN               201

SYS_UID_MAX               999

GID_MIN                  1000

GID_MAX                 60000

SYS_GID_MIN               201

SYS_GID_MAX               999

CREATE_HOME yes

UMASK           077

USERGROUPS_ENAB yes

ENCRYPT_METHOD SHA512

[root@localhost etc]#

[root@localhost etc]# useradd hyl  //创建新用户hyl

[root@localhost etc]# grep hyl /etc/passwd  //从用户信息文件里显示hyl信息

hyl:x:1001:1001::/home/hyl:/bin/bash

[root@localhost etc]# id hyl   //显示用户基本信息

uid=1001(hyl) gid=1001(hyl) 组=1001(hyl)

[root@localhost ~]# passwd hyl  //给用户hyl设置密码(交互式)

更改用户 hyl 的密码 。

新的 密码:666

无效的密码: 密码是一个回文

重新输入新的 密码:666

passwd:所有的身份验证令牌已经成功更新。

[root@localhost ~]# su - hyl  //切换到hyl用户

[hyl@localhost ~]$ passwd   //设置密码

更改用户 hyl 的密码 。

为 hyl 更改 STRESS 密码。

(当前)UNIX 密码:

新的 密码:

[hyl@localhost ~]$ exit 

登出

[root@localhost ~]#

[root@localhost ~]# useradd xq  //创建新用户xq

[root@localhost ~]# echo 123 | passwd --stdin xq  //直接设置密码123(非交换机密码)

更改用户 xq 的密码 。

passwd:所有的身份验证令牌已经成功更新。

[root@localhost ~]#

[root@localhost ~]# tail /etc/passwd

avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

shisi:x:1000:1000:shisi:/home/shisi:/bin/bash

hyl:x:1001:1001::/home/hyl:/bin/bash

xq:x:1002:1002::/home/xq:/bin/bash

hh:x:1003:1003::/home/hh:/bin/bash

yy:x:1004:1004::/home/yy:/bin/bash

ll:x:1005:1005::/home/ll:/bin/bash

[root@localhost ~]# usermod -u 1100 ll  //修改ll 的uid

[root@localhost ~]# tail /etc/passwd  //查看

xq:x:1002:1002::/home/xq:/bin/bash

hh:x:1003:1003::/home/hh:/bin/bash

yy:x:1004:1004::/home/yy:/bin/bash

ll:x:1100:1005::/home/ll:/bin/bash

[root@localhost ~]# tail -1 /etc/passwd

ll:x:1100:1005::/home/ll:/bin/bash

[root@localhost ~]# usermod -s /sbin/nologin ll  //禁止用户ll 登录服务器

[root@localhost ~]# tail -1 /etc/passwd

ll:x:1100:1005::/home/ll:/sbin/nologin

[root@localhost ~]# cat /etc/shells   //查看解释器信息

/bin/sh

/bin/bash

[root@localhost ~]# ls /home

hh  hyl  ll  shisi  xq  yy

[root@localhost ~]# userdel yy  //删除用户

[root@localhost ~]# ls /home   //没有删除yy的家目录

hh  hyl  ll  shisi  xq  yy

[root@localhost ~]# tail /etc/passwd  //yy用户已删除

[root@localhost ~]# userdel -r hh  //删除用户的同时删除家目录(一般不常用)

[root@localhost ~]# ls /home  //hh用户家目录被删除

hyl  ll  shisi  xq  yy

[root@localhost ~]# tail /etc/passwd  //hh用户已删除

[root@localhost ~]#

[root@localhost ~]# groupadd mm  //创建新组mm

[root@localhost ~]# groupadd nn

[root@localhost ~]# cat /etc/group  //查看组信息

shisi:x:1000:shisi

hyl:x:1001:

xq:x:1002:

ll:x:1005:

xhyl:x:1101:

mm:x:1102:

nn:x:1103:

[root@localhost ~]# id xhyl

uid=1101(xhyl) gid=1101(xhyl) 组=1101(xhyl)

[root@localhost ~]#

[root@localhost ~]# tail -1 /etc/passwd

各部分解释:

xhyl:x:1101:1101::/home/xhyl:/bin/bash

用户名。密码占位符。Uid。Gid。用户解释符。家目录。解释器

[root@localhost ~]#

[root@localhost ~]# tail -1 /etc/passwd

xhyl:x:1101:1101::/home/xhyl:/bin/bash

[root@localhost ~]# id xhyl

uid=1101(xhyl) gid=1101(xhyl) 组=1101(xhyl)

[root@localhost ~]#

[root@localhost ~]#

[root@localhost ~]#

[root@localhost ~]# gpasswd -a xhyl mm

正在将用户“xhyl”加入到“mm”组中

[root@localhost ~]# id xhyl   //查看用户(用户uid、基本组gid、附属组mm

uid=1101(xhyl) gid=1101(xhyl) 组=1101(xhyl),1102(mm)

[root@localhost ~]# tail /etc/group  //查看组信息

postfix:x:89:

ntp:x:38:

tcpdump:x:72:

shisi:x:1000:shisi

hyl:x:1001:

xq:x:1002:

ll:x:1005:

xhyl:x:1101:

mm:x:1102:xhyl  (各部分含义:组名、组的密码占位符、组id、组的成员列表)

nn:x:1103:

[root@localhost ~]#

[root@localhost ~]# gpasswd -a hyl mm

正在将用户“hyl”加入到“mm”组中

[root@localhost ~]# tail /etc/group

postfix:x:89:

ntp:x:38:

tcpdump:x:72:

shisi:x:1000:shisi

hyl:x:1001:

xq:x:1002:

ll:x:1005:

xhyl:x:1101:

mm:x:1102:xhyl,hyl

nn:x:1103:

[root@localhost ~]# gpasswd -b hyl mm

gpasswd:无效选项 -- b

用法:gpasswd [选项] 组

选项:

  -a, --add USER                向组 GROUP 中添加用户 USER

  -d, --delete USER             从组 GROUP 中添加或删除用户

  -h, --help                    显示此帮助信息并推出

  -Q, --root CHROOT_DIR         要 chroot 进的目录

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

  -R, --restrict                向其成员限制访问组 GROUP

  -M, --members USER,...        设置组 GROUP 的成员列表

  -A, --administrators ADMIN,... 设置组的管理员列表

除非使用 -A 或 -M 选项,不能结合使用这些选项。

[root@localhost ~]# gpasswd -d hyl mm  //删除

正在将用户“hyl”从“mm”组中删除

[root@localhost ~]# tail /etc/group

postfix:x:89:

ntp:x:38:

tcpdump:x:72:

shisi:x:1000:shisi

hyl:x:1001:

xq:x:1002:

ll:x:1005:

xhyl:x:1101:

mm:x:1102:xhyl

nn:x:1103:

[root@localhost ~]#

解压tar 及用户权限管理

[root@localhost ~]# mkdir /ldxy   //创建文档ldxy

[root@localhost ~]# ls /

bak   bin   dev  home     kkk   lib    logg.txt  media    mnt  proc  qyy   run   srv  tmp  var

bakk  boot  etc  hyl.txt  ldxy  lib64  ls.txt    mmm.txt  opt  qy    root  sbin  sys  usr  vcd

[root@localhost ~]# tar -zcf /ldxy/wlgc21.tar.gz  /home /root  //将home目录和root目录压缩为ldxy目录下的wlgc21.tar.gz 文件

tar: 从成员名中删除开头的“/”

[root@localhost ~]# ls /ldxy

wlgc21.tar.gz

[root@localhost ~]#

不同的压缩格式,有不同的后缀和 -J、-j、-z

[root@localhost ~]# mkdir /zzz

[root@localhost ~]# tar -Jcf /zzz/xx.tar.xz /home /root

tar: 从成员名中删除开头的“/”

[root@localhost ~]# ls /zzz

xx.tar.xz

[root@localhost ~]# tar -jcf /zzz/lll.tar.bz2 /home /root

tar: 从成员名中删除开头的“/”

[root@localhost ~]# ls /zzz

lll.tar.bz2  xx.tar.xz

[root@localhost ~]# tar -zcf /zzz/wlgc21.tar.gz  /home /root

tar: 从成员名中删除开头的“/”

[root@localhost ~]# ls /zzz

lll.tar.bz2  wlgc21.tar.gz  xx.tar.xz

[root@localhost ~]#

[root@localhost ~]# tar -tf /zzz/xx.tar.xz  //查看压缩包清单列表

[root@localhost ~]# mkdir /sss

[root@localhost ~]# tar -xf /zzz/lll.tar.bz2 -C /sss   //解压zzz目录中lll到sss目录

[root@localhost ~]# ls /sss

home  root

[root@localhost ~]# mkdir /qqq

[root@localhost ~]# tar -xf /zzz/mldehyl.tar.xz -C /qqq

[root@localhost ~]# ls /qqq

root

[root@localhost ~]#

[root@localhost ~]# ls -ld /qqq  //查看目录详细信息 (权限)

drwxr-xr-x. 3 root root 39 10月 13 16:14 /qqq  //文件类型d:目录文件、l:快捷方式、-:文本文件;2-10位:设置权限;第一个root:拥有者;第二个root:所属组;39:文件大小;10月-14:时间;/qqq:根下的qqq目录

[root@localhost ~]#

[root@localhost ~]# ls -lh /hyl.txt   //查看文件详细属性信息(权限)

-rw-r--r--. 1 root root 30 10月 10 17:38 /hyl.txt

[root@localhost ~]#

[root@localhost ~]# ls -ld /qqq

drwxr-xr-x. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod u-w /qqq   //拥有者u部分删除w权限

[root@localhost ~]# ls -ld /qqq

dr-xr-xr-x. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod g+w /qqq  //所属组g部分加w权限

[root@localhost ~]# ls -ld /qqq

dr-xrwxr-x. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod o-x /qqq   //其他人o部分删除x权限

[root@localhost ~]# ls -ld /qqq

dr-xrwxr--. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]#

[root@localhost ~]# ls -ld /qqq

d-w-------. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod u+x,g+x,o+x /qqq

[root@localhost ~]# ls -ld /qqq

d-wx--x--x. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod o+r+w /qqq

[root@localhost ~]# ls -ld /qqq

d-wx--xrwx. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]#

r对应4

w对应2

x对应1

[root@localhost ~]#

[root@localhost ~]# chmod u=rw,g=rw,o=rw /qqq

[root@localhost ~]# chmod ugo=rw /qqq

[root@localhost ~]# ls -ld /qqq

drw-rw-rw-. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod 444 /qqq  //444:u=r=4;g=r=4;o=r=4

[root@localhost ~]# ls -ld /qqq

dr--r--r--. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod 333 /qqq  //333:u=wx=2+1;g=wx=2+1;o=wx=2+1

[root@localhost ~]# ls -ld /qqq

d-wx-wx-wx. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]#

[root@localhost ~]# chmod 347 /qqq   //347:u=wx=2+1;g=r=4;o=rwx=4+2+1;

[root@localhost ~]# ls -ld /qqq

d-wxr--rwx. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]#

[root@localhost ~]# chmod u+w,o+w+x /qqq

[root@localhost ~]# ls -ld /qqq

drwxrwxrwx. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod 777 /qqq

[root@localhost ~]# ls -ld /qqq

drwxrwxrwx. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]# chmod ugo=rwx /qqq  //直接赋予权限

[root@localhost ~]# ls -ld /qqq

drwxrwxrwx. 3 root root 39 10月 13 16:14 /qqq

[root@localhost ~]#

[root@localhost ~]#

[root@localhost ~]# mkdir /110   //创建目录

[root@localhost ~]# touch /222.txt   //创建文件

[root@localhost ~]# chmod ugo=rw /222.txt   //更改文件权限

[root@localhost ~]# ls -lh /222.txt

-rw-rw-rw-. 1 root root 0 10月 14 10:44 /222.txt

[root@localhost ~]# touch /shisi.txt

[root@localhost ~]# chmod 777 /shisi.txt

[root@localhost ~]# ls -lh /shisi.txt

-rwxrwxrwx. 1 root root 0 10月 14 10:45 /shisi.txt

[root@localhost ~]#

修改文件、目录、属主/属组

chown  [选项]  属主    文件或目录

chown  [选项]  :属组    文件或目录

chown  [选项]  属主:属组    文件或目录

[root@localhost ~]# groupadd ccnn  //创建组ccnn

[root@localhost ~]# ls -ld /110  //查看110目录

drwxr-xr-x. 2 root root 6 10月 14 10:43 /110

[root@localhost ~]# chown shisi /110   //修改110目录属主

[root@localhost ~]# ls -ld /110

drwxr-xr-x. 2 shisi root 6 10月 14 10:43 /110

[root@localhost ~]# chown :ccnn /110   //修改110目录属组

[root@localhost ~]# ls -ld /110

drwxr-xr-x. 2 shisi ccnn 6 10月 14 10:43 /110

[root@localhost ~]# mkdir /119  //创建组119

[root@localhost ~]# useradd hyl   //创建新用户hyl

[root@localhost ~]# chown hyl:ccnn /119   //修改属主:属组

[root@localhost ~]# ls -ld /119

drwxr-xr-x. 2 hyl ccnn 6 10月 14 11:01 /119

[root@localhost ~]#

[root@localhost ~]# mkdir /118

[root@localhost ~]# ls -ld /118

drwxr-xr-x. 2 root root 6 10月 14 11:24 /118

[root@localhost ~]# gpasswd -a shisi root   //shisi加入到root

正在将用户“shisi”加入到“root”组中

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

root:x:0:shisi

[root@localhost ~]# cd /118

[root@localhost 118]# mkdir xixixi

[root@localhost 118]# touch hahaha.txt

[root@localhost 118]# cd

[root@localhost ~]# su - shisi  //切换到shisi用户

上一次登录:日 9月 24 00:21:53 CST 2023:0 上

[shisi@localhost ~]$ cd /118   //进入118目录

[shisi@localhost 118]$ touch ll.txt   //不能创建文件和目录

touch: 无法创建"ll.txt": 权限不够

[shisi@localhost 118]$ exit

登出

[root@localhost ~]# ls -ld /118

drwxr-xr-x. 3 root root 38 10月 14 11:29 /118

[root@localhost ~]# chmod g+w /118   //给属组加w(写)权限

[root@localhost ~]# ls -ld /118

drwxrwxr-x. 3 root root 38 10月 14 11:29 /118

[root@localhost ~]# su - shisi   //切换到shisi用户

上一次登录:六 10月 14 11:29:56 CST 2023pts/1 上

[shisi@localhost ~]$ cd /118   //进入118目录

[shisi@localhost 118]$ touch ll.txt   //可以创建目录和文件

[shisi@localhost 118]$

[root@localhost ~]# useradd lisi

[root@localhost ~]# useradd hyl

[root@localhost ~]# mkdir /aaa

[root@localhost ~]# groupadd aaa

[root@localhost ~]# gpasswd -a lisi aaa

正在将用户“lisi”加入到“aaa”组中

[root@localhost ~]# gpasswd -a hyl aaa

正在将用户“hyl”加入到“aaa”组中

[root@localhost ~]# ls -ld /aaa   //属组有r,w,x,权限

drwxrwxr-x. 2 root root 6 10月 14 11:49 /aaa

[root@localhost ~]# chmod o=--- /aaa

[root@localhost ~]# chown :aaa /aaa

[root@localhost ~]# ls -ld /aaa   //其他人没有r,w,x,权限

drwxrwx---. 2 root aaa 6 10月 14 11:49 /aaa

[root@localhost ~]#

[root@localhost ~]# su - xq

上一次登录:三 10月 11 09:49:22 CST 2023pts/1 上

[xq@localhost ~]$ cd /aaa

-bash: cd: /aaa: 权限不够

[xq@localhost ~]$ exit

登出

[root@localhost ~]# su - lisi

[lisi@localhost ~]$ cd /aaa

[lisi@localhost aaa]$ touch nmnm.txt   //创建文件成功

[lisi@localhost aaa]$

[lisi@localhost aaa]$ mkdir /mbmb

mkdir: 无法创建目录"/mbmb": 权限不够

[lisi@localhost aaa]$ exit

登出

[root@localhost ~]#

[root@localhost ~]# useradd lz   //创建新用户lz

[root@localhost ~]# setfacl -m u:lz:rx /aaa   //用acl给lz单独赋予权限r x (读和执行)

[root@localhost ~]# ls -ld /aaa

drwxrwx---+ 2 root aaa 22 10月 14 14:52 /aaa

[root@localhost ~]# getfacl /aaa   //查看用acl赋予权限的目录aaa

getfacl: Removing leading '/' from absolute path names

# file: aaa

# owner: root

# group: aaa

user::rwx

user:lz:r-x

group::rwx

mask::rwx

other::---

[root@localhost ~]# su - lz

[lz@localhost ~]$ cd /aaa   //lz有执行权限

[lz@localhost aaa]$

特殊权限:

[root@localhost ~]# mkdir /fff

[root@localhost ~]# ls -ld /fff

drwxr-xr-x. 2 root root 6 10月 14 15:43 /fff

[root@localhost ~]# groupadd jyz

[root@localhost ~]# ls /

110  222.txt  bakk  caiwu   etc   hyl.txt  lib       ls.txt  mmm.txt  proc  qyy   sbin       sss  usr  xxx

118  aaa      bin   caiwub  fff   kkk      lib64     media   mnt      qqq   root  shisi.txt  sys  var  zzz

119  bak      boot  dev     home  ldxy     logg.txt  mkm     opt      qy    run   srv        tmp  vcd

[root@localhost ~]# chown :jyz /fff   //目录组改为jyz

[root@localhost ~]# ls -ld /fff

drwxr-xr-x. 2 root jyz 6 10月 14 15:43 /fff

[root@localhost ~]# cd /fff

[root@localhost fff]# mkdir /666

[root@localhost fff]# ls -ld /666    //在fff下创建的目录没有继承修改的属组jyz

drwxr-xr-x. 2 root root 6 10月 14 15:48 /666

[root@localhost fff]# cd

[root@localhost ~]# chmod g+s /fff    //g+s:属组添加特殊权限 s--继承

[root@localhost ~]# ls -ld /fff

drwxr-sr-x. 2 root jyz 6 10月 14 15:43 /fff

[root@localhost ~]# mkdir /fff/dddd   //在fff目录下新创建文件

[root@localhost ~]# ls -ld /fff/dddd

drwxr-sr-x. 2 root jyz 6 10月 14 15:56 /fff/dddd

[root@localhost ~]# cd /fff

[root@localhost fff]# ls -ld /fff/dddd   //继承了属组jyz

drwxr-sr-x. 2 root jyz 6 10月 14 15:56 /fff/dddd

[root@localhost fff]# touch dddd.txt   //在fff下创建新文件

[root@localhost fff]# ls

dddd  dddd.txt

[root@localhost fff]# ls -lh /fff/dddd.txt   //继承了属组jyz

-rw-r--r--. 1 root jyz 0 10月 14 15:57 /fff/dddd.txt

[root@localhost fff]# ls -ld /fff/dddd.txt

-rw-r--r--. 1 root jyz 0 10月 14 15:57 /fff/dddd.txt

[root@localhost fff]#

[root@localhost /]# touch /gly/jl1.txt

[root@localhost /]# ls -l /gly/jl1.txt

-rw-r--r--. 1 root root 0 10月 16 09:11 /gly/jl1.txt

[root@localhost /]# chmod g+s /gly  //加特殊权限

[root@localhost /]# ls -ld /gly  //继承,cw

drwxrwsr-x. 2 root cw 21 10月 16 09:11 /gly

[root@localhost /]# touch /gly/jl2.txt

[root@localhost /]# ls -l /gly/jl2.txt

-rw-r--r--. 1 root cw 0 10月 16 09:22 /gly/jl2.txt

[root@localhost /]#

[root@localhost /]# chmod g-x /gly   //属组没有x执行权限,特殊权限用大S表示

[root@localhost /]# ls -ld /gly

drwxrwSr-x. 2 root cw 36 10月 16 09:22 /gly

[root@localhost /]# chmod g+x /gly   //属组有x执行权限,特殊权限用小s表示

[root@localhost /]# ls -ld /gly

drwxrwsr-x. 2 root cw 36 10月 16 09:22 /gly

实现用户laoxi ,访问/etc/shadow 文件,能想出几种方法?

1、修改其他人权限:

[root@localhost ~]# ls -l /etc/shadow

----------. 1 root root 2058 10月 16 18:18 /etc/shadow

[root@localhost ~]# id root

uid=0(root) gid=0(root) 组=0(root)

[root@localhost ~]# useradd laoxi

[root@localhost ~]# chmod o=rxw /etc/shadow

[root@localhost ~]# ls -l /etc/shadow

-------rwx. 1 root root 2086 10月 17 16:57 /etc/shadow

2、修改属组为laoxi

[root@localhost ~]# chown :laoxi /etc/shadow

[root@localhost ~]# ls -l /etc/shadow

-------rwx. 1 root laoxi 2086 10月 17 16:57 /etc/shadow

3、修改属主为laoxi(拥有者)

[root@localhost ~]#

[root@localhost ~]# chown laoxi /etc/shadow

[root@localhost ~]# ls -l /etc/shadow

-------rwx. 1 laoxi laoxi 2086 10月 17 16:57 /etc/shadow

4、acl 权限

[root@localhost ~]#

[root@localhost ~]# setfacl -m u:laoxi:rwx /etc/shadow

[root@localhost ~]# getfacl /etc/shadow

getfacl: Removing leading '/' from absolute path names

# file: etc/shadow

# owner: laoxi

# group: laoxi

user::---

user:laoxi:rwx

group::---

mask::rwx

other::rwx

[root@localhost ~]#

[root@localhost ~]#

[root@localhost ~]# mkdir /eee

[root@localhost ~]# /usr/bin/mkdir eeee   //绝对路径方式执行命令

[root@localhost ~]# ls /root

anaconda-ks.cfg  caiwu.txt  grub2   hyl.txt               laozong  ldxy3.txt  视频  下载

backup.tar.gz    eeee       hh.txt  initial-setup-ks.cfg  ldxy2    公共       图片  音乐

caiwu            gly        hyl     jyh                   ldxy3    模板       文档  桌面

[root@localhost ~]# useradd yljj   //创建新用户

[root@localhost ~]# cp /usr/bin/mkdir /usr/bin/yldir  //将执行命令做个复制备份重命名yldir

[root@localhost ~]# /usr/bin/yldir deee  //复制命令和原命令功能一样

[root@localhost ~]# ls /root

anaconda-ks.cfg  caiwu.txt  gly     hyl                   jyh      ldxy3      模板  文档  桌面

backup.tar.gz    deee       grub2   hyl.txt               laozong  ldxy3.txt  视频  下载

caiwu            eeee       hh.txt  initial-setup-ks.cfg  ldxy2    公共       图片  音乐

[root@localhost ~]# chmod u+s /usr/bin/yldir  //将复制命令赋予uid特殊命令

[root@localhost ~]# ls -l /usr/bin/yldir   //查看

-rwsr-xr-x. 1 root root 79760 10月 17 17:56 /usr/bin/yldir

[root@localhost ~]# su - yljj  //切换普通用户

[yljj@localhost ~]$ /usr/bin/mkdir xqx   //正常创建目录,拥有者为普通用户yljj

[yljj@localhost ~]$ pwd

/home/yljj

[yljj@localhost ~]$ ls -ld /home/yljj/xqx

drwxrwxr-x. 2 yljj yljj 6 10月 17 17:59 /home/yljj/xqx

[yljj@localhost ~]$ /usr/bin/yldir /xwx  //使用赋予特殊权限的命令创建目录,拥有者为管理员root,实现特权

[yljj@localhost ~]$ ls -ld /xwx

drwxrwxr-x. 2 root yljj 6 10月 17 18:00 /xwx

[yljj@localhost ~]$

安装软件

[root@localhost ~]# mkdir /dvd

[root@localhost ~]# ls /dvd

[root@localhost ~]# mount /dev/cdrom /dvd   //将dev/cdrom 挂载到dvd

mount: /dev/sr0 写保护,将以只读方式挂载

[root@localhost ~]# ls /dvd

CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7

EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

[root@localhost ~]#

[root@localhost ~]# ls /dvd/Packages   //Linux软件包都是以 .rpm 结尾

 [root@localhost ~]# rpm -q vsftpd

未安装软件包 vsftpd

[root@localhost ~]# rpm-ivh/dvd/Packages/vsftpd-3.0.2-22.el7.x86_64.rpm   //安装软件包

警告:/dvd/Packages/vsftpd-3.0.2-22.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY

准备中...                          ################################# [100%]

正在升级/安装...

   1:vsftpd-3.0.2-22.el7              ################################# [100%]

[root@localhost ~]# rpm -q vsftpd    //查看是否安装软件包

vsftpd-3.0.2-22.el7.x86_64

[root@localhost ~]#

[root@localhost ~]#

[root@localhost ~]# rpm -e vsftpd   //卸载安装包,-e

[root@localhost ~]# rpm -q vsftpd

未安装软件包 vsftpd         

[root@localhost ~]# rpm -q bind

未安装软件包 bind

[root@localhost ~]# rpm -i /dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm  安装软件包

警告:/dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY

[root@localhost ~]# rpm -ivh /dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm

警告:/dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY

准备中...                          ################################# [100%]

软件包 bind-32:9.9.4-61.el7.x86_64 已经安装

[root@localhost ~]# rpm -ih /dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm

警告:/dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY

################################# [100%]

软件包 bind-32:9.9.4-61.el7.x86_64 已经安装

[root@localhost ~]# rpm -iv /dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm

警告:/dvd/Packages/bind-9.9.4-61.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY

软件包准备中...

软件包 bind-32:9.9.4-61.el7.x86_64 已经安装

[root@localhost ~]#

磁盘及磁盘分区

[root@localhost ~]# lsblk   //查看添加的磁盘

NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda               8:0    0   20G  0 disk

├─sda1            8:1    0    1G  0 part /boot

└─sda2            8:2    0   19G  0 part

  ├─centos-root 253:0    0   17G  0 lvm  /

  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]

sdb               8:16   0   20G  0 disk

sr0              11:0    1  8.8G  0 rom  /run/media/root/CentOS 7 x86_64

[root@localhost ~]#

[root@localhost ~]# fdisk /dev/sdb    //将sdb进行分区

欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。

使用写入命令前请三思。

Device does not contain a recognized partition table

使用磁盘标识符 0xde6ad7b8 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):m

命令操作

   n   add a new partition  //创建分区

   q   quit without saving changes   //退出(不保存)

   w   write table to disk and exit    //保存当前操作

   d    //删除分区     

命令(输入 m 获取帮助):n

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p):

Using default response p

分区号 (1-4,默认 1):

起始 扇区 (2048-41943039,默认为 2048):

将使用默认值 2048

Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+2G

分区 1 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区

   设备 Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048     4196351     2097152   83  Linux

/dev/sdb2         4196352     6293503     1048576   83  Linux

命令(输入 m 获取帮助):w

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁盘。

[root@localhost ~]# lsblk   //查看新添加的磁盘信息

NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda               8:0    0   20G  0 disk

├─sda1            8:1    0    1G  0 part /boot

└─sda2            8:2    0   19G  0 part

  ├─centos-root 253:0    0   17G  0 lvm  /

  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]

sdb               8:16   0   20G  0 disk

├─sdb1            8:17   0    2G  0 part

├─sdb2            8:18   0    1G  0 part

└─sdb3            8:19   0    2G  0 part

sr0              11:0    1  8.8G  0 rom  /run/media/root/CentOS 7 x86_64

[root@localhost ~]#

[root@localhost ~]# fdisk /dev/sdb   //添加分区

欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。

使用写入命令前请三思。

命令(输入 m 获取帮助):n

Partition type:

   p   primary (3 primary, 0 extended, 1 free)

   e   extended

Select (default e): p

已选择分区 4

起始 扇区 (10487808-41943039,默认为 10487808):

将使用默认值 10487808

Last 扇区, +扇区 or +size{K,M,G} (10487808-41943039,默认为 41943039):+1G

分区 4 已设置为 Linux 类型,大小设为 1 GiB

命令(输入 m 获取帮助):p   //查看

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理):512 字节 / 512 字节

I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘标签类型:dos

磁盘标识符:0xde6ad7b8

   设备 Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048     4196351     2097152   83  Linux

/dev/sdb2         4196352     6293503     1048576   83  Linux

/dev/sdb3         6293504    10487807     2097152   83  Linux

/dev/sdb4        10487808    12584959     1048576   83  Linux

命令(输入 m 获取帮助):d    //删除

分区号 (1-4,默认 4):

分区 4 已删除

命令(输入 m 获取帮助):n

Partition type:

   p   primary (3 primary, 0 extended, 1 free)

   e   extended

Select (default e):

Using default response e

已选择分区 4

起始 扇区 (10487808-41943039,默认为 10487808):

将使用默认值 10487808

Last 扇区, +扇区 or +size{K,M,G} (10487808-41943039,默认为 41943039):

将使用默认值 41943039

分区 4 已设置为 Extended 类型,大小设为 15 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理):512 字节 / 512 字节

I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘标签类型:dos

磁盘标识符:0xde6ad7b8

   设备 Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048     4196351     2097152   83  Linux

/dev/sdb2         4196352     6293503     1048576   83  Linux

/dev/sdb3         6293504    10487807     2097152   83  Linux           //主分区

/dev/sdb4        10487808    41943039    15727616    5  Extended   //扩展分区

/dev/sdb5        10489856    12587007     1048576   83  Linux

/dev/sdb6        12589056    14686207     1048576   83  Linux

/dev/sdb7        14688256    16785407     1048576   83  Linux        //逻辑分区

命令(输入 m 获取帮助):w

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁盘。

[root@localhost ~]#

命令(输入 m 获取帮助):n   //创建新分区

Partition type:

   p   primary (1 primary, 1 extended, 2 free)  //主分区

   l   logical (numbered from 5)   //逻辑分区

Select (default p): l   //创建逻辑分区选“l”  ;创建主分区选“p”

添加逻辑分区 6

[root@localhost ~]#

[root@localhost ~]# mkfs.ext4 /dev/sdd1   //设置sdd1分区的文件系统为ext4

[root@localhost ~]# blkid  //查看文件系统

/dev/sda1: UUID="d065f535-39b7-4a88-8915-23053d174cd6" TYPE="xfs"

/dev/mapper/centos-root: UUID="7cb0552d-a3d1-4419-8b77-088f185e5d57" TYPE="xfs"

/dev/mapper/centos-swap: UUID="0937fac0-7291-4ecd-8904-f4d3df880d9c" TYPE="swap"

[root@localhost ~]# mkfs.xfs /dev/sdd2   //设置sdd2分区的文件系统为xfs

mkfs.xfs: /dev/sdd2 appears to contain a partition table (dos).

mkfs.xfs: Use the -f option to force overwrite.

[root@localhost ~]# mkfs.xfs -f /dev/sdd2   //设置sdd2分区的文件系统为xfs

meta-data=/dev/sdd2              isize=512    agcount=4, agsize=131072 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0, sparse=0

data     =                       bsize=4096   blocks=524288, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal log           bsize=4096   blocks=2560, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@localhost ~]# blkid  //查看

/dev/sda1: UUID="d065f535-39b7-4a88-8915-23053d174cd6" TYPE="xfs"

/dev/sda2: UUID="6IcmGo-jv7C-1rei-h5mC-27Uw-j01b-TXUqvL" TYPE="LVM2_member"

/dev/sdd1: UUID="8e880bce-2fab-4258-b322-9d7481cf210c" TYPE="ext4"

/dev/sr0: UUID="2018-05-07-12-53-47-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"

/dev/mapper/centos-root: UUID="7cb0552d-a3d1-4419-8b77-088f185e5d57" TYPE="xfs"

/dev/mapper/centos-swap: UUID="0937fac0-7291-4ecd-8904-f4d3df880d9c" TYPE="swap"

/dev/sdc2: PTTYPE="dos"

/dev/sdc5: PTTYPE="dos"

/dev/sdd2: UUID="2cfb9378-e32e-4d64-bbbd-7025b227280c" TYPE="xfs" 

[root@localhost ~]# mkfs.xfs -f /dev/sdd2  //-f 强制

[root@localhost ~]#

[root@localhost ~]# mkdir /pt1   //创建pt1目录,作为挂载点

[root@localhost ~]# mount /dev/sdd1 /pt1  //将sdd1分区挂载到pt1目录下

[root@localhost ~]# ls /pt1

lost+found

[root@localhost ~]# touch /pt1/pt1.txt   //通过挂载点创建文件存储在分区下

[root@localhost ~]# ls /pt1

lost+found  pt1.txt

[root@localhost ~]# mkdir /pt2  //创建pt2 目录,挂载点

[root@localhost ~]# mount /dev/sdd2 /pt2  //将sdd2分区挂载到pt2目录下

[root@localhost ~]# ls /pt2

[root@localhost ~]# touch /pt2/pt2.txt   //通过挂载点创建文件存储在分区下

[root@localhost ~]# ls /pt2

pt2.txt

[root@localhost ~]# df -h  //查看分区是否挂载成功

文件系统                 容量  已用  可用 已用% 挂载点

/dev/mapper/centos-root   17G  3.8G   14G   23% /

devtmpfs                 895M     0  895M    0% /dev

tmpfs                    911M     0  911M    0% /dev/shm

tmpfs                    911M   11M  901M    2% /run

tmpfs                    911M     0  911M    0% /sys/fs/cgroup

/dev/sda1               1014M  170M  845M   17% /boot

tmpfs                    183M   20K  183M    1% /run/user/0

/dev/sr0                 8.8G  8.8G     0  100% /run/media/root/CentOS 7 x86_64

/dev/sdd1                2.0G  6.0M  1.8G    1% /pt1

/dev/sdd2                2.0G   33M  2.0G    2% /pt2

[root@localhost ~]#

挂载、卷

[root@localhost ~]# lvextend -L 12G /dev/wl01/wl01lv   //扩展逻辑卷空间

  Size of logical volume wl01/wl01lv changed from 10.00 GiB (2560 extents) to 12.00 GiB (3072 extents).

  Logical volume wl01/wl01lv successfully resized.

[root@localhost ~]# lvs

  LV     VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  root   centos   -wi-ao---- <17.00g                                                    

  swap   centos   -wi-ao----   2.00g                                                    

  wl01lv wl01     -wi-ao----  12.00g                                                    

  mylv   wlgcvg12 -wi-ao----   7.00g                                                    

[root@localhost ~]# df -h   //查看格式化之后文件大小

文件系统                   容量  已用  可用 已用% 挂载点

/dev/mapper/centos-root     17G  3.8G   14G   23% /

devtmpfs                   895M     0  895M    0% /dev

tmpfs                      911M     0  911M    0% /dev/shm

tmpfs                      911M   11M  901M    2% /run

tmpfs                      911M     0  911M    0% /sys/fs/cgroup

/dev/sda1                 1014M  170M  845M   17% /boot

/dev/mapper/wlgcvg12-mylv  7.0G   33M  7.0G    1% /lv

/dev/mapper/wl01-wl01lv     10G   33M   10G    1% /hyl1

tmpfs                      183M  4.0K  183M    1% /run/user/42

tmpfs                      183M   20K  183M    1% /run/user/0

/dev/sr0                   8.8G  8.8G     0  100% /run/media/root/CentOS 7 x86_64

[root@localhost ~]#   

[root@localhost ~]# vgextend wl01 /dev/sdc3   //扩展卷组大小,+sdc3

  Physical volume "/dev/sdc3" successfully created.

  Volume group "wl01" successfully extended

[root@localhost ~]# vgs

  VG       #PV #LV #SN Attr   VSize   VFree

  centos     1   2   0 wz--n- <19.00g     0

  wl01       3   1   0 wz--n- <18.99g <6.99g

  wlgcvg12   2   1   0 wz--n-   9.99g  2.99g

[root@localhost ~]# lvextend -L 18G /dev/wl01/wl01lv   //扩展逻辑卷至18G

  Size of logical volume wl01/wl01lv changed from 12.00 GiB (3072 extents) to 18.00 GiB (4608 extents).

  Logical volume wl01/wl01lv successfully resized.

[root@localhost ~]# lvs  //查看

  LV     VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  root   centos   -wi-ao---- <17.00g                                                    

  swap   centos   -wi-ao----   2.00g                                                    

  wl01lv wl01     -wi-ao----  18.00g                                                    

  mylv   wlgcvg12 -wi-ao----   7.00g                                                    

[root@localhost ~]# xfs_growfs /dev/wl01/wl01lv   //刷新文件系统

meta-data=/dev/mapper/wl01-wl01lv isize=512    agcount=4, agsize=655360 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0 spinodes=0

data     =                       bsize=4096   blocks=2621440, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal               bsize=4096   blocks=2560, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

data blocks changed from 2621440 to 4718592

[root@localhost ~]# df -h

文件系统                   容量  已用  可用 已用% 挂载点

/dev/mapper/centos-root     17G  3.8G   14G   23% /

devtmpfs                   895M     0  895M    0% /dev

tmpfs                      911M     0  911M    0% /dev/shm

tmpfs                      911M   11M  901M    2% /run

tmpfs                      911M     0  911M    0% /sys/fs/cgroup

/dev/sda1                 1014M  170M  845M   17% /boot

/dev/mapper/wlgcvg12-mylv  7.0G   33M  7.0G    1% /lv

/dev/mapper/wl01-wl01lv     18G   33M   18G    1% /hyl1

tmpfs                      183M  4.0K  183M    1% /run/user/42

tmpfs                      183M   20K  183M    1% /run/user/0

/dev/sr0                   8.8G  8.8G     0  100% /run/media/root/CentOS 7 x86_64

[root@localhost ~]#

[root@localhost ~]# lvremove /dev/wl01/wl01lv  //删除逻辑卷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值