这一阵操作虚拟机Linux比较多,一些操作使用也比较频繁,在这里总结一下,方便回顾。
刚装的centos,你可能获取不到动态IP,类似下面的情况:
解决方案:
首先确定你网卡的名称,使用指令ip addr
即可查看。如上图,我的当前网卡名称是 ens33
修改网卡配置文件:指令 vi /etc/sysconfig/network-scripts/ifcfg-[网卡名称]
将配置文件中修改两处:BOOTPROTO=dhcp
,ONBOOT=yes
。修改后(记得保存)如下:
BOOTPROTO=dhcp
DEVICE=eth0
HWADDR=52:54:00:d2:5a:83
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no
重启网络服务:systemctl restart network
再次测试:ip addr
,就可以看到动态的IP了
科普:SELinux是一个增强型的Linux,它是一个Linux的一个内核模块,是Linux的一个安全子系统。
具体可以参读:
一文彻底明白linux中的selinux到底是什么 - 知乎 (zhihu.com)
关闭步骤:
首先查看SELinux的状态是否开启,使用命令:cat /etc/selinux/config
如果你的SELINUX=enforcing
,说明SELinux处于开启
状态
将SELINUX
的值修改为disabled
(如下)即可关闭(需要重启机器才能生效,快速重启:shutdown -r now
)。
[root@VM-16-12-centos ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
查看防火墙状态:systemctl status firewalld
或firewall-cmd --state
查看所有开启的端口:firewall-cmd --list-ports
手动开启端口:firewall-cmd --zone=public --add-port=[端口号]/[tcp或者udp] --permanent
设置防火墙开机自启:systemctl enable firewalld.service
查看防火墙是否开机自启:systemctl is-enabled firewalld.service ;echo &?
[root@VM-16-12-centos ~]# systemctl is-enabled firewalld.service ;echo &?
enabled
[1] 27811
-bash: ?: 未找到命令
[1]+ 完成 echo
防火墙重启:systemctl restart firewalld.service
查看防火墙版本:firewall-cmd --version
永久关闭防火墙:chkconfig iptables off
有些系统一些命令是没有预先安装的,需要我们手动安装一下,巧用工具
yum
工具搞定,步骤如下:
先用yum search [命令]
找一下你需要安装哪个插件和工具,如这里我要找lsof
相关的:
使用yum install [名称]
进行安装
等安装完成后,就可以使用这个命令了。
(安装其他的命令也同理)
vi或者vim是linux里很强大的编辑工具,具体如何使用可以百度一下,这里不详细展开。
下面是一些常用的配置,让你的vi或者vim用起来更加丝滑。
vi ~/.vimrc
set number
set mouse=a
,然后回车确认即可,只在本次生效。~/.vimrc
文件,在里面添加一行set mouse=a
,保存即可,全局生效。vi
或者vim
的,为方便我们在容器内部编辑文件,可以手动安装一下该工具文件属性信息:
参考:
https://www.cnblogs.com/JZjuechen/p/15689796.html
演示:
权限标示:r-可读,w-可写,x-可执行
权限分配角色:u-文件创建者 g-用户所在组,o-用户组外的其他人
参考学习:tar 解压缩命令详解_追求者2016的博客-CSDN博客_tar
使用系统自带的 tar 工具即可完成文件的压缩与解压。
参数说明:
- -c: 建立压缩档案
- -x:解压
- -t:查看内容
- -r:向压缩归档文件末尾追加文件
- -u:更新原压缩包中的文件
可选参数:
- -z:有gzip属性的
- -j:有bz2属性的
- -J:具有xz属性的
- -Z:有compress属性的
- -v:显示所有过程
- -O:将文件解开到标准输出
仅打包,不压缩。
命令:tar -cvf [打包后文件名称] [要打包的文件]
示例:创建两个文件夹,并打包(如果打包全部文件,可以直接用*
代替)
[root@VM-16-12-centos demo]# touch d1.txt
[root@VM-16-12-centos demo]# touch d2.java
[root@VM-16-12-centos demo]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月 24 23:31 d1.txt
-rw-r--r-- 1 root root 0 8月 24 23:31 d2.java
[root@VM-16-12-centos demo]# tar -cvf d.tar ./*
./d1.txt
./d2.java
[root@VM-16-12-centos demo]# ls
d1.txt d2.java d.tar
[root@VM-16-12-centos demo]# ll
总用量 12
-rw-r--r-- 1 root root 0 8月 24 23:31 d1.txt
-rw-r--r-- 1 root root 0 8月 24 23:31 d2.java
-rw-r--r-- 1 root root 10240 8月 24 23:32 d.tar
先打包,后压缩。以gzip压缩 在参数f后面的压缩文件名是自己取的,习惯上用tar来做,如果加z参数,则以tar.gz 或tgz来代表gzip压缩过的tar file文件。
命令:tar -zcvf [打包后文件名] [-C] [指定输出父目录] [文件]
示例:
[root@VM-16-12-centos demo]# ls
d1.txt d2.java
[root@VM-16-12-centos demo]# tar -zcvf d.tar.gz ./*
./d1.txt
./d2.java
[root@VM-16-12-centos demo]# ls
d1.txt d2.java d.tar.gz
命令:tar -zxvf [压缩包] [-C] [解压位置]
示例:
[root@VM-16-12-centos demo]# ls
d.tar.gz
[root@VM-16-12-centos demo]# tar -zxvf d.tar.gz
./d1.txt
./d2.java
[root@VM-16-12-centos demo]# ls
d1.txt d2.java d.tar.gz
[root@VM-16-12-centos demo]#
tar 的命令参数一定要写完整,否则会出现卡住,无响应的情况。
否则会报错:
自定义主机名:hostnamectl set-hostname [主机名]
查看主机名:hostname
路径C:\Windows\System32\drivers\etc
末尾添加:192.168.220.128 node1
ping测试:打开cmd,直接ping主机名
问题:
linux =>win ping不通
win => linux 可以ping通
解决:win的防火墙没关 关闭防火墙命令:
netsh advfirewall set allprofiles state off
yum 主要功能是更方便的
添加
/删除
/更新
RPM包,自动解决
包的依赖性问题,便于管理大量系统的更新问题。学习参考:
https://baijiahao.baidu.com/s?id=1708418392526536542&wfr=spider&for=pc
原装镜像源:位于/etc/yum.repos.d/
的CentOS-Base.repo
优质镜像源:
如要修改原镜像源,请:
/etc/yum.repos.d/
目录下yum clear all
yum makecache
补充:
tail用于查看文件的内容
常用参数:
-f
:查阅正在改变的日志文件
原使用文档如下:
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=K output the last K bytes; or use -c +K to output
bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
-F same as --follow=name --retry
-n, --lines=K output the last K lines, instead of the last 10;
or use -n +K to output starting with the Kth
--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to see if it has been unlinked or renamed
(this is the usual case of rotated log files);
with inotify, this option is rarely useful
--pid=PID with -f, terminate after process ID, PID dies
-q, --quiet, --silent never output headers giving file names
--retry keep trying to open a file if it is inaccessible
-s, --sleep-interval=N with -f, sleep for approximately N seconds
(default 1.0) between iterations;
with inotify and --pid=P, check process P at
least once every N seconds
-v, --verbose always output headers giving file names
--help 显示此帮助信息并退出
--version 显示版本信息并退出
启动命令开头加上nohup
,应用就会在后台启动,即使关掉当前窗口,程序依然运行,如要停止服务,找到程序的pid号,使用kill -9 pid号
即可。
命令后加 1>/dev/null 2>&1
,表示将命令产生的输入和错误都输入到空设备,也就是不输出的意思。其中/dev/null
代表空设备。
使用whereis + [应用名]
即可找到
[root@VM-16-12-centos ~]# whereis nginx
nginx: /usr/bin/nginx
[root@VM-16-12-centos ~]# whereis java
java: /usr/bin/java /usr/share/man/man1/java.1
[root@VM-16-12-centos ~]# where is docker
-bash: where: 未找到命令
临时生效:使用export
可以临时添加环境变量,但是当你下次开机的时候它就不在了。如:
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
永久有效:编辑文件/etc/profile
,在里面添加环境变量。(修改后要立即生效,记得运行source /etc/profile
即可)
如果你输入ip addr
或者ifconfig
没有看到自己网卡下面有对应的IP,尝试下面的步骤:
NetworkManager
,使用命令systemctl stop NetworkManager
systemctl restart network
上面的情况报错的原因是文件的换行符
不对,你在win上编辑好脚本保存时换行符是CRLF
,但在linux上要求换行符是LF
,这时候你需要手动更正一下。
方式1:将脚本拖出到win,然后用文本编辑器修改换行符形式,如上图。
方式2:直接在linux里面,使用vi
或者vim
编辑器打开改文件,然后切换到命令模式,输入命令set ff=unix
,回车执行,然后保存退出,即可完成修改。
使用自带crontab即可实现。常用操作如下:
crontab -l
:查看当前用户定时任务列表crontab -e
:编辑当前用户的定时任务crontab -r
:删除当前用户的定时任务无论是shell脚本还是python脚本,用它来做定时任务都是很方便的呢。