(5)用户登录-服务和进程-history-du-touch-date-stat-cp

目录

一、知识补充

        1.1 Linux的一个小习惯

1.2 sh和bash

1.3 yum provides pstree

        1.4 yum install psmisc -y

        1.5 pstree

二、 关于新建用户和用户登录

        2.1 Linux中密码和用户信息存放在两个文件中

        2.1.1 !!和*的区别

        2.2 用户类型

三、服务和进程

        3.1端口

        3.1.1 举例(不同服务对应不同的端口)

四、Linux命令

        4.1 mikir

        4.2 rmdir

        4.3 rm

        4.4 history

        4.4.2 用法

        4.5 du命令

        4.5.1 常用命令选项

        4.5.2 【问】为什么ll -h和du -sh算出来的总量不同

        4.6 touch

        4.6.1 date

        4.6.2 stat

        4.7 cp

        4.7.1 复制文件

        4.7.1 复制文件


一、知识补充

        1.1 Linux的一个小习惯

Linux中,不推荐我们直接使用root用户登录,如果你需要root的权限做某些事情时,可以使用su切换过去。(即先使用普通用户登录,然后再切换至root)(原因:这样暴露root用户密码的几率会降低,更安全)

1.2 sh和bash

sh 较为古老的shell

bash 主流shell

[root@192 lianxi]# sh    使用sh这个shell
sh-4.2# exit    退出sh
exit
[root@192 lianxi]#

【补充】su切换用户的过程,背后就会产生一个bash

1.3 yum provides pstree

yum 是linux下的软件管理的命令

provides 提供

pstree 一个命令的名字        

[root@192 lianxi]# yum provides pstree
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.163.com
base                                                       | 3.6 kB  00:00:00
extras                                                     | 2.9 kB  00:00:00
updates                                                    | 2.9 kB  00:00:00
updates/7/x86_64/primary_db                                |  13 MB  00:00:03
updates/7/x86_64/filelists_db                              | 7.4 MB  00:00:01
psmisc-22.20-17.el7.x86_64 : Utilities for managing processes on your system
源    :base
匹配来源:
文件名    :/usr/bin/pstree

        1.4 yum install psmisc -y

install 安装

psmisc 需要安装的软件包

-y 之后不再需要输入y来确认安装 yes

        1.5 pstree

查看整个linux里的进程树,所有的进程以及它们之间的关系

后面接-p的选项可看到进程号pstree -p

[root@192 lianxi]# pstree
systemd─┬─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─lvmetad
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───bash───pstree
        │      └─sshd───sftp-server
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───2*[{vmtoolsd}]

第14行代码 ├─sshd─┬─sshd───bash───pstree含义:在bash中输入了命令pstree,就产生了一个子进程

【注】自己可尝试输入一个sh后再输入pstree,观察这里会发生什么变化 ├─sshd─┬─sshd───sshd───bash───sh───pstree

二、 关于新建用户和用户登录

1.新建用户和设置密码是两个独立过程。在新建用户时,可以不设置密码

2.在Linux中,未设置密码的用户不可通过终端登录(直接登录、远程登录),但可以使用su切换到新用户

        原因:远程登陆需要检查/etc/passwd和/etc/shadow文件,但su 切换登录只检查/etc/passwd

3.在Linux中新建的用户,如若没有设置没密码就不能登录系统

        2.1 Linux中密码和用户信息存放在两个文件中

/etc/passwd 记录整个Linux系统中有哪些用户。useradd就是往这个文件中添加内容

/etc/shadow 存放整个Linux系统里的用户的密码(用密文存储)。!!和*都表示禁用用户(锁定用户)。passwd就是往这个文件中添加内容

        2.1.1 !!和*的区别

!! 是普通用户(用户新建的普通用户)。

* 是系统用户(程序用户),专门用来启动程序,完成某一样功能的,大多都是系统自带的。

        2.2 用户类型

  1. 超级用户:root
  2. 普通用户:登录和进行基本操作:useradd
  3. 程序用户:不能登陆系统、但是可以启动程序

【注】root用户切换到任何用户都不需要密码

三、服务和进程

sshd服务:远程登录的功能

服务-->进程-->占用端口

        3.1端口

端口:是进程对外提供的接口(不同的服务会占用不同的端口)

        3.1.1 举例(不同服务对应不同的端口)

  1. nginx是提供网站服务==>80端口
  2. mysql是提供数据库服务==>3306端口

四、Linux命令

        4.1 mikir

用途:创建新的目录

格式:mkdir [-p] [/路径/]目录名

        4.2 rmdir

用途:用来删除空文件夹,非空文件夹不能删

        4.3 rm

用途:删除文件或目录

格式:rm [选项]... 文件或目录

详情请见(3)ls-wc-管道符号-alias-路径-touch-通配符

        4.4 history

查看当前用户曾经使用过的历史命令

4.4.1 .bash_history

每个用户的家目录下都有一个文件.bash_history,当用户注销离开Linux系统时,Linux会将该用户使用过的命令都保存到.bash_hisotry里,覆盖.bash_history里之前的内容

Linux里默认保留最近1000条历史命令

        4.4.2 用法

1. !number:执行第number条历史命令

[lihua@192 ~]$ history
    1  hostname
    2  pwd
    3  cd /
    4  cd home
    5  ls
    6  cd lihua/
    7  exit
    8  mkdir li
    9  ls
   10  ip add
   11  cat /etc/passwd
   12  exit
   13  pstree
   14  pstree
   15  pstree -p
   16  mkdir china
   17  pstree
   18  sh
   19  history
[lihua@192 ~]$ !5
ls
china  li

2. !接字符串:执行最近以该字符串开头的命令

  226  mkdir china
  227  mkdir lihua
  228  ls
  229  history
[root@192 lianxi]# rm -rf china lihua
[root@192 lianxi]# ls
create_user.sh  hunan  xiaoliu
[root@192 lianxi]# !mkdir   执行最近的以mkdir开头的命令
mkdir lihua

        4.5 du命令

用途:统计目录及文件的空间占用情况(disk usage),大小并不准确

格式:du [选项]... [目录或文件名]

        4.5.1 常用命令选项

(各个命令之间的选项没有关系。)

如若du命令后不接选项,直接接文件夹,则会分别统计文件夹内各个文件的大小

  1. -a:统计时包括所有的文件,而不仅仅只统计目录
  2. -s:只统计每个参数所占用空间总的大小
  3. -h:以更易度的字节单位i(K、M等)显示信息

[root@192 /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
[root@192 /]# du -sh etc
32M     etc
[root@192 /]# du -sh home
136K    home

        4.5.2 【问】为什么ll -h和du -sh算出来的总量不同

[root@192 ~]# du -sh anaconda-ks.cfg
4.0K    anaconda-ks.cfg
[root@192 ~]# ll -h anaconda-ks.cfg
-rw-------. 1 root root 1.3K 12月 15 19:07 anaconda-ks.cfg

ll -h:只能统计文件大小

du -sh:既能统计文件又能统计文件夹大小

【答】

  • ls -l -h /etc/passwd 会打开这个文件统计这个文件里有多少字符,消耗了多少磁盘空间

        一个英文字母占一个字节,一个中文字符占4个字节。一个回车符也占一个字节……

[root@192 lianxi]# ll -h lihua.txt
-rw-r--r--. 1 root root 13 2月  14 20:47 lihua.txt
[root@192 lianxi]# cat lihua.txt
lihua
linux

[root@192 lianxi]#
  • du 看磁盘里文件系统的使用情况(统计一个文件占用了多少块(block))

磁盘会进行分区(比如Windows中分为C盘、D盘等),区域格式化,会有最小分配单元(假设为4k),文件存放在小块区域内,一个文件至少占一个区域

格式化:按照某种格式要求进行划分为很多小块区域

[root@192 lianxi]# du -sh lihua.txt
4.0K    lihua.txt

        4.6 touch

  1. 新建空文件
  2. 如果文件存在就更新文件的创建时间,默认更新时间为系统的当前时间
[root@192 lianxi]# ll lihua.txt
-rw-r--r--. 1 root root 0 2月  14 21:11 lihua.txt
[root@192 lianxi]# date
2022年 02月 14日 星期一 21:14:40 CST
[root@192 lianxi]# touch lihua.txt
[root@192 lianxi]# ll lihua.txt
-rw-r--r--. 1 root root 0 2月  14 21:14 lihua.txt

        4.6.1 date

查看系统的当前时间

[root@192 lianxi]# date
2022年 02月 14日 星期一 21:16:49 CST

        4.6.2 stat

查看一个文件的详细信息


[root@192 lianxi]# stat lihua.txt
  文件:"lihua.txt"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd00h/64768d      Inode:51043943    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-02-14 21:11:30.730668670 +0800
最近更改:2022-02-14 21:11:30.730668670 +0800
最近改动:2022-02-14 21:11:30.730668670 +0800
创建时间:-
  • LANG=en_US.UTF-8 修改当前系统的语言编码,改为英语编码

        en==>English

        us==>美国

        LANG==>language 语言

        UTF-8万国码,将全球所有国家的文字都统一收集了,解决文字与文字之间的乱码问题


[root@192 lianxi]# LANG=en_US.UTF-8
[root@192 lianxi]# stat lihua.txt
  File: ‘lihua.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 51043943    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-14 21:14:46.887811528 +0800
Modify: 2022-02-14 21:14:46.887811528 +0800
Change: 2022-02-14 21:14:46.887811528 +0800
 Birth: -
  • LANG=zh_CN.UTF-8 修改当前系统的语言编码,改为中文编码

        zh==>zhong 中文

        CN==>China

[root@192 lianxi]# LANG=zh_CN.UTF-8
[root@192 lianxi]# stat lihua.txt
  文件:"lihua.txt"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd00h/64768d      Inode:51043943    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-02-14 21:14:46.887811528 +0800
最近更改:2022-02-14 21:14:46.887811528 +0800
最近改动:2022-02-14 21:14:46.887811528 +0800
创建时间:-

        3个时间

  1. 访问时间(Access time)-->atime:只要查看了文件内容就会更新这个时间==>cat、vim
  2. 修改时间(Modify time)-->mtime:改动了文件的内容==>vim 新增/删除内容
  3. 更改时间(Change time)-->ctime:修改了文件的拥有者、时间、权限、大小,强调修改了文件的属性==>touch、chmod(改权限)
[root@192 lianxi]# chmod 777 lihua.txt

+0800:东八区

[root@192 lianxi]# cat lihua.txt
[root@192 lianxi]# stat lihua.txt
  文件:"lihua.txt"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd00h/64768d      Inode:51043943    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-02-14 21:34:52.811400454 +0800
最近更改:2022-02-14 21:14:46.887811528 +0800
最近改动:2022-02-14 21:14:46.887811528 +0800
创建时间:-

        4.7 cp

用途:复制(Copy)和粘贴文件或目录。会同时完成复制和粘贴的操作

格式:cp [选项]... 源文件或目录... 目标文件或目录

-r:递归复制整个目录树

-a:复制时保留链接、文件属性,并递归地复制目录

        4.7.1 复制文件

1. 复制单个文件

cp /etc/passwd . 将/etc/passwd文件复制到当前目录

[root@192 lianxi]# ls
[root@192 lianxi]# cp /etc/passwd .
[root@192 lianxi]# ls
passwd

cp /etc/passwd lihua将/etc/passwd文件复制到lihua文件夹中

[root@192 lianxi]# ls
passwd
[root@192 lianxi]# mkdir lihua
[root@192 lianxi]# cp /etc/passwd lihua
[root@192 lianxi]# ls
lihua  passwd
[root@192 lianxi]# cd lihua
[root@192 lihua]# ls
passwd

2. 复制多个文件

cp li.txt hua.txt lihua 前面都是文件,最后一个必须要是文件夹,才可以存放多个文件

[root@192 lianxi]# ls
hua.txt  lihua  li.txt  passwd
[root@192 lianxi]# cp li.txt  hua.txt lihua
[root@192 lianxi]# ls lihua
hosts  hua.txt  li.txt  passwd

3. 是否覆盖替换


[root@192 lianxi]# cp /etc/hosts lihua
[root@192 lianxi]# cp /etc/hosts lihua  第二次复制过来(此时lihua内已经含有该文件
cp:是否覆盖"lihua/hosts"? y
[root@192 lianxi]#

4. 复制、粘贴、重命名一步到位

cp passwd xiaohong 复制passwd到当前目录下,改名为xiaohong

        4.7.1 复制文件

复制文件一定要接选项-r

[root@192 lianxi]# mkdir liming
[root@192 lianxi]# cp lihua liming
cp: 略过目录"lihua"

1. 复制单个文件夹

[root@192 lianxi]# cp -r lihua liming

2. 复制多个文件夹

[root@192 lianxi]# cp lihua liming lihong -r
[root@192 lianxi]# ls lihong
lihua  liming

同时复制文件和文件夹

[root@192 lianxi]# mkdir li
[root@192 lianxi]# cp li.txt lihua passwd liming li -r
[root@192 lianxi]# ls li
lihua  liming  li.txt  passwd

3. 直接覆盖替换,不提醒

[root@192 lianxi]# which cp
alias cp='cp -i'
        /bin/cp
[root@192 lianxi]# cp li.txt lihua passwd liming li -r
cp:是否覆盖"li/li.txt"?
cp:是否覆盖"li/lihua/passwd"?
cp:是否覆盖"li/lihua/hosts"?
cp:是否覆盖"li/lihua/li.txt"?
cp:是否覆盖"li/lihua/hua.txt"?
cp:是否覆盖"li/passwd"?
cp:是否覆盖"li/liming/lihua/passwd"?
cp:是否覆盖"li/liming/lihua/hosts"?
cp:是否覆盖"li/liming/lihua/li.txt"? ^C
[root@192 lianxi]# /bin/cp li.txt lihua passwd liming li -r
[root@192 lianxi]#

        【问】为什么使用cp命令的绝对路径(/bin/cp)就会没有提示

因为我们平时输入cp时,实际上就是使用cp -i命令

-i ,-interactive 交互式(一问一答,可以避免误替换数据)

4. 复制、粘贴、重命名一步到位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值