0

I have installed Ubuntu server 16.04(Actually it's 3.4.113-sun8i ) with Openbox. and put this command startx inside /etc/rc.local to have graphics after boot. but the problem is that it automatically logins as root user, and because of this, my C++ program doesn't work correctly(As I asked before, they say it's because the root user can't start some libraries like pulseaudio or gtk automatically). And I think it's right because I should start them manually.

By the way, I am looking for a way that change autologin from root to non-root user. how can I do it?

EDIT: my goal is building a gadget, users only can turn on/off it by physical button(I mean no menu,option,etc). The gadget must execute my C++ code after boot, and code uses gtk and pulseaudio libraries.

user3486308
  • 609
  • 3
  • 17
  • 37
  • what you need is to boot to 'kiosk' mode. have a look at this -> https://thepcspy.com/read/converting-ubuntu-desktop-to-kiosk/ – rajaganesh87 Nov 23 '17 at 14:48

3 Answers3

1

/etc/rc.local is executed as user root. You should thus use su -l jdoe -c startx in rc.local, assuming jdoe is the user you want to start Xorg.

I doubt you have systemd, because systemd, by default, ignores rc.local unless you upgraded from a prior version that did not have systemd and even then, not sure ... I don't run systemd.

If you do have systemd, you need to create a unit file for this. There are many resources online for this.

I searched google for "systemd unit file" and found the following: Put the following in the file: /etc/systemd/system/autologin.service (assuming that is where systemd lives on your system)

[Unit]
Description=Autologin service

[Service]
Type=fork
user=jdoe
group=users
ExecStart=/path/to/your/startx
TimeoutSec=0

[Install]
WantedBy=multi-user.target

You will have to adapt the unit, somewhat .... you probably do not want to start X as jdoe but as another user. Also, users might not be a group the user is a member of, last but not least, /path/to/your/startx is not the path to your startx ... run which startx to find out the path.

Next, you run sudo systemctl daemon-reload to reload the configuration and reboot.

EDIT (following comments from op):

You want this:

[Unit]
Description=Autologin service

[Service]
Type=fork
user=m
group=m
ExecStart=/usr/bin/startx
TimeoutSec=0

[Install]
WantedBy=multi-user.target

Assuming your startx is in /usr/bin/startx, that "should" work.

thecarpy
  • 3,935
  • I have no /etc/x11 folder! My kernel is 3.4.113-sun8i. Also I have etc/xdg with openbox and systemd folders within. – user3486308 Nov 19 '17 at 20:30
  • may you give me some idea about what should I search about exactly? – user3486308 Nov 19 '17 at 20:33
  • Thank you for help! I have a question about user's group. I have only one non-root user named m, when I type groups m the result is m: m. So should I change that line in your code to group=m ? – user3486308 Nov 20 '17 at 07:53
  • Yup, user m is member of group m ... so you want group=m – thecarpy Nov 20 '17 at 07:56
  • Should I change the [Unit] too? change to what? – user3486308 Nov 20 '17 at 08:05
  • Unfortunately it didn't work. Before this, when I put startx inside the /etc/rc.local after boot it was showing openbox window(a gray windows with a mouse pointer and right-click menu). But when I removed it from rc.local and did what you say above, now I have nothing! only a blank black screen. – user3486308 Nov 20 '17 at 10:07
1

You told me in other comment that your system shows this:

$ systemctl get-default
graphical.target

So, change it to multi-user.target:

$ sudo systemctl set-default multi-user.target

And multi-user.target will start at tty1 (Ctrl+Alt+F1):

$ systemctl list-dependencies multi-user.target | grep getty
● ├─getty.target
● │ ├─getty-static.service
● │ └─getty@tty1.service

So, we override tty1 file:

$ sudo systemctl edit getty@tty1

With this content (which m is your username):

[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin m %I $TERM

/sbin/agetty above might different in your system, ensure your check the correct path with:

$ type -a agetty
agetty is /sbin/agetty
agetty is /sbin/agetty

Press Esc, then shift + z + z to save and quit (this keys is for vim, you can run ps a in other terminal tab while edit to know its command is editor /etc/systemd/system/getty@tty1.service.d/.#override.blahblah, in which editor is symlink to vim.gtk3 in my system, your system may vary).

$ type -a editor
editor is /usr/bin/editor
editor is /usr/bin/editor
$ realpath /usr/bin/editor 
/usr/bin/vim.gtk3

Then cat the getty@tty1 and navigate to bottom, you will know it simply append this lines make override effect:

$ sudo systemctl cat getty@tty1 
 ...
# /etc/systemd/system/getty@tty1.service.d/override.conf
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin xiaobai %I $TERM

Auto login done by --autologin above, otherwise you need manually type username and password to login in tty1 after boot.

After loggedin to tty1 with username m, it will execute /home/m/.profile, so add this line at the end of your /home/m/.profile:

echo "$DISPLAY" > /tmp/hole1
echo "$XDG_VTNR" > /tmp/hole2
if [ -z "$DISPLAY" ] && [ -n "$XDG_VTNR" ] && [ "$XDG_VTNR" -eq 1 ]; then
  echo lolo > /tmp/hole3
  exec startx -- -nocursor
fi
echo lili > /tmp/hole4

/tmp/holeN above is for debug purpose only, in order to know it run as desired. e.g. If /tmp/hole3 not created but /tmp/hole2 was created, that's means you need to cat /tmp/hole2 to check the value of "$XDG_VTNR" .

startx will then execute /home/m/.xinitrc, edit it:

$ cat /home/m/.xinitrc                                                                                                                             
#!/usr/bin/env bash                                                                                                                                   
echo 55 > /tmp/test1
exec openbox-session
echo 55 > /tmp/test2

openbox-session will then execute /home/m/.config/openbox/autostart, edit it as usual:

$ cat /home/m/.config/openbox/autostart 
echo 7 > /tmp/yy
/home/m/img &
echo 8 > /tmp/yy2

After all, reboot, will take you to shows your image directly, the instruction above can be summarized as:

multi-user.target -> tty1 -> run getty@tty1.service -> ExecStart auto login with desired username -> logged-in will execute ~/.profile -> exec startx -> startx will execute ~/.xinitrc -> exec openbox-session -> openbox will execute ~/.config/openbox/autostart -> /home/m/img & will pop up.

[UPDATE]

I chat with OP in chat room (transcript) and solved his problem.

rc.local able to startx because of rc.local run startx as root, but ~/.profile is not run startx as root. We do startx > /tmp/my.log 2>&1 to ensure startx run correctly, then /tmp/my.log will shows we need to check /home/m/.local/share/xorg/Xorg.0.log, and eventually I noticed the first (EE) is (EE) open /dev/fb8: Permission denied.

OP need to add user m to video group to obtain permission when startx to access desired FRAMEBUFFER /dev/fb8.

sudo usermod -a -G audio m
sudo usermod -a -G video m

ref1, ref2

p/s: I wonder add to audio group above is necessary. ref

林果皞
  • 5,156
  • 3
  • 33
  • 46
  • I have a question. If I have a program that it needs to specify options to run, ./app -nmic yes how can I change this line inside /.configure/openbox/autostart /home/m/app & – user3486308 Nov 30 '17 at 05:40
  • @user145959 <app_full_path> -nmic yes & should work. – 林果皞 Nov 30 '17 at 06:40
  • I have a program(pocketsphinx) that when I run it with command line, it shows it's results in terminal. but when I compile it and put it's executable inside../openbox/autostart I can not see anything in LCD and it seems program can't start terminal inside the LCD. Hoc can I fix this? – user3486308 Dec 03 '17 at 08:48
  • I mean if I have a console program, how to put it inside autostart file to shows terminal results? – user3486308 Dec 03 '17 at 08:49
  • @user145959 you can start your desired terminal emulator with program parameter which support run specific program on startup. – 林果皞 Dec 03 '17 at 10:48
  • Before I see your answer I created a new question. may you answer there with more detail? https://unix.stackexchange.com/questions/408522/how-to-start-a-console-program-at-startupinside-openbox-autostart – user3486308 Dec 03 '17 at 11:05
  • As my program is in this address /home/m/test I tried gnome-terminal -- /home/m/test but didn't work. I also tried xterm -e /home/m/ttt but it opens a new terminal. It doesn't open my program. (my program prints please num1: after running) – user3486308 Dec 03 '17 at 13:50
0

Start by removing startx from /etc/rc.local, then find out which display manager you have (most likely lightdm, since you're on Ubuntu).

Then read up on systemd services and display manager configuration, configure autologin on the display manager and enable its service.

Mio Rin
  • 3,040