目录
Linux的命令语法
命令名 [选项]…… [参数]……
1.命令名:一般由多个小写字母组成
2.选项:[]里面表示可有可无
... 表示可以有多个选项
v 选项的格式:
以 - 开头的,后面一般跟一个字母或数字,多数情况下多个选项可以合并
以 -- 开头的,后面一般跟单词,不能合并
没有 - 或 --,而直接是小写字母,说明有多个选项是可以合并
3.参数:提供给命令操作的文件或者目录等等
## 一定要注意空格,空格是默认的分隔符
1. who 查看当前登入shell的用户
[root@web-server ~]# who -q //查看当前登入shell的用户并统计
-q 所有登入的用户名和登录用户的数量
**查看当前登入shell的用户**
2. ls 列出文件信息
1. ls [选项]... [参数]...
[root@localhost ~]# ls //列出当前目录下所有文件(不包含隐藏文件)
[root@localhost ~]# ls -a //列出/a目录下所有文件(不包含隐藏文件)
选项:
-a all 列出所有文件,包括隐藏文件
-l list 以列表的形式列出文件的详细信息
-d directory 列出目录本身。和 -l 结合使用列出目录本身的详细信息
-h human 与 -l 一起使用,以人类易于阅读的方式显示文件的详细信息
-i indoe 显示文件的inode号
-R recursive 递归显示子目录
-t time 以时间顺序排序,最新的文件在上边,默认以文件名排序
2.文件查看补充内容[统配符]
* 代表零个或多个任意字符
?代表一个任意字符
3.系统 关机命令
1.先将服务都关闭掉,再执行halt -pf拔电源:
[root@localhost ~]# init 0
2.立刻关机,先给用户发送消息,然后关闭服务,拔掉电源:
[root@localhost ~]# shutdown -h now
3.延迟关机:
[root@localhost ~]# shutdown -h 10 //十分钟之后关机4.取消正在运行的关机命令:
[root@localhost ~]# shutdown -c //cancel,取消关机
[root@localhost ~]# poweroff //直接关闭电源,容易丢失文件,不建议使用
[root@localhost ~]# halt
以上关机命令不需要全部掌握,选一个适合自己的就好
4.重启系统命令
重启虚拟机
1. [root@localhost ~]#init 6
2. [root@localhost ~]# reboot
3. [root@localhost ~]# shutdown -r now //立刻重启
延迟重启:[root@localhost ~]#shutdown -r 10
5. 查看系统信息命令
1、查看磁盘信息
[root@localhost ~]# df -h #也可以查看系统的挂载情况[root@localhost ~]# lsblk
2、查看内存信息
[root@localhost ~]# top[root@localhost ~]# free -m
[root@localhost ~]# cat /proc/meminfo
3、查看CPU信息
[root@localhost ~]#lscpu[root@localhost ~]# cat /proc/cpuinfo //processor标号从0开始,那么一个cpu就是0,两个就有0和1
[root@localhost ~]# top 然后按下数字1,展开cpu信息,能够看到有多少个cpu
Cpu0 : 0.7%us, 0.0%sy, 0.0%ni......
Cpu1 : 0.0%us, 0.0%sy, 0.0%ni......
以上信息表示有2个CPU
按q退出top命令
4、查看Linux操作系统的版本
[root@localhost ~]# cat /etc/redhat-release //只能查看Redhat版本的
CentOS Linux release 7.9.2009 (Core) //release:发行
[root@web-server ~]# lsb_release -a //适合所有的Linux发行版本
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core5、查看系统的内核版本
[root@localhost ~]# uname -r
3.10.0-1160.36.2.el7.x86_64
主版本号.次版本号.末版本号-更新次数.enterprise linux 6平台.架构
次版本号:奇数——开发版
偶数——稳定版
6.获取命令的帮助
当不记得某个命令的具体用法或某个选项及功能,可以选择用 命令 --help 查看,或者使用man手册。
一般 help 和 man手册 用得较多,推荐使用 help 和 man手册!!!
1. help
1.内部命令(内建命令):是shell自带的
查看系统中有哪些内部命令: help 命令名称
[root@web-server ~]# help cd
2.外部命令:由某个软件包安装的命令 如:tree yum
外部命令的帮助: 命令名称 --help
[root@web-server ~]# ls --help
3.查看一个命令是外部命令还是内部命令: type 命令名称
[root@web-server ~]# type cd
cd is a shell builtin
[root@web-server ~]# type top
top is hashed (/usr/bin/top)
**查看 who 命令的帮助**
2.man --manual
1)man 命令
man手册里支持搜索功能: /关键字 —— 所谓关键字,就是你想要查找的东西
在搜索模式下:按n,继续向下查找
按N:向上查找
退出:q2)man的级别
[root@web-server ~]# man man //man手册分为九个部分,常用的是1和5,需要了解1,5,8
** 1 Executable programs or shell commands //可执行程序或shell命令
2 System calls (functions provided by the kernel) //系统调用
3 Library calls (functions within program libraries) //库调用
4 Special files (usually found in /dev) //特殊文件
** 5 File formats and conventions eg /etc/passwd //文件格式,规范
6 Games //游戏
7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) //规范,标准和其他页面
** 8 System administration commands (usually only for root) //系统管理命令
9 Kernel routines [Non standard] //内核程序## 特别记住带有星号的man手册--1、5、8
例子:
[root@web-server ~]# man ls
[root@web-server ~]# man 5 文件名
[root@web-server ~]# man 8 httpd 这里插入代码片
3.info
能够查看命令的来源、选项、功能等。
**查看mkdir的详细信息**
4.whatis
查看命令的用途
1.[root@web-server ~]# whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
2. [root@web-server ~]# whatis ls
ls: nothing appropriate #没有whatis数据库
解决:生成whatis数据库
[root@web-server ~]# makewhatis
3.数据库文件的位置:/var/cache/man/whatis
7. sar
sar 既能报告当前数据,也能报告历史数据。
不带选项执行默认会以10 分钟为间隔报告自午夜起当天的CPU利用率。
脚本sal 收集了这些历史数据,它是sar软件包的一部分,必须设置为定期从cron里运行
sar把自已收集的数据以二进制格式保存在 /var/log/sa 里。
工作中使用 "sar" 命令可以说是事半功倍了。
## 安装 sar 命令
yum install -y sysstat
##使用 sar 命令
sar -d 可以得到当天磁盘活动的情况汇总
sar -n DEV 则能给出网络接口的统计信息
sar -A 可以报告所有的信息
sar 适用于快速粗略了解历史信息。
sar 命令行的常用格式: sar [options] [-o file] t [n]
options 为命令行选项,sar命令的选项很多,下面只列出常用选项:
-A:所有报告的总和。
-u:CPU利用率
-v:进程、I节点、文件和锁表状态。
-d:硬盘使用报告。
-r:没有使用的内存页面和硬盘块。
-g:串口I/O的情况。
-b:缓冲区使用情况。
-a:文件读写情况。
-c:系统调用情况。
-R:进程的活动情况。
-y:终端设备活动情况。
-w:系统交换活动。
8. Linux提示没有ssh-copy-id命令
解决办法--参考如下,亲证有效!!
[root@web-server ~]# vim ssh-copy-id
#ssh-copy-id命令实际是一个shell脚本,位于/usr/bin下,脚本内容如下:
#!/bin/sh
# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.
ID_FILE="${HOME}/.ssh/id_rsa.pub"
if [ "-i" = "$1" ]; then
shift
# check if we have 2 parameters left, if so the first is the new ID file
if [ -n "$2" ]; then
if expr "$1" : ".*\.pub" > /dev/null ; then
ID_FILE="$1"
else
ID_FILE="$1.pub"
fi
shift # and this should leave $1 as the target name
fi
else
if [ x$SSH_AUTH_SOCK != x ] ; then
GET_ID="$GET_ID ssh-add -L"
fi
fi
if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
GET_ID="cat ${ID_FILE}"
fi
if [ -z "`eval $GET_ID`" ]; then
echo "$0: ERROR: No identities found" >&2
exit 1
fi
if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
exit 1
fi
{ eval "$GET_ID" ; } | ssh $1 "exec sh -c 'umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon ~/.ssh ~/.ssh/authorized_keys >/dev/null 2>&1 || true)'" || exit 1
cat <<EOF
Now try logging into the machine, with "ssh '$1'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
EOF
#加上可执行权限,并移动到/usr/bin 目录下
[root@web-server ~]# chmod +x ssh-copy-id
[root@web-server ~]# mv ssh-copy-id /usr/bin
#试试命令是否可以使用
[root@web-server ~]# ssh-copy-id