sudo运行kate的方法

有时需要用root运行kate,然而使用下面的命令:

sudo kate 文件名

会得到下面一段话:

Running Kate with sudo can cause bugs and expose you to security vulnerabilities
. Instead use Kate normally and you will be prompted for elevated privileges whe
n saving documents if needed.

也就是建议使用下面的命令运行:

kate 文件名

但是这样做只适用于/etc/fstab这种普通用户可以读取的文件,原因是kate打开时会用普通用户读取文件,保存时再通过polkit提示输入密码(su用的root密码或sudo用的当前用户密码)以root保存文件,然而如果是像/root/.bashrc这种的普通用户没有读取权限的文件,就会报错或者按照空文件打开。

有的教程建议使用下面的命令:

SUDO_EDITOR=kate sudoedit 文件名

然而这会生成临时文件再编辑,这会使得自动语法着色等功能失效,用起来体验也不是很好。

其实很简单,使用下面的命令即可成功以root打开kate:

sudo SUDO_USER= kate 文件名

如果是kdesu的话(kdesu已不推荐使用,因此不在$PATH中),可使用下面的命令:

/usr/lib/x86_64-linux-gnu/libexec/kf5/kdesu SUDO_USER= KDESU_USER= kate 文件名

原理是kate会检查当前UID是不是0(root),是的话会再检查SUDO_USER和KDESU_USER环境变量是否有值,如果有值的话说明是使用sudo或kdesu以root运行的,kate会拒绝运行,因此清除这两个环境变量即可绕过这个限制。

kwrite/dolphin也有类似的限制,同样可以使用这种方法以root运行。

注:

kate/kwrite/dolphin在2017年的17.04开始加入不允许以root运行的限制,但由于当时Kali等发行版会以root运行整个桌面(2020年Kali 2020.1改为默认非root运行桌面),这个改变造成了这些发行版不能正常使用kate/kwrite/dolphin,2018年的18.08改为不允许使用sudo和kdesu运行,但允许以root运行。

17.04、17.08、17.12、18.04这几个版本的kate/kwrite/dolphin使用任何方法均无法以root运行(当前仍在支持的系统中主要影响Ubuntu/Kubuntu 18.04以及其衍生版),除非重新编译或二进制修改,也可以安装并使用其它编辑器,或使用sudoedit等方式解决这个问题。

可使用下面的命令获得kate/kwrite/dolphin版本:

kate -v
kwrite -v
dolphin -v

参考资料:

kate源码(apps/kate/main.cpp或apps/lib/kateapp.cpp)

#if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
    // Prohibit using sudo or kdesu (but allow using the root user directly)
    if (getuid() == 0) {
        setlocale(LC_ALL, "");
        bindtextdomain("kate", KDE_INSTALL_FULL_LOCALEDIR);
        if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
            auto message = kli18n(
                "Running this editor with sudo can cause bugs and expose you to security vulnerabilities. "
                "Instead use this editor normally and you will be prompted for elevated privileges when "
                "saving documents if needed.");
            std::cout << dgettext("kate", message.untranslatedText()) << std::endl;
            exit(EXIT_FAILURE);
        } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
            auto message = kli18n(
                "Running this editor with kdesu can cause bugs and expose you to security vulnerabilities. "
                "Instead use this editor normally and you will be prompted for elevated privileges when "
                "saving documents if needed.");
            std::cout << dgettext("kate", message.untranslatedText()) << std::endl;
            exit(EXIT_FAILURE);
        }
    }
#endif

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值