Ubuntu – Access to /dev/ttyUSB0 and sudo

command linepermissionssudousb

This is my first question here.

I am running Ubuntu 12.04 and have an application accessing the USB port of computer. My Ubuntu username is gadu. Until today, i've always used the following command:

sudo ./gadumaster

and entered my password (gadumaster is the application accessing the USB). This command was working, as well as a call to system function reboot() used to restart my laptop when certain external condition from USB occurred.

Today I had to change things so this application runs automatically after laptop starts up. Therefore i prepared a script file, and searched a way how to pass the password to the script. After reading few articles I decided to allow access to USB for my user by adding it to dialout group:

sudo adduser gadu dialout

After rebooting I was able to start my application by simply typing:

./gadumaster

This was excellent, but I needed a way to enable also reboot() function. What was my surprise when i figured out that the following command is not working any more:

sudo ./gadumaster

Yes, executing the application with sudo gives me error "Permission denied" when connecting to USB! Note that without sudo it works!

Tried to remove my user from dialout group:

sudo deluser gadu dialout

After rebooting I got into situation that both with sudo and without sudo commands are not working! Even reboot() function is not working in neither situation.

Could anybody describe what is wrong with my ubuntu?
Thanks much in advance.

/etc/sudoers file:

#
# 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

Directory /etc/sudoers.d contains just one README file.

Error shown is:

OpenComm() failed: Permission denied.

printed via following code snippet – part of gadumaster app (see perror() function):

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;
}

NOTE: The mystery is why sudo ./gadumaster is not working anymore. What could it be that no permission is granted to /dev/ttyUSB0 for superuser?

Best Answer

As suggested by CheddieMerai, using ls -l gadumaster may tell you that you set incorrect file permissions. This is not an issue with the USB connection anymore.

You can resolve this by rebuilding your application or by changing the file permissions via chmod 775 gadumaster (or similar) and/or chown $USER gadumaster (you can replace $USER with the user supposed to "own" the file).