-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux
731 lines (644 loc) · 17.7 KB
/
linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
--------------------
linux指令
拷贝
cp -r a b // 把a文件夹复制成b文件夹
删除
rm -rf xxx//-r向下递归-f强制
文件列表
ll 会列出该地址下所有文件的详细信息
ls 只列出文件名或目录名
linux隐藏文件以.开头
查看所有文件 ll -a
查看隐藏文件 l.
撤销
esc u
--------------------
新建并编辑文件
echo "xxx" >> xxx.filetype
cd切运行路径时,按tab键自动补全,匹配文件名
ifconfig 看ip
当前目录
ls
创建目录
mkdir xx
mkdir -m 777 xx可读可改,最高权限
mkdir -p xx/aa/ss
删除空目录
rmdir xx
创建/查看/编辑文件
vim xx
按i,在光标前开始编辑
按a,在光标后开始编辑
按o,在当前行的下一行开始编辑
底行模式:按 shift+冒号。
:q! 强制退出
:wq 保存退出
复制文件
cp oldName newName
cp -r oldDirName newDirName
修改文件名,移动文件
mv oldName newName
mv oldFile newFile
mv oldName dir/newName
浏览文件
cat a
当前路径
pwd
清屏
clear
解压并打印解压信息
tar -xvf xxx
重新加载文件
source /etc/profile
运行jar包
java -jar xxx.war &
下次关防火墙 永久
chkconfig iptables off
临时关闭防火墙 立即生效
service iptables stop
centos6 iptables开放某一端口
iptables -I INPUT -p tcp --dport 50070 -j ACCEPT
或者
vim /etc/sysconfig/iptables
添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPT
重启
service iptables restart
查询进程root pid等
ps -ef
| 管道
搜索条件
ps -ef | grep 8081.war
ps -ef | grep java*
杀进程
kill 3592 被使用时,可能不成功
kill -15 3660 友好杀死
kill -9 3660 强制杀死
文本中高亮显示xxx
:/xxx
-----------------------------
文本中
全文替换,s表替换,%表整个文件,g当前行全局
:%s/7000/7001/g(特殊字符\转义,比如\/)
-----------------------------
执行shell脚本
sh xxx.sh
-----------------------------
centos中
redis的安装
1.安装gcc依赖
yum install -y gcc
2.下载安装包
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxvf redis-5.0.3.tar.gz
3.编译
cd redis-5.0.3
make
4.安装到指定目录
make install prefix=/opt/module/redis/redisInstall
5.1前台启动
/opt/module/redis/redisInstall/bin/redis-server
5.2后台启动
修改redis.conf,
#bind 127.0.0.1
protected-mode no
requirepass 密码
daemonize yes
/opt/module/redis/redisInstall/bin/redis-server /opt/module/redis/redis-5.0.3/redis.conf
6.开机启动
vi /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/opt/module/redis/redisInstall/bin/redis-server /opt/module/redis/redis-5.0.3/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service
创建 redis 命令软链接
ln -s /opt/module/redis/redisInstall/bin/redis-cli /usr/bin/redis
7.测试
redis
ping
8.服务操作命令
systemctl start redis.service #启动redis服务
systemctl stop redis.service #停止redis服务
systemctl restart redis.service #重新启动服务
systemctl status redis.service #查看服务当前状态
systemctl enable redis.service #设置开机自启动
systemctl disable redis.service #停止开机自启动
-----------------------------
gitlab的安装
1.安装ssh,postfix
2.下载gitlab.rpm
3.安装rpm -i gitlab-ce-13.5.4-ce.0.el7.x86_64.rpm
4.vim /etc/gitlab/gitlab.rb
external_url 'http://ip:端口'
5.gitlab-ctl reconfigure
gitlab-ctl restart
6.开启防火墙端口,阿里云端口
-----------------------------
nginx的安装
1.安装插件
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.下载安装包
比如wget https://nginx.org/download/nginx-1.19.9.tar.gz --no-check-certificate
tar -zxvf 解压,进入
3.设置安装目录
./configure --prefix=/opt/module/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
4.切换到安装目录的sbin目录下,
./nginx启动
./nginx -s reload重启
./nginx -s stop停止
5.vim /etc/profile,配置环境变量
source /etc/profile生效
---------------
centos chmod 777 filename
777的含义
user group everyone
rwx rwx rwx
421 421 421
7 7 7
权限数字模式
rwx
4210
读、写、执行、无
drwxr-xr-x解读
所属主,同一组,其余用户
rwx,r-x,r-x
读写执行,有读执行没写,有读执行没写
给子目录也一起赋权限
chmod -R 755 xxx
查看8进制权限数字
stat -c "%a" filename
---------------
centos7
vim
复制单行
yy复制所在行 p
复制多行
esc v 选中文本 v 移动到位置 p粘贴
多行注释
ctrl + v
上下移动选中行
shift + i
#
esc
取消多行注释
ctrl + v
上下移动选中行
x
上下移动整行
法1:
dd剪切当前行
移动光标
p粘贴
法2:
esc
:m+1(向下移动一行)
:m-2(向上移动一行)
删除选中文本
esc
v
选中
d
搜索文本
esc
/关键字
n(下一项)
N(上一项)
查找文件
find /查找目录/ -name 文件名
查找目录xxx
find / -name xxx -type d
查找内容
find . | xargs grep -ril '文件内容' 只显示文件名称
指定目录查找内容
grep -r 192.168.30.81 /opt/module/DataSphereStudio/linkis | less
按文件内容查找文件
find / -type f -name "*.*" | xargs grep "hello"
查看文件
less 文件
F 实时刷新文档
f 下一页
b 上一页
g 文件头部
G 文件尾部
q 退出
按时间排序
ll -rt 时间降序
查看防火墙的状态
systemctl status firewalld.service
关闭防火墙(本次)
systemctl stop firewalld service
开机禁止防火墙服务器
systemctl disable firewalld.service
开机启动防火墙服务器
systemctl enable firewalld.service
centos开服务时,一定要将防火墙的对应端口打开,不然客户端连接不上
比如在centos上开启了zookeeper(2181端口),
操作:
1.永久开启2181端口
firewall-cmd --zone=public --add-port=2181/tcp --permanent
2.重启防火墙
systemctl restart firewalld.service
3.查看防火墙开放端口
firewall-cmd --list-ports
对指定ip开放防火墙
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.20.10" accept' --permanent
防火墙重新载入生效规则
firewall-cmd --reload
查看防火墙所有规则
firewall-cmd --list-all
---------------
扩容虚拟机
虚拟机如有快照,完整克隆
看硬盘大小及使用情况
df -h
分区
fdisk /dev/sda
p 查看已分区数量
n 新增加一个分区
p 分区类型我们选择为主分区
3(因为1,2已经用过了,sda1是分区1,sda2是分区2,sda3分区3)
回车 默认(起始扇区)
回车 默认(结束扇区)
t 修改分区类型
3 选分区3(可省略)
8e 修改为LVM(8e就是LVM)
w 写分区表 (有时写入后会自动退出,就可以省略最后一步了)
q 完成,退出fdisk命令
reboot 重启虚拟机
mkfs.ext3 /dev/sda3 格式化分区3
添加新LVM到已有的LVM组,实现扩容:
lvm 进入lvm管理
lvm>pvcreate /dev/sda3 这是初始化刚才的分区3
lvm>vgextend centos /dev/sda3 将初始化过的分区加入到虚拟卷组centos (卷和卷组的命令可以通过 vgdisplay )
lvm>vgdisplay -v 查看free PE /Site(注意最后的Free PE)
lvm>lvextend -l+变量 /dev/mapper/centos-root 扩展已有卷的容量( 变量 是通过vgdisplay查看free PE /Site的大小)
lvm>pvdisplay 查看卷容量,这时可以看到一个很大的卷
lvm>quit 退出
上面只是卷扩容了,下面是文件系统的真正扩容,输入以下命令:
xfs_growfs /dev/mapper/centos-root
df -h
参考自https://blog.csdn.net/IDMANOR/article/details/106690344
---------------
yum install失败
如果原因是:
报错最下行:
Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again
是因为/etc/yum.repos.d/epel.repo 配置源的问题
将[epel]下面,baseurl解开注释,metalink加上注释即可
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
EPEL 全称 Extra Packages for Enterprise Linux
相当于添加了一个第三方源
CentOS 源包含的大多数的库都是比较旧的。并且,很多流行的库也不存在。EPEL 在其基础上不仅全,而且还够新,解决了很多人安装库的烦恼。
source /etc/profile
不单会重新读取profile文件,还会遍历读取profile.d下面所有.sh文件
---------------
配置ssh免密登录
找到.ssh文件夹
find / -name .ssh -type d
cd /root/.ssh/
生成公钥
ssh-keygen -t rsa
将公钥复制到远程机器的authorized_keys文件中,也能有到远程机器的home, ~./ssh , 和 ~/.ssh/authorized_keys的权利
ssh-copy-id bigDataNew102
ssh-copy-id bigDataNew103
ssh-copy-id bigDataNew104
现在bigDataNew102账号能免密登录bigDataNew103,bigDataNew104
将bigDataNew103,bigDataNew104重复上面命令
---------------
scp 可以实现服务器与服务器之间的数据拷贝
[root@bigDataNew102 module]# scp -r /opt/module/java8/ root@bigDataNew103:/opt/module/
rsync
rsync 和 scp 区别:用 rsync 做文件的复制要比 scp 的速度快,rsync 只对差异文件做更新。scp 是把所有文件都复制过去
xsync 集群分发 脚本
循环复制文件到所有节点的相同目录下
期望脚本在任何路径都能使用(脚本放在声明了全局环境变量的路径)
echo $PATH
/usr/local/bin
在这创建 xsync 文件
vim xsync
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in bigDataNew102 bigDataNew103 bigDataNew104
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4. 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
#6. 获取当前文件的名称
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
给脚本添加执行权限
chmod +x xsync
试一下,把bin目录发到bigDataNew103 bigDataNew104
cd ..
xsync bin
xshell工具-发送键输入到所有会话
只有键输入有效,粘贴无效
---------------
/root 系统管理员的主目录
/home 普通用户主目录的基点
---------------
cd ~ 用户家目录
如果普通用户/home/root下面没有.ssh文件夹
ssh localhost即可生成
---------------
linux中命令换行后面加 \
---------------
linux简单任务调度:
Crontab
---------------
给文件添加行号
:set nu
---------------
ubuntu找apt源
进 https://developer.aliyun.com/mirror/
ubuntu和k8s是分开的源
ubuntu的apt源配置文件地址
/etc/apt/sources.list
也支持多个源
sources.list.d/kubernetes.list
替换源后需要
apt-get update
从服务器更新最新的下载索引
apt报错:
Unable to locate package xxx 找不到xxx包
原因:
1.源不对
2.源没有更新
apt报错:
Temporary failure resolving 'mirrors.aliyun.com' 无法用阿里云下载
方式:
1.ping一下百度试试
2.给网卡加dns(223.5.5.5,223.6.6.6)
---------------
ubuntu apt查找所有安装的包
apt list --installed
ubuntu apt查找是否安装某个包
apt list docker
---------------
centos设置简单密码
echo 'newPassword'|passwd --stdin user
---------------
端口占用
查得到服务,却没有pid的,看看是不是其它用户起的,比如切换到root用户看看。
---------------
linux软链接,类似于windows下的快捷方式
ln -s a b
a是源文件,b是链接文件名,其作用是当进入b目录,实际上是链接进入了a目录
创建软连接时,a目录已经建立,目录b没有建立
删除软链接
rm -rf b,不是rm -rf b/
---------------
查看文件或目录大小
du -sh file
查看当前目录总大小
du -sh ./
查看当前目录下所有子文件与子目录的大小,并列出
du -sh *
查看当前目录下所有子文件与子目录的大小,并排序列出
du -sh * | sort -nr
将文件夹中的文件按大小排序,而且只看最大的几个
du -a|sort -rn|head -5
服务器报No space left on device,没有空间错误
1.先df -h查看挂载盘使用情况
2.如果都满了,在根目录开始du -sh *,查看最大的那个文件
3.删除合适的最大的那个文件,再lsof | grep "xxx.log",结束那个进程
---------------
将服务添加到 service(老模式,不推荐使用) 和 systemctl
以nginx举例:
service方式:
1.添加nginx启动脚本
vim /etc/init.d/nginx
2.添加内容
#!/bin/bash
#Startup script for the nginx Web Server
#chkconfig: 2345 85 15
nginx=/opt/module/nginx/sbin/nginx
conf=/opt/module/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done."
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done."
;;
test)
$nginx -t -c $conf
echo "Success."
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done."
;;
restart)
$nginx -s reload
echo "reload done."
;;
*)
echo "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac
3.添加执行权限
chmod +x nginx
4.启动和关闭
service nginx start
service nginx stop
systemctl方式:
1.添加启动文件
vim /usr/lib/systemd/system/nginx.service
2.添加内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/opt/module/nginx/logs/nginx.pid
ExecStart=/opt/module/nginx/sbin/nginx
ExecReload=/opt/module/nginx/sbin/nginx -s reload
ExecStop=/opt/module/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3.启动和关闭
systemctl start nginx
systemctl stop nginx
---------------
linux遇到PATH缓存问题,当前xshell重新连接即可
---------------
查找指定指令
history | grep 'ln -snf'
查找最近n条命令
history n
查看某一条命令前后的命令
history | less +命令序号
history | less -命令序号
---------------
查看服务器所有python软链
ls -al /usr/bin | grep python
将python软链修改指向为python2或者python3
ln -snf python2 /usr/bin/python
ln -snf /opt/module/python3/bin/python3 /usr/bin/python
python软链修改为python3后,修改yum配置文件,使yum能正常使用
vi /usr/bin/yum
vi /usr/libexec/urlgrabber-ext-down
将 #!/usr/bin/python 改为 #!/usr/bin/python2.7
yum install可正常使用
---------------
查看端口占用
netstat -lnp|grep 20303
---------------
两台服务器传输文件
进本地/a/下b文件,传到192.168.10.211的/c/目录下
rsync -av --progress /a/b [email protected]:/c/
再输入root密码
---------------
vim批量操作
esc - ctrl+v - shift+i - 输入操作 - esc
---------------
按时间顺序列出日志文件
ll -t 从新到旧
ll -t | tac 从旧到新
ll -t | less 分页
---------------
centos yum源不正确时
去https://developer.aliyun.com/mirror/ 更换阿里镜像
或者搜索框搜索如nginx对应源到本地
1.rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
2.yum info nginx
3.yum install nginx
4.service nginx start
---------------
让内核参数立即生效
sudo sysctl -p
---------------
查看资源使用情况
top
按m或M,按内存使用排序
---------------
查看内存使用情况
free -h
---------------
装了java环境后,就能运行jps指令,查看java进程了
---------------
查看进程和端口
netstat -ltnp
netstat -ltnp | grep 25672
---------------
查看pip安装模块路径
pip uninstall xxx 即可看到
---------------
压缩
zip -r a.zip xxx/
压缩排除某些目录
zip -r a.zip xxx/ -x 'node_modules/*' -x 'node_modules/*'
只压缩某些目录
zip -r a.zip xxx/ -i 'node_modules/*' -i 'node_modules/*'
解压
unzip a.zip
---------------
rpm安装另一版本报错时
如:
erlang-20.3.8.8-1.el7.centos.x86_64 conflicts with file from package erlang-erts-R16B-03.18.el7.x86_64
强制卸载这个包(可在任何目录执行)
rpm -e --nodeps erlang-erts-R16B-03.18.el7.x86_64
再安装即可
---------------
linux网卡配置
网卡配置文件位置:
/etc/sysconfig/network-scripts/ifcfg-enoxxx
一般设置静态ip所需:
BOOTPROTO="static"
ONBOOT="yes"
GATEWAY=192.168.30.1
IPADDR="192.168.30.117"
NETMASK="255.255.255.0"
DNS1="223.5.5.5"
DNS2="223.6.6.6"
修改配置后重启网卡:
1.命令重启
2.机器重启
3.上面两种方法都失败时,可以在系统ui界面右上角的网络配置里,设置静态ip和dns,再开关网卡按钮,使配置生效
---------------
将/dev/shm目录误删,恢复方法
umount /dev/shm 误删
vi /etc/fstab
添加
tmpfs /dev/shm tmpfs defaults 0 0
mount /dev/shm
df -h
已恢复/dev/shm目录
---------------
杀死所有java进程
ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9
或
ps -ef | grep xxx | grep -v grep | awk '{print $2}' | xargs kill -9
grep -v grep -v反选,排除grep查询进程本身
awk '{print $2}': 以空格作为分隔符,打印第2个字段(也就是进程号)
xargs kill -9 将前面获取到的进程号通过空格分割,传递给kill -9,杀死所有进程
---------------
xargs 参数在命令间传递
find /files/ -type f -name "._*tmp" | xargs rm -rf
find查出._tmp后缀的文件,然后rm -rf 都删除
---------------
shell判断文件/文件夹是否存在
判断文件夹是否存在,不存在就创建文件夹
if [ ! -d "./log/" ];then
mkdir log
else
echo "文件夹已经存在"
fi
# 继续下面的写入日志语句
...
判断文件是否存在
if [ -f "./data/xxx.sql" ];then
echo "文件存在"
else
echo "文件不存在"
fi
---------------
awk
处理文本,将文本按照指定的格式输出。
---------------
查看linux架构是x86还是arm
uname -r
e.g.
3.10.0-229.el7.x86_64 // x86
---------------
linux拖拽上传(适合小文件,大文件还是sftp, ftp快)
yum -y install lrzsz
---------------