linux基础命令(一)

Linux基础命令

1、设置主机名

1.1、 hostname

查看主机名

[root@ansible ~]# cat /etc/hostname
ansible
或
[root@ansible ~]# hostname
ansible

注意: 主机名中不允许使用下划线 “_”,可以用短横线 “-”

1.2、hostname

临时修改主机名

# 临时修改直接修改的是内存中的,重启会失效
[root@ansible ~]# hostname ansible.org

[root@ansible ~]# hostname
ansible.org

1.3、hostnamectl

永久修改主机名

# 永久修改其实是修改的配置文件,重启不会失效,hostnamectl修改了两个地方,一个是内存中的hostname,一个是磁盘文件
[root@ansible ~]# hostnamectl set-hostname ansible.gxy

[root@ansible ~]# hostname
ansible.gxy
[root@ansible ~]# cat /etc/hostname
ansible.gxy

2、whoami

显示当前用户

[root@ansible ~]# whoami
root
或
[root@ansible ~]# who am i
root     pts/0        2021-12-25 11:52 (192.168.31.114)
或
[root@ansible ~]# who
root     pts/0        2021-12-25 11:52 (192.168.31.114)

3、init

关机或重启

init 0		关机
init 6		重启

4、修改终端格式和颜色

4.1、临时修改

[root@ansible ~]#PS1="\[\e[1;31m\][\u@\h \w]\\$\[\e[0m\]"	# 31字体是红色
[root@ansible ~]#PS1="\[\e[1;32m\][\u@\h \w]\\$\[\e[0m\]"	# 32字体是绿色 
[root@ansible ~]#PS1="\[\e[1;33m\][\u@\h \w]\\$\[\e[0m\]"	# 33字体是黄色
[root@ansible ~]#PS1="\[\e[1;34m\][\u@\h \w]\\$\[\e[0m\]"	# 34字体是蓝色
[root@ansible ~]#PS1="\[\e[1;35m\][\u@\h \w]\\$\[\e[0m\]"	# 35字体是紫色
[root@ansible ~]#PS1="\[\e[1;36m\][\u@\h \w]\\$\[\e[0m\]"	# 36字体是蓝绿色
[root@ansible ~]#PS1="\[\e[1;37m\][\u@\h \w]\\$\[\e[0m\]"	# 37字体是白色

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HMWAJ7jp-1644760205824)(F:\typora\linux常见命令\images\主机名显示颜色.jpg)]

注意: 退出终端颜色会失效!

4.2、永久修改

# 放在/etc/profile.d/目录下的任何以 .sh结尾的都行,不一定是env.sh,这个是个人习惯
[root@ansible ~]#echo 'PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"' > /etc/profile.d/env.sh

[13:21:45 root@ansible ~]#

注意: 以上操作在centos上实验的,Ubuntu不一定适用

5、lscpu

查看CPU信息

[14:00:38 root@ansible ~]#lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    1		# 一个物理CPU
socket:               4
NUMA node(s):         1
Vendor ID:           GenuineIntel
CPU family:           6
Model:                 45
Model name:           Intel(R) Xeon(R) CPU E5-2665 0 @ 2.40GHz
Stepping:             7
CPU MHz:             2400.000
BogoMIPS:            4800.00
Hypervisor vendor:     VMware
Virtualization type:   full	  # 支持虚拟化
L1d cache:          32K		 # 数据缓存
L1i cache:          32K		 # 指令缓存
L2 cache:            256K
L3 cache:            20480K
NUMA node0 CPU(s):   0-3
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat spec_ctrl intel_stibp flush_l1d arch_capabilities

可以通过cat /proc/cpuinfo 来查看CPU信息

# 显示的更详细,每个核的具体信息都有显示
[20:39:31 root@ansible ~]#cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2665 0 @ 2.40GHz
stepping        : 7
microcode       : 0x718
cpu MHz         : 2400.000
cache size      : 20480 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat spec_ctrl intel_stibp flush_l1d arch_capabilities
bogomips        : 4800.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2665 0 @ 2.40GHz
stepping        : 7
microcode       : 0x718
cpu MHz         : 2400.000
cache size      : 20480 KB
physical id     : 2
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 2
initial apicid  : 2
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat spec_ctrl intel_stibp flush_l1d arch_capabilities
bogomips        : 4800.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

processor       : 2
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2665 0 @ 2.40GHz
stepping        : 7
microcode       : 0x718
cpu MHz         : 2400.000
cache size      : 20480 KB
physical id     : 4
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 4
initial apicid  : 4
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat spec_ctrl intel_stibp flush_l1d arch_capabilities
bogomips        : 4800.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

processor       : 3
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2665 0 @ 2.40GHz
stepping        : 7
microcode       : 0x718
cpu MHz         : 2400.000
cache size      : 20480 KB
physical id     : 6
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 6
initial apicid  : 6
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat spec_ctrl intel_stibp flush_l1d arch_capabilities
bogomips        : 4800.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual

5、which

查看外部命令的位置

# which用来查看外部命令的路径,是无法查看内部命令的路径的
[16:49:47 root@ansible ~]#which hostname
/usr/bin/hostname
[16:49:58 root@ansible ~]#which cd
/usr/bin/cd
[16:50:15 root@ansible ~]#which type
/usr/bin/which: no type in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[16:50:22 root@ansible ~]#which enable
/usr/bin/which: no enable in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

6、whereis

查看外部命令的路径,同时可以显示外部命令帮助文档的路径,如果是内部命令,只能显示帮助文档的路径

# 外部命令
[16:53:21 root@ansible ~]#whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/mann/cd.n.gz

# 内部命令
[16:53:47 root@ansible ~]#whereis type
type: /usr/share/man/man1/type.1.gz

7、alias

别名

# 先查看系统中是存在此命令,避免和系统中命令重复
[12:41:58 root@ansible ~]#type cdnetwork
-bash: type: cdnetwork:not found

# 创建别名,例如:
[12:42:01 root@ansible ~]# alias cdnetwork="cd /etc/sysconfig/network-scripts"
[12:41:58 root@ansible ~]#type cdnetwork
cdnetwork 是 `cd /etc/sysconfig/network-scripts' 的别名

[12:44:39 root@ansible ~]#cdnetwork
[12:44:42 root@ansible network-scripts]#

7.1、显示所有别名

# 用alias显示所有别名
[14:15:32 root@ansible ~]#alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

7.2、使别名永久生效

[14:20:22 yang@ansible ~]$vim .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
alias cdnetwork='cd /etc/sysconfig/network-scripts'		# 添加此行

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

刷新使其生效

# 刷新使其生效,. 和 source 效果一样
[14:21:00 yang@ansible ~]$. .bashrc

[14:21:07 yang@ansible ~]$alias
alias cdnetwork='cd /etc/sysconfig/network-scripts'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[14:21:12 yang@ansible ~]$cdnetwork
[14:21:17 yang@ansible network-scripts]$

此命令只针对当前用户生效!

7.3、取消单个别名

# unalias是临时取消,当终端退出重新进入会失效,如果想永久取消需要修改.bashrc配置文件
[14:28:08 root@ansible ~]#unalias cdnetwork
[14:28:27 root@ansible ~]#cdnetwork
-bash: cdnetwork: 未找到命令

7.4、取消全部别名

# 临时取消全部别名,终端重进进入会失效
[14:46:40 root@ansible ~]#unalias -a

7.5、将内部命令定义成别名

# enable是内部命令,如果将内部名enable定义了别名,由于优先级来看,会执行别名而不会执行内部命令
[14:53:13 root@ansible ~]#alias enable='hostname -I'
[14:54:42 root@ansible ~]#hostname -I
192.168.31.201 172.18.0.1 172.17.0.1
[14:54:51 root@ansible ~]#enable
192.168.31.201 172.18.0.1 172.17.0.1

# 而有时候我们需要要执行内部命令,这个时候我们可以通过以下方式来执行内部命令
[14:55:16 root@ansible ~]#\enable	# 或 'enable'
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
enable cd

优先级:别名>内部命令>外部命令

针对当前用户:修改当前用户家目录下的 .bashrc文件

针对全部用户:修改 /etc/bashrc文件

注意: 别名也是临时性的,终端退出会失效

8、lsblk

查看分区

[14:31:20 root@ansible ~]#lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk				# 磁盘
├─sda1            8:1    0  200M  0 part /boot			# 分区
└─sda2            8:2    0  188G  0 part
  ├─centos-root 253:0    0   30G  0 lvm  /
  ├─centos-swap 253:1    0    8G  0 lvm  [SWAP]
  ├─centos-home 253:2    0  100G  0 lvm  /home
  └─centos-data 253:3    0  650G  0 lvm  /data
sdb               8:16   0  600G  0 disk
└─sdb1            8:17   0  600G  0 part
  └─centos-data 253:3    0  650G  0 lvm  /data
sr0              11:0    1  918M  0 rom				# 光盘

9、uptime

查看开机时间

# 开机已经有49天了
[root@VM-16-15-centos ~]# uptime
14:40:35 up 49 days, 22:00,  1 user,  load average: 0.00, 0.01, 0.05

10、ls

# ls既有别名又是个外部命令,但是执行时别名优先,同时又是 --color=auto,所以执行执行ls列出的内容是有颜色的,但是想让它执行 外部命令就需要用 "\"+ls来执行了
[19:41:33 root@ansible ~]#type ls
ls 是 `ls --color=auto' 的别名

[19:42:37 root@ansible ~]#ll /bin/ls
-rwxr-xr-x. 1 root root 117680 10月 31 2018 /bin/ls

# 直接执行ls
[19:41:26 root@ansible ~]#ls
111   1114           222                                   data_tmp            docker-compose-Linux-x86_64  harbor-offline-installer-v2.1.4.tgz         script        testlog.sql    xswitch-free-master.zip
1111  1115           anaconda-ks.cfg                       doc                 env.sh                       mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz  sql           test.sql
1112  192.168.1.135  apache-tomcat-8.5.65.tar.gz           docker-20.10.5.tgz  freeswitch-1.6.0.tar.gz      mysql57-community-release-el7-9.noarch.rpm  test          txy
1113  221            CentOS-7-aarch64-Everything-1810.iso  docker-20.10.6.tgz  harbor                       ossutil64

# 当执行它原本的意思需要用以下方式,其实是没有颜色的
[19:41:28 root@ansible ~]#\ls
111   1114           222                                   data_tmp            docker-compose-Linux-x86_64  harbor-offline-installer-v2.1.4.tgz         script        testlog.sql    xswitch-free-master.zip
1111  1115           anaconda-ks.cfg                       doc                 env.sh                       mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz  sql           test.sql
1112  192.168.1.135  apache-tomcat-8.5.65.tar.gz           docker-20.10.5.tgz  freeswitch-1.6.0.tar.gz      mysql57-community-release-el7-9.noarch.rpm  test          txy
1113  221            CentOS-7-aarch64-Everything-1810.iso  docker-20.10.6.tgz  harbor                       ossutil64                                   testdata.sql  Xmanager5.exe

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yjooxTyw-1644760205828)(F:\typora\linux常见命令\images\ls.jpg)]

11、id

查看用户的uid

# 默认显示自己的uid
[20:20:07 root@ansible ~]#id
uid=0(root) gid=0(root) 组=0(root)

# 查看某个用户的相关信息
[20:20:09 root@ansible ~]#id yang
uid=1001(yang) gid=1001(yang) 组=1001(yang)

# 只查看用户的uid
[20:20:18 root@ansible ~]#id -u yang
1001

12、bc

计算器

# 将十进制转换成2进制
[20:26:55 root@ansible ~]#bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=2		# 我只需要约定输出是2进制即可
120
1111000

13、一次执行多个命令用 “;”

[20:32:20 root@ansible doc]#ls;pwd;hostname -I;
liantong_doc.zip  magedu_jiagou.zip  magedu_jiuye.zip  server.zip  typora_doc.zip
/root/doc
192.168.31.201 172.18.0.1 172.17.0.1

14、查看详细的内存信息

[20:54:35 root@ansible ~]#cat /proc/meminfo
MemTotal:        8009192 kB
MemFree:         6027200 kB
MemAvailable:    6798732 kB
Buffers:            3176 kB
Cached:          1047908 kB
SwapCached:            0 kB
Active:          1031888 kB
Inactive:         668916 kB
Active(anon):     643592 kB
Inactive(anon):   100884 kB
Active(file):     388296 kB
Inactive(file):   568032 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       8388604 kB
SwapFree:        8388604 kB
Dirty:                56 kB
Writeback:             0 kB
AnonPages:        649720 kB
Mapped:           232712 kB
Shmem:             94756 kB
Slab:             116820 kB
SReclaimable:      73404 kB
SUnreclaim:        43416 kB
KernelStack:        8608 kB
PageTables:        10788 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    12393200 kB
Committed_AS:    4263456 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      161604 kB
VmallocChunk:   34359341052 kB
HardwareCorrupted:     0 kB
AnonHugePages:    440320 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      167808 kB
DirectMap2M:     8220672 kB

/proc 是内存目录,proc显示的当前系统的状态,proc的内容都是以内存的形式保存的

# 我们可以看到占用空间都是0,这就说明它不占用内存空间,因为这些数据都是来自于内存
[20:54:54 root@ansible ~]#ll /proc/meminfo
-r--r--r-- 1 root root 0 12月 26 20:57 /proc/meminfo
[20:57:32 root@ansible ~]#ll /proc/cpuinfo
-r--r--r-- 1 root root 0 12月 26 20:57 /proc/cpuinfo

15、查看内核版本 uname -r

查看内核版本

[21:01:43 root@ansible ~]#uname -r
3.10.0-957.el7.x86_64

16、查看操作系统发行版本

[21:01:46 root@ansible ~]#cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

[21:04:00 root@ansible ~]#cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

17、查看磁盘信息

# 除了我们所熟悉的df命令还可以用过以下命令查看,场景:如果NAS没有df命令就可以用以下命令查看
[21:11:03 root@ansible ~]#cat /proc/partitions
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  197139456 sda2
   8       16  629145600 sdb
   8       17  629144576 sdb1
  11        0     940032 sr0
 253        0   31457280 dm-0
 253        1    8388608 dm-1
 253        2  104857600 dm-2
 253        3  681574400 dm-3

18、关机

halt	# 关机
poweroff # 关机

19、重启

reboot		# 重启

20、关机或重启

# 只执行shoudown命令并不会关机,系统会在你执行命令一分钟后进行关机
-bash-4.2# date
2021年 12月 30日 星期四 13:26:15 GMT
-bash-4.2# shutdown
Shutdown scheduled for 四 2021-12-30 13:27:20 GMT, use 'shutdown -c' to cancel.
-bash-4.2#
Broadcast message from root@lnmp (Thu 2021-12-30 13:26:20 GMT):

The system is going down for power-off at Thu 2021-12-30 13:27:20 GMT!

马上关机

-bash-4.2# shutdown -h now

取消关机

# 用 -c 参数取消关机
-bash-4.2# shutdown -c

Broadcast message from root@lnmp (Thu 2021-12-30 13:26:27 GMT):

The system shutdown has been cancelled at Thu 2021-12-30 13:27:27 GMT!

多长时间以后关机

# 10分钟以后关机
-bash-4.2#
Broadcast message from root@lnmp (Thu 2021-12-30 13:31:48 GMT):

The system is going down for power-off at Thu 2021-12-30 13:41:48 GMT!

设置几点几分关机

# 设置2:30分进行关机,只能精确到分,不能精确到秒
-bash-4.2# shutdown 02:30
Shutdown scheduled for 五 2021-12-31 02:30:00 GMT, use 'shutdown -c' to cancel.

21、w

查看当前有几个用户在登录,并且能看到每个用户在执行什么操作

# 一共有两个用户登录了系统,其中一个用户在执行yes命令
[21:41:08 root@ansible ~]#w
 21:41:09 up  1:23,  2 users,  load average: 0.36, 0.14, 0.14
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/1    192.168.31.114   20:20    5.00s  0.01s  0.01s -bash
root     pts/2    192.168.31.114   21:41    5.00s  0.58s  0.57s yes

22、screen

Linux screen命令用于多重视窗管理程序。

# 安装
[root@VM-16-15-centos ~]# yum install screen

通过screen后台运行某个程序,及时关闭终端,后台程序仍在运行

# 运行screen,会从新打开一个新终端
[root@VM-16-15-centos ~]# screen

# 执行一个yes,然后将终端关闭
[root@VM-16-15-centos ~]# yes

# 在另一终端进行查看yes是否在后台运行
[root@VM-16-15-centos ~]# ps aux|grep yes
root     11241 59.2  0.0 111172   640 pts/9    R+   21:03   0:13 yes
root     11393  0.0  0.0 115928  1008 pts/10   S+   21:04   0:00 grep --color=auto yes

远程协助

# xiaohong 需要 xiaoming 远程协助解决问题,xiaohong 需要做如下操作
[root@VM-16-15-centos ~]# screen -S xiaohong		# 大写S
[root@VM-16-15-centos ~]#
# xiaoming 需要做如下操作,先查看当前可用的连接
[root@VM-16-15-centos ~]# screen -ls
There are screens on:
        1471.xiaohong   (Attached)		# 发现xiaohong可用
        5358.pts-0.VM-16-15-centos      (Detached)
        11207.pts-8.VM-16-15-centos     (Detached)
        8325.pts-0.VM-16-15-centos      (Detached)
        6876.pts-0.VM-16-15-centos      (Detached)
        6415.pts-0.VM-16-15-centos      (Detached)
6 Sockets in /var/run/screen/S-root.

# 连接,此时已经互相能看到xiaohong的终端了,实时同步
[root@VM-16-15-centos ~]# screen -x xiaohong
[root@VM-16-15-centos ~]#

# 退出,协助完成需要退出,可以用Ctrl+a,d Ctrl+a不松手在按d
[detached from 3625.xiaohong]
[root@VM-16-15-centos ~]#

# 如果xiaohong 不需要协助了,可以退出终端,用exit
[root@VM-16-15-centos ~]# exit
[screen is terminating]
[root@VM-16-15-centos ~]#

注意: 协助的双方必须连接的是同个主机和同一个用户。

23、echo

选项

-n		不自动换行
-E		(默认)不支持\解释功能-n不自动换行
-e		启用\字符的解释功能

显示变量

# 命名一个变量
[root@VM-16-15-centos ~]# name=gxy
[root@VM-16-15-centos ~]# echo $name
gxy
[root@VM-16-15-centos ~]# echo "$name"		# 双引号会显示变量名
gxy
[root@VM-16-15-centos ~]# echo '$name'		# 单引号里边是什么则会打印什么
$name

-n 参数

[root@VM-16-15-centos ~]# echo -n $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/jdk1.8.0_161/bin:/root/bin[root@VM-16-15-centos ~]#

启用命令选项-e,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出

\a 发出警告声
\b 退格键
\c 最后不加上换行符号
\e escape,相当于\033
\n 换行且光标移至行首
\r 回车,即光标移至行首,但不换行
\t 插入tab
\\ 插入\字符
\Onnn插入nnn (八进制)所代表的ASCII字符
\xHH插入HH(十六进制)所代表的ASCI数字(man 7 ascii)

25、命令行扩展和被括起来的集合

命令行扩展:``和$()

把一个命令的输出打印给另一个命令的参数,放在 `` 中的一定是有输出信息的命令

# 两者相等效果
$(COMMAND)或`COMMAND`

[root@VM-16-15-centos ~]# echo "This system's name is $(hostname)"
This system's name is VM-16-15-centos
[root@VM-16-15-centos ~]# echo "This system's name is `hostname`" 
This system's name is VM-16-15-centos

范例:比较"",‘’,``三者区别

# 比较"",'',``三者区别
[root@VM-16-15-centos ~]# echo "echo $HOSTNAME" 
echo VM-16-15-centos
[root@VM-16-15-centos ~]# echo 'echo $HOSTNAME' 
echo $HOSTNAME
[root@VM-16-15-centos ~]# echo `echo $HOSTNAME`
VM-16-15-centos

结论

**单引号:**六亲不认,变量和命令都不识别,都当成了普通的字符串
反向单引号: 变量和命令都识别,并且会将反向单引号的内容当成命令进行执行后,再交给调用反向单引号的命令继续
双引号: 不能识别命令,可以识别变量

24、括号扩展:{}

例1:

[root@VM-16-15-centos tmp]# touch file{1,3,5}.txt
[root@VM-16-15-centos tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1月  16 16:11 file1.txt
-rw-r--r-- 1 root root 0 1月  16 16:11 file3.txt
-rw-r--r-- 1 root root 0 1月  16 16:11 file5.txt

例2:

[root@VM-16-15-centos tmp]# touch file{1..5}.txt 
[root@VM-16-15-centos tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1月  16 16:12 file1.txt
-rw-r--r-- 1 root root 0 1月  16 16:12 file2.txt
-rw-r--r-- 1 root root 0 1月  16 16:12 file3.txt
-rw-r--r-- 1 root root 0 1月  16 16:12 file4.txt
-rw-r--r-- 1 root root 0 1月  16 16:12 file5.txt

例3:

[root@VM-16-15-centos tmp]# touch file{3..5}.txt
[root@VM-16-15-centos tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1月  16 16:13 file3.txt
-rw-r--r-- 1 root root 0 1月  16 16:13 file4.txt
-rw-r--r-- 1 root root 0 1月  16 16:13 file5.txt

例4:

# 2是间隔数,打印出来的包含3的,如果第二位是9,则也会打印file9.txt
[root@VM-16-15-centos tmp]# touch file{3..8..2}.txt
[root@VM-16-15-centos tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1月  16 16:16 file3.txt
-rw-r--r-- 1 root root 0 1月  16 16:16 file5.txt
-rw-r--r-- 1 root root 0 1月  16 16:16 file7.txt

例5:

# 同样适用于字母
[root@VM-16-15-centos tmp]# echo {a..z..2}
a c e g i k m o q s u w y

例6:

[root@VM-16-15-centos tmp]# echo {000..20..2}
000 002 004 006 008 010 012 014 016 018 020

例7:

[root@VM-16-15-centos tmp]# touch file{1..5}.{txt,log}
[root@VM-16-15-centos tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1月  16 16:25 file1.log
-rw-r--r-- 1 root root 0 1月  16 16:25 file1.txt
-rw-r--r-- 1 root root 0 1月  16 16:25 file2.log
-rw-r--r-- 1 root root 0 1月  16 16:25 file2.txt
-rw-r--r-- 1 root root 0 1月  16 16:25 file3.log
-rw-r--r-- 1 root root 0 1月  16 16:25 file3.txt
-rw-r--r-- 1 root root 0 1月  16 16:25 file4.log
-rw-r--r-- 1 root root 0 1月  16 16:25 file4.txt
-rw-r--r-- 1 root root 0 1月  16 16:25 file5.log
-rw-r--r-- 1 root root 0 1月  16 16:25 file5.txt

25、sleep

过多久执行什么操作

# 过5秒执行 echo "hello word"
[root@VM-16-15-centos ~]# sleep 5;echo "hello word"
hello word

26、字符集

我们都知道计算机是二进制语言,我们输入的文字或者字母计算机是无法识别的,如何才能让计算机识别呢,这个时候就用到了字符集,字符集就是个表格,它把字符和计算机二进制做了对应关系,例如以下表格,本次只举例,不论证对应表是否正确:

字母二进制
a01001
b01002

unkode字符集

全球通用字符集,unkode包含:UTF8、UTF16、UTF32三种编码

27、编码

字符集简单来说就是字符和二进制的对应关系,编码决定了如何在磁盘上保存,它的保存形式不一样,例如一个人的姓名,名字不变的情况下你可以使用宋体,楷体或者黑体来写,编码决定了它最终的表现形式。

UTF8 :边长,1到4个字节,它在表示不同文字的时候所占的磁盘空间不一样,有可能会占1个字节,或2个字节,或者3个字节,或者4个字节。

UTF16: 变长,2或4个字节。

UTF32:固定长度,4个字节。

查看当前系统用的那种编码格式

[root@VM-16-15-centos ~]# echo $LANG
en_US.utf8		#前边是语言,后边是编码格式

# 修改语言
[root@VM-16-15-centos ~]# LANG=zh_CN.UTF-8
[root@VM-16-15-centos ~]# echo $LANG      
zh_CN.UTF-8

28、history

当执行命令后,系统默认会在内存记录执行过的命令

当用户正常退出时,会将内存的命令历史存放对应历史文件中,默认是~/.bash_history

登录shell时,会读取命令历史文件中记录下的命令加载到内存中

登录进shell后新执行的命令只会记录在内存的缓存区中;这些命令会用户正常退出时“追加"至命令历史文件中

.bash_history文件会记录多少条,我们可以用一下命令来查看

# 一共可以存多少条都在“HISTSIZE”变量中记录
[root@VM-16-15-centos ~]# echo $HISTSIZE
3000

# 修改记录条数为10000(这个在centos7.6中改完并没有生效,后续在查找)
[root@VM-16-15-centos ~]# sed -i 's/HISTSIZE=1000/HISTSIZE=10000/g' /etc/profile
[root@VM-16-15-centos ~]# source /etc/profile
[root@VM-16-15-centos ~]# echo $HISTSIZE
3000
#  查看历史记录
[root@VM-16-15-centos ~]# history
...
  599  2022-01-16 12:18:14 echo -e "\033[31m 红色字 \033[0m"
  600  2022-01-16 12:29:48 ll
  601  2022-01-16 12:30:18 ll
  602  2022-01-16 12:31:58 ll
  603  2022-01-16 12:32:33 ll
  604  2022-01-16 13:37:57 echo $LANG
  605  2022-01-16 13:38:00 ll
  606  2022-01-16 16:43:56 cat .bash_history 
  607  2022-01-16 16:45:05 history

执行历史命令

# 用“!”号加历史命令前的编号
[root@VM-16-15-centos ~]# !605
ll
total 24
drwxrwxr-x 9 lighthouse lighthouse 4096 May 31  2021 apache-tomcat-8.5.66
drwxr-xr-x 8 lighthouse lighthouse 4096 Dec 20  2017 jdk1.8.0_161
-rw-r--r-- 1 root       root       9224 Nov 15 09:40 mysql57-community-release-el7-9.noarch.rpm
-rw-r--r-- 1 root       root          0 Jan 16 15:45 nginx_2022-01-16.log
drwxr-xr-x 2 root       root       4096 Jan 16 16:25 tmp

# 或
[root@VM-16-15-centos ~]# !-3		# 倒数第3条
ll
total 24
drwxrwxr-x 9 lighthouse lighthouse 4096 May 31  2021 apache-tomcat-8.5.66
drwxr-xr-x 8 lighthouse lighthouse 4096 Dec 20  2017 jdk1.8.0_161
-rw-r--r-- 1 root       root       9224 Nov 15 09:40 mysql57-community-release-el7-9.noarch.rpm
-rw-r--r-- 1 root       root          0 Jan 16 15:45 nginx_2022-01-16.log
drwxr-xr-x 2 root       root       4096 Jan 16 16:25 tmp

常用选项

-c:清空历史命令
-d offset:删除历史中指定的第offset个命令
n:显示最近的n条历史
-a:追加本次会话新执行的命令历史列表至历史文件
-r:读历史文件附加到历史列表
-w︰保存历史列表到指定的历史文件
-n:读历史文件中未读过的行到历史列表
-p:展开历史参数成多行,但不存在历史列表中
-s:展开历史参数成一行,附加在历史列表后

清除历史记录

# 有时候我们在输入一些敏感信息的时候并不想让其他人知道,这个时候我们可以清楚历史记录
[root@VM-16-15-centos ~]# mysql -uroot -ppasswd

[root@VM-16-15-centos ~]# history
...
  629  2022-01-16 17:05:40 history
  630  2022-01-16 17:05:47 echo $HISTSIZE
  631  2022-01-16 17:09:15 mysql -uroot -ppasswd		# 密码在历史中保存
  632  2022-01-16 17:09:23 history
  
# 清除历史记录,发现此时已经看不到了
[root@VM-16-15-centos ~]# history -c
[root@VM-16-15-centos ~]# history
    1  2022-01-16 17:10:05 h
    2  2022-01-16 17:10:17 history

# 退出终端在进行history查看也是看不到的

查看删除记录

[root@VM-16-15-centos ~]# rm -rf .bash_history
[root@VM-16-15-centos ~]# history -c

# 直接关闭窗口在登录,发现删除的操作还是会被查看到,即使前边已经清除了记录
Last login: Sun Jan 16 17:35:04 2022 from 171.11.0.69
[root@VM-16-15-centos ~]# history
    1  2022-01-16 17:35:39 rm -rf .bash_history
    2  2022-01-16 17:36:26 history

注意: 通过history命令查看的历史记录都是在内存中的,当我们退出终端的时候会写入到 .bash_history文件,如果我们没有退出的时候写入的新命令则会先写入内存,如果此时清除历史消息,是不会写入.bash_history文件的。

伪造历史记录

# 伪造删除/data盘的记录
[root@VM-16-15-centos ~]# history -s rm -rf /data 
drwxr-xr-x 2 root       root       4096 Jan 16 16:25 tmp

# 查看/data目录还在
[root@VM-16-15-centos ~]# ll /
total 72
lrwxrwxrwx.   1 root root     7 Mar  7  2019 bin -> usr/bin
dr-xr-xr-x.   5 root root  4096 Nov  6 16:39 boot
drwxr-xr-x    2 root root  4096 Nov  5  2019 data
drwxr-xr-x   20 root root  3040 Nov  6 16:40 dev
drwxr-xr-x.  96 root root 12288 Jan 16 17:02 etc
drwxr-xr-x.   3 root root  4096 Nov  6 16:38 home
lrwxrwxrwx.   1 root root     7 Mar  7  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Mar  7  2019 lib64 -> usr/lib64
drwx------.   2 root root 16384 Mar  7  2019 lost+found
drwxr-xr-x.   2 root root  4096 Apr 11  2018 media
drwxr-xr-x.   2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x.   7 root root  4096 Nov  6 16:58 opt
dr-xr-xr-x  120 root root     0 Nov  6 16:39 proc
dr-xr-x---.  13 root root  4096 Jan 16 17:35 root
drwxr-xr-x   25 root root   840 Jan 13 20:39 run
lrwxrwxrwx.   1 root root     8 Mar  7  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x   13 root root     0 Jan 16 17:39 sys
drwxrwxrwt.  10 root root  4096 Jan 16 17:26 tmp
drwxr-xr-x.  14 root root  4096 Nov  9 18:44 usr
drwxr-xr-x.  20 root root  4096 Jan  8  2021 var

# 查看历史记录,发现有伪造删除data盘记录
[root@VM-16-15-centos ~]# history
    1  2022-01-16 17:35:39 rm -rf .bash_history
    2  2022-01-16 17:36:26 history
    3  2022-01-16 17:39:06 ll
    4  2022-01-16 17:39:10 ll /
    5  2022-01-16 17:39:45 rm -rf /data
    6  2022-01-16 17:39:53 ll
    7  2022-01-16 17:39:57 ll /
    8  2022-01-16 17:40:05 history

命令历史相关环境变量

HISTSIZE:命令历史记录的条数
HISTFILE:指定历史文件,默认为~/.bash_history
HISTFILESIZE:命令历史文件记录历史的条数
HISTTIMEFORMAT="%F %T `whoami`"显示时间和用户
HISTIGNORE="str1: str2* : ..."忽略str1命令,str2开头的历史
HISTCONTROL:控制命令历史的记录方式
ignoredups是默认值,可忽略重复的命令,连续且相同为“重复”
ignorespace忽略所有以空白开头的命令
ignoreboth相当于ignoredups,ignorespace的组合
erasedups删除重复命令
# 让历史记录显示日期和用户,终端退出再次登录仍然生效(重启服务器是否生效待测试)

[root@VM-16-15-centos ~]# export HISTTIMEFORMAT="%F %T `whoami` "
[root@VM-16-15-centos ~]# history
    1  2022-01-16 17:35:39 root rm -rf .bash_history
    2  2022-01-16 17:36:26 root history
    3  2022-01-16 17:39:06 root ll
    4  2022-01-16 17:39:10 root ll /
    5  2022-01-16 17:39:45 root rm -rf /data
    6  2022-01-16 17:39:53 root ll
    7  2022-01-16 17:39:57 root ll /
    8  2022-01-16 17:40:05 root history
    9  2022-01-16 17:44:34 root echo $HISFILE
   10  2022-01-16 17:44:49 root echo $HISTFILE
   11  2022-01-16 17:52:03 root export HISTTIMEFORMAT="%F %T `whoami`"
   12  2022-01-16 17:52:10 root history

调用命令历史

# !?+要搜索的关键词,也可以是包含在关键词中的字符
[root@VM-16-15-centos ~]# !?gre		# 执行最后一个包含gre的命令
grep 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lighthouse:x:1000:1000::/home/lighthouse:/bin/bash
mysql:x:306:306::/data/mysql:/bin/bash

# 执行最近的以d开头的命令
[root@VM-16-15-centos ~]# !d
date
Sun Jan 23 11:12:52 CST 2022

# 把前一个命令的参数作为之后执行命令的参数
[root@VM-16-15-centos ~]# cat /etc/redhat-release /etc/issue
CentOS Linux release 7.6.1810 (Core)
\S
Kernel \r on an \m

[root@VM-16-15-centos ~]# ll !*		# 表示把前一个命令的参数作为后一个命令执行的参数
ll /etc/redhat-release /etc/issue
-rw-r--r--. 1 root root 23 Nov 23  2018 /etc/issue
lrwxrwxrwx. 1 root root 14 Mar  7  2019 /etc/redhat-release -> centos-release

[root@VM-16-15-centos ~]# cat !*
cat /etc/redhat-release /etc/issue
CentOS Linux release 7.6.1810 (Core)
\S
Kernel \r on an \m

# 还有用!?表示上个命令的所有参数(一般使用于1个参数的情况)
[root@VM-16-15-centos ~]# cat nginx_2022-01-16.log
123

[root@VM-16-15-centos ~]# ll !$
ll nginx_2022-01-16.log
-rw-r--r-- 1 root root 4 Jan 23 11:22 nginx_2022-01-16.log

# 调用某一个参数
[root@VM-16-15-centos tmp]# ll a.txt b.txt
-rw-r--r-- 1 root root 4 Jan 23 11:25 a.txt
-rw-r--r-- 1 root root 4 Jan 23 11:25 b.txt
# 调用第一个参数
[root@VM-16-15-centos tmp]# cat !:1
cat a.txt
123
# 调用第二个参数
[root@VM-16-15-centos tmp]# ll a.txt b.txt
-rw-r--r-- 1 root root 4 Jan 23 11:25 a.txt
-rw-r--r-- 1 root root 4 Jan 23 11:25 b.txt
[root@VM-16-15-centos tmp]# cat !:2
cat b.txt
456
# 或
[root@VM-16-15-centos tmp]# cat !$		# $表示最后一个(一共就两个参数)
cat b.txt
456
# 也可以用快捷键 ll 按下ESC,松手后按下 “.” ,或者ll 按住Alt不松手在按下“.” 
[root@VM-16-15-centos tmp]# cat a.txt b.txt
123
456
[root@VM-16-15-centos tmp]# ll b.txt
-rw-r--r-- 1 root root 4 Jan 23 11:25 b.txt

29、bash中的快捷键

Ctrl + l	清屏,相当于clear命令
ctr1 + o	执行当前命令,并重新显示本命令
ctrl + s	阻止屏幕输出,锁定	# 分工具
ctr1 + q	允许屏幕输出,解锁	# 分工具
ctr1 + c	终止命令
ctrl + z	挂起命令
ctrl + a	光标移到命令行首,相当于Home
ctrl + e	光标移到命令行尾,相当于End
ctrl + f	光标向右移动一个字符
ctrl + b	光标向左移动一个字符
ctrl + xx	光标在命令行首和光标之间移动
Alt + f		光标向右移动一个单词尾
Alt + b		光标向左移动一个单词首
ctrl + u	从光标处删除至命令行首
ctr1 + k	从光标处删除至命令行尾
A1t + r		删除当前整行
ctrl + w	从光标处向左删除至单词首
Alt + d		从光标处向右删除至单词尾
Alt + Backspace删除左边单词
ctrl + d	删除光标处的一个字符
ctrl + h	删除光标前的一个字符
ctrl + y	将删除的字符粘贴至光标后
A1t + c		交换光标处和之前的字符位置
A1t + u		从光标处开始,将右边一个单词更改为大写
Alt + 1		从光标处开始,将右边一个单词更改为小写
ctrl + t	交换光标处和之前的字符位置
A1t + t		交换光标处和之前的单词位置
A1t + #		提示输入指定字符后,重复显示该字符#次

实例1:

# ctrl + u	从光标处删除至命令行首
[root@VM-16-15-centos ~]# cat -n nginx_2022-01-16.log
[root@VM-16-15-centos ~]#  nginx_2022-01-16.log		# 光标在-n后,敲下ctrl+u

实例2:

# ctr1 + k	从光标处删除至命令行尾
[root@VM-16-15-centos ~]# cat -n nginx_2022-01-16.log
[root@VM-16-15-centos ~]# cat			# 光标在ctrl后,选项和参数都会被删除

30、linux文件类型

# linux下期中文件类型
- 普通文件
d 目录文件directory
b 块设备block
c 字符设备character
l 符号链接文件link
p 管道文件pipe
s 套接字文件socket

31、dirname与basename

文件所在目录 dirname

文件名 basename

[12:02:41 root@ansible network-scripts]#ll /etc/sysconfig/network-scripts/ifcfg-ens192
-rw-r--r--. 1 root root 412 9月  14 22:57 /etc/sysconfig/network-scripts/ifcfg-ens192
dirname : /etc/sysconfig/network-scripts/
basename : ifcfg-ens192

命令类型

dirname与basename都是外部命令

[12:03:21 root@ansible network-scripts]#type dirname
dirname 是 /usr/bin/dirname

[12:06:08 root@ansible network-scripts]#type basename
basename 是 /usr/bin/basename

取一个文件的dirname与basename

[12:06:28 root@ansible network-scripts]#dirname /etc/sysconfig/network-scripts/ifcfg-ens192
/etc/sysconfig/network-scripts
[12:09:02 root@ansible network-scripts]#basename /etc/sysconfig/network-scripts/ifcfg-ens192
ifcfg-ens192

32、cd

回到某个用户的家目录

# 回到yang用户的家目录(当前用户是root)
[12:20:28 root@ansible ~]#cd ~yang
[12:20:37 root@ansible yang]#pwd
/home/yang

普通用户无法切回管理员的家目录

# 当前用yang
[12:22:03 yang@ansible ~]$cd ~root
bash: cd: /root: 权限不够

33、help

查看内部命令的帮助

[12:30:14 root@ansible ~]#type enable 
enable 是 shell 内嵌
[12:30:21 root@ansible ~]#help enable
enable: enable [-a] [-dnps] [-f 文件名] [名称 ...]
    启用和禁用 shell 内嵌。
    
    启用和禁用 shell 的内嵌命令。禁用使您能够执行一个和内嵌
    命令同名的磁盘上的命令,而无须使用完整的路径名。
    
    
    选项:
      -a        打印一个内嵌的列表,并显示其中每一个是否启用
      -n        禁用每一个 NAME 内嵌或者显示一个被禁用的内嵌的列表
      -p        以可重用的格式打印一个内嵌的列表
      -s        仅打印Posix `special'  内嵌的名称
    
    控制动态加载的选项:
      -f        从共享对象 FILENAME 文件中加载 NAME 内嵌
      -d        删除以 -f 选项加载的内嵌
    
    不带选项时,每一个 NAME 内嵌都被启用。
    
    如果要使用 $PATH 中找到的 `test' 而不是 shell 内嵌的版本,
    输入 `enable -n test'。
    
    退出状态:
    返回成功,除非 NAME 不是一个 shell 内嵌或者有错误发生。

外部命令是无法通过help查看的帮助的

# less为外部命令
[12:31:50 root@ansible ~]#type less
less 是 /usr/bin/less
[12:31:54 root@ansible ~]#help less
-bash: help: 没有与 `less' 匹配的帮助主题。尝试 `help help' 或者 `man -k less' 或者 `info less'。

外部命令可以通过命令±-help查看帮助

[13:14:11 root@ansible ~]#less --help
               SUMMARY OF LESS COMMANDS

      Commands marked with * may be preceded by a number, N.
      Notes in parentheses indicate the behavior if N is given.
      A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.

  h  H                 Display this help.
  q  :q  Q  :Q  ZZ     Exit.
 ---------------------------------------------------------------------------

                           MOVING

  e  ^E  j  ^N  CR  *  Forward  one line   (or N lines).
  y  ^Y  k  ^K  ^P  *  Backward one line   (or N lines).
  f  ^F  ^V  SPACE  *  Forward  one window (or N lines).
  b  ^B  ESC-v      *  Backward one window (or N lines).
  ...

34、ls

ls是别名

[13:10:51 root@ansible ~]#type ls 
ls 是 `ls --color=auto' 的别名

用ls原本的意思需要用“\”ls

# 所有文件没有颜色
[13:12:37 root@ansible ~]#\ls

ls常见选项

-a	包含隐藏文件
-l	显示额外的信息
-R	目录递归		# 大写
-ld	目录和符号链接信息
-l	文件分行显示
-S	按从大到小排序		# 大写
-t	按mtime排序
-u	配合-t选项,显示并按atime从新到旧排序
-U	按目录存放顺序显示	# 大写
-×	按文件后缀排序

-R 目录递归

[13:23:06 root@ansible ~]#ll -R tmp
tmp:
总用量 0
-rw-r--r-- 1 root root  0 2月  13 13:22 a.txt
drwxr-xr-x 2 root root 19 2月  13 13:22 test

tmp/test:
总用量 0
-rw-r--r-- 1 root root 0 2月  13 13:22 b.log

# 通过tree查看目录结构
[13:23:08 root@ansible ~]#tree tmp
tmp
├── a.txt
└── test
    └── b.log

1 directory, 2 files

-S 按从大到小排序,默认的单位“k”

[13:25:30 root@ansible ~]#ll -S
总用量 1240244
-rw-r--r--. 1 root root 562196899 9月   4 13:29 harbor-offline-installer-v2.1.4.tgz
-rw-r--r--. 1 root root 341603885 10月 18 09:28 mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 root root  57763752 8月  25 10:33 Xmanager5.exe
-rw-r--r--. 1 root root  12737304 9月  16 14:22 docker-compose-Linux-x86_64
-rwxr-xr-x. 1 root root  10360930 7月  12 2021 ossutil64
-rw-r--r--. 1 root root     12371 11月  4 15:23 testdata.sql
-rw-r--r--. 1 root root      9224 9月  12 2016 mysql57-community-release-el7-9.noarch.rpm

# 为了方便看可以加上“-h”参数
[13:25:25 root@ansible ~]#ll -Sh
总用量 1.2G
-rw-r--r--. 1 root root 537M 9月   4 13:29 harbor-offline-installer-v2.1.4.tgz
-rw-r--r--. 1 root root 326M 10月 18 09:28 mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 root root  67M 8月   9 2021 docker-20.10.6.tgz
-rw-r--r--. 1 root root  56M 8月  25 10:33 Xmanager5.exe
-rw-r--r--. 1 root root  13M 9月  16 14:22 docker-compose-Linux-x86_64
-rwxr-xr-x. 1 root root 9.9M 7月  12 2021 ossutil64
-rw-r--r--. 1 root root  13K 11月  4 15:23 testdata.sql
-rw-r--r--. 1 root root 9.1K 9月  12 2016 mysql57-community-release-el7-9.noarch.rpm

-ld 目录和符号链接信息

# 想看某个目录的属性,并不关系目录里边的文件熟悉可以加上“-ld”参数
[13:31:14 root@ansible ~]#ll /boot
总用量 107124
-rw-r--r--. 1 root root   151918 11月  9 2018 config-3.10.0-957.el7.x86_64
drwxr-xr-x. 3 root root       17 8月   4 2021 efi
drwxr-xr-x. 2 root root       27 8月   4 2021 grub
drwx------. 5 root root       97 8月   4 2021 grub2
-rw-------. 1 root root 57146933 8月   4 2021 initramfs-0-rescue-63a9758edf8c421abeb9517633ba9553.img
-rw-------. 1 root root 21682829 8月   4 2021 initramfs-3.10.0-957.el7.x86_64.img
-rw-------  1 root root 13556486 2月  13 11:10 initramfs-3.10.0-957.el7.x86_64kdump.img
-rw-r--r--. 1 root root   314036 11月  9 2018 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 root root  3543471 11月  9 2018 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 root root  6639904 8月   4 2021 vmlinuz-0-rescue-63a9758edf8c421abeb9517633ba9553
-rwxr-xr-x. 1 root root  6639904 11月  9 2018 vmlinuz-3.10.0-957.el7.x86_64

# 只显示该文件夹本身的属性
[13:34:29 root@ansible ~]#ll -ld /boot
dr-xr-xr-x. 5 root root 4096 2月  13 11:10 /boot

35、文件的时间

文件时间有三种,分别是modify time、change time、access time

查看三种时间的方法

# mtime:
[13:56:02 root@ansible ~]#ll env.sh 
-rw-r--r-- 1 root root 90 12月 25 13:17 env.sh
# ctime
[13:56:08 root@ansible ~]#ll --time=ctime env.sh 
-rw-r--r-- 1 root root 90 12月 25 13:17 env.sh
# atime
[13:56:17 root@ansible ~]#ll --time=atime env.sh  
-rw-r--r-- 1 root root 90 12月 25 13:17 env.sh

modify time:修改文件的时间,针对文件内容

modify time:例如修改了文件内容,
# ll查看到的是mtime及modify time
[13:43:41 root@ansible ~]#ll -h anaconda-ks.cfg                 
-rw-------. 1 root root 1.5K 8月   4 2021 anaconda-ks.cfg

# 用vim进行修改
13:43:41 root@ansible ~]#vim anaconda-ks.cfg

# 修改后时间已经变成了
[13:44:42 root@ansible ~]#ll -h anaconda-ks.cfg
-rw------- 1 root root 1.5K 2月  13 13:44 anaconda-ks.cfg

change time:修改属性的时间,针对属主,属组,读写执行等属性,修改属性modify time不会改变

# 查看ctime时间
[13:56:08 root@ansible ~]#ll --time=ctime env.sh 
-rw-r--r-- 1 root root 90 12月 25 13:17 env.sh
# 修改文件属性
[13:57:50 root@ansible ~]#chown -R yang:yang env.sh
# 查看ctime时间,发现ctime时间已经变了
[13:58:11 root@ansible ~]#ll --time=ctime env.sh
-rw-r--r-- 1 yang yang 90 2月  13 13:58 env.sh

修改文件内容ctime也会改变

# 修改文件内容是大小有可能会发生变化,时间会必会发生变化
# 修改前mtime和ctime一样
[21:36:38 root@ansible ~]#ll -h testlog.sql
-rw-r--r--. 1 root root 291 11月  1 16:23 testlog.sql
[21:36:46 root@ansible ~]#ll --time=ctime testlog.sql
-rw-r--r--. 1 root root 291 11月  1 16:23 testlog.sql

# 修改文件
[21:38:31 root@ansible ~]#vim testlog.sql

# 修改文件后
[21:39:25 root@ansible ~]#ll -h testlog.sql
-rw-r--r-- 1 root root 300 2月  13 21:39 testlog.sql
[21:39:28 root@ansible ~]#ll --time=ctime testlog.sql  
-rw-r--r-- 1 root root 300 2月  13 21:39 testlog.sql

注意: 注意,只修改属性,例如属主,mtime并不会发生变化。

access time:文件只要被读就会被access time记录

# 查看文件atime
[14:02:58 root@ansible ~]#ll --time=atime  testdata.sql 
-rw-r--r--. 1 root root 12371 11月  4 15:23 testdata.sql
# 查看文件
[14:03:07 root@ansible ~]#cat testdata.sql
# 发现文件atime已经变了
[14:04:55 root@ansible ~]#ll --time=atime  testdata.sql
-rw-r--r--. 1 root root 12371 2月  13 14:04 testdata.sql

同时查看三个时间

[21:50:01 root@ansible ~]#stat test.sql 
  文件:"test.sql"
  大小:38              块:8          IO 块:4096   普通文件
设备:fd00h/64768d      Inode:52102       硬链接:1
权限:(0644/-rw-r--r--)  Uid:( 1001/    yang)   Gid:(    0/    root)
最近访问:2021-11-01 16:24:44.644054348 +0800
最近更改:2021-10-12 14:48:20.410504560 +0800
最近改动:2022-02-13 21:44:28.076626061 +0800
创建时间:-

35、ss命令

# 服务端IP地址和端口192.168.31.206:22  客户端通过192.168.31.114:54661来访问服务端
yang@yang-user:~$ ss -nt
State     Recv-Q    Send-Q     		Local Address:Port     Peer Address:Port          Process                      
ESTAB    0             0            192.168.31.206:22      192.168.31.114:54661

36、diff 命令

yang@yang-user:~/tmp$ cat -n test1
     1  #! /bin/bash
     2  echo "hollo"
     3  echo "test"
     4  echo "this is test1"
yang@yang-user:~/tmp$ cat -n test2 
     1  #! /bin/bash
     2  echo "word"
     3  echo "test"
     4  echo "this is test2"
yang@yang-user:~/tmp$ diff test1 test2 
2c2
< echo "hollo"
---
> echo "word"
4c4
< echo "this is test1"
---
> echo "this is test2"

37、chage

更改用户密码过期时间
在 CentOS 上,可以使用 chage 命令来管理用户密码的到期和延期。以下是一些常用的命令示例:

查看用户密码信息和到期日期:

chage -l <username>

将 替换为要查看密码信息的用户名。

禁用用户密码过期:

chage -M 99999 <username>

将 替换为要禁用密码过期的用户名。将密码最大使用天数设置为 99999 将有效地禁用密码过期。

设置用户密码到期日期:

chage -E YYYY-MM-DD <username>

将 替换为要设置密码到期日期的用户名,YYYY-MM-DD 是日期格式。例如,2023-12-31 表示将密码到期日期设置为 2023 年 12 月 31 日。

强制用户在下次登录时更改密码:

chage -d 0 <username>

将 替换为要强制用户更改密码的用户名。设置密码的最小天数为 0,这将使用户在下次登录时被提示更改密码。

请注意,对于某些特定的用户(例如 root 用户),可能不应禁用密码过期或将密码到期日期延期太久。确保在进行密码到期管理时谨慎操作,并根据实际需求来设置合理的密码策略。

38、centos中bash test.sh和source tesh.sh的区别

执行结果:

# 三种执行结果一样
root@VM-4-14-centos shell]# ./nginx_pid.sh 
Nginx is running with PID: 13024
13025
13026
[root@VM-4-14-centos shell]# bash nginx_pid.sh 
Nginx is running with PID: 13024
13025
13026
[root@VM-4-14-centos shell]# source nginx_pid.sh 
Nginx is running with PID: 13024
13025
13026

在 CentOS 中,bash test.sh 和 source test.sh 是两种执行脚本的不同方式,具有一些区别。

1、bash test.sh:

  • 使用 bash 命令显式地执行脚本 test.sh。
  • 新开一个子进程来执行脚本,并在子进程中运行。
  • 脚本的执行结果不会影响当前 shell 环境。
  • 脚本中的变量和环境设置只在子进程中有效,不会影响当前 shell 环境。

2、source test.sh 或 . test.sh:

  • 使用 source 或 . 命令在当前 shell 环境中执行脚本 test.sh。
  • 在当前 shell 环境中运行,而不是新开一个子进程。
  • 脚本中的变量和环境设置会影响当前 shell 环境,例如,修改变量的值或添加新的环境变量。
  • 脚本中的函数和别名会在当前 shell 环境中生效。

通常情况下,如果您希望脚本中的变量、函数和别名等在当前 shell 环境中生效,可以使用 source 或 . 命令。这对于加载配置文件、设置环境变量等操作非常有用。如果您只需要在子进程中执行脚本,并且不需要对当前 shell 环境做出任何修改,可以使用 bash 命令来执行脚本。

39、centos中su - user和su user的区别

在 CentOS 中,su - user 和 su user 是两种切换用户身份的方式,有一些区别:

1、su - user:

  • 使用 - 参数切换到指定用户 user 的身份,并同时加载该用户的环境变量和配置文件。
  • 在切换到目标用户之前,会先切换到目标用户的家目录。
  • 该方式会重新加载目标用户的环境,包括修改的环境变量、别名、路径等。
  • 执行 su - user 后,当前工作目录会变为目标用户的家目录。

2、su user:

  • 不使用 - 参数切换到指定用户 user 的身份,不会加载该用户的环境变量和配置文件。
  • 不会切换到目标用户的家目录,而是继续在当前工作目录中运行。
  • 执行 su user 后,当前用户的环境保持不变,包括环境变量、别名等。

例:

# su - user
[root@VM-4-14-centos ~]# pwd
/root
[root@VM-4-14-centos ~]# su - yang
Last login: Mon Jun  5 22:15:47 CST 2023 on pts/0
[yang@VM-4-14-centos ~]$ pwd
/home/yang

# su user
[root@VM-4-14-centos ~]# pwd
/root
[root@VM-4-14-centos ~]# su yang
[yang@VM-4-14-centos root]$ pwd
/root

通常情况下,如果您需要完全切换到目标用户的身份,并获取目标用户的环境变量和配置文件,可以使用 su - user 命令。这对于需要运行以目标用户身份执行的命令、操作目标用户的文件等非常有用。

如果您只是暂时切换到目标用户的身份执行一些命令,但不需要加载目标用户的环境变量,可以使用 su user 命令。

请注意,使用 su 命令切换到其他用户时需要输入目标用户的密码。确保在切换用户时谨慎操作,并遵循安全最佳实践。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值