linux相关命令使用记录
Linux镜像
Section titled “Linux镜像”一键安装docker, 一键换软件源等 LinuxMirrors
安装docker并配置加速器
Section titled “安装docker并配置加速器”yum -y install dockerhostnamectl set-hostname manager43获取ip地址
Section titled “获取ip地址”$(hostname -I|cut -d" " -f 1)配置hosts文件(可配置可不配置)
Section titled “配置hosts文件(可配置可不配置)”vi /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.31.43 manager43192.168.31.188 node188192.168.31.139 node139使用scp复制到node主机
Section titled “使用scp复制到node主机”scp /etc/hosts root@192.168.31.188:/etc/hosts`firewall-cmd --zone=public --list-ports` 和 `netstat -tlunp`systemctl disable firewalld.servicesystemctl stop firewalld.service# 防火墙配置# 官方文档
# centos7采用firewalld来配置防火墙,默认不开放接口。官方文档中给出的方案比较底层。这里我们采取自定义Service的方式来配置
# 创建文件vi /etc/firewalld/services/docker.xml# 加入以下内容<?xml version="1.0" encoding="utf-8"?> <service> <short>docker</short> <description>docker daemon for remote access</description> <port protocol="tcp" port="2375"/> </service># 查看默认zone(一般是public)firewall-cmd --get-default-zone# 在zone中加入这个servicefirewall-cmd --zone=public --add-service=docker --permanent# 重新加载firewall-cmd --reload# 查询端口firewall-cmd --service=docker --get-ports --permanent# 参考链接https://my.oschina.net/u/4560825/blog/4314028vi /etc/sysconfig/network-scripts/ifcfg-enp0s3# DNS2=114.114.114.114systemctl restart networkvi /etc/resolv.conf # 查看结果配置yum镜像源
Section titled “配置yum镜像源”# 备份你的原镜像文件,以免出错后可以恢复mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup# 下载新的CentOS-Base.repo 到/etc/yum.repos.d/wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo# 进入CentOS-Base.repo文件, 修改文件中的baseurl地址(可省略这步)vi CentOS-Base.repo# 清楚原有yum缓存yum clean all# 生成缓存yum makecache# 查看配置好的yum源是否正常yum repolist安装docker
Section titled “安装docker”# 使用官方安装脚本自动安装# 安装命令如下:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun# 也可以使用国内 daocloud 一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh# 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的yum install -y yum-utils device-mapper-persistent-data lvm2# 配置镜像源yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo# 通过命令把https://download-stage.docker.com替换为http://mirrors.aliyun.com/docker-cevi /etc/yum.repos.d/docker-ce.repo# 命令如下::%s#https://download.docker.com#http://mirrors.aliyun.com/docker-ce#g# 更新yum缓存yum makecache fast# 这时,可通过阿里镜像安装doker了yum install docker-ce# 查看服务状态,一秒一次,启动之后可通过IP:端口访问界面watch -n 1 docker stack services hadoop# 不打印正常信息,打印错误信息(/dev/null 代表空设备)xargs docker rmi > /dev/null# 正常和错误信息都不打印xargs docker rmi &> /dev/null打印文件或文件夹列表时,排除文件或文件夹
Section titled “打印文件或文件夹列表时,排除文件或文件夹”# 排除多个文件或文件夹ls | grep -v 'a\|b'nodejs安装
Section titled “nodejs安装”# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash# nvm install 14.16.0# npm install -g nrm# nrm ls# nrm use taobao# npm config ls# rm -rf ./node_modules# npm install# npm run build:testdocker访问权限问题
Section titled “docker访问权限问题”sudo groupadd docker #添加docker用户组sudo gpasswd -a $USER docker #将当前用户添加至docker用户组newgrp docker #更新docker用户组查看端口占用
Section titled “查看端口占用”# 查看53端口的占用情况sudo netstat -anlp | grep -w LISTEN | grep 53定义环境变量
Section titled “定义环境变量”export DOCKER_HOST=tcp://localhost:2375crontab使用
Section titled “crontab使用”# 格式minute hour day month week command# 添加任务crontab -e# 查看任务列表crontab -l# 删除任务crontab -r# 查看已执行过的任务tail -f /var/log/cron设置网络时间
Section titled “设置网络时间”# 查看当前时间date "+%Y-%m-%d %H:%M:%S"# 查看时区date "+%Z"# 使用cat /etc/sysconfig/clock查看当前时区cat /etc/sysconfig/clock# 设置时区cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 查看硬件时间hwclock# 同步硬件时间hwclock -w# 同步系统时间clock -w
# ntpdate# 即使是硬件时间也会和网络时间有差异,想要和网络时间完全一致,我们就需要获取网络时间更新本地时间。# 安装工具:yum -y install ntp ntpdate# 设置系统时间与网络时间同步:ntpdate cn.pool.ntp.org# 将系统时间写入硬件时间:hwclock –systohc
# 同步时间服务器ntpdate -u ntp.api.bz# ntp常用服务器:# 中国国家授时中心:210.72.145.44# NTP服务器(上海) :ntp.api.bz# 美国: time.nist.gov# 复旦: ntp.fudan.edu.cn# 微软公司授时主机(美国) :time.windows.com# 北京邮电大学 : s1a.time.edu.cn# 清华大学 : s1b.time.edu.cn# 北京大学 : s1c.time.edu.cn# 台警大授时中心(台湾):asia.pool.ntp.org
# simplecp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimentpdate -u ntp.api.bzhwclock -w查看linux系统版本
Section titled “查看linux系统版本”# 只适用于redhat系linux系统cat /etc/redhat-release# 方式二rpm -q centos-releaseLinux文件字符串替换命令
Section titled “Linux文件字符串替换命令”# perl命令替换# -e 执行指定的脚本。# -i 原地替换文件,并将旧文件用指定的扩展名备份。不指定扩展名则不备份。# -n 自动循环,相当于 while(<>) { 脚本; }# -p 自动循环+自动输出,相当于 while(<>) { 脚本; print; }
# 用法示例:# 将所有C程序中的foo替换成bar,旧文件备份成.bakperl -p -i.bak -e 's/\bfoo\b/bar/g' *.c# 将当前文件夹下lishan.txt和lishan.txt.bak中的“shan”都替换为“hua”perl -p -i -e "s/shan/hua/g" ./lishan.txt ./lishan.txt.bak
# ##################################################################
# sed命令替换# -i 表示inplace edit,就地修改文件
# s表示替换,d表示删除# 格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` 文件名
# 示例:# 把当前目录下lishan.txt里的shan都替换为huased -i "s/shan/hua/g" lishan.txt# 使用变量替换(使用双引号)sed -e "s/$var1/$var2/g" filename# 删除文本中空行和空格组成的行以及#号注释的行grep -v ^# filename | sed /^[[:space:]]*$/d | sed /^$/d# 复制目录 -- 即将[/www/temp/java/BOOT-INF/lib]里面的所有内容,复制到这个[/www/temp/java/lib]目录里面cp -a /www/temp/java/BOOT-INF/lib /www/temp/java/libtop命令使用
Section titled “top命令使用”# 使用`1`显示每个CPU的使用情况# 使用`t`显示CPU使用百分比进度条# 使用`m`显示内存使用百分比进度条- 相关文章
- [[centos使用date命令同步网络时间]]
- [[timedatectl命令怎么同步网络时间]]
- [[timedatectl命令怎么同步网络时间]]
# 查看时间timedatectldate# 修改时间date -s "2024-01-19"date -s "10:30:00"timedatectl set-time "YYYY-MM-DD HH:MM:SS"
# 开启自动同步时间timedatectl set-ntp true# 手动同步网络时间date -s "$(curl -s --head http://www.baidu.com | grep '^Date:' | cut -d' ' -f3-6)Z"查看域名对应IP
Section titled “查看域名对应IP”ping -c 1 example.com