centos7-常用命令-第一篇

1、安全配置:防火墙

设置root用户指定IP登录vi /etc/ssh/sshd_config 添加AllowUsers root@183.21.89.249
service sshd restart
查看防火墙状态:firewall-cmd --state
查看开放的端口:firewall-cmd --list-ports
开启防火墙:systemctl start firewalld.service
关闭防火墙:systemctl stop firewalld.service
加入开机启动:systemctl enable firewalld.service
移除开机启动:systemctl disable firewalld.service
开放80端口:firewall-cmd --zone=public --add-port=80/tcp --permanent 
关闭80端口:firewall-cmd --zone=public --remove-port=80/tcp --permanent
重新加载配置:firewall-cmd --reload

2、查看监听的端口

yum install -y net-tools wget
显示所有已开放端口,请使用以下命令:netstat -anp
要列出正在侦听的所有 TCP 或 UDP 端口,包括使用端口和套接字状态的服务,请使用以下命令:netstat -tunlp
此命令中使用的选项具有以下含义:
-t – 显示 TCP 端口。-u – 显示 UDP 端口。-n – 显示数字地址而不是主机名。-l – 仅显示侦听端口。-p – 显示进程的 PID 和名称。仅当您以 root 或 sudo 用户身份运行命令时,才会显示此信息。
查看监听的端口:netstat -tnlp
检查端口被哪个进程占用:netstat -tnlp | grep 80
查看进程的详细信息:ps 8888
中止进程:kill -9 8888

3、du命令:查看文件大小信息

显示目录或者文件所占空间
[root@localhost data]# du
44      ./file/redis-5.0.9/deps/hiredis/adapters
40      ./file/redis-5.0.9/deps/hiredis/examples
1296    ./file/redis-5.0.9/deps/hiredis
368     ./file/redis-5.0.9/deps/jemalloc/bin
88      ./file/redis-5.0.9/deps/jemalloc/build-aux
324     ./file/redis-5.0.9/deps/jemalloc/doc
684     ./file/redis-5.0.9/deps/jemalloc/include/jemalloc/internal
768     ./file/redis-5.0.9/deps/jemalloc/include/jemalloc
12      ./file/redis-5.0.9/deps/jemalloc/include/msvc_compat/C99
20      ./file/redis-5.0.9/deps/jemalloc/include/msvc_compat
788     ./file/redis-5.0.9/deps/jemalloc/include
16      ./file/redis-5.0.9/deps/jemalloc/m4
24      ./file/redis-5.0.9/deps/jemalloc/msvc/projects/vc2015/jemalloc
24      ./file/redis-5.0.9/deps/jemalloc/msvc/projects/vc2015/test_threads
48      ./file/redis-5.0.9/deps/jemalloc/msvc/projects/vc2015
24      ./file/redis-5.0.9/deps/jemalloc/msvc/projects/vc2017/jemalloc
24      ./file/redis-5.0.9/deps/jemalloc/msvc/projects/vc2017/test_threads
48      ./file/redis-5.0.9/deps/jemalloc/msvc/projects/vc2017
96      ./file/redis-5.0.9/deps/jemalloc/msvc/projects
12      ./file/redis-5.0.9/deps/jemalloc/msvc/test_threads
120     ./file/redis-5.0.9/deps/jemalloc/msvc
8       ./file/redis-5.0.9/deps/jemalloc/scripts
39244   ./file/redis-5.0.9/deps/jemalloc/src
156     ./file/redis-5.0.9/deps/jemalloc/test/include/test
156     ./file/redis-5.0.9/deps/jemalloc/test/include
4       ./file/redis-5.0.9/deps/jemalloc/test/integration/cpp
88      ./file/redis-5.0.9/deps/jemalloc/test/integration
64      ./file/redis-5.0.9/deps/jemalloc/test/src
4       ./file/redis-5.0.9/deps/jemalloc/test/stress
540     ./file/redis-5.0.9/deps/jemalloc/test/unit
860     ./file/redis-5.0.9/deps/jemalloc/test
19520   ./file/redis-5.0.9/deps/jemalloc/lib
62188   ./file/redis-5.0.9/deps/jemalloc
128     ./file/redis-5.0.9/deps/linenoise
316     ./file/redis-5.0.9/deps/lua/doc
40      ./file/redis-5.0.9/deps/lua/etc
1916    ./file/redis-5.0.9/deps/lua/src
84      ./file/redis-5.0.9/deps/lua/test
2380    ./file/redis-5.0.9/deps/lua
66016   ./file/redis-5.0.9/deps
104     ./file/redis-5.0.9/src/modules
52540   ./file/redis-5.0.9/src
12      ./file/redis-5.0.9/tests/assets
4       ./file/redis-5.0.9/tests/cluster/tests/helpers
4       ./file/redis-5.0.9/tests/cluster/tests/includes
80      ./file/redis-5.0.9/tests/cluster/tests
4       ./file/redis-5.0.9/tests/cluster/tmp
96      ./file/redis-5.0.9/tests/cluster
12      ./file/redis-5.0.9/tests/helpers
104     ./file/redis-5.0.9/tests/integration
20      ./file/redis-5.0.9/tests/modules
4       ./file/redis-5.0.9/tests/sentinel/tests/includes
36      ./file/redis-5.0.9/tests/sentinel/tests
4       ./file/redis-5.0.9/tests/sentinel/tmp
44      ./file/redis-5.0.9/tests/sentinel
60      ./file/redis-5.0.9/tests/support
4       ./file/redis-5.0.9/tests/tmp
8       ./file/redis-5.0.9/tests/unit/moduleapi
188     ./file/redis-5.0.9/tests/unit/type
436     ./file/redis-5.0.9/tests/unit
828     ./file/redis-5.0.9/tests
12      ./file/redis-5.0.9/utils/create-cluster
8       ./file/redis-5.0.9/utils/graphs/commits-over-time
8       ./file/redis-5.0.9/utils/graphs
8       ./file/redis-5.0.9/utils/hashtable
12      ./file/redis-5.0.9/utils/hyperloglog
20      ./file/redis-5.0.9/utils/lru
20      ./file/redis-5.0.9/utils/releasetools
136     ./file/redis-5.0.9/utils
119792  ./file/redis-5.0.9
121736  ./file
28      ./redis-data
2056    ./redis-log
123820  .
查看当前目录总大小
[root@localhost data]# du -sh
121M    .
查看当前目录下各个文件及目录占用空间大小
[root@localhost data]# du -sh *
119M    file
28K     redis-data
2.1M    redis-log
查看目录下所有文件的大小并按照大小排序
[root@localhost data]# du -sh * | sort -rh
119M    file
2.1M    redis-log
28K     redis-data
查看该文件的大小
[root@localhost data]# du -sh redis-data
28K     redis-data

4、cat命令:查看文件内容

查看redis_cluster_7001.log文件内容
[root@localhost redis-log]# cat redis_cluster_7001.log 
1680:C 16 Feb 2023 05:56:02.854 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1680:C 16 Feb 2023 05:56:02.854 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1680, just started
1680:C 16 Feb 2023 05:56:02.854 # Configuration loaded
1681:M 16 Feb 2023 05:56:02.856 * Increased maximum number of open files to 10032 (it was originally set to 1024).
查看redis_cluster_7001.log文件的内容,并且由1开始对所有输出行进行编号。(包括空白行)
[root@localhost redis-log]# cat -n redis_cluster_7001.log 
     1  1680:C 16 Feb 2023 05:56:02.854 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
     2  1680:C 16 Feb 2023 05:56:02.854 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1680, just started
     3  1680:C 16 Feb 2023 05:56:02.854 # Configuration loaded
     4  1681:M 16 Feb 2023 05:56:02.856 * Increased maximum number of open files to 10032 (it was originally set to 1024).
查看redis_cluster_7001.log文件的内容,并且由1开始对所有输出行进行编号。(不对空白行编号)
[root@localhost redis-log]# cat -b redis_cluster_7001.log 
     1  1680:C 16 Feb 2023 05:56:02.854 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
     2  1680:C 16 Feb 2023 05:56:02.854 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1680, just started
     3  1680:C 16 Feb 2023 05:56:02.854 # Configuration loaded
     4  1681:M 16 Feb 2023 05:56:02.856 * Increased maximum number of open files to 10032 (it was originally set to 1024).
当遇到有连续两行或两行以上的空白行,就代换为一行的空白行。
[root@localhost redis-log]# cat -s redis_cluster_7001.log 
1680:C 16 Feb 2023 05:56:02.854 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1680:C 16 Feb 2023 05:56:02.854 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1680, just started
1680:C 16 Feb 2023 05:56:02.854 # Configuration loaded
1681:M 16 Feb 2023 05:56:02.856 * Increased maximum number of open files to 10032 (it was originally set to 1024).
在输出内容的每一行后面加一个$符号。(包括空白行)
[root@localhost redis-log]# cat -e redis_cluster_7001.log 
1680:C 16 Feb 2023 05:56:02.854 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo$
1680:C 16 Feb 2023 05:56:02.854 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1680, just started$
1680:C 16 Feb 2023 05:56:02.854 # Configuration loaded$
1681:M 16 Feb 2023 05:56:02.856 * Increased maximum number of open files to 10032 (it was originally set to 1024).$
同时显示f1.txt和f2.txt文件内容,注意文件名之间以空格分隔,而不是逗号。
[root@localhost redis-log]# cat redis_cluster_7001.log redis_cluster_7002.log 
1680:C 16 Feb 2023 05:56:02.854 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1680:C 16 Feb 2023 05:56:02.854 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1680, just started
1680:C 16 Feb 2023 05:56:02.854 # Configuration loaded
1681:M 16 Feb 2023 05:56:02.856 * Increased maximum number of open files to 10032 (it was originally set to 1024).
对linux1.txt文件中每一行加上行号后然后写入到linux2.txt中,会覆盖原来的内容,文件不存在则创建它。
[root@localhost redis-log]# cat -n linux1.txt>linux2.txt
对f1.txt文件中每一行加上行号后然后追加到f2.txt中去,不会覆盖原来的内容,文件不存在则创建它。
[root@localhost redis-log]# cat -n linux1.txt>>linux2.txt

5、tail命令:监听文件尾部内容

默认10行,相当于增加参数 -n 10,刷新显示在屏幕上。
[root@localhost redis-log]# tail -f redis_cluster_7001.log 
1681:M 16 Feb 2023 06:19:36.475 * Marking node 8d0038d833e13d602edd3b555bd8578c3fdf8c79 as failing (quorum reached).
1681:M 16 Feb 2023 06:19:36.475 * Marking node 56052e2c8c86c81b17268357fb60091b49aa6411 as failing (quorum reached).
1681:M 16 Feb 2023 06:19:36.475 # Cluster state changed: fail
1681:signal-handler (1676546396) Received SIGTERM scheduling shutdown...
1681:M 16 Feb 2023 06:19:56.222 # User requested shutdown...
1681:M 16 Feb 2023 06:19:56.222 * Calling fsync() on the AOF file.
1681:M 16 Feb 2023 06:19:56.222 * Saving the final RDB snapshot before exiting.
1681:M 16 Feb 2023 06:19:56.224 * DB saved on disk
1681:M 16 Feb 2023 06:19:56.224 * Removing the pid file.
1681:M 16 Feb 2023 06:19:56.224 # Redis is now ready to exit, bye bye...

6、echo命令:打印字符或者回显,一般起到一个提示的作用

echo [选项] [输出内容]
-n   不在最后自动换行。
-e   使用-e扩展参数选项时,与如下参数一起使用,有不同含义,支持反斜线控制的字符转换
	\a 发出警告声。
	\b 删除前一个字符。
	\c 最后不加上换行符号。
	\f 换行但光标仍旧停留在原来的位置。
	\n 换行且光标移至行首。
	\r 光标移至行首,但不换行。
	\t 制表符,也就是Tab键。
	\v 与\f相同。
	\\ 插入\字符。
	\033[30m 黑色字 \033[0m
	\033[31m 红色字 \033[0m
	\033[32m 绿色字 \033[0m
	\033[33m 黄色字 \033[0m
	\033[34m 蓝色字 \033[0m
	\033[35m 紫色字 \033[0m
	\033[36m 天蓝字 \033[0m[openresty.md](%B7%A2%B2%BC%2Fopenresty%2Fopenresty.md)
	\033[37m 白色字 \033[0m
	\033[40;37m 黑底白字 \033[0m
	\033[41;37m 红底白字 \033[0m
	\033[42;37m 绿底白字 \033[0m
	\033[43;37m 黄底白字 \033[0m
	\033[44;37m 蓝底白字 \033[0m
	\033[45;37m 紫底白字 \033[0m
	\033[46;37m 天蓝底白字 \033[0m
	\033[47;30m 白底黑字 \033[0m
输出hello world
[root@localhost redis-log]# echo "hello\tworld"
hello\tworld
[root@localhost redis-log]# echo -e "hello\tworld"
hello   world
颜色打印输出内容
vi color.sh
#color.sh内容如下
echo -e "\033[35mHello World\033[0m"
echo -e "\033[46;37mHello World\033[0m"
echo "Hello World"
覆盖log.txt里面的内容
[root@localhost redis-log]# echo Hello World > log.txt
在log.txt追加内容
[root@localhost redis-log]# echo Hello World2 >> log.txt
[root@localhost redis-log]# cat log.txt 
Hello World
Hello World2

7、同步服务器时间

a、安装ntp服务的软件包

yum install -y ntp

b、将ntp服务设置为开机启动

systemctl enable ntpd

c、修改启动参数,增加-g -x参数,允许ntp服务在系统时间误差较大时也能正常工作

vi /etc/sysconfig/ntpd
# Command line options for ntpd
OPTIONS="-g -x"

d、启动ntp服务

systemctl restart ntpd.service

e、将系统时区改为上海时间 (亦即CST时区)

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

f、测试

[root@backup2 ~]# date
Wed Mar  1 18:12:17 CST 2023

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值