Page 1 of 1

[SOLVED] How to change $PATH with saving a new $PATH after reboot

Posted: 03 Sep 2017, 15:31
by Blaze
Anybody knows how to change $PATH with saving a new $PATH after reboot?

I need to change $PATH for root user (for this app), like this:

Code: Select all

root@porteus:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
root@porteus:~# export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
root@porteus:~# echo $PATH
/opt/cprocsp/bin/amd64:/opt/cprocsp/sbin/amd64:/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
root@porteus:~#
but after reboot or after closing the terminal window my $PATH is set by default

Code: Select all

root@porteus:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
root@porteus:~#

How to change $PATH with saving a new $PATH after reboot

Posted: 03 Sep 2017, 17:19
by fanthom
You need to edit /etc/profile and save somewhere (rootcopy, module, changes, etc):

Code: Select all

# Set the default system $PATH:
PATH="/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/porteus-scripts"

# For root users, ensure that /usr/local/sbin, /usr/sbin, and /sbin are in
# the $PATH.  Some means of connection don't add these by default (sshd comes
# to mind).
if [ "`id -u`" = "0" ]; then
  echo $PATH | grep /usr/local/sbin 1> /dev/null 2> /dev/null
  if [ ! $? = 0 ]; then
    PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH
  fi
fi

How to change $PATH with saving a new $PATH after reboot

Posted: 04 Sep 2017, 16:52
by Blaze
Hi fanthom.
I added this code:

Code: Select all

if [ "$USER" != "root" ]; then
  export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
fi
to /etc/profile after your code. After reboot it changed $PATH for guest, but I need to do this only for root user

Code: Select all

guest@porteus:~$ echo $PATH
/opt/cprocsp/bin/amd64:/opt/cprocsp/sbin/amd64:/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/porteus-scripts
guest@porteus:~$ su
root@porteus:/home/guest# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
root@porteus:/home/guest#
any suggestion are welcome. Thanks.

How to change $PATH with saving a new $PATH after reboot

Posted: 04 Sep 2017, 17:07
by Ed_P
Blaze wrote:
04 Sep 2017, 16:52
I added this code:

Code: Select all

if [ "$USER" != "root" ]; then
  export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
fi
Try

Code: Select all

if [ "$USER" == "root" ]; then
  export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
fi

How to change $PATH with saving a new $PATH after reboot

Posted: 04 Sep 2017, 18:36
by Blaze
Hi Ed_P.
No result for both guest and root users

Code: Select all

guest@porteus:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/porteus-scripts
guest@porteus:~$ su
Пароль: 
root@porteus:/home/guest# cd
root@porteus:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
root@porteus:~# 

How to change $PATH with saving a new $PATH after reboot

Posted: 04 Sep 2017, 19:08
by Ed_P
:oops:

Ok this is what I normally use to check for a non-root user: (from Re: Script help (Post by brokenman #20035))

Code: Select all

if [ `whoami` != "root" ]; then
So I think you should try:

Code: Select all

if [ `whoami` == "root" ]; then
This may work also:

Code: Select all

if [ "$UID" -eq 0 ]; then
but I haven't tested.

Before your "if" put these in your script:

Code: Select all

echo USER=$USER
echo whoami=`whoami`
echo UID=$UID
read
Press Enter after reviewing the display.


-update-

FYI

Code: Select all

guest@porteus:~$ echo $USER
guest
guest@porteus:~$ echo $UID
1000
guest@porteus:~$ echo `whoami`
guest
guest@porteus:~$ su
Password: 
root@porteus:/home/guest# echo $USER
root
root@porteus:/home/guest# echo $UID
0
root@porteus:/home/guest# echo `whoami`
root
root@porteus:/home/guest# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/lib64/java/bin:/usr/lib64/qt/bin
root@porteus:/home/guest# exit
exit
guest@porteus:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/porteus-scripts:/usr/lib64/java/bin:/usr/lib64/qt/bin:/usr/lib64/java/bin:/usr/lib64/qt/bin:/usr/lib64/java/bin:/usr/lib64/qt/bin
guest@porteus:~$ 

How to change $PATH with saving a new $PATH after reboot

Posted: 05 Sep 2017, 17:56
by Blaze
With these lines

Code: Select all

if [ `whoami` != "root" ]; then
  export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
fi

if [ `whoami` == "root" ]; then
  export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
fi

if [ "$UID" -eq 0 ]; then
  export PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH"
fi

Code: Select all

guest@porteus:~$ echo $PATH
/opt/cprocsp/bin/amd64:/opt/cprocsp/sbin/amd64:/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/porteus-scripts
guest@porteus:~$ su
Пароль: 
root@porteus:/home/guest# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
root@porteus:/home/guest# 
Ed_P wrote:
04 Sep 2017, 19:08
Before your "if" put these in your script:

echo USER=$USER
echo whoami=`whoami`
echo UID=$UID
read

Press Enter after reviewing the display.
This is freezes boot up and Xorg does not started.
I logged as root and via vi removed these lines.
Now with Xorg all ok.

-update-

I found it in /etc/login.defs

Code: Select all

#
# REQUIRED  The default PATH settings, for superuser and normal users.
#
# (they are minimal, add the rest in the shell startup files)
ENV_SUPATH     PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
ENV_PATH       PATH=/usr/local/bin:/bin:/usr/bin
NOTE.
for root user need to tweak /etc/login.defs

Code: Select all

ENV_SUPATH     PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
for guest user need to tweak /etc/profile

Code: Select all

# Set the default system $PATH:
PATH="/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/porteus-scripts"
A new issue. How I can add output of this variable $(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')

Code: Select all

root@porteus:~# echo $(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')
/opt/cprocsp/bin/amd64:/opt/cprocsp/sbin/amd64:
root@porteus:~#
to ENV_SUPATH PATH=HERE/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin

How to change $PATH with saving a new $PATH after reboot

Posted: 05 Sep 2017, 22:52
by Ed_P
I would think this would be worth a shot.

Code: Select all

sudo echo "ENV_SUPATH PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH >> /etc/login.defs
Or not.

Code: Select all

guest@porteus:~$ sudo echo "ENV_SUPATH PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH >> /etc/login.defs
/bin/ls: cannot access '/opt/cprocsp/sbin/*': No such file or directory
/bin/ls: cannot access '/opt/cprocsp/bin/*': No such file or directory
bash: /etc/login.defs: Permission denied
guest@porteus:~$ sudo echo "ENV_SUPATH PATH="`/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':'`$PATH >> /etc/login.defs
/bin/ls: cannot access '/opt/cprocsp/sbin/*': No such file or directory
/bin/ls: cannot access '/opt/cprocsp/bin/*': No such file or directory
bash: /etc/login.defs: Permission denied
guest@porteus:~$ 

Code: Select all

guest@porteus:~$ su
Password:
root@porteus:/home/guest# echo "ENV_SUPATH PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')$PATH >> /etc/login.defs
/bin/ls: cannot access '/opt/cprocsp/sbin/*': No such file or directory
/bin/ls: cannot access '/opt/cprocsp/bin/*': No such file or directory
root@porteus:/home/guest# 
File got updated

Code: Select all

ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
but not with your change. :wall:

How to change $PATH with saving a new $PATH after reboot

Posted: 06 Sep 2017, 17:41
by Blaze
Ed_P, I love sed :magic:

Code: Select all

su
toor
sed -i "s|ENV_SUPATH     PATH=|ENV_SUPATH     PATH="$(/bin/ls -d /opt/cprocsp/{s,}bin/*|tr '\n' ':')"|g" /etc/login.defs
Note. About

Code: Select all

/bin/ls: cannot access '/opt/cprocsp/sbin/*': No such file or directory
/bin/ls: cannot access '/opt/cprocsp/bin/*': No such file or directory
you are need to activate this kc1 app

[SOLVED] How to change $PATH with saving a new $PATH after reboot

Posted: 07 Sep 2017, 00:15
by Ed_P
I'm glad "we" were able to SOLVE it. :happy62:

I've heard of sed but have never used it myself.