Others

  • 获取公网IP

    1. curl ifconfig.me
  • 磁盘空间

    1. df -hl 查看磁盘剩余空间
    2. df -h 查看每个根路径的分区大小
    3. du -sh [目录名] 返回该目录的大小
    4. du -sm [文件夹] 返回该文件夹总M数
    5. du -h [目录名] 查看指定文件夹下的所有文件大小(包含子文件夹)
  • make

    1. 通过外部编译器的,比如linux中的gcc集来编译源码
    2. 获取Makefile文件的命令触发编译
  • curl

    1. -X GET/POST
    2. -I 获取head
    3. curl有cache
  • 查看tcp/udp端口

    1. netstat -tunlp
    2. sudo su - && lsof -i:8080
  • 查看系统启动时间

    1. who -b
    2. date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
  • 查看进程的启动时间

    1. ps -p PID -o lstart
  • 查看cpu

    1. sar -u 1 5 每1秒采集一次,共5次
    2. top
  • 查看进程占用内存

    1. cat /proc/27930/status
    2. ps -e -o 'pid,args,rsz,uid,user,cmd' | sort -nrk3 | less
  • 查看进程个数

    1. ps -ef | wc -l
  • 查看进程个数限制

    1. ulimit -a
  • 查看ip

    1. hostname -i
    2. ifconfig
  • 查看环境变量

    1. export
  • 查看目录大小 :

    1. du -sh * | grep G | sort -nr
    2. du -s * | sort -nr
    3. du -h . 列出子目录
    4. du -sh * 不列出子目录
    5. df -h . 列出总量和使用情况
  • 压缩/解压缩

    1. tar
      1. 解压 tar -xzvf xx.tar.gz
      2. -c : 建立压缩档案
      3. -x:解压
      4. -f : 文件名字,切记,这个参数是最后一个参数,后面只能文件名。
      5. -z:有gzip属性的 gz
      6. -v:显示所有过程
    2. unzip x.zip/x.jar . : 解压缩
  • 查看系统内核

    1. uname -a
  • 获取RedHat版本

    1. cat /etc/redhat-release
  • 1>/dev/null 2>&1的含义

  1. 代表重定向到哪里,例如:echo "123" > /home/123.txt

  2. 1 表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null"
  3. 2 表示stderr标准错误
  4. & 表示等同于的意思,2>&1,表示2的输出重定向等同于1
  5. /dev/null,或称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操作成功)
  6. 这句话等同于1>/dev/null 2>/dev/null 含义是将标准输入和输出丢弃
  • 遍历目录下文件

      for file in *
      do  
          if [ -d "$file" ]  
          then   
            echo "a forder"  
          else if [ -f "$file" ]
          then
            echo "a file"
          fi  
      done
    
  • 获取当前shell的路径: basepath=$(cd dirname $0; pwd)

  • 字符串替换: sed -i "s/aaa/bbb/g" test.txt

  • find .|xargs grep -ri "IBM" : 查找字符在哪个文件

  • alias aName='xxx'

  • grep -A/-B 10 'XXX' XX.log: 带上grep语句后/前 10条

  • less -N: 显示行号

  • history 10: 察看前10条历史命令

  • chmod -R 777 folder: 赋权限

  • free -m -o | grep 'Mem' | awk '{print $4}': 查看服务器空间

  • crontab -e: 进入CRON JOB VI模式

  • ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' | grep oracle | sort -nrk5: 查看进程

  • ps -ef | awk '{print $2}' | xargs kill -9: kill进程

  • :set ff=unix: vi 模式下将文件格式转换成unix,可以解决windows下shell脚本和Liunx格式不一样问题

  • awk

    1. echo abcde | awk '{print substr($0, 0, 2)}'
    2. ps -e -o 'cmd' | awk '{print substr($0,index($0,"'$APP_HOME'"),20)}'
    3. CURRENT_DATE=date ps -e -o 'rsz,cmd' | awk '{print $1, "'"$CURRENT_DATE"'"}'

      awk引用外部变量

  • nohup sh process_monitor.sh > process_monitor.out & : 后台持续启动脚本

  • find /xx/xx/ -name 'xx*.log': 查找文件

  • find /xx/xx/ -name 'xx*.log' | grep -v 'xxx' | xargs ls -l: find 和 ls 和用

  • wc *.out: 显示二进制文件的行数

Centos系统

CentOS 7系统安装

  • 官网下载DVD版本的iso镜像文件
  • 官网下载UltraIOS,将iso文件写入U盘,制作成为U盘启动盘
  • 通过BIOS设置从U盘载入,重启
  • 教程: https://www.augsky/599.html

系统版本

  • Centos操作系统版本
    1. cat /etc/redhat-release
  • Linux内核版本
    1. uname -a

使用鼠标

  • yum install gpm
  • service gpm start

基本操作

  • 关机 : poweroff
  • 重启 : reboot
  • 待机/挂起 : systemctl suspend
  • 休眠 : systemctl hibernate

笔记本关闭盖子不待机,只锁屏

  • vi /etc/systemd/logind.conf
  • 配置详解
    1. HandlePowerKey : 按下电源键后会触发的行为
    2. HandleSuspendKey : 按下挂起键后会触发的行为
    3. HandleHibernateKey : 按下休眠键后会触发的行为
    4. HandleLidSwitch : 关闭笔记本盖子后会触发的行为
  • 行为
    1. ignore、poweroff、reboot、suspend、hibernate、hybrid-sleep、lock 以及 halt, kexec
    2. HandleLidSwitch=lock : 关闭盖子锁屏

用户与权限

User

  • whoami :查看当前登录用户名
  • su user:切换用户,exit回到上个用户
  • cat /etc/passwd |cut -f 1 -d : : 查看用户
  • useradd name :添加用户
    1. -g:指定用户所属的群组。值可以使组名也可以是GID。用户组必须已经存在的,期默认值为100,即users。
    2. -N:取消建立以用户名称为名的群组。默然group是 users
    3. -Umr: 创建同名组,home目录,系统用户
  • userdel user: 删除用户
    1. 删除用户会删除默然同名组
    2. 删除用户后用户文件属于user id,比如1000,当创建下个用户时user id会被占用,用户文件会属于新建的用户
    3. -r : 同时删除home和mail
  • passwd user passwd :设置密码
  • usermod : 修改用户
    1. -G :将用户添加进组
    2. -g : 将用户修改为属于组

Group

  • groups
    1. 查看当前登录用户组
    2. groups jude查看jude用户所在的组
  • groupadd/groupdel/groupmod

权限

  • which yum
    1. 查看yum命令的路径,然后可以看到所有者和权限

sudo : /etc/sudoers

  • 命令

    1. sudo -l
    2. visudo
    3. man sudo
  • 别名类型(Alias_Type):别名类型包括如下四种

    1. Host_Alias 定义主机别名;
    2. User_Alias 用户别名,别名成员可以是用户,用户组(前面要加%号)
    3. Runas_Alias 用来定义runas别名,这个别名指定的是“目的用户”,即sudo 允许切换至的用户;
    4. Cmnd_Alias 定义命令别名;
  • 别名规则定义格式如下:

    1. Alias_Type NAME = item1, item2
    2. Alias_Type NAME = ALL
  • 授权规则

    1. 授权用户 主机=命令动作
    2. jude ALL=(root)/bin/chmod,/bin/chmod
      1. 可以在任何可能出现的主机名的主机中,可以切换到root下执行 /bin/chown ,可以切换到任何用户招执行/bin/chmod 命令
    3. jude ALL= NOPASSWD: /bin/chown,/bin/chmod
      1. 不需要输入 用户的密码执行chown,但是执行chmod 要输入自己的密码
    4. !表示排除
    5. 授权规则3项都可以用别名代替

软件安装

wget

  • 后台自动下载网络资源
  • 安装 : yum install wget
  • wget基本命令
    1. wget http://xxx.rpm
    2. wget -i xxx.txt : 批量下载
    3. wget -r -np -nd http://xxx/download
    4. --accept rmp : 只下载.rpm文件
    5. -c : 断点续传
    6. -P : 指定目录

apt

  • apt-get update : 更新apt
  • apt-get install vim : 可以在docker容器中安装vi
  • sudo apt install yum : ubuntu安装yum

YUM

  • yum基本概念
    1. 基於RPM包管理,能够从指定的服务器自动下载RPM包并且安装
    2. 可以自动处理依赖性关系,并且一次安装所有依赖的软体包
    3. yum提供了查找、安装、删除某一个、一组甚至全部软件包的命令
  • yum源
    1. 默认源配置目录 : /etc/yum.repos.d/
    2. 配置里面所有enabled=1的(默认)源
    3. 命令
      1. yum repolist 查看当前可用源
      2. yum grouplist:显示包组
      3. yum list {available|installed|updates} :显示包列表
      4. yum list vsftpd* 显示和vsftpd匹配的列表
    4. 除了支持远程仓库源,还支持本地挂载目录和远程挂载目录
    5. yum-config-manager
      1. 安装:yum install -y yum-utils
      2. yum-config-manager --add-repo xxx.repo
      3. yum-config-manager --enable docker-ce-edge : 相当于手动将enabled=0
      4. yum-config-manager --disable docker-ce-edge
  • EPEL源
    1. 更丰富的,高质量的,更新的yum源
    2. yum search epel安装
    3. rpm下载安装
      1. 阿里巴巴开源镜像站
      2. 其他yum源设置disabled=0
  • Docker源
    1. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • yum命令选项
    1. --nogpgcheck:禁止进行gpgcheck
    2. -y: 自动回答为“yes”
    3. -q:静默模式
    4. --disablerepo=repoidglob:临时禁用此处指定的repo
    5. --enablerepo=repoidglob:临时启用此处指定的repo
    6. --noplugins:禁用所有插件
  • yum安装卸载
    1. yum install/groupinstall package
      1. yum install package-version 安装指定版本
    2. yum restall package:重做
    3. yum update package:更新包
    4. yum check-update
    5. yum remove package1 [package2]
  • yum查询
    1. yum search xxx :搜索带有某个关键字的安装包
    2. yum info 查看search到的程序包信息
    3. yum list package --showduplicates : 查看所有版本
    4. yum list installed : 查看所有已安装的package

RPM

  • rpm命令
    1. 安装 rpm -ivh --prefix=$path XX.rpm
    2. 查看 rpm -qa XX
    3. 查看安装的文件 rpm -ql XX
    4. 卸载 rpm -e --nodeps 察看得到的包名字

gcc编译源码

命令安装路径

  • which less

网络设置

CentOS连接WIFI

  • 检查无线网卡/网口
    1. dmesg | grep firmware
    2. iw dev : Interface后的是网口名,这里是wlp3s0
  • 激活无线网口
    1. ip link set wlp3s0 up
    2. ip a: BROADCAST,MULTICAST,UP,LOWER_UP中的UP代表激活
  • 安装NetworkManager
    1. nmcli d status : 如果wlp3s0的status是unmanaged,说明没有NetworkManager
    2. yum install NetworkManager-wifi
    3. systemctl restart NetworkManager
  • wifi操作
    1. nmcli r wifi on/off : 打开/关闭wifi功能
    2. nmcli d wifi : 查找wifi
    3. nmcli c: 查看已经连接的wifi
    4. nmcli c up/down/delete Jude : 连接/关闭/删除已设置过的wifi
    5. nmcli --ask d wifi connect Jude : 连接wifi Jude,输入密码
      1. error connection activation failed (7) secrets were required but not provided : 重启电脑

Linux连接WIFI

  • 检查无线网络连接情况
    1. iw wlp3s0 link
  • 扫描可连接的wifi
    1. iw wlp3s0 scan | grep SSID
  • 连接指定的SSID
    1. wpa_supplicant -B -i wlp3s0 -c <(wpa_passphrase "[SSID]" "[PIN]")
  • 用dhcp 获得 IP 分配
    1. dhclient [name]
  • 测试是否成功地从路由器获取了ip
    1. ip a show [name]

与windows组成局域网

  • 需要linux与windows拥有相同的子网掩码,相同的网关,同段位ip
    1. 可以通过连接到相同的猫
    2. 连接到相同的路由器
    3. 同处于一个wifi
  • windows在Internate选项的网络共享里,需要开启网络发现和文件共享,linux才可以ping通windows

putty连接SSH

  • 打开centos的SSH服务
    1. service sshd start
  • 下载putty连接局域网ip

alias for gradle and git

  • alias

      unalias -a
    
      alias build="disableCB && gradle ktlintformat clean build && enableCB"
      alias branch="git branch -vv && git cherry -v"
      alias push="~/jude/alias_job.sh push_branch push"
      alias fast_commit_push="~/jude/alias_job.sh fast_commit_push"
      alias merge_develop="git merge origin/develop"
      alias develop_branch="git checkout develop && git pull && branch"
      alias show_all_branch="~/jude/alias_job.sh show_all_branch"
    
      alias bbset_branch="~/jude/alias_job.sh set_current_branch"
      alias bbcreate_all_branch="~/jude/alias_job.sh create_all_branch"
      alias bbpush_branch="~/jude/alias_job.sh push_branch push_new"
    
      alias ccbuild="ccenable && gradle ktlintformat clean build"
      alias ccenable="gradle enableCompositeBuild"
      alias ccdisable="gradle disableCompositeBuild"
      alias bbnew_branch="git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream && branch"
      alias bbdelete_branch="~/jude/alias_job.sh delete_current_branch"
      alias bbresert_hard="~/jude/alias_job.sh hard_reset"
    
      alias aaa="cat ~/.bash_profile"
      alias eee="vi ~/.bash_profile"
      alias rrr="source ~/.bash_profile"
      alias la="ls -tral"
      alias ll="ls -trl"
      alias killjava="ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9"
    
  • alias job

              #!/bin/bash
    
      set_current_branch() {
      	echo $1 > ~/jude/current_branch
      	cat ~/jude/current_branch
      }
    
      hard_reset() {
      	read -r -p "Git Hard Reset your local change? [Y/n] : " input
    
      	case $input in
      		[Y])
      			git reset --hard
      			;;
      		*)
      			echo "do nothing"
      			exit 1
      			;;
      	esac
      }
    
      delete_current_branch() {
      	read -r -p "Delete current branch `cat ~/jude/current_branch`? [Y/n] : " input
    
      	case $input in
      		[Y])
      			git checkout develop && git branch -d `cat ~/jude/current_branch` && git branch -vv
      			;;
      		*)
      			echo "do nothing"
      			exit 1
      			;;
      	esac
      }
    
      fast_commit_push() {
      	git branch -vv && git status
    
      	read -r -p "Commit and push branch `cat ~/jude/current_branch`? [Y/n] : " input
    
      	case $input in
      		[Y])
      			branch=`cat ~/jude/current_branch`
      			git commit -m $branch -a
      			push_branch push
      			;;
      		*)
      			echo "do nothing"
      			exit 1
      			;;
      	esac
      }
    
      push_branch() {
      	case $1 in
      		"push_new")
      			git push --set-upstream origin `cat ~/jude/current_branch` && git branch -vv && git cherry -v
      			;;
      		"push")
      			git pull && git push && git branch -vv && git cherry -v
      			;;
      		*)
      			echo "do nothing"
      			exit 1
      			;;
      	esac
      	current=`pwd`
      	branch=`cat ~/jude/current_branch`
      	case $current in
      		"/Users/jude.sheng/IdeaProjects/paymentacceptance-adaptor")
      			open https://github.com/airwallex/paymentacceptance-adaptor/compare/develop...$branch?expand=1
      			;;
      		"/Users/jude.sheng/IdeaProjects/paymentacceptance-executor")
      			open https://github.com/airwallex/paymentacceptance-executor/compare/develop...$branch?expand=1
      			;;
      		"/Users/jude.sheng/IdeaProjects/paymentacceptance-core")
      			open https://github.com/airwallex/paymentacceptance-core/compare/develop...$branch?expand=1
      			;;
      		"/Users/jude.sheng/IdeaProjects/paymentacceptance-gateway")
      			open https://github.com/airwallex/paymentacceptance-gateway/compare/develop...$branch?expand=1
      			;;
      		"/Users/jude.sheng/IdeaProjects/paymentacceptance-tokenservice")
      			open https://github.com/airwallex/paymentacceptance-tokenservice/compare/develop...$branch?expand=1
      			;;
      		"/Users/jude.sheng/IdeaProjects/paymentacceptance-common")
      			open https://github.com/airwallex/paymentacceptance-common/compare/develop...$branch?expand=1
      			;;
      		*)
      			echo "do nothing"
      			exit 1
      			;;
      	esac
      }
    
      create_all_branch() {
      	current=`pwd`
      	cd /Users/jude.sheng/IdeaProjects
      	cd paymentacceptance-adaptor
      	pwd && git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream
      	cd ../paymentacceptance-common
      	pwd && git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream
      	cd ../paymentacceptance-executor
      	pwd && git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream
      	cd ../paymentacceptance-core
      	pwd && git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream
      	cd ../paymentacceptance-gateway
      	pwd && git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream
      	cd ../paymentacceptance-tokenservice
      	pwd && git pull && git checkout origin/develop -b `cat ~/jude/current_branch` && git branch --unset-upstream
    
      	cd /Users/jude.sheng/IdeaProjects
      	cd paymentacceptance-adaptor
      	git branch -vv
      	cd ../paymentacceptance-common
      	git branch -vv
      	cd ../paymentacceptance-executor
      	git branch -vv
      	cd ../paymentacceptance-core
      	git branch -vv
      	cd ../paymentacceptance-gateway
      	git branch -vv
      	cd ../paymentacceptance-tokenservice
      	git branch -vv
    
      	cd $current
      }
    
      show_all_branch() {
      	current=`pwd`
      	cd /Users/jude.sheng/IdeaProjects
      	cd paymentacceptance-adaptor
      	echo "=============================" && pwd && git status && echo "----------------------" && cherry -v
      	cd ../paymentacceptance-common
      	echo "=============================" && pwd && git status && echo "----------------------" && cherry -v
      	cd ../paymentacceptance-executor
      	echo "=============================" && pwd && git status && echo "----------------------" && cherry -v
      	cd ../paymentacceptance-core
      	echo "=============================" && pwd && git status && echo "----------------------" && cherry -v
      	cd ../paymentacceptance-gateway
      	echo "=============================" && pwd && git status && echo "----------------------" && cherry -v
      	cd ../paymentacceptance-tokenservice
      	echo "=============================" && pwd && git status && echo "----------------------" && cherry -v
      	cd $current
      }
    
      case $1 in
      	"set_current_branch")
      		set_current_branch $2
      		;;
      	"hard_reset")
      		hard_reset
      		;;
      	"delete_current_branch")
      		delete_current_branch
      		;;
      	"create_all_branch")
      		create_all_branch
      		;;
      	"push_branch")
      		push_branch $2
      		;;
      	"fast_commit_push")
      		fast_commit_push
      		;;
      	"show_all_branch")
      		show_all_branch
      		;;
      	*)
      		echo "do nothing"
      		exit 1
      		;;
      esac