Linux常用命令集锦

查看系统版本

lsb

yum install lsb
lsb_release -a
[root@MiWiFi-R3-srv ~]# lsb_release -a
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 8.0.1905 (Core)
Release:        8.0.1905
Codename:       Core

内核版本

[root@VM_0_9_centos k8s]# cat /proc/version
Linux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018

发行版本

[root@MiWiFi-R3-srv ~]# cat /etc/issue
\S
Kernel \r on an \m

注解

这种版本信息有什么作用?

查看CPU信息

# 查看物理CPU个数
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
# 查看每个物理CPU中core的个数(即核数)
cat /proc/cpuinfo| grep "cpu cores"| uniq
# 查看逻辑CPU的个数
cat /proc/cpuinfo| grep "processor"| wc -l
# 查看CPU信息(型号)
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

设置python3为默认python

在`~/.bash_profile`中加入

PATH=$PATH:$HOME/bin
PATH=$PATH:/usr/local/bin
alias python="/usr/local/bin/python3"
alias pip="/usr/local/bin/pip3"

export PATH

注解

使用了alias

执行

source ~/.bash_profile

vim

替换字符串

#
:s/elseif/elif/
:s/elseif/elif/g
# 全文替换;将elseif替换为elif
:%s/elseif/elif/g

查看日志

systemctl

systemctl status kube-controller-manager

journalctl

journalctl -u kube-controller-manager

环境变量

printenv

打印全部或部分环境信息,等同 echo

chugangdeMacBook-Pro:yummy_sphinx_theme cg$ printenv
TERM_PROGRAM=iTerm.app
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/b3/mjqwy7314nx3n66p9c6tk7100000gn/T/
TERM_PROGRAM_VERSION=3.2.8
OLDPWD=/Users/cg
TERM_SESSION_ID=w0t4p0:E55A812E-A906-4A6C-B29D-DABCC95AE1F6
USER=cg
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ssfPPRJbf5/Listeners
__CF_USER_TEXT_ENCODING=0x0:25:52
PATH=/Users/cg/.jenv/shims:/Users/cg/.jenv/bin:/usr/local/opt/node@8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/usr/local/go/bin:/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin:/usr/local/bin/python3:/Users/cg/Library/Python/3.7/bin
JENV_LOADED=1
IDEA_MAVEN=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3
PWD=/Users/cg/Library/Python/3.7/lib/python/site-packages/yummy_sphinx_theme
LANG=zh_CN.UTF-8
ITERM_PROFILE=Default
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/cg
COLORFGBG=7;0
GOROOT=/usr/local/go
ITERM_SESSION_ID=w0t4p0:E55A812E-A906-4A6C-B29D-DABCC95AE1F6
LOGNAME=cg
GOPATH=/Users/cg/go
COLORTERM=truecolor
_=/usr/bin/printenv
chugangdeMacBook-Pro:yummy_sphinx_theme cg$ printenv TERM_PROGRAM
iTerm.app

切割文件

ps -ef | grep java | awk '{print $2}' | xargs kill

查看使用端口的进程

chugangdeMacBook-Pro:kubecfg cg$ lsof -i:50759
COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
___apiser 47136   cg    8u  IPv4 0xe71e8a144c814c21      0t0  TCP localhost:http-alt->localhost:50759 (ESTABLISHED)
___contro 47194   cg    7u  IPv4 0xe71e8a1452321c21      0t0  TCP localhost:50759->localhost:http-alt (ESTABLISHED)

使用 50759 端口的是 __contro 进程。localhost:50759->localhost:http-alt 这种格式对应的进程是使用该端口的进程。

tar语法

压缩

tar -czvf ***.tar.gz
tar -cjvf ***.tar.bz2

解压缩

tar -xzvf ***.tar.gz
tar -xjvf ***.tar.bz2