访问 /dev/ttyUSB0 和 sudo

nik*_*kal 8 permissions command-line sudo usb

这是我在这里的第一个问题。

我正在运行 Ubuntu 12.04 并且有一个应用程序访问计算机的 USB 端口。我的 Ubuntu 用户名是gadu。直到今天,我一直使用以下命令:

sudo ./gadumaster
Run Code Online (Sandbox Code Playgroud)

并输入我的密码(gadumaster 是访问 USB 的应用程序)。此命令正在工作,并且调用系统函数reboot()用于在 USB 发生某些外部条件时重新启动我的笔记本电脑。

今天我不得不改变一些东西,所以这个应用程序在笔记本电脑启动后自动运行。因此我准备了一个脚本文件,并搜索了如何将密码传递给脚本的方法。在阅读了几篇文章后,我决定通过将它添加到拨出组来允许我的用户访问 USB :

sudo adduser gadu 拨出

重新启动后,我只需键入以下内容即可启动我的应用程序:

./gadumaster

这很好,但我需要一种方法来启用reboot()功能。当我发现以下命令不再起作用时,我感到惊讶的是什么:

须藤./gadumaster

是的,在连接到 USB 时,使用sudo执行应用程序会给我错误“权限被拒绝”!请注意,没有 sudo 它可以工作!

试图从拨出组中删除我的用户:

sudo deluser gadu 拨号

重新启动后,我遇到了使用sudo和不使用sudo命令都不起作用的情况!即使是reboot()函数在这两种情况下都不起作用。

有人可以描述我的 ubuntu 有什么问题吗?非常感谢。

/etc/sudoers文件:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
Run Code Online (Sandbox Code Playgroud)

目录 /etc/sudoers.d 只包含一个 README 文件。

显示的错误是:

OpenComm() 失败:权限被拒绝。

通过以下代码片段打印 - gadumaster 应用程序的一部分(请参阅 perror() 函数):

bool SerialComm::OpenComm(const char* pszCommport, int nBaudRate, eParity Parity, eStopbits Stopbits)
{
// pszCcommport: /dev/ttyUSB0
m_fdSerial = open(pszCommport, O_RDWR | O_NOCTTY | O_NDELAY);

if(m_fdSerial < 1)
{
    m_fdSerial = 0;
    perror("OpenComm() failed: ");
    return false;
}

fcntl(m_fdSerial, F_SETFL, 0);

if(nBaudRate == 9600)
    nBaudRate = B9600;
else if(nBaudRate == 19200)
    nBaudRate = B19200;
else if(nBaudRate == 38400)
    nBaudRate = B38400;
else
{
    // OpenComm(): Unsupported baudrate!
    return false;
}

// setting baud rates and stuff
struct termios options;
tcgetattr(m_fdSerial, &options);
m_OriginalOptions = options;

cfsetispeed(&options, nBaudRate);
cfsetospeed(&options, nBaudRate);
options.c_cflag |= (CLOCAL | CREAD);

// next 4 lines setting 8N2
options.c_cflag &= ~PARENB;
options.c_cflag |= CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

// raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

// disable software flow control; disable NL->CR conversion; enable marking of Frame/Parity errors
options.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL | PARMRK);

options.c_oflag &= ~(OPOST | ONLCR);

tcsetattr(m_fdSerial, TCSANOW, &options);
tcsetattr(m_fdSerial, TCSAFLUSH, &options);

//required to make flush work, for some reason
sleep(2);
tcflush(m_fdSerial, TCIOFLUSH);

return true;
}
Run Code Online (Sandbox Code Playgroud)

注意:神秘的是为什么sudo ./gadumaster不再工作了。超级用户没有授予 /dev/ttyUSB0 权限是什么原因?

Apo*_*oLV 2

正如CheddieMerai所建议的,使用ls -l gadumaster可能会告诉您设置了不正确的文件权限。这不再是 USB 连接的问题。

您可以通过重建应用程序或通过chmod 775 gadumaster(或类似)和/或chown $USER gadumaster(您可以将 $USER 替换为应该“拥有”该文件的用户)更改文件权限来解决此问题。