背景
新裝的 CentOS7.6
環境,準備做一個測試機,因爲是內網做着玩的,裝完第一件事就是把防火牆關了,然後安裝了其他的開發環境。
問題
今天想打開防火牆測試一個功能的時候,發現報了這樣一個錯:
[root@localhost ~]# firewall-cmd --state
Traceback (most recent call last):
File "/usr/bin/firewall-cmd", line 24, in <module>
from gi.repository import GObject
ModuleNotFoundError: No module named 'gi'
我就是想查一下防火牆現在的狀態,未果。
問題分析
命令行已經提示出來了,是 /usr/bin/firewall-cmd
這個文件出了問題,而這個文件本身我並沒有動過,所以基本可以肯定,是環境的問題。
打開 /usr/bin/firewall-cmd
文件看一眼:
[root@localhost ~]# vim /usr/bin/firewall-cmd
文件內容截取如下:
#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009-2016 Red Hat, Inc.
#
# Authors:
# Thomas Woerner <[email protected]>
# Jiri Popelka <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from gi.repository import GObject
import sys
sys.modules['gobject'] = GObject
...
可以看到第一行寫的執行腳本使用的命令。
靈光一閃。
我把 CentOS7.6
默認自帶的 Python 2.7.5
升級成了 Python 3.7.0
,而上面的 /usr/bin/firewall-cmd
腳本需要由 python2
來執行。
問題分析出來了,那就開始解決。
解決方法
修改 /usr/bin/firewall-cmd
文件的第一行,使用 python2
來執行。
針對我這裏,就是把第一行修改成了下面這樣:
#!/usr/bin/python2 -Es
解決。
拓展
其實解決問題的方法挺簡單的,但囉囉嗦嗦說了一大堆,主要是想引出這部分的擴展。
如果你的服務器上,某些系統級的指令突然沒辦法執行了,八成就是環境變了導致的。
比如:
CentOS7
中升級了 python2
爲 python3
之後,yum
指令就不好用了,需要修改以下幾個文件:
/usr/bin/yum
/usr/bin/yum-config-manager
修改方式和上面一樣,改一下對應文件的第一行。
所以,這篇文章的重點不是解決問題,而是解決問題的思路,以及由這個問題展開的其他類似問題的解決方法。
與大家共勉。