软连接与硬连接

  • Linux中的链接文件类似于windows中的快捷方式
  • 软连接特点:软连接可以跨分区,可以对目录进行链接,源文件删除后,链接文件不可用
  • 软连接命令格式:ln -s 源文件路径 目标路径
  • 注意:创建链接时一定要写目录或文件的绝对路径,哪怕是在当前路径下,也要写绝对路径·
  1. [root@localhost ~]# touch hello.soft
  2. [root@localhost ~]# ls
  3. #创建软连接(必须要绝对路径创建)
  4. [root@localhost ~]# ln -s /root/hello.soft /opt
  5. [root@localhost ~]# ls /opt
  6. #查看连接文件详细属性
  7. [root@localhost ~]# ls -l /opt/hello.soft
  8. lrwxrwxrwx. 1 root root 16 3 21 14:28 /opt/hello.soft -> /root/hello.soft
  9. #提示:链接文件的权限最终取决于源文件的权限
  10. #普通用户验证
  11. [lisi@localhost ~]$ ls /opt
  12. hello.soft
  13. [lisi@localhost ~]$ ls -l /opt/hello.soft
  14. lrwxrwxrwx. 1 root root 16 3 21 14:28 /opt/hello.soft -> /root/hello.soft
  15. [lisi@localhost ~]$ cat /opt/hello.soft
  16. cat: /opt/hello.soft: 权限不够
  17. #提示:由于源文件存放于/root目录下,而普通用户对/root目录没有任何权限,所以普通用户无法查看
  18. #删除源文件
  19. [root@localhost ~]# rm -f /root/hello.soft
  20. [root@localhost ~]# ls
  21. #山粗源文件后,软链接文件不可用
  22. [root@localhost ~]# ls -l /opt/hello.soft
  23. lrwxrwxrwx. 1 root root 16 3 21 14:28 /opt/hello.soft -> /root/hello.soft
  24. #创建文件并创建软连接
  25. [root@localhost ~]# touch hello.soft
  26. [root@localhost ~]# ln -s /root/hello.soft /opt
  27. [root@localhost ~]# ls -l /opt/hello.soft
  28. lrwxrwxrwx. 1 root root 16 3 21 14:39 /opt/hello.soft -> /root/hello.soft
  29. #删除链接文件后,源文件仍然可用
  30. [root@localhost ~]# rm -f /opt/hello.soft
  31. [root@localhost ~]# ls
  32. [root@localhost ~]# cat hello.soft
  33. #对目录创建软连接
  34. [root@localhost ~]# ln -s /root/test1 /opt/
  35. [root@localhost ~]# ls -ld /opt/test1
  36. lrwxrwxrwx. 1 root root 11 3 21 14:44 /opt/test1 -> /root/test1
  37. 3创建链接时一定要写目录或文件的绝对路径,哪怕是在当前路径下,也要写绝对路径
  38. [root@localhost ~]# ln -s hello.soft /opt
  39. [root@localhost ~]# ls /opt
  40. hello.soft test1
  41. [root@localhost ~]# ls -l /opt/hello.soft
  42. lrwxrwxrwx. 1 root root 10 3 21 14:47 /opt/hello.soft -> hello.soft
  • 硬链接特点:硬连接不可以跨分区,不可以对目录进行链接,源文件删除后,链接文件仍然可用
  • 硬连接命令格式:ln 源文件路径 目标路径
  1. #创建文件,并创建硬连接
  2. [root@localhost ~]# touch hello.hard
  3. [root@localhost ~]# ln /root/hello.hard /opt/
  4. [root@localhost ~]# ls /opt
  5. hello.hard hello.soft test1
  6. #向硬连接的源文件写入内容
  7. root@localhost ~]# echo 123 > /root/hello.hard
  8. #查看源文件内容
  9. [root@localhost ~]# cat /root/hello.hard
  10. 123
  11. #查看链接文件内容,以同步更新
  12. [root@localhost ~]# cat /opt/hello.hard
  13. 123
  14. #向链接文件写入内容,查看源文件以同步更新
  15. [root@localhost ~]# echo xx >> /opt/hello.hard
  16. #擦看源文件,以同步更新
  17. [root@localhost ~]# cat /root/hello.hard
  18. 123
  19. xx
  20. #硬连接文件的特点可以保持文件属性不发生改变
  21. [root@localhost ~]# ls -l /root/hello.hard
  22. -rw-r--r--. 2 root root 7 3 21 14:55 /root/hello.hard
  23. [root@localhost ~]# ls -l /opt/hello.hard
  24. -rw-r--r--. 2 root root 7 3 21 14:55 /opt/hello.hard
  25. #并且硬连接文件的i节点号相同
  26. [root@localhost ~]# ls -i /root/hello.hard
  27. 33711090 /root/hello.hard
  28. [root@localhost ~]# ls -i /opt/hello.hard
  29. 33711090 /opt/hello.hard
  30. #硬连接不允许对目录进行连接
  31. root@localhost ~]# ln /root/test1 /opt
  32. ln: "/root/test1": 不允许将硬链接指向目录
  33. #硬连接源文件删除后,链接文件仍然可用
  34. [root@localhost ~]# rm -f /root/hello.hard
  35. [root@localhost ~]# cat /opt/hello.hard
  36. 123
  37. xx
  38. #向硬连接文件写入内容
  39. [root@localhost ~]# echo abc >> /opt/hello.hard
  40. [root@localhost ~]# cat /opt/hello.hard
  41. 123
  42. xx
  43. abc
  44. #硬连接不允许跨分区
  45. [root@localhost ~]# lsblk
  46. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  47. sda 8:0 0 20G 0 disk
  48. ├─sda1 8:1 0 1G 0 part /boot
  49. └─sda2 8:2 0 19G 0 part
  50. ├─centos-root 253:0 0 17G 0 lvm /
  51. └─centos-swap 253:1 0 2G 0 lvm [SWAP]
  52. sr0 11:0 1 4.3G 0 rom
  53. [root@localhost ~]# ln /root/hello.soft /boot
  54. ln: 无法创建硬链接"/boot/hello.soft" => "/root/hello.soft": 无效的跨设备连接

内部命令与外部命令

  • 什么是命令:用来实现某一种功能的指令或程序
  • 命令的执行依赖于解释器(例如:/bin/bash),/etc/shells文件存放系统可用的shell
    • 用户——解释器(shell外壳)——内核
  • [root@localhost ~]# shell 终端 交互接口 用户接口
  1. #搜索命令所在的绝对路径
  2. [root@localhost ~]# which ls
  3. alias ls='ls --color=auto'
  4. /usr/bin/ls
  5. root@localhost ~]# ls /usr/bin/ls
  6. /usr/bin/ls
  7. #直接运行程序文件
  8. [root@localhost ~]# /usr/bin/ls
  9. [root@localhost ~]# /usr/bin/ls /opt
  10. hello.hard hello.soft t1 test1 test.txt
  11. [root@localhost ~]# which cat
  12. [root@localhost ~]# ls /usr/bin/cat
  13. /usr/bin/cat
  14. [root@localhost ~]# which pwd
  15. /usr/bin/pwd

Linux命令的分类

  • 内部命令:shell程序自带的基本管理命令
  • 外部命令:有独立的外部可执行程序文件命令
  • type 用于区别内部命令与外部命令
  • which 用于查找可以执行程序文件位置
  1. [root@localhost opt]# type ls
  2. [root@localhost opt]# type cat
  3. [root@localhost opt]# type hash
  4. [root@localhost ~]# echo $PATH
  5. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  6. [root@localhost ~]# hash
  7. 命中 命令
  8. 1 /usr/bin/cat
  9. 1 /usr/bin/ls
  10. [root@localhost opt]# hash -r
  11. [root@localhost opt]#
  12. [root@localhost opt]# hash
  13. hash: 哈希表为空
  14. [root@localhost opt]# ls
  15. hello.hard hello.soft t1 test1 test.txt
  16. [root@localhost opt]# hash
  17. 命中 命令
  18. 1 /usr/sbin/ls
  • 总结:
    • shell程序是用户和系统之间的接口,用于解释用户的命令
    • 查找命令对应的程序文件所在位置:which 命令
    • shell程序大多数存放在/etc/shells文件中
    • 系统默认使用的shell为/bin/bash
    • 查看当前使用的shell:echo $SHELL
    • 区别内部命令与外部命令的方式:typt 命令
    • shell程序查找可执行程序文件路径定义在$PATH环境变量中
    • shell查找的外部命令路径结果会记录到缓存的hash表中

help 命令帮助手册

  • help命令用于查看shell内部命令的帮助信息,包括使用方法、选项等…
  • 命令格式:help [选项] 命令
  1. #获取内部命令帮助信息
  2. [root@localhost etc]# help cd
  3. #help无法获取外部命令的帮助信息
  4. root@localhost etc]# help ls
  5. bash: help: 没有与 `ls' 匹配的帮助主题。尝试 `help help' 或者 `man -k ls' 或者 `info ls'。
  6. [root@localhost etc]# type help
  7. help 是 shell 内嵌
  8. #获取help命令本身的帮助信息
  9. [root@localhost etc]# help help
  10. [root@localhost etc]# type cat
  11. cat 是 /usr/bin/cat
  12. [root@localhost etc]# help cat
  13. bash: help: 没有与 `cat' 匹配的帮助主题。尝试 `help help' 或者 `man -k cat' 或者 `info cat'。
  14. #查看命令帮助手册(命令自带)
  15. [root@localhost etc]# cat --help
  16. [root@localhost etc]# ls --help

man 获取命令帮助手册

  • man 命令用于查看系统命令的帮助信息,包括使用方法、选项、使用例子等…,对比—help ,mna 输出的信息更加详细
  • 命令格式:man [-选项] 命令
  • 常用快捷操作
    • 向下键向下移一行
    • 向上键向上移一行
    • [Page Down] 向下翻一页
    • [Page Up] 向上翻一页
    • /关键字 #搜索关键字,配合n(向下查询)、N(向上查询)
    • q 退出
  1. [root@localhost etc]# man ls
  2. [root@localhost etc]# man cat
  3. [root@localhost etc]# man touch
  4. [root@localhost etc]# man mkdir
  1. [root@localhost etc]# info ls

Google/百度(Google,算法)

Linux系统的运行级别

Linux系统运行级别:linux系统有7个运行级别,不同的运行级别运行的程序和功能都不一样,而Linux系统默认是运行在一个标准的级别上,系统运行级别文件/etc/inittab文件

运行级别 0:所有进程被终止,机器将有序的停止,关机时系统处于这个运行级别(关机)

运行级别 1:单用户模式,(root用户进行系统维护),系统里运行的所有服务也都不会启动

运行级别 2:多用户模式(网络文件系统NFS服务没有被启动)

运行级别 3:完全多用户模式,(有NFS网络文件系统)标准的运行级别

运行级别 4:系统未使用

运行级别 5:登录后,进入带GUI的图形化界面,标准的运行级别

运行级别 6:系统正常关闭并重启
runlevel: 查看当前系统运行级别
查看系统运行等级:[root@localhost ~]# systemctl get-default
graphical.target
[root@localhost ~]# systemctl set-default XXXX.target 更改系统运行级别

  1. #查看当前系统运行级别
  2. [root@localhost etc]# runlevel
  3. N 5
  4. #解释;当前系统处于的运行级别
  5. #解释:N代表没有从任何级别跳转过来
  6. #切换系统运行级别
  7. [root@localhost ~]# init N
  8. #查看运行级别文件内容
  9. [root@localhost ~]# cat /etc/inittab
  10. # inittab is no longer used when using systemd.
  11. #
  12. # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
  13. #
  14. # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
  15. #
  16. # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
  17. #
  18. # multi-user.target: analogous to runlevel 3 #运行级别3
  19. # graphical.target: analogous to runlevel 5 #运行级别5
  20. #
  21. # To view current default target, run:
  22. # systemctl get-default #查看当前系统默认的运行级别
  23. #
  24. # To set a default target, run:
  25. # systemctl set-default TARGET.target #修改当前系统默认运行级别
  26. #查看默认运行级别
  27. [root@localhost ~]# systemctl get-default
  28. graphical.target #默认运行级别为5
  29. #修改默认运行级别为3
  30. [root@localhost ~]# systemctl set-default multi-user.target
  31. [root@localhost ~]# systemctl get-default
  32. multi-user.target
  33. #修改默认运行级别为5
  34. [root@localhost ~]# systemctl set-default graphical.target
  35. [root@localhost ~]# systemctl get-default
  36. graphical.target

关机与重启

  • linux下常用的关机命令有:shutdown、halt、poweroff、init
    • init 0 关机
    • halt #立刻关机
    • poweroff #立刻关机 (记这个)
    • shutdown –h now #立刻关机
    • shutdown -h 10 #10分钟后自动关机
  1. [root@localhost ~]# poweroff
  • 重启命令:reboot shutdown
    • reboot #立刻重启 (记这个)
    • shutdown -r now #立刻重启
    • shutdown -r 10 #过十分钟后重启
  1. [root@localhost ~]# reboot