linux - 常用命令

93 阅读3分钟

linux 常用命令

命令大全参考: www.runoob.com/linux/linux…

ssh登录

ssh -o StrictHostKeyChecking=no -i /root/.ssh/private root@175.178.1.249

建议使用 git 简单安装 2.31.1

#安装源  
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm  
#安装git  
yum install git  
#更新git  
yum update git

安装nodejs

wget https://cdn.npm.taobao.org/dist/node/v16.18.0/node-v16.18.0-linux-x64.tar.xz
tar -xf node-v16.18.0-linux-x64.tar.xz
mv node-v16.18.0-linux-x64 /var
ln -s /var/node-v16.18.0-linux-x64/bin/node /usr/bin/node
ln -s /var/node-v16.18.0-linux-x64/bin/npm /usr/bin/npm
ln -s /var/node-v16.18.0-linux-x64/bin/npx /usr/bin/npx
rm -rf node-v16.18.0-linux-x64.tar.xz

添加环境变量

vi /etc/profile

在文件最后添加以下配置:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/git/bin:/usr/local/node/bin

刷新配置

source /etc/profile

ssh客户端一会断开解决

找到文件

vim /etc/ssh/sshd_config

修改

# ClientAliveInterval 0      // 心跳间隔时间
# ClientAliveCountMax 3      // 尝试次数

改为

ClientAliveInterval 30
ClientAliveCountMax 86400

重启ssh服务

service sshd restart

centos7 网卡休眠问题

blog.csdn.net/zhanglong_4…

最近有两台服务器,网卡老自动断网. 不造啥原因.

解决方案如下:

一. 重装 ssh

  1. 卸载 先查看openssh的相关包
[root@hadoop008 ~]# rpm -qa openssh*

openssh-7.4p1-16.el7.x86_64

openssh-clients-7.4p1-16.el7.x86_64

openssh-server-7.4p1-16.el7.x86_64

然后依次卸载

[root@hadoop008 ~]#  rpm -e openssh-7.4p1-16.el7.x86_64

[root@hadoop008 ~]#  rpm -e openssh-clients-7.4p1-16.el7.x86_64

[root@hadoop008 ~]#  rpm -e openssh-server-7.4p1-16.el7.x86_64
  1. 安装
yum install -y openssh-server

yum install -y openssh-clients

3.重新启动ssh服务

service sshd start

二. 长时间不工作,网卡会自动休眠,动动鼠标或者键盘又能重新联网。

解决方法:vi /etc/default/grub (centons7之前的系统,路径为vi /etc/grub.conf)

添加 pcie_aspm=off 重启,该方法是关闭电源管理系统,防止休眠。

image.png

centos 服务器时间同步问题

systemctl stop vmtoolsd
yum install ntp   # 安装 ntp
ntpdate ntp3.aliyun.com   # 同步时间,这里以阿里云3号服务器为例,你可以根据需要选择其他服务器
date   # 查看时间

解决 docker容器服务器不准确问题

安装上面解决了 centos 服务器同步问题后

增加映射使用主机时区  
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime

shell循环

#!/bin/bash'
for i in $(seq 1 200)
do
    curl 'http://localhost:3000'
    echo
    sleep 0.3
done

查看操作系统信息

cat /etc/redhat-release

查看cpu个数

cat /proc/cpuinfo| grep "processor"| wc -l

查看内存

free -m

查看磁盘空间

df -h

查看端口

netstat -lnpt

查看进程

ps  -ef

echo用于字符串的输出

显示命令执行结果

echo `date`

注意: 这里使用的是反引号 `, 而不是单引号 '。

 

输出字符串或变量不换行

echo -n  "taidi"

1. read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

#!/bin/sh\
read name\
echo "$name It is a test"

curl的使用说明

curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。

参考博客:www.ruanyifeng.com/blog/2019/0…

官方手册:curl.se/docs/manpag…

curl -X POST -H "Content-Type:application/json;charset=utf-8" -d '{"name":"hello","password":"123"}' http://localhost:3

  1. -s参数将不输出错误和进度信息。

curl -s <http://172.31.24.30:8888/chfs/shared/java/app.info.txt>

1. >将内容重定向到app.info文件

curl -s <http://172.31.24.30:8888/chfs/shared/java/app.info.txt> >[app.info](http://app.info)

1. >>将内容追加到app.info文件

curl -s <http://172.31.24.30:8888/chfs/shared/java/app.info.txt> >[app.info](http://app.info/)

jq工具的使用说明

1. 简介

jq 是一款命令行下处理 JSON 数据的工具。其可以接受标准输入,命令管道或者文件中的 JSON 数据,经过一系列的过滤器(filters)和表达式的转后形成我们需要的数据结构并将结果输出到标准输出中。jq 的这种特性使我们可以很容易地在 Shell 脚本中调用它。

参考博客:www.linuxidc.com/Linux/2017-…

官方手册:stedolan.github.io/jq/tutorial…

1. centos7系统下载jq   添加epel源

wget <http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm>\
rpm -ivh epel-release-latest-7.noarch.rpm\
yum repolist\
yum -y install jq

1. -r参数返回标准输出

curl -s <http://172.31.24.30:8888/chfs/shared/java/app.info.txt> | /usr/bin/jq  -r ".info.kkcloud_snowflake[0].git"

sed命令的使用说明

1. 简介

Sed表示流编辑器(Stream Editor)的缩写。这是一个简单但功能强大的工具,分析文本,并无缝地转换它。 SED是在1973-1974年由贝尔实验室的李E. McMahon开发。如今,它运行在所有主要的操作系统。

参考博客:www.yiibai.com/sed

1. -n参数读取指定的行

sed  -n  '2p'  [app.info](http://app.info/)   #读取第二行

1. -i参数和s命令与g命令一起使用全局替换字符串

sed -i  "s|parameter01|$app|g" start.sh   # 变量$app全局替换parameter01

wget命令的使用说明

1. 简介

是Linux系统用于从Web下载文件的命令行工具,支持 HTTP、HTTPS及FTP协议下载文件,而且wget还提供了很多选项,例如下载多个文件、后台下载,使用代理等等,使用非常方便。

参考博客:juejin.cn/post/702618…

1. -q参数,安静模式 (无信息输出)。

wget  -q  <http://172.31.24.31:8888/script/newdockerfile.sh>

1. -O参数将下载的文件重命名,并有覆盖作用

wget  -q  <http://172.31.24.31:8888/script/newdockerfile.sh>  -O  [dockerfile.sh](http://172.31.24.31:8888/script/newdockerfile.sh)

shell脚本if判断语句

1. 例子

if [ $deploy_name == "kkcloud_snowflake" ];then\
echo 'idy'\
else\
echo 'taidi'\
fi