远程登录客户端
- putty(小巧方便、免费)
- xshell(功能强大、付费)
- secureCRT(功能强大、付费)
- finalshell(多终端支持,免费, 支持win远程桌面)
xshell
secureCRT
finalShell
目录说明以及管理
文件基本属性与基本的操作
常用的命令
#查看文件列表
ls
ll
#进入目录
cd
#显示当前所在的目录的位置
pwd
#列出N条历史命令
history [n]
#执行上一条命令
!!
#执行模糊匹配到的历史命令
!keyword
#命令补全、文件补全
tab
#中断正在进行
ctrl+c
#查看时间
date
#查看命令手册
man command
#清除屏幕
ctrl+l
或者
clear
文件操作
#拷贝文件
cp
#删除文件
rm
#移动文件
mv
#创建文件夹
mkdir
#创建一个空文件夹或者修改时间
touch
#查看文件内容:
cat more less tail head
例子:less text.log
#编辑文件:
vi/vim
#修改文件权限相关:
chown chogrp chmod
例子:chown -R test:test /opt
chmod +x xxx.sh
#查找文件
locate keyword
find . -name keyword
find . -size +/-500M
find . -type fdbcs[文件,目录,块文件,字符文件,socket文件]
#查找文件内容
grep keyword
#显示磁盘分区上可以使用的磁盘空间
df [-h]
#显示每个文件和目录的磁盘使用空间
du [-h] file
文件编辑
三种模式:
一般模式(进入vi的默认模式,可以复制、删除、粘贴、不能输入)
编辑模式(在一般模式下输入i,I,a,A,o,O,R,r)
命令行模式(输入:或者/或者?任何一种进入)
ESC:回到一般模式
在一般模式下输入 :set number可以显示行号
移动光标 j k h l 下上左右(数字+命令) ctrl+f(page down)
ctrl+b(page up) o(行首)
¥(行尾) gg(文件第一行) nG(文件第n行) G (文件最后一行)
查找、替换/向下查找? 向上查找 n
重复前一个查找动作 N ; 反向进行前一个查找动作 :n1,n2s/word1/woed2g(n1行到n2行内替换):1,$s/word1/word2/g (第一行到最后一行内替换):1,$s/word1/word2/gc(第一行到最后一行内替换+确认机制)
复制、删除、粘贴x/X 向后/前删除一个字符
dd删除光标所在的行
ndd向下删除n行 d1G删除从光标到第一行
dG删除从光标到最后一行
d$删除光标到行尾
d0删除光标到行首
u撤销 ctrl+r重做
将d换成y就是复制
p/P粘贴
压缩与解压
1.压缩/解压tar.gz文件
压缩:tar -zcvf 文件名.tar.gz 要压缩的文件路径或者文件名
解压:tar -zxvf 文件名.tar.gz -C 目的
查看压缩文件中的文件而不解压:tar -tvf 文件名.tar.gz
解释:z代表gzip格式,也就是后缀名gz所代表的文件
c代表create创建压缩文件
x代表extract提取文件,也就是压缩的意思
v代表verbose显示文件的详情信息
f代表file文件,所以f参数一定要放在最后面
t代表list,显示文件中的文件而不解压
-C指定解压文件到xx目录
网络管理
网卡配置
网卡信息位于/etc/sysconfig/network-scripts,一般名称为icfg-eth0,如果有多张网卡,
一般为ifcfg-eth1,ifcfg-eth2,....
cd /etc/sysconfig/network-scripts/
vim iifcfg-eth0
关注点为带注释的地方,一般也只需要配置这些地方
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no # 静态ip BOOTPROTO=static DEFROUTE=yes NAME=eth0 UUID=f7c70419-fbb0-4005-9ff7-59c5b541a2ea DEVICE=eth0 # 开机启动 ONBOOT=yes # ip IPADDR=172.24.108.223 # 网关 GATEWAY=172.24.108.1 # 子网掩码 NETMASK=255.255.255.0 # dns DNS1=61.139.2.69
最后重启网络
#重启网络服务
systemctl restart network
#查看网卡信息
ip addr
端口放行
最简单快捷的方式,是关闭防火墙
#关闭防火墙服务
systemctl stop fireword
#禁用防火墙服务
systemctl disable fireword
但此方法过于简单粗暴, 有安全风险,不建议使用.
安全的做法是根据需要自定义开放,关闭端口
使用firewall -cmd命令
#开放5672端口,--permanent 代表永久有效
firewall-cmd --zone=public --add-port=5672/tcp --permanent
#关闭5672端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent
#配置立即生效
firewall-cmd --reload
#查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
软件包管理
yum
Usage: yum [options] COMMAND
List of Commands:
check 检查 RPM 数据库问题
check-update 检查是否有可用的软件包更新
clean 删除缓存数据
deplist 列出软件包的依赖关系
distribution-synchronization 已同步软件包到最新可用版本
downgrade 降级软件包
erase 从系统中移除一个或多个软件包
fs Acts on the filesystem data of the host, mainly for removing docs/lanuages for minimal hosts.
fssnapshot Creates filesystem snapshots, or lists/deletes current snapshots.
groups 显示或使用、组信息
help 显示用法提示
history 显示或使用事务历史
info 显示关于软件包或组的详细信息
install 向系统中安装一个或多个软件包
langavailable Check available languages
langinfo List languages information
langinstall Install appropriate language packs for a language
langlist List installed languages
langremove Remove installed language packs for a language
list 列出一个或一组软件包
load-transaction 从文件名中加载一个已存事务
makecache 创建元数据缓存
provides 查找提供指定内容的软件包
reinstall 覆盖安装软件包
repo-pkgs 将一个源当作一个软件包组,这样我们就可以一次性安装/移除全部软件包。
repolist 显示已配置的源
search 在软件包详细信息中搜索指定字符串
shell 运行交互式的 yum shell
swap Simple way to swap packages, instead of using shell
update 更新系统中的一个或多个软件包
update-minimal Works like upgrade, but goes to the 'newest' package match which fixes a problem that affects your system
updateinfo Acts on repository update information
upgrade 更新软件包同时考虑软件包取代关系
version 显示机器和/或可用的源版本。
Options:
-h, --help 显示此帮助消息并退出
-t, --tolerant 忽略错误
-C, --cacheonly 完全从系统缓存运行,不升级缓存
-c [config file], --config=[config file]
配置文件路径
-R [minutes], --randomwait=[minutes]
命令最长等待时间
-d [debug level], --debuglevel=[debug level]
调试输出级别
--showduplicates 在 list/search 命令下,显示源里重复的条目
-e [error level], --errorlevel=[error level]
错误输出级别
--rpmverbosity=[debug level name]
RPM 调试输出级别
-q, --quiet 静默执行
-v, --verbose 详尽的操作过程
-y, --assumeyes 回答全部问题为是
--assumeno 回答全部问题为否
--version 显示 Yum 版本然后退出
--installroot=[path] 设置安装根目录
--enablerepo=[repo] 启用一个或多个软件源(支持通配符)
--disablerepo=[repo] 禁用一个或多个软件源(支持通配符)
-x [package], --exclude=[package]
采用全名或通配符排除软件包
--disableexcludes=[repo]
禁止从主配置,从源或者从任何位置排除
--disableincludes=[repo]
disable includepkgs for a repo or for everything
--obsoletes 更新时处理软件包取代关系
--noplugins 禁用 Yum 插件
--nogpgcheck 禁用 GPG 签名检查
--disableplugin=[plugin]
禁用指定名称的插件
--enableplugin=[plugin]
启用指定名称的插件
--skip-broken 忽略存在依赖关系问题的软件包
--color=COLOR 配置是否使用颜色
--releasever=RELEASEVER
在 yum 配置和 repo 文件里设置 $releasever 的值
--downloadonly 仅下载而不更新
--downloaddir=DLDIR 指定一个其他文件夹用于保存软件包
--setopt=SETOPTS 设置任意配置和源选项
--bugfix Include bugfix relevant packages, in updates
--security Include security relevant packages, in updates
--advisory=ADVS, --advisories=ADVS
Include packages needed to fix the given advisory, in
updates
--bzs=BZS Include packages needed to fix the given BZ, in
updates
--cves=CVES Include packages needed to fix the given CVE, in
updates
--sec-severity=SEVS, --secseverity=SEVS
Include security relevant packages matching the
severity, in updates
常用命令:
#列出所有可更新的软件清单命令:
yum check-update
#更新所有软件的命令:
yum update
#仅安装指定的软件命令:
yum install <package_name>
#仅更新指定的软件命令
yum update <package_name>
#列出所有可安装的软件清单命令:
yum list
#删除软件包的命令
yum remove <package_name>
#查找软件包的命令
yum search <keyword>
rpm语法
rpm [-acdhilqRsv][-b<完成阶段><套间档>+][-e<套件挡>][-f<文件>+][-i<套件档>][-p<套件档>+][-U<套件档>][-vv][--addsign<套件档>+][--allfiles][--allmatches][--badreloc][--buildroot<根目录>][--changelog][--checksig<套件档>+][--clean][--dbpath<数据库目录>][--dump][--excludedocs][--excludepath<排除目录>][--force][--ftpproxy<主机名称或IP地址>][--ftpport<通信端口>][--help][--httpproxy<主机名称或IP地址>][--httpport<通信端口>][--ignorearch][--ignoreos][--ignoresize][--includedocs][--initdb][justdb][--nobulid][--nodeps][--nofiles][--nogpg][--nomd5][--nopgp][--noorder][--noscripts][--notriggers][--oldpackage][--percent][--pipe<执行指令>][--prefix<目的目录>][--provides][--queryformat<档头格式>][--querytags][--rcfile<配置档>][--rebulid<套件档>][--rebuliddb][--recompile<套件档>][--relocate<原目录>=<新目录>][--replacefiles][--replacepkgs][--requires][--resign<套件档>+][--rmsource][--rmsource<文件>][--root<根目录>][--scripts][--setperms][--setugids][--short-circuit][--sign][--target=<安装平台>+][--test][--timecheck<检查秒数>][--triggeredby<套件档>][--triggers][--verify][--version][--whatprovides<功能特性>][--whatrequires<功能特性>]
rpm -qa | frep package
#安装列表
rpm -qa | grep package
#安装
rpm -ivh package
#忽略报错,强制安装
rpm --force -ivh package
#忽略报错,强制安装,不检查依赖
rpm --force -ivh package --nodeps
#卸载,不检查依赖
rpm -e --nodeps package
第六节:环境变量
1.临时变量
仅针对当前终端有效
PATH=$PATH:URL=www.sina.com
1.用户变量
# 编辑bash_profile
vim ~/.bash_profile
在最后添加
export URL2=www.qq.com
export PATH=URL2:PATH
# 使其生效
source ~/.bash_profile
验证
3.全局变量
vim /etc/profiles
添加过程和用户变量设置一致
注意:当PATH修改有误的时候,可能造成很多命令不可用,紧急修复,在终端中输入
export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
然后将PATH中修改地方还原
进程与资源管理
远程复制
scp语法
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2
示例:
#拷贝本机/home/adminstrator/test 整个目录至远程主机192..168.1.100:/root/
scp -r /home/adminstrator/test/ root@192.168.1.100:/root/
#拷贝单个文件至远程主机
scp /home/adminstrator/Desktop/old/driver/test/test.txt root@192.168.1.100:/root/
#远程文件/文件夹的下载
scp -r root@192.168.62.10:/root/ /home/adminstrator/Desktop/new/
查看端口
语法:netstat -anp | grep port
usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces display interface table
-g, --groups display multicast group memberships
-s, --statistics display networking statistics (like SNMP)
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-W, --wide don't truncate IP addresses
-n, --numeric don't resolve names
--numeric-hosts don't resolve host names
--numeric-ports don't resolve port names
--numeric-users don't resolve user names
-N, --symbolic resolve hardware names
-e, --extend display other/more information
-p, --programs display PID/Program name for sockets
-o, --timers display timers
-c, --continuous continuous listing
-l, --listening display listening server sockets
-a, --all display all sockets (default: connected)
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
-Z, --context display SELinux security context for sockets
示例:
#查看8080端口是否被占用
netstat -anp | grep 8080
查看进程
ps -ef ps aux
字段含义如下: UID PID PPID C STIME TTY TIME CMD zzw 14124 13991 0 00:38 pts/0 00:00:00 grep
UID :程序被该 UID 所拥有
PID :就是这个程序的 ID 29826
PPID :则是其上级父程序的ID
C :CPU使用的资源百分比
STIME :应用启动时间
TTY :登入者的终端机位置
TIME :使用掉的CPU时间
CMD :所下达的是什么指令
关闭进程
kill -signal PID 例子:
kill -9 PID号
常用signal1启动被终止的进程,类似重新启动9强制中断一个进程的运行15以正常的结束进程来终止该进程
内存资源
free -b/k/m/g bkmg指的是查看单位
total系统总的可用物理内存大小
used已经被使用的物理内存的大小
free还有多少物理内存可用
shared 被共享使用的物理内存的大小
cache 使用的物理内存的大小
available 还可以被应用程序使用的内存大小
其中有两个概念需要注意:
free与available的区别:
free是真正尚未被使用的物理内存数量
available是应用程序认为可用内存数量
CPU的资源
top
free
关注点:系统负载(<1) 任务运行状态(僵尸状态) 总体CPU占用率(%wa指I/Owait)
总体内存占用率 单个线程CPU,
内存占用率
排序 默认按CPU占用率 按内存占用排序:M 恢复CPU排序:P
管道
| 每个管道后面必须接命令,且该命令必须要能接收到standard input才行 例如:less
more head tail grep cut sort wc uniq等的 反例:ls cp mv就不能接在管道的后面,
例如:ps -ef | less -N
系统管理
开机启动项
chkconfig
用法:chkconfig [--list] [--type <type>] [name]
chkconfig --add <name>
chkconfig --del <name>
chkconfig --override <name>
chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
案例:
vim /etc/init.d/test
#!/bin/bash
# chkconfig: 3 88 88
echo $(date) >> /opt/test.txt
#添加执行权限
chmod +x /etc/init.d/test
#添加到chkconfig
chkconfig --add test
#开启开机自启动
chkconfig test on
#查看
chkconfig --list test
#关闭开机启动
chkconfig test off
#删除
chkconfig --del test
定时任务
crontab语法:
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x <mask> enable debugging
格式
minute hour day month week command
#编辑
crontab -e
#列出全部定时任务
crontab -l
#重新加载,使其生效
crond reload
案例:
#每分钟将时间输出到test.log中
* * * * * echo $(date) >> /opt/test.log
时间校准
Linux中有个ntp包可以自动校准时间
#安装
yum install ntp
#校准命令
ntpdate cn.pool.ntp.org
shell编程
案例
./start.sh p1 p2
一般第一个参数是脚本本身, 参数从第二个开始,从编程的角度来说,也就是下标为1, 即$1
判断
if ,结束反写if ,即就是fi
示例:
#判断参数1是否传入
if [ -z "$1" ]; then
echo "please input params"
fi
#判断pid是否为空
if [ "$pid" = "" ];then
echo 进程不存在
fi
获取命令执行的结果
语法$(command)
示例:
# 查询tomcat进程的pid,将结果赋值给pid
pid=$(ps -ef|grep tomcat|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
数组定义
语法(e1 e2 ...),用() 括起来,元素之间用空格分开
示例:
BUSINESS_SERVICES=(ascii-mp-action-record ascii-mp-application ascii-mp-auth ascii-mp-constant ascii-mp-statistics ascii-mp-workstation ascii-cs-configcenter)
for循环
语法 for vailable in array; do...done
示例:
for service_name in ${BUSSINESS_SERVICES[@]};do
start $service_name
sleep 30
done
函数的定义与调用
function name(){...}
#定义
function test(){
echo "test function"
}
#调用
test
参考
#!/bin/bash
JAVA_BASE_DIR=$JAVA_HOME
BASE_DIR=/opt/app
PROFILE=production
BASE_SERVICES=(ascii-eureka sae-config-center-data sae-config-center-admin sae-config-center-web ascii-server-admin ascii-oauth2 ascii-zuul ascii-user-center ascii-client-management)
BUSINESS_SERVICES=(ascii-mp-action-record ascii-mp-application ascii-mp-auth ascii-mp-constant ascii-mp-statistics ascii-mp-workstation ascii-cs-configcenter)
if [ -z "$1" ]; then
PROFILE=$1
fi
function start(){
if [ ! -d "$BASE_DIR/$1" ]; then
echo $BASE_DIR/$1不存在
fi
if [ -d "$BASE_DIR/$1" ]; then
echo 启动$1
cd $BASE_DIR/$1
sh start.sh $JAVA_BASE_DIR $PROFILE
fi
}
function stop(){
if [ ! -d "$BASE_DIR/$1" ]; then
echo $BASE_DIR/$1不存在
fi
if [ -d "$BASE_DIR/$1" ]; then
echo 停止$1
cd $BASE_DIR/$1
sh stop.sh
fi
}
function startAll(){
echo 启动基础服务...
echo 启动redis
cd $BASE_DIR/redis/linux
sh start.sh
sleep 10
cd $BASE_DIR
for service_name in ${BASE_SERVICES[@]}; do
start $service_name
sleep 30
done
echo 基础服务启动完成...
echo 启动业务服务...
for service_name in ${BUSINESS_SERVICES[@]}; do
start $service_name
sleep 30
done
ps -ef | grep java
echo 业务服务启动完成...
}
function stopAll(){
echo 停止业务服务...
for service_name in ${BUSINESS_SERVICES[@]}; do
stop $service_name
done
echo 业务服务停止完成...
echo 停止基础服务...
for service_name in ${BASE_SERVICES[@]}; do
stop $service_name
done
cd $BASE_DIR/redis/linux
sh stop.sh
echo 基础服务停止完成...
}
function restartAll(){
stopAll
startAll
}
function restart(){
stop $1
start $1
}
if [ -z "$1" ]; then
echo 'please excute ./ascii.sh start appname profile|start all profile|stop appname|stop all|restart appname profile|restart all profile'
else
if [ -n "$3" ]; then
PROFILE=$3
fi
if [ "$1" = start ]; then
if [ "$2" = all ]; then
startAll
elif [ "$2" = redis ]; then
cd $BASE_DIR/redis/linux
sh start.sh
cd $BASE_DIR
elif [ -z "$2" ]; then
echo 'please excute ./ascii.sh start appname profile|all profile'
else
start $2
ps -ef | grep java
fi
elif [ "$1" = stop ]; then
if [ "$2" = all ]; then
stopAll
elif [ "$2" = redis ]; then
cd $BASE_DIR/redis/linux
sh stop.sh
cd $BASE_DIR
elif [ -z "$2" ]; then
echo 'please excute ./ascii.sh stop appname|all'
else
stop $2
fi
elif [ "$1" = restart ]; then
if [ "$2" = all ]; then
restartAll
elif [ "$2" = redis ]; then
cd $BASE_DIR/redis/linux
sh stop.sh
sh start.sh
cd $BASE_DIR
elif [ -z "$2" ]; then
echo 'please excute ./ascii.sh restart appname profile|all profile'
else
restart $2
fi
else
echo 'please excute ./ascii.sh start appname|start all|stop appname|stop all|restart appname|restart all'
fi
fi
资料推荐
书: linux 101 鸟哥的linux私房菜
网站: 菜鸟:Linux 教程 | 菜鸟教程