21

Trying to execute the source command using "lfs" user on ubuntu 14.04 and getting this:

root@linux:~/lfs# su lfs - -c "source ~/.bash_profile"
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

Any ideas?

Background info: I am following the LFS book, making a script out of it, so in my script which I execute with sudo, when it gets to this part, after creating the lfs user and his .bashrc and .bashprofile, I guess it is loading it.

Context:

echo "info: create 'lfs' group and user..."
groupadd -f lfs
id -u lfs &>/dev/null || useradd -s /bin/bash -g lfs -m -k /dev/null lfs
passwd -d -q lfs

echo "info: make 'lfs' own the 'tools' and 'sources' directories..."
chown lfs $LFS/tools
chown lfs $LFS/sources

echo "info: creating a clean '.bash_profile' as user 'lfs'..."
su lfs - -c "cat > ~/.bash_profile << \"EOF\"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF"

echo "info: creating a clean '.bashrc' as user 'lfs'..."
su lfs - -c "cat > ~/.bashrc << \"EOF\"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF"

su lfs - -c "source ~/.bash_profile"

Edit:

It has been suggested that my syntax is incorrect, I am not at my linux machine at the moment, so will try to use different syntax when I get the chance. However, for info, I got the syntax from the answer here: https://serverfault.com/questions/411307/cannot-set-terminal-process-group-during-su-to-another-user-as-login-shell

2
  • Do you want su to run bash as if it were a login shell? If so, then try su - lfs -c ... or su --login lfs -c ... or su lfs --login -c .... Commented Nov 21, 2016 at 19:38
  • 1
    @MarkPlotnick That gives the same error.
    – sprocket12
    Commented Nov 21, 2016 at 20:15

2 Answers 2

12
  • su - username runs the login shell of username as an interactive shell.

  • su username command arguments runs command arguments non-interactively under the account username.

You command su lfs - -c "source ~/.bash_profile" means run - -c "source ~/.bash_profile" as the user lfs non-interactively. Now the shell sees the option - and says, I am to run as an interative login shell, and tries to initialize the terminal, but su has disconnected the child process from the controlling terminal.

In short: The - is either misplaced or erroneous.

For a longer discussion, see practically the the same question on serverfault.

3
  • What do you mean by “run … non-interactively”? And by “su has disconnected the child process from the controlling terminal”? Either you're using the wrong terminology (but I can't figure out what you're intended) or you're confused as to what is going on. Commented Nov 21, 2016 at 23:50
  • 1
    Summary: the correct syntax is either su username command arguments, su username, or su - username. The form used by the original poster, su username - command arguments is incorrect.
    – AlexP
    Commented Nov 22, 2016 at 6:51
  • @AlexP Thanks for your reply. Why do you think the previous lines in the script work? They use the same syntax.
    – sprocket12
    Commented Nov 22, 2016 at 9:20
2

I had the same problem when switching user inside the docker container. I fixed it by adding --pty (-P) option that creates a pseudo terminal for the session and avoids TIOCSTI ioctl terminal injection. So the following command should fix the issue:

su lfs -l -P -c ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .