一些基本操作
Linux基础操作命令
显示已经安装的软件包yum list installed
查找可以安装的软件包/已经安装的软件包 (以 tomcat 为例):yum list tomcat
安装软件包 (以 tomcat 为例):yum install tomcat
卸载软件包 (以 tomcat 为例):yum remove tomcat
升级某一个软件包 ,以升级 tomcat 为例:yum update tomcat
- Linux下复制粘贴快捷键:
在终端下:
复制命令:Ctrl + Shift + C 组合键.
粘贴命令:Ctrl + Shift + V 组合键.
在控制台下:
复制命令:Ctrl + Insert 组合键 或 用鼠标选中即是复制。
粘贴命令:Shift + Insert 组合键 或 单击鼠标滚轮即为粘贴。
- Linux命令行操作
linux命令行常用光标移动快捷键
挪光标位置:
ctrl+左右键
:在单词之间跳转
ctrl+a
:跳到本行的行首
ctrl+e
:跳到页尾
剪切复制:
Ctrl+u
:删除当前光标前面的文字 (还有剪切功能)
ctrl+k
:删除当前光标后面的文字(还有剪切功能)
Ctrl+y
:粘贴Ctrl+u或ctrl+k剪切的内容
清空删除:
Ctrl+L
:进行清屏操作
Ctrl+w
:删除光标前面的单词的字符
Alt – d
:由光标位置开始,往右删除单词。往行尾删(与上面相反).
- ssh相关
- 命令行登录与退出:
参考在命令行中使用ssh连接远程服务器使用账号密码登录(与原文不同,这里不需要输p
字母):
[c:\~]$ ssh root@192.168.1.104 22
弹出提示框,让输入密码,输入之后就可以了。
退出:一段时间不使用会自动断开连接,也可手动断开连接输入logout或Ctrl + D
。
- 使用ssh密钥免密登录
应该是需要用户机也是linux才可以
[root@localhost ~]# ssh-keygen
[root@localhost ~]# ssh-copy-id root@192.168.1.104
[root@localhost ~]# ssh root@192.168.1.104
下载一个项目经历的的过程
TinyWebServer: https://github.com/qinguoyi/TinyWebServer
- nohup: 忽略输入并把输出追加到"nohup.out"
[centos@localhost network]$ nohup wget https://github.com/qinguoyi/TinyWebServer&
[2] 3973
[centos@localhost network]$ nohup: 忽略输入并把输出追加到"nohup.out"
nohup wget https://github.co^C
[centos@localhost network]$ nohup wget https://github.com/qinguoyi/TinyWebServer 2>> nohup.out &
[1] 4081
[centos@localhost network]$ jobs
[1]+ 完成 nohup wget https://github.com/qinguoyi/TinyWebServer 2>> nohup.out
[centos@localhost network]$ ls
nohup.out TinyWebServer TinyWebServer.1
解决nohup: 忽略输入并把输出追加到"nohup.out"或者nohup: 忽略输入重定向错误到标准输出端: nohup启动脚本的时候,没有指定输出路径,默认使用当前目录的nohup.out
- 查看文件信息
Linux - 查看文件信息的三个命令
ls命令 - list directory contents
显示文件详细信息:ls -l <file name>
file命令 — determine file type
确定文件类型 :file <file name>
output MIME type strings (–mime-type and --mime-encoding) :file -i
stat命令 - display file or file system status
显示文件状态信息:stat <file name>
显示文件在系统的状态信息:stat -f <file name>
简明显示文件的状态信息:stat -t <file name>
[centos@localhost network]$ jobs
[1]+ 完成 nohup wget https://github.com/qinguoyi/TinyWebServer 2>> nohup.out
[centos@localhost network]$ ls
nohup.out TinyWebServer TinyWebServer.1
[centos@localhost network]$ cd TinyWebServer
bash: cd: TinyWebServer: 不是目录
[centos@localhost network]$ ls -l TinyWebServer
-rw-rw-r--. 1 centos centos 225637 3月 31 15:59 TinyWebServer
[centos@localhost network]$ stat TinyWebServer
文件:"TinyWebServer"
大小:225637 块:448 IO 块:4096 普通文件
设备:fd00h/64768d Inode:53086609 硬链接:1
权限:(0664/-rw-rw-r--) Uid:( 1000/ centos) Gid:( 1000/ centos)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2022-03-31 15:59:51.556711126 +0800
最近更改:2022-03-31 15:59:52.310715405 +0800
最近改动:2022-03-31 15:59:52.310715405 +0800
创建时间:-
[centos@localhost network]$ file TinyWebServer
TinyWebServer: HTML document, UTF-8 Unicode text, with very long lines
- 使用git安装
centos@localhost network]$ su
密码:
[root@localhost network]# cd /opt
[root@localhost opt]# yum list | grep git
crontabs.noarch 1.11-6.20121102git.el7 @anaconda
git.x86_64 1.8.3.1-21.el7_7 @anaconda
hyperv-daemons.x86_64 0-0.34.20180415git.el7 @anaconda
...
[root@localhost opt]# yum search git
...
git.x86_64 : Fast Version Control System
...
[root@localhost opt]# yum info git.x86_64
已安装的软件包
名称 :git
架构 :x86_64
...
可安装的软件包
名称 :git
架构 :x86_64
版本 :1.8.3.1
[root@localhost opt]# yum list installed | grep git
crontabs.noarch 1.11-6.20121102git.el7 @anaconda
git.x86_64 1.8.3.1-21.el7_7 @anaconda
...
# 切换了用户和目录之后下载的
[centos@localhost pulled]$ git clone https://github.com/qinguoyi/TinyWebServer
正克隆到 'TinyWebServer'...
remote: Enumerating objects: 816, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (7/7), done.
...
经验流程
Linux CPU占用过高怎么办
参考[Linux]-服务器CPU占用过高处理方案,Linux系统中CPU占用率较高问题排查思路与解决方法.
第一步:使用top
命令,然后按shift+p
按照CPU排序,找到占用CPU过高的进程的pid
第二步:使用top -H -p [进程pid]
,找到进程中消耗资源最高的线程的id。该命令表示展示-p
指定的pid的私有线程(-H
)。
第三步:使用echo 'obase=16;[线程id]' | bc
或者printf "%x\n" [线程id]
(字母要小写,括号[]去掉),将线程id转换为16进制。其中,bc是linux的计算器命令。
第四步:执行jstack [进程id] |grep [16进制线程ID小写英文]
,查看线程状态信息。jstack定位问题代码。
上面的第二步,也可以用另一个命令:
使用
ps -mp 进程 -o THREAD,tid,time
列出进程中的线程占用CPU情况。其中:
-m 显示所有的线程
-p 指定进程id
-o 该参数后是用户自定义格式
稀碎知识点
杀死一个进程
linux如何杀死进程,kill -9 有什么不好的地方?
-
linux如何杀死进程
参考杀死linux进程的N种方法,Linux杀死进程的多种方法。
首先,查看进程pid,例如ps ax | grep firefox
然后,杀死进程kill -9 [进程pid]
.
上面两步,可以合为一个命令:pkill -9 pycharm
.这个命令中,指令pkill
是pgrep
(直接针对进程,打印pid,如pgrep pycharm
)和kill
的结合体。而且pkill或者pgrep只要给出进程名的一部分就可以终止进程。 -
kill -9 有什么不好的地方
参考kill 与 kill -9 的区别: kill相当于赐死,kill -9相当于斩立决
kill命令默认为kill -15
,被称为优雅的退出。此时,系统会发送一个SIGTERM的信号给对应的程序。当程序接收到该信号后,可以选择:
1、立即停止程序
2、释放响应资源后停止程序
3、忽略该信号,继续执行程序
因此,有的时候使用kill命令是没办法"杀死"应用的原因,因为默认的SIGTERM(15)的信号是可以被阻塞和忽略的。
和kill -15相比,kill -9
就相对强硬一点,系统会发出SIGKILL信号,他要求接收到该信号的程序应该立即结束运行,不能被阻塞或者忽略。所以这通常会带来一些副作用,数据丢失或者终端无法恢复到正常状态等。