Linux常用命令

vi编辑:

跳转到最后一行:G,就是shift + g
跳转到首行 gg

跳转到当前行的第一个字符:在当前行按“0”
跳转到当前行的末尾:$,就是shift + 4

删除全部内容:
1.输入gg进入到第1行
2.按下d,然后输入G回车,此时内容被全部清除

 

grep查找

grep -C 5 ‘abc’ #查找目标前后5行 ,没有参数C也可以

grep -B 5 ‘abc’ #查找目标前5行

grep -A 5 ‘abc’ #查找目标后5行

find ./ -name "server.xml" -exec grep "Connector port" {} \;   #在当前目录下查找所有server.xml文件,并搜素关键字“Connector port”

find ./ -name "server.xml" -exec grep "Connector port=" ./ {} \;    #在当前目录下查找所有server.xml文件,并搜素关键字“Connector port”,结果显示所在文件

 -exec 选项后面跟随着所要执行的命令, 然后是一对 {}, 一个空格和一个 \,最后是一个分号

 -exec如果后面要执行多个命令,则需要将其放到脚本中,如: -exec ./text.sh {} \;

 

#将zabbix_agentd.conf文件中的11.11.11.11替换为/4.4.4.4
sed -i 's/11.11.11.11/4.4.4.4/g' zabbix_agentd.conf # g表示全局替换,不加g,则只替换第一个搜索到的内容

#将zabbix_agentd.conf文件中第4行的11.11.11.11替换为/4.4.4.4
sed -i '4{s/11.11.11.11/4.4.4.4/}' zabbix_agentd.win.conf

sed -n '5,10p' filename 这样你就可以只查看文件的第5行到第10行。

sed 's/4.4.4.4/zabbix.s.yx/g' zabbix_agentd.win.conf   #并不会真正修改源文件内容

grep Hostname zabbix_agentd.win.conf |sed 's/11.11.11.11/zabbix.s.yx/g'  #并不会真正修改源文件内容

sed参考:https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html

 

tr命令,简化版sed

echo "Hello Lily" |tr -t ' ' '\n' #将空格替换为换行符,默认参数为 -t,可省略
echo "Hello Lily" |tr '[a-z]' '[A-Z]' #将小写字母替换为大写,结果:HELLO LILY
echo "HelloLily" |tr [:lower:] [:upper:] #将小写字母替换为大写,结果:HELLOLILY
echo "Hello Li ly" |tr -s ' ' #压缩重复字符,结果:Hello Li ly
echo "Hello2Li34l8y" |tr -d [0-9] #删除指定字符,结果:HelloLily

 

cp -r /abc/aa/* /a02/ #将/abc/aa下的所有文件夹及子文件夹拷贝到 /a02目录下

cp -r /abc/aa /a02/ #将/abc 下的目录aa及其子文件夹拷贝到 /a02目录下,a02下会出现aa目录

scp -P 51000 jdk-8u73-linux-x64.tar.gz root@10.10.20.8:/root/

du -h --max-depth=0  / #查看 / 目录下所有一级目录的大小

du -h --max-depth=2 | sort -nr | head -12 #排序,只查看前12个

dh -sh #当前目录总大小

 

vim删除全部:ggdG

netstat -ntlp
-n域名解析
-t显示tcp相关
-l仅监听
-p有关的程序和进程pid

-a: 列出所有
-u:显示udp
-r:显示路由信息

 cp -rf 递归移动所有目录及子目录

 

删除非空目录不提示:rm -rf abc

 创建快捷方式:ln -s /usr/local/redis/bin/redis-server /usr/bin/redis-server

修改文件权限chmod

u 表示“用户(user)”,即文件或目录的所有者。
g 表示“同组(group)用户”,即与文件属主有相同组ID的所有用户。
o 表示“其他(others)用户”。
a 表示“所有(all)用户”,也就是包括了u、g、o,它是系统默认值。

操作符号可以是:
+ 添加某个权限。
- 取消某个权限。
= 重新分配权限。

设置mode所表示的权限可用下述字母的任意组合:
r 可读。4
w 可写。2
x 可执行。1

给sdsc文件为root添加执行权限,以下方式效果相同:
chmod 744 /usr/local/src/sdsc  
chmod u+x /usr/local/src/sdsc

 

 

压缩解压

语法:tar [-zjxcvfpP] filename
-z :是否同时用gzip压缩
-j :是否同时用bzip2压缩
-x :解包或者解压缩
-t :查看tar包里面的文件
-c :建立一个tar包或者压缩文件包
-v :可视化
-f :后面跟文件名,压缩时跟-f文件名,意思是压缩后的文件名为filename,解压时跟-f文件名,意思是解压filename。请注意,如果是多个参数组合的情况下带有-f,请把f写到最后面

tar -zcvf abc.tar.gz abc/   #对目录abc进行打包压缩

tar -zcvf home.tar.gz   /home --exclude=/home/afish   --exclude=/home/www/afish.php  #-exclude 排除不打包的目录

tar -tf abc.tar.gz   #查看压缩包文件

tar -zxvf abc.tar.gz  #对abc.tar.gz解压

tar -jxvf abc.tar.bz2   #对abc.tar.bz2解压

如果提示"tar (child): bzip2:无法 exec: 没有那个文件或目录",则yum install -y bzip2

 

#对/usr/local下的redis目录及其子目录文件打包,不包含源目录结构
tar -zcvf /data1/redis0.tar -C /usr/local redis  # -C 解压到指定目录
#查看redis0.tar信息:
tar -tf redis0.tar

 

#将redis0.tar解压到/usr/local目录下,因为redis0.tar中已包含redis目录,则在解压时无需再创建该目录
tar -zxvf redis0.tar -C /usr/local

tar -zcvf /data1/redis0.tar  /usr/local/redis打包结果如下,包含了上层目录结构,使用参数--strip-components可忽略上层目录结构

 tar -zxvf redis0.tar --strip-components 2 -C /usr/local/   #表示将 redis1目录及其所有子目录解压到/usr/local 下,忽略最上面两层目录

 

gzip -d halog.gz @解压非tar包的.gz压缩包

 

YUM

#查看当前yum源:

yum repolist

#更换yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup #修改系统自带的yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #Centos7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #Centos6
yum makecache #生成缓存
yum -y update #更新系统

 

下载test.rpm文件到指定目录而不安装
yum install -y yum-downloadonly.noarch
yum install test.rpm -y --downloadonly --downloaddir=/usr/local/src


#查看已安装软件ruby的版本:
yum list kubelet --showduplicates | sort -r #查找可用版本
yum list installed |grep ruby
yum remove ruby #卸载ruby

 

查看开机启动项
systemctl list-unit-files |grep enabled
systemctl list-unit-files --type=service | grep enabled

chkconfig --list keepalived

 参考:https://www.linuxprobe.com/systemd-command.html

 

vi /etc/rc.local

禁用开机启动项:
systemctl stop bluetooth.service
systemctl disable bluetooth.service

杀死服务:
systemctl kill httpd


查看防火墙状态:systemctl status firewalld
永久关闭防火墙:systemctl stop firewalld; systemctl disable firewalld

 

查看本机IPTABLES的设置情况
iptables -nL

开放端口,编辑配置
vim /etc/sysconfig/iptables,修改配置文件后永久保存,需要service iptables restart生效,不需要运行service iptables save

通过以下命令添加规则后即时生效,然后运行service iptables save永久生效

iptables -I INPUT -p tcp -m tcp  --dport 37982 -j ACCEPT  #允许访问本地37982端口,添加到input的第一条
iptables -A INPUT -p tcp -m tcp  --dport 37982 -j ACCEPT #允许访问本地37982端口,添加到input的最后一条
iptables -D INPUT -p tcp -m tcp  --dport 37982 -j ACCEPT #删除访问规则
iptables -I INPUT 3 -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT  #插入序号为3的规则,其他规则向后移动一位
iptables -I INPUT -s 10.4.4.4 -p tcp --dport 25 -j DROP #拒绝4.4连接本地25端口
service iptables save #保存

 

#查找5044端口状态
iptables -nL |grep 5044

iptables永久保存:https://blog.csdn.net/weixin_42497941/article/details/105219278

 

firewalld开启端口:

firewall-cmd --add-port=379/tcp --permanent #允许访问本地379端口,永久生效
firewall-cmd --reload 

https://blog.csdn.net/oToyix/article/details/108707112

http://blog.itpub.net/69955379/viewspace-2783915/

http://blog.chinaunix.net/uid-20329764-id-5863874.html

查看所有打开的端口:

firewall-cmd --zone=public --list-ports

查看想开的端口是否已开:
firewall-cmd --query-port=8080/tcp

添加指定需要开放的端口:
firewall-cmd --add-port=8080/tcp --permanent
重载入添加的端口:
firewall-cmd --reload
查询指定端口是否开启成功:
firewall-cmd --query-port=8080/tcp

移除指定端口:
firewall-cmd --permanent --remove-port=8080/tcp

 

#开放22、80、443端口,允许所有访问,永久生效
firewall-cmd --add-port=22/tcp --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
#开放icmp、vrrp协议,允许所有访问,永久生效
firewall-cmd --add-rich-rule="rule protocal value="icmp" accept" --permanent
firewall-cmd --add-rich-rule="rule protocal value="vrrp" accept" --permanent
#允许192.168.1.1访问所有
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.1" accept"

#查看防火墙规则
firewall-cmd --list-all
#规则生效
firewall-cmd --reload

#开放3306端口,仅允许某个ip访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="ip" port protocol="tcp" port="3306" accept" #reject

#删除上面开放的3306端口
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="ip" port protocol="tcp" port="3306" accept""

#删除开放的22端口
firewall-cmd --remove-port=22/tcp --permanent

 

posted on 2021-08-03 15:42  momingliu11  阅读(100)  评论(0编辑  收藏  举报