2.Linux基础

发布时间 2023-06-24 01:01:02作者: 人间丶迷走

1.查看系统版本信息

  • 查看操作系统内核

    # centos
    [demo@centos ~]$ uname -r
    3.10.0-693.el7.x86_64
    
    # ubuntu
    walker@ubuntuLinux:~$ uname -r
    5.11.0-43-generic
    
  • 主机名查看与设置

    # 查看主机名
    [demo@centos ~]$ hostname
    centos
    
    # 查看主机IP
    [demo@centos ~]$ hostname -I
    10.0.2.15 172.32.115.11 
    
    # 设置主机名-临时修改
    [root@centos ~]# hostname new-name
    
    # 设置主机名-永久修改,其实是修改了/etc/hostname文件
    [root@centos ~]# hostnamectl set-hostname my-new-name
    
  • 查看登录信息

    # 查看当前登录用户
    [demo@centos ~]$ whoami
    demo
    
    # 查看当前终端号
    [demo@centos ~]$ tty
    /dev/pts/0
    
    # 查看登录用户与终端号 登录时间
    [demo@centos ~]$ who am i
    demo     pts/0        2023-06-24 07:24 (172.32.115.1)
    
    # 查看登录用户默认shell
    [demo@centos ~]$ echo $SHELL
    /bin/bash
    
    
    # 查看所有登录用户 
    	#虚拟终端:本机登录,dev/tty/2,后面没有IP地址
    	#伪终端:SSH远程登录,dev/pts/0,后面有IP地址
    [demo@centos ~]$ who
    demo     pts/0        2023-06-24 07:24 (172.32.115.1)
    root     tty/2        2023-06-24 07:24 
    
    # 查看登录用户当前操作
    [demo@centos ~]$ w
     08:03:18 up 39 min,  2 users,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    172.32.115.1     07:24    6.00s  0.04s  0.01s w
    demo     pts/1    172.32.115.1     08:03    6.00s  0.06s  0.06s vim demo.txt
    
  • 修改终端提示符

    # 查看默认提示符配置
    [root@centos ~]# echo $PS1
    [\u@\h \W]\$
    
    
    # 添加高亮 [1;31m] 颜色为红色 5是闪烁
    # 临时修改
    [root@centos ~]#PS1="\[\e[1;31m\][\u@\h \W]\\$\[\e[0m\]"
    
    # centos永久修改,在/etc/profile.d/下新建一个脚本
    [root@centos ~]#echo 'PS1="\[\e[1;31m\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/myps.sh
    [root@centos ~]#chmod 755 /etc/profile.d/myps.sh
    
    # Ubuntu主机添加到家目录下的.bashrc文件即可
    
    
  • 内部命令与外部命令

    # 查看内部命令 
    [root@centos ~]#enable
    
    [root@centos ~]#enable | wc -l
    61
    
    # type 区分内部命令与外部命令 
    [root@centos ~]#type echo
    echo is a shell builtin
    [root@centos ~]#type hostname
    hostname is hashed (/usr/bin/hostname)
    
    # 说明:每个外部命令必须有对应的文件才能执行,执行外部命令的时候会根据环境变量的路径一次去查找外部命令所对应的文件
    [root@centos ~]#echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    
    

2、查看硬件信息和系统版本信息

  • 查看cpu

    [demo@centos7 ~]$ cat /proc/cpuinfo
    [demo@centos7 ~]$ lscpu
    
  • 查看内存

    [demo@centos7 ~]$ free -h
                  total        used        free      shared  buff/cache   available
    Mem:           2.0G         89M        1.8G        8.4M        112M        1.7G
    Swap:          2.0G          0B        2.0G
    
    [demo@centos7 ~]$ cat /proc/meminfo
    MemTotal:        2048744 kB
    MemFree:         1841380 kB
    MemAvailable:    1808728 kB
    Buffers:            2108 kB
    Cached:            78268 kB
    
  • 查看磁盘

    [demo@centos7 ~]$ lsblk
    NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   15G  0 disk 
    ├─sda1            8:1    0    2M  0 part 
    ├─sda2            8:2    0    1G  0 part /boot
    └─sda3            8:3    0   14G  0 part 
      ├─centos-root 253:0    0   12G  0 lvm  /
      └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
    sr0              11:0    1 1024M  0 rom  
    
    [demo@centos7 ~]$ cat /proc/partitions
    major minor  #blocks  name
    
      8        0   15728640 sda
      8        1       2048 sda1
      8        2    1048576 sda2
      8        3   14676992 sda3
      11        0    1048575 sr0
    
    253        0   12578816 dm-0
    253        1    2097152 dm-1
    [demo@centos7 ~]$ 
    
  • 查看操作系统发行版版本

    [demo@centos7 ~]$ cat /etc/redhat-release 
    CentOS Linux release 7.4.1708 (Core) 
    
    [demo@centos7 ~]$ 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"
    

    3、强引用与弱引用(引号)

  • 强引用:单引号(''),把单引号中的变量强制作为字符串输出。 变量和命令均无法识别

    [demo@centos7 ~]$ echo '$PATH'
    $PATH
    
    [demo@centos7 ~]$ echo $PATH
    /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/zk/.local/bin:/home/zk/bin
    
  • 弱引用:双引号(""),把双引号中的变量转化为变量所代表的实际值输出。 仅能识别变量

    [demo@centos7 ~]$ echo "$PATH"
    /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/zk/.local/bin:/home/zk/bin
    
    [demo@centos7 ~]$ echo $PATH
    /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/zk/.local/bin:/home/zk/bin
    
  • 执行命令:反引号(``),把反引号中的内容当作命令执行,输出命令的执行结果。 变量和命令都可以识别

    [demo@centos7 ~]$ echo `echo $HOSTNAME`
    centos7
    
  • 执行命令:$(),作用跟反引号相同,支持嵌套,反引号不支持嵌套

    [demo@centos7 ~]$ echo $(echo $HOSTNAME)
    centos7
    
    [demo@centos7 ~]$ ls $(touch $(date '+%F').txt)
    2022-06-08.txt
    
  • 样例:生成文件名含主机、日期的文件

    [demo@centos7 ~]$ echo `hostname`_`date '+%F'`.txt
    centos7_2022-06-08.txt
    [demo@centos7 ~]$ echo $(hostname)_$(date '+%F').txt
    centos7_2022-06-08.txt
    
    [demo@centos7 ~]$ touch $(hostname)_$(date '+%F').txt
    [demo@centos7 ~]$ ls
    a.txt  centos7_2022-06-08.txt
    
  • 批量操作:大括号{},中间的内容支持列举,指定范围,设置步长间隔,并支持多个{}组合

    #列举
    [demo@centos7 ~]$ echo name{1,3,5}
    name1 name3 name5
    
    #指定范围,倒序
    [demo@centos7 ~]$ echo file{7..4}
    file7 file6 file5 file4
    
    #指定范围3到9,并设置步长间隔为2 
    [demo@centos7 ~]$ echo aa{3..9..2}
    aa3 aa5 aa7 aa9
    
    #指定范围3到9,并设置步长间隔为2,并指定格式003
    [demo@centos7 ~]$ echo aa{003..9..2}
    aa003 aa005 aa007 aa009
    
    #支持字母
    [demo@centos7 ~]$ echo aa{C..F}
    aaC aaD aaE aaF
    
    #组合
    [demo@centos7 ~]$ echo {1..4}.{txt,log}
    1.txt 1.log 2.txt 2.log 3.txt 3.log 4.txt 4.log
    

cat指令的高级用法

[root@centos ~]# cat <<EOF > file.txt
> num1=20
> num2=18
> num3=33
> EOF
[root@centos ~]# cat file.txt 
num1=20
num2=18
num3=33
[root@centos ~]#

更简洁的

[root@centos ~]# cat > file.txt
num1=20
num2=18
num3=33

#ctrl+d退出输入

[root@centos ~]# cat file.txt 
num1=20
num2=18
num3=33

vim ss.sh

a=`cat file.txt |cut -d "=" -f 2 |head -1`
b=`cat file.txt |cut -d "=" -f 2 |head -2|tail -1`
c=`cat file.txt |cut -d "=" -f 2 |tail -1`
echo $a $b $c