LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch
User Name
Password
Linux From Scratch This Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.

Notices


Reply
  Search this Thread
Old 03-24-2016, 06:30 PM   #1
SoftAllan
LQ Newbie
 
Registered: Jan 2011
Location: Denmark
Distribution: Linux Mint
Posts: 6

Rep: Reputation: 0
Setting LFS environment variable at login


As recommended in chapter 2.4. Setting The $LFS Variable I am trying to set the LFS environment variable at login. But it is a bit difficult for me to do.

What I have tried so far is to add the line
Code:
export LFS=/mnt/lfs
to the /root/.bash_profile and to the lfs user in /home/lfs/.bash_profile. But that did not work.

I have then studied the /etc/profile and found that it runs each script (.sh) file in a folder called /etc/profile.d/. So I decided to make my own lfsenv.sh script file in this folder with the following lines.

Code:
# LFS environment default værdi.
# Bruges til Linux From Scratch.

echo 'Kører LFS'
export LFS=/mnt/lfs
echo $LFS
Then I logged out out and logged ind again (with the console mode from Ctrl+Alt+F2).

What I can see is that the lines are executed since the echo lines does get printed when I log in with the lfs user. And the second echo line prints "/mnt/lfs" just fine. But when I try to print the LFS variable by entering
Code:
lfs@debian:~$ echo $LFS
in the command promt I only get a blank line in return. So the LFS environment variable has not survived the login. It must be out of scope somehow.

Could someone please help me find out why this variable does not stay in scope of the login session?

Host system is a Debian 8.3 VMWare machine.
 
Old 03-24-2016, 09:07 PM   #2
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Rep: Reputation: 185Reputation: 185
Why not open a terminal, run that export command, and continue on? That's what the book means for you to do.

When you get to the steps in Chapter 4 to create the lfs user, the LFS variable gets established by the lfs user's .bashrc file from that point on.
 
Old 03-25-2016, 06:13 AM   #3
SoftAllan
LQ Newbie
 
Registered: Jan 2011
Location: Denmark
Distribution: Linux Mint
Posts: 6

Original Poster
Rep: Reputation: 0
The whole idea of LFS for me is to learn the inner workings of a Linux machine. And the book suggest that I should add the LFS env variable at this point. The chapter 4 I have not gotten to yet but thanks for giving me the heads up on that.

Of course I could issue the command at the time spend next to nothing when logged in. And yes I have probably spend more time trying to get this right in the auto login process than doing this manually would cost for the next couple of years but that kind of defeat the purpose of learning the stuff.

This could also be a case others wanted a help with and that is why I have started this thread.

Is it the wrong spot to try to make an environment variable using my own script? I thought this would be the right way instead of altering the /evt/profile script.

On a second note. The book state that
Quote:
...In addition,
the shell specified in the /etc/passwd file for all users that need the LFS variable needs to be bash to
ensure that the /root/.bash_profile file is incorporated as a part of the login process.
As I understand it the /root/.bash_profile script file is executed for all users when they log in. Can that be right? Does a normal user have access rights for the /root/.bash_profile as part of the login?
 
Old 03-25-2016, 06:43 AM   #4
SoftAllan
LQ Newbie
 
Registered: Jan 2011
Location: Denmark
Distribution: Linux Mint
Posts: 6

Original Poster
Rep: Reputation: 0
It works and then it don't.

It turns out that the lfsenv.sh script is running as it should for other users than the lfs user after a reboot. If logged in as root or another user the $LFS environment variable is still present when examined by the command prompt
Code:
echo $LFS
It is only the lfs user that it is not working for. Now the book has instructed me to make a special line in the /lfs/.bash_profile script

Code:
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
and that might be the line that is causing me trouble. Commenting the line out like this

Code:
#exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
verifies that the LFS environment variable survives the login so something is causing the login to alter with this line.

I have tried to enter the export command for the variable after the evt command:
Code:
echo 'Running new env'
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
echo 'Ready to create $LFS'
export LFS=/mnt/lfs
echo $LFS
Now when the lfs user login the line "Running new env" is shown and then nothing more shows. The command "exec env..." is stopping the script right after it is executed and preventing the rest of the script to run. I think that is also why the lfsenv.sh script is not executed either.

Is the book wrong here in suggesting setting the environment variable up or am I still doing something wrong here?

Last edited by SoftAllan; 03-25-2016 at 06:46 AM.
 
Old 03-25-2016, 07:04 AM   #5
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 6,771
Blog Entries: 17

Rep: Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051
Quote:
Originally Posted by SoftAllan View Post
As I understand it the /root/.bash_profile script file is executed for all users when they log in. Can that be right? Does a normal user have access rights for the /root/.bash_profile as part of the login?
The book is a trifle obscure there. What it is trying to say is that the LFS variable has to be set for root as well as for the lfs user. The simplest way to ensure this is to put it into root's .bash_profile file. But as this file is only read when the shell is bash, people with unusual shells must also edit their /etc/passwd file to ensure that root uses only bash for the duration of the build. As you have rightly guessed, normal users do not have access to any of root's files.

Setting up the LFS user correctly is absolutely core to getting the build off to a good start. If you get it wrong here, things can collapse further down the line. I appreciate your desire to learn by experiment but this is not the right place for it, especially in your first LFS build. I suggest that you follow the book slavishly and set up a separate project to learn about scripts and environmental variables.

Last edited by hazel; 03-25-2016 at 07:11 AM.
 
Old 03-25-2016, 07:56 AM   #6
SoftAllan
LQ Newbie
 
Registered: Jan 2011
Location: Denmark
Distribution: Linux Mint
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by hazel View Post
I suggest that you follow the book slavishly and set up a separate project to learn about scripts and environmental variables.
Well I might be fighting a loosing battle here but it was note in the book that suggested that I should set the LFS environment variable during login. But that seems to be a bad advice since the lfs user cannot have it set on login due to the altered and simplified bash login.

Thanks for clearing up the part with the root .bash_profile.

On to the project without an automated LFS environment variable setting it is
 
Old 03-25-2016, 08:59 AM   #7
SoftAllan
LQ Newbie
 
Registered: Jan 2011
Location: Denmark
Distribution: Linux Mint
Posts: 6

Original Poster
Rep: Reputation: 0
I definitely jumped the gun on this one. What I have been doing is remembering the note in chapter 2 about the LFS environment variable and while I was going through chapter 4.4 I made a mistake. I jumped out of the follow-the-book and started adding the LFS environment variable on my own. But I should have paid special attention to what the book says about the new user:
Quote:
When logged on as user lfs, the initial shell is usually a login shell which reads the /etc/profile of the host (probably containing some settings and environment variables) and then .bash_profile. The exec env -i.../bin/bash command in the .bash_profile file replaces the running shell with a new one with a completely empty environment, except for the HOME, TERM, and PS1 variables. This ensures that no unwanted and potentially hazardous environment variables from the host system leak into the build environment. The technique used here achieves the goal of ensuring a clean environment.
And just as it was meant to stop the bash scripts from running I was driving strait into the wall by trying to execute the automatic LFS environment variable with /etc/profile... go figure. And if I had bothered to read just a few lines further I can see that they in the very next step are setting up the LFS environment variable for the lfs user in the .bashrc script. Just as stoat was telling me.

Lession learned!

Last edited by SoftAllan; 03-25-2016 at 09:01 AM.
 
Old 03-25-2016, 09:44 AM   #8
stoat
Member
 
Registered: May 2007
Distribution: LFS
Posts: 628

Rep: Reputation: 185Reputation: 185
Very well. But just so you'll know, the issue will return at the end of Chapter 5 when you stop working as the lfs user and begin working as root in Chapter 6 and beyond. But it's again a very short-lived issue. The LFS variable is important only for the first few commands in Chapter 6 after which you enter the chroot environment and the LFS variable becomes irrelevant until the end of the book when you logout and unmount the filesystems.

I only mention this now so you will know it's coming later on. When you get to Chapter 6, I recommend that you simply become root in a terminal, run the export command again, double-check the LFS variable, and begin working. Within a very few steps in Chapter 6, you will enter chroot and all this LFS variable stuff is over until the final steps in the book.

Last edited by stoat; 03-25-2016 at 02:09 PM.
 
Old 01-18-2023, 03:15 AM   #9
Petols
LQ Newbie
 
Registered: Jan 2023
Location: Sweden
Posts: 2

Rep: Reputation: 0
I have the same question but can't figure it out why the $LFS variable is empty when I'm logged into the lfs account.

Is it because I use a virtual terminal in Gnome?
 
Old 01-18-2023, 03:45 AM   #10
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 6,771
Blog Entries: 17

Rep: Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051
That could be the case. You need to have the variable set in the LFS user's login scripts. The Book recommends using .bash_profile for this, as it's the file generally used for setting environmental variables. But graphical terminals may not run this script on starting so you might need to put the instructions in .bashrc. See the note at the end of https://www.linuxfromscratch.org/lfs.../aboutlfs.html for details.
 
Old 01-18-2023, 04:50 AM   #11
Petols
LQ Newbie
 
Registered: Jan 2023
Location: Sweden
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by hazel View Post
That could be the case. You need to have the variable set in the LFS user's login scripts. The Book recommends using .bash_profile for this, as it's the file generally used for setting environmental variables. But graphical terminals may not run this script on starting so you might need to put the instructions in .bashrc. See the note at the end of https://www.linuxfromscratch.org/lfs.../aboutlfs.html for details.
Ok, I think I understand. The book says I should use a .bashrc file, but does that render the .bash_profile file superfluous?

In chapter 4.4 (Setting Up the Environment) you create a .bashrc file that seems to declare the $LFS variable, so I don't understand why it was empty when logged into the lfs user?

I used Debian 11 for the build process, but maybe there are distros that are better suited for LFS?

Last edited by Petols; 01-18-2023 at 04:54 AM.
 
Old 01-18-2023, 04:57 AM   #12
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 6,771
Blog Entries: 17

Rep: Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051Reputation: 4051
Slackware or an existing LFS are probably the best, but lots of people have done it using Debian. There are two things you need to look out for specially: Debian uses dash as its system shell (the one used by default in build scripts) and mawk as its awk. That will not work. You need to install gawk to replace mawk and make bash your system shell for the duration of the build. I used to do that by temporarily linking /bin/sh to /bin/bash but there's probably a more elegant way to do it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting an environment variable... netpumber Linux - General 15 11-12-2010 07:52 AM
setting environment variable madani Linux - Software 5 03-06-2009 12:15 PM
environment variable setting chii-chan Linux - General 5 01-23-2009 07:29 PM
setting environment variable kb_ganesh Ubuntu 3 09-10-2005 01:58 PM
Setting an environment variable kharris Linux - General 3 10-02-2003 04:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration