个人用_linux常用命令

目录

网络 路由 抓包 iptables 相关

# 查看路由网关信息
ip route
# ip 地址信息
ip -4 -o addr
# tcpdump 指定网卡查看包信息
tcpdump -i <interface>
# 查看当前iptables的所有规则
iptables-save -c
复制代码
# 查看当前iptables内核模块中所有的tables
lsmod | grep ip_tables
# 查看某个iptables table的chain和rule
iptables -t <filter> -nvL
复制代码

确认网络端口相关

# 持续 ping 并将结果记录到日志
ping api.jpush.cn  | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()) } ' >> /tmp/jiguang.log &`
# 查看tcp连接状态
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
# 查找80端口请求数最高的前20个IP
netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20`
复制代码
# master_vip
master_vip='10.47.162.19'
port='6443'
result=`echo quit | timeout --signal=9 2 telnet $master_vip $port 2>&1 | grep -w 'Connected'`
echo $result
status=`echo $result | grep -wc 'Connected'`
if [ $status -eq 1 ]; then
    echo -e '\\n$master_vip $port connected OK\\n'
else
    echo "$master_vip $port connected NG\!\!" 1> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
fi

# sys-node flannel
sys_nodes='
10.47.113.176
10.47.113.248
'
port='8472'
for sys_node in $sys_nodes; do
    result=`nc -v -u -z -w 3 $sys_node $port 2>&1 | grep -w ''Connected`
    echo $result
    status=`echo $result | grep -wc 'Connected'`
    if [ $status -eq 1 ]; then
        echo -e "\\n$sys_node $port connected OK\\n"
    else
        echo "$sys_node $port connected NG\!\!" 1> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
    fi
done

# etcd
etcd_ips='
10.47.113.19
10.47.113.23
10.47.113.120
'
port='2379'
for etcd_ip in $etcd_ips; do
    result=`echo quit | timeout --signal=9 2 telnet $etcd_ip $port 2>&1 | grep -w 'Connected'`
    echo $result
    status=`echo $result | grep -wc 'Connected'`
    if [ $status -eq 1 ]; then
        echo -e "\\n$etcd_ip $port connected OK\\n"
    else
        echo "$etcd_ip $port connected NG\!\!" 1> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
    fi
done

# docker_registry_proxy
registry_proxy_vip='10.47.160.116'
ports='80 443 8080 9090'
for port in $ports; do
    result=`echo quit | timeout --signal=9 2 telnet $registry_proxy_vip $port 2>&1 | grep -w 'Connected'`
    echo $result
    status=`echo $result | grep -wc 'Connected'`
    if [ $status -eq 1 ]; then
        echo -e "\\n$registry_proxy_vip $port connected OK\\n"
    else
        echo "$registry_proxy_vip $port connected NG\!\!" 1> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
    fi
done
复制代码

rpm、yum相关

# 查找是否有安装某个软件
rpm -qa jen*
# 列出软件的文件内容
rpm -aql jenkins*
# 查看rpm包安装的文件和依赖项
rpm -qp package.rpm --provides
rpm -qp package.rpm --requires
# yum 只使用其中一个源
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# yum 只下载 rpm 包,利用 yumdownloader,会将 rpm 包下载到当前目录
yumdownloader kubeadm-1.11.9-0.x86_64 --disableexcludes=kubernetes
# 安装yum downloadonly 插件
yum install yum-plugin-downloadonly
# 下载rpm包以及依赖包
# 默认下载目录为: /var/cache/yum/ 的 rhel-{arch}-channel/packages
yum install --downloadonly <package-name>
# 指定下载目录
yum install --downloadonly --downloaddir=/root/mypackages/ httpd
# yum 安装本地rpm包 yum localinstall
yum localinstall jdk-8u40-linux-x64.rpm
# 指定一个url作为yum localinstall的目标
yum localinstall 'http://javadl.sun.com/webapps/download/AutoDL?BundleId=111740

复制代码

查找文件相关

替换目录中CR(\r\n)换行符为R(\n)换行符

$ find ./ -type f | xargs -n1 -I {} sed -i 's@\r@@g' {}

$ find ./ -maxdepth 1 -type f | xargs -n1 -I {} sed -i 's@\r@@g' {}

find与grep结合使用,从多个文件中搜索关键字并显示文件名

$ find ./ -type f -print | xargs grep 'hoge'

排除某个目录,排除多个指定目录

# 排除多个目录
$ find SRC-PATH -path 'IGNOR-PATH' -prune -o -path 'IGNOR-PATH2' -prune -o -print
# 另一种方式,使用!逻辑操作符
$ find SRC-PATH ! -path 'IGNOR-PATH*' ! -path 'IGNOR-PATH2*' -print
复制代码

进程信息相关

# 找出当前系统内存使用量较高的进程
ps -aux | sort -rnk 4 | head -20
# 找出当前系统CPU使用量较高的进程
ps -aux | sort -rnk 3 | head -20
复制代码

停启进程

#!/bin/bash

services=(
    kubelet
    kube-proxy
    docker
    flanneld
)

for service in ${services[@]} ; do
    systemctl stop $service
done

services=(
    flanneld
    docker
    kube-proxy
    kubelet
)

for service in ${services[@]} ; do
    systemctl start $service
done
复制代码

磁盘清理相关

因为/data/archive是 NFS 网盘,整理本地硬盘的数据不需要包含进去,因此用du --excluede来排除一边不检查此 FS。

alias dua="du --exclude=/data/archive --exclude=/hash -sh * |sort -hr"

重定向、管道相关

# 使用while read line语句,将程序标准错误stderr用红色字体打印
COMMAND 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
# 应用篇 用红色字体打印
echo "$master_vip $port connected NG\!\!" 1> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
复制代码

tee、EOF的使用

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://bdmekofg.mirror.aliyuncs.com"]
}
EOF
复制代码

ansible相关

ssh 互信免密码登录设置

# 在master上生成ssh key pair
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
# 使用ssh-copy-id在master上传递公钥到node上
ssh-copy-id ${USER}@${target_host}
# 指定公钥文件传递
ssh-copy-id -i ${identity_file} ${USER}@${target_host}
# 使用命令传递公钥的方法
cat ~/.ssh/id_rsa.pub | ssh ${USER}@${target_host} "mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys"

复制代码

ansible 语法检查

# 列出主机
$ ansible-playbook -i HOSTS_FILE --list-hosts PLAYBOOK_FILE
# 语法检查
$ ansible-playbook --syntax-check PLAYBOOK_FILE
# 列出task
$ ansible-playbook -i HOSTS_FILE --list-tasks PLAYBOOK_FILE
# 检查语法是否正确
$ ansible-playbook --check PLAYBOOK_FILE
复制代码

golang相关

# Mac 下编译 Linux 和 Windows 64位可执行程序

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

# Linux 下编译 Mac 和 Windows 64位可执行程序

$ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

# Windows 下编译 Mac 和 Linux 64位可执行程序

$ SET CGO_ENABLED=0
$ SET GOOS=darwin
$ SET GOARCH=amd64
$ go build main.go

$ SET CGO_ENABLED=0
$ SET GOOS=linux
$ SET GOARCH=amd64
$ go build main.go
复制代码

GOOS:目标平台的操作系统(darwin、freebsd、linux、windows) GOARCH:目标平台的体系架构(386、amd64、arm) 交叉编译不支持 CGO 所以要禁用它

上面的命令编译 64 位可执行程序,你当然应该也会使用 386 编译 32 位可执行程序 很多博客都提到要先增加对其它平台的支持,但是我跳过那一步,上面所列的命令也都能成功,且得到我想要的结果,可见那一步应该是非必须的,或是我所使用的 Go 版本已默认支持所有平台。

helm 相关

cd /data/helm/all-chart/34a2b228-a5f7-4b17-ae67-b9aaf804073c/
cd palite-sharecloud-stg2_0.0.1

file_to_sed=`find ./ -type f -print | xargs grep 'palite-aggregate1.0.0' | awk -F: '{print $1}'`
echo $file_to_sed

app_name=`ls | sed -ne 's/-stg2//gp'`
app_name=$app_name'1.0.0'
echo $app_name

for file in `echo $file_to_sed`; do
    sed -i "s/palite-aggregate1.0.0/$app_name/g" $file
done

file_to_sed=`find ./ -type f -print | xargs grep 'paliteaggregate' | awk -F: '{print $1}'`

app_name=`ls | sed -ne 's/-stg2//g; s/-//gp'`

for file in `echo $file_to_sed`; do
    sed -i "s/paliteaggregate/$app_name/g" $file
done

file_to_sed=`find ./ -type f -print | xargs grep 'shb-dmz-stg-ae5a45f9' | awk -F: '{print $1}'`

for file in `echo $file_to_sed`; do
    sed -i "s/shb-dmz-stg-ae5a45f9/shb-dmz-stg-f7847ac7/g" $file
done
复制代码

docker 相关

# build -t标记选项,在创建镜像文件的同时打标签 . 为使用当前目录下Dockerfile文件为Dockerfile
docker build -t hub.yun.paic.com.cn/library/go-server:latest .
# run --expose 指定暴露端口,expose在应用上一般只是作为哪个端口提供哪个服务的提示,在宿主机上并不能访问到此端口。
docker run --expose 9860 -t -i --rm hub.yun.paic.com.cn/library/go-server sh
# run -p 发布端口,127.0.0.1:9860为宿主机端口。
docker run -p 127.0.0.1:9860:9860/tcp --rm hub.yun.paic.com.cn/library/go-server
# 列出所有镜像
docker images -a
# 删除镜像
docker rmi Image Image
# 删除所有不再被使用的数据(按照顺序: 停止的容器,没有容器使用的卷,没有容器使用的镜像)。如果要删除从未被使用的数据,可以使用'-a'参数。
docker system prune
# 另外还有:
docker container prune
docker image prune
docker network prune
docker volume prune
复制代码

kubernetes 相关

# list pods
n_namespace=$(grep -iw namespace /root/.kube/config | cut -d':' -f2 | sed 's/^ *//g')
kubectl get pods --namespace=<namespace>

# get node show-label
kubectl get nodes --show-labels | sed -n '1p;/\<30\.4\.171\.24\>/p'

# create resource
# check yaml errors
kubectl create --dry-run --validate -f ./go-server.yaml
# use --record 来记录resource被执行过的命令,命令会被记录在resource的annotation的kubernetes.io/change-cause当中。
kubectl create -f ./go-server.yaml --record
# create configmap using file as content
kubectl create configmap nginx --namespace=shb-dmz-qiye-portal-stg-9d135f6b --from-file=./nginx.conf

# kill pod forcibly
kubectl -n shb-sf-stg-046a2940 delete pods sps-search-service-74fddcdfc-lbf7n --grace-period=0 --force

# watch log
kubectl logs -f --tail=100 traefik-ingress-lb-9qdhv -n kube-system

# enter container
kubectl exec falcon-caas-watcher-5d6b5874-wsdcw --namespace=kube-system -i -t -- bash -il

# copy content from container to host filesystem
kubectl cp itax-core-taxation-6857465f7f-jl7gb:/root/heap.hprof /tmp/itax-core-taxation_heap_30.4.171.20.hprof --namespace=shb-sf-stg-cf156ca8

# kubectl jsonpath
# 查看当前集群网络使用的所有子网 podCIDR
kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'

# kubectl json
$ kubectl get no -o json \
| jq '.items[] | { node_ip: .metadata.labels."kubernetes.io/hostname", host_name: .metadata.labels.host_name, labels: .metadata.labels }' \
| grep -v -e 'beta.kubernetes.io/arch' -e 'beta.kubernetes.io/os' -e 'kubernetes.io/hostname'

# go-template
# write below to a file named 'nodes-taints.tmpl'
{{printf "%-50s %-12s\n" "Node" "Taint"}}
{{- range .items}}
    {{- if $taint := (index .spec "taints") }}
        {{- .metadata.name }}{{ "\t" }}
        {{- range $taint }}
            {{- .key }}={{ .value }}:{{ .effect }}{{ "\t" }}
        {{- end }}
        {{- "\n" }}
    {{- end}}
{{- end}}
# then reference like this:
kubectl get nodes -o go-template-file="./nodes-taints.tmpl"
# you'll get output like so:
Node                                            Taint
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=etcd:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=jenkins:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=etcd:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=containerlinux-canary-channel-workers:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=jenkins:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=etcd:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=etcd:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=etcd:NoSchedule
ip-xxx-xxx-xxx-xxx.us-west-2.compute.internal   dedicate=jenkins:NoSchedule

# 另外一个go-template,获取节点ip和hostname
{{printf "%-50s %-12s\n" "Node" "HostName"}}
{{- range .items}}
    {{- $name:=.metadata.name}}
        {{- if $labels:= (index .metadata "labels") }}
            {{- range $k,$v:= .metadata.labels}}
                {{- if eq $k "host_name"}}
                    {{- $name}}{{ "\t" }} {{- $v}}{{ "\t" }} {{- "\n" }}
                {{- end}}
            {{- end}}
        {{- end}}
{{- end}}

# check node allocated resouces
nodes=(
    '30.12.23.152'
    '30.12.23.153'
    '30.12.94.38'
    '30.12.94.39'
    '30.12.94.40'
    '30.12.94.41'
)
for node in ${nodes[@]} ; do
    echo $node
done
for node in ${nodes[@]} ; do
    kubectl describe no "${node}" | grep -A4 'Allocated resources:'
done
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值