返回列表 发帖

CentOS8基本命令

笺注:这是在 CentOS8编译安装Zabbix4.4.5 的基础上进行的。


检测防火墙服务firewalld是否运行中:

方法一:(要按“q”键退出)
systemctl status firewalld
图片1.png
2021-1-17 22:34




方法二:
[root@centos8 ~]# systemctl | grep firewalld
firewalld.service loaded active running firewalld - dynamic firewall daemon
[root@centos8 ~]#
[root@centos8 ~]# echo $?
0

注释:返回值为0时,才代表True



方法三:
[root@centos8 ~]# ps -ef |grep firewalld
root         970       1  0 17:16 ?        00:00:00 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
root        2249    2189  0 17:27 pts/1    00:00:00 grep --color=auto firewalld
[root@centos8 ~]#
[root@centos8 ~]# echo $?
0

[root@centos8 ~]# ps -ef |grep firewalld |grep -v grep
root         970       1  0 17:16 ?        00:00:00 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
[root@centos8 ~]#
[root@centos8 ~]# echo $?
0



停止服务firewalld:
[root@centos8 ~]# systemctl stop firewalld


服务firewalld停止后的效果:
图片2.png
2021-1-17 22:35




[root@centos8 ~]# systemctl | grep firewalld
[root@centos8 ~]#
[root@centos8 ~]# echo $?
1



[root@centos8 ~]# ps -ef |grep firewalld
root        2338    2189  0 17:30 pts/1    00:00:00 grep --color=auto firewalld
[root@centos8 ~]# echo $?
0
[root@centos8 ~]#
[root@centos8 ~]# ps -ef |grep firewalld |grep -v grep
[root@centos8 ~]#
[root@centos8 ~]# echo $?
1



启动服务firewalld:
[root@centos8 ~]# systemctl start firewalld


重启服务firewalld:
[root@centos8 ~]# systemctl restart firewalld





查看服务firewalld是否开机自动启动:(服务firewalld默认就是开机自动启动)
[root@centos8 ~]# systemctl is-enabled firewalld
enabled



查看开机自动启动的服务的列表:(截图有省略)
systemctl list-unit-files|grep enabled
图片3.png
2021-1-17 22:37




禁止开机时自动启动服务firewalld:
[root@centos8 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@centos8 ~]# systemctl is-enabled firewalld
disabled



查看开机时不可自动启动的服务的列表:(截图有省略)
systemctl list-unit-files|grep disabled
图片4.png
2021-1-17 22:38




开机时自动启动服务firewalld:
[root@centos8 ~]# systemctl enable firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.













######

使用命令netstat检测本机的某个端口是否运行中

检测TCP 80端口是否运行中:
[root@centos8 ~]# netstat -anp | grep 80
-bash: netstat: 未找到命令

注释:最小化安装是没有命令netstat


安装相关软件包:
[root@centos8 ~]# dnf -y install net-tools

图片5.png
2021-1-17 22:39





启动MariaDB:
[root@centos8 ~]# systemctl start mysqld


检测TCP 3306端口是否运行中:
[root@centos8 ~]# netstat -anp | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      1103/mysqld
[root@centos8 ~]# echo $?
0

注释:返回值为0时,才代表True


命令netstat也可以检测服务是否在运行中:
netstat -anp | grep mysqld
图片6.png
2021-1-17 22:40



[root@centos8 ~]# netstat -anp | grep mysqld > /dev/null
[root@centos8 ~]# echo $?
0





关闭MariaDB:
[root@centos8 ~]# systemctl stop mysqld

[root@centos8 ~]# netstat -anp | grep 3306
[root@centos8 ~]#
[root@centos8 ~]# echo $?
1

[root@centos8 ~]# netstat -anp | grep mysqld
[root@centos8 ~]#
[root@centos8 ~]# echo $?
1










######

开机自动执行某些命令:
[root@centos8 ~]# vi /etc/rc.local
在末尾追加自定义的命令:(服务器重启后,无论系统用户root登录与否,命令都会执行)
echo 'Welcome to zhuohua' > /zhuohua.txt

如下图:
cat /etc/rc.local
图片7.png
2021-1-17 22:41


cat /etc/rc.d/rc.local
图片8.png
2021-1-17 22:41



[root@centos8 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 4月  24 2020 /etc/rc.local -> rc.d/rc.local

笺注:
/etc/rc.local 是/etc/rc.d/rc.local的链接文件;
/etc/rc.local  的默认权限是777
/etc/rc.d/rc.local  的默认权限是644


若不先给予/etc/rc.d/rc.local执行的权限,CentOS7/8不会开机自动执行/etc/rc.local里的命令:
[root@centos8 ~]# chmod a+x /etc/rc.d/rc.local
[root@centos8 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 590 1月  17 21:55 /etc/rc.d/rc.local



CentOS8重启系统:
[root@centos8 ~]# which init
/usr/sbin/init
[root@centos8 ~]#
[root@centos8 ~]# init 6


服务器重启后,文件自动创建了:
[root@centos8 ~]# ll /*.txt
-rw-r--r-- 1 root root 19 1月  17 22:05 /zhuohua.txt

[root@centos8 ~]# cat /zhuohua.txt
Welcome to zhuohua



CentOS8关闭系统:
[root@centos8 ~]# init 0





相关文章:
最小化安装CentOS8
PuTTY远程管理CentOS8
Xshell4远程管理CentOS8

CentOS8使用计划任务
CentOS8使用命令ftp/lftp/scp

返回列表