Linux的基本命令


Linux执行命令的格式要求:
命令+空格+选项+空格+参数
命令: 需要执行的操作
选项: 要执行操作的特性
参数: 要执行操作的对象
注: 三者之间要用空格隔开,在执行命令时,选项和参数可以没有,但不是所有的命令都支持,如果没有相关的选项和参数,一般都有默认的(格式会自己判断)
长短选项也可以组合使用
例:

[root@www /]# ls --all -l -h
total 40K
dr-xr-xr-x.  19 root root  264 Nov  9 23:00 .
dr-xr-xr-x.  19 root root  264 Nov  9 23:00 ..
dr-xr-xr-x.   2 root root    6 Aug 10  2021 afs
lrwxrwxrwx.   1 root root    7 Aug 10  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root 4.0K Oct 30 19:16 boot
drwxr-xr-x.  20 root root 3.4K Mar  5 16:11 dev
drwxr-xr-x. 132 root root 8.0K Mar  5 16:11 etc
-rw-------.   1 root root  12K Nov  6 19:44 .haha.swp
drwxr-xr-x.   3 root root   20 Feb 28 21:32 home
lrwxrwxrwx.   1 root root    7 Aug 10  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Aug 10  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Aug 10  2021 media
drwxr-xr-x.   3 root root   18 Oct 30 19:03 mnt
drwxr-xr-x.   2 root root    6 Aug 10  2021 opt
dr-xr-xr-x. 369 root root    0 Mar  5 16:11 proc
dr-xr-x---.  13 root root 4.0K Mar  5 16:34 root
drwxr-xr-x.  43 root root 1.2K Mar  5 16:11 run
lrwxrwxrwx.   1 root root    8 Aug 10  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Aug 10  2021 srv
dr-xr-xr-x.  13 root root    0 Mar  5 16:11 sys
drwxr-xr-x.   4 root root   24 Nov 11 21:29 test
drwxrwxrwt.  20 root root 4.0K Mar  5 16:34 tmp
drwxr-xr-x.  12 root root  144 Oct 30 19:01 usr
drwxr-xr-x.  20 root root 4.0K Oct 30 19:16 var
[root@www /]# 

“ - ”:后面跟选项,称为短选项
“ – ”:后面也更选项,称为长选项
长选项和短选项的区别:因为短选项可以合并(例如“ls -l -h”等同于“ls -lh”)长短选项是用于区分选项是一个还是两个(如果要将“ls -lh”表示为一个选项则用到长选项“ls --lh”)

man手册查看命令的选项

查看各个命令选项及各个选项的作用需要用到man语句(全称manual,释义为手册),各个命令的组合用法多种多样,基本全都包含在man手册里面,每一个命令的选项都是独有的
man命令的语法结构:
man 章节编号 需要查询手册的命令
(注:章节编号不一定需要,但默认的显示的是最开头的章节)
例:man 1 ls
在这里插入图片描述
图片中:
ls(1):(1)表示章节,命令的man手册的选项有很多,所以被他们分为了众多的章节,但所有的命令最多有9个章节,并不是每个命令都拥有所有章节,每个章节的说明和帮助的角度不一样。
NAME: 用来解释命令的作用
SYOPSIS: 命令语法的格式
DESCRIPTION: 对命令的一般性描述及选项的描述
对章节的操作:
【回车】:往后按照一行一行的阅读
【空格】:往后按页进行翻页阅读
【q】:退出man手册,返回命令行
有一些命令的man手册不一定有所有的章节
例:

[root@www /]# man 2 passwd
No manual entry for passwd in section 2
[root@www /]# 

远程建立链接

ssh root@IPV4网段
ssh: 用于连接的命令
root: 用户名root指的是用户超级管理员用户
@: 表示分隔符

[C:\~]$ ssh root@IPV4网段


Connecting to IPV4网段:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Sun Mar  5 16:34:13 2023 from 
[root@www ~]# 

远程关机

shutdown -h now: 立即关机(注:远程登录普通用户登录并不能关机,只能切换到超级管理员用户登录)

[redhat@www ~]$ shutdown -h now------------(普通用户)
User root is logged in on sshd.
Please retry operation after closing inhibitors and logging out other users.
Alternatively, ignore inhibitors and users with 'systemctl poweroff -i'.
[redhat@www ~]$ su - root--------------(切换到超级管理员用户)
Password: 
[root@www ~]# shutdown -h now
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.119.128:22) at 22:06:15.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

退出当前用户登录

logout: 退出当前用户登录,当只登陆了一个用户的时候,使用logout命令是退出用户登录,并断开链接,当登录用户后,再切换用户登录,这时使用logout命令就会退出当前用户回到上一次用户登录的状态。
情况一:

[root@www ~]# logout

Connection closed.

Disconnected from remote host(192.168.119.128:22) at 22:08:54.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

情况二:

[root@www ~]# su - redhat------------(切换到redhat用户)
[redhat@www ~]$ logout
[root@www ~]#

退出用户登录,且当前用户断开链接

exit: 退出登录用户,当前会话断开链接(任何登录用户都可以使用),先退出再断开链接,在已经多次切换用户的状态下使用此命令则效果等同于logout,回到上一个登录的用户,再次使用exit就会退出登录用户,断开来链接。

[root@www ~]# su - redhat
[redhat@www ~]$ su - root
Password: 
[root@www ~]# exit
logout
[redhat@www ~]$ exit
logout
[root@www ~]# exit
logout

Connection closed.

Disconnected from remote host(192.168.119.128:22) at 22:29:32.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

重启系统

reboot: 重启系统(注:此命令只能在超级管理员用户登录状态下执行)

[redhat@www ~]$ reboot-------------(普通用户登录状态)
User root is logged in on sshd.
Please retry operation after closing inhibitors and logging out other users.
Alternatively, ignore inhibitors and users with 'systemctl reboot -i'.
[redhat@www ~]$ su - root-----------------(切换到超级管理员用户)
Password: 
[root@www ~]# reboot
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.119.128:22) at 22:20:31.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@192.168.119.128


Connecting to 192.168.119.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Sun Mar  5 22:20:27 2023
[root@www ~]# 

切换用户

需要使用到su相关命令
注: 在切换用户的时候,普通用户切换到任何用户的时候都需要验证目标用户的密码,而超级管理员用户切换到任何用户都不需要输入密码,在password那一行需要输入密码,本人是看不见的,输入密码后回车就能进行用户切换
su 仅一个su命令,效果是默认切换到root用户(超级管理员用户),不会改变登录用户环境变量

[redhat@www ~]$ pwd
/home/redhat
[redhat@www ~]$ su
Password: 
[root@www redhat]# pwd
/home/redhat
[root@www redhat]# 

su - 切换用户身份及环境变量

[redhat@www ~]$ pwd
/home/redhat
[redhat@www ~]$ su -
Password: 
[root@www ~]# pwd
/root
[root@www ~]# 

su 用户名 切换到指定的用户身份,只变换用户身份,但不改变用户环境变量

[root@www ~]# pwd
/root
[root@www ~]# su redhat
[redhat@www root]$ pwd
/root
[redhat@www root]$

su - 用户名切换用户身份,且切换用户环境变量

[redhat@www ~]$ pwd
/home/redhat
[redhat@www ~]$ su - root
Password: 
[root@www ~]# pwd
/root
[root@www ~]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值