LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-14-2017, 09:45 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
change prompt when I issue su for root


I have got it to change when I issue su - but not properly and when I issue just su
it only goes to #
and the arrow keys I can only get garbage out when I use them.

Code:
userz@voider [~]\>>$su
Password: 
# ls
Desktop    Downloads  Pictures	Templates  perform
Documents  Music      Public	Videos	   scripts
# ^[[A
using the up arrow to reissue the last command I get that # ^[[A

I am wanting to get a better prompt when going into su with colors (after I get this to work)
something like
Quote:
root@HostName [current directory]>>#
but even when I add this code into .bashrc (prompt code is for test purposes)
Code:
if [[ $( id -u ) -eq 0 ]] ; then
PS1="\[\e[34m\]\u\[\e[m\]@\[\e[34m\]\H\[\e[m\] [\[\e[33m\]\W\[\e[m\]]\\>>$"
else
PS1='[\u@\h \W]\<>$ '
fi
I get this
Code:
[userz@voider ~]\<>$ su
Password: 
# exit
[userz@voider ~]\<>$ su -
Password: 
-sh: 16: [[: not found
\[\e[1;32m\]\u [ \[\e[0m\]\w\[\e[1;32m\] ]$ \[\e[0m\]
How do I get this to work on a system wide platform?
I already know I have to use /etc/bashrc just what is it that needs to be put inside of these files?

my system has bashrc in /etc/bash/bashrc btw
 
Old 01-14-2017, 10:39 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 5,561
Blog Entries: 16

Rep: Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301Reputation: 3301
Quote:
Originally Posted by Beyond Linux From Scratch
# Setup a red prompt for root and a green one for users.
NORMAL="\[\e[0m\]"
RED="\[\e[1;31m\]"
GREEN="\[\e[1;32m\]"
if [[ $EUID == 0 ]] ; then
PS1="$RED\u [ $NORMAL\w$RED ]# $NORMAL"
else
PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL"
fi
They recommend doing this in /etc/profile. The root user often has history turned off for security reasons, which may be why your arrow keys don't work.
 
Old 01-14-2017, 10:55 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Before you edit...
Your bash_history arrow keys don't work because
Code:
su
gets you get root without root's environment, were
Code:
su -
you gets you root with root's environment.

Code:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
in both user and root .bashrc files. Source 'em or logout and back in.
Gets you a red prompt for root, but you have to use
Code:
su -
to invoke it.

Last edited by Habitual; 01-14-2017 at 10:59 AM.
 
1 members found this post helpful.
Old 01-14-2017, 11:54 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by hazel View Post
They recommend doing this in /etc/profile. The root user often has history turned off for security reasons, which may be why your arrow keys don't work.
haha actaully tried that one and it didn't work -- lead me to having to post a how to in here. thanks!
 
Old 01-14-2017, 11:54 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Habitual View Post
Before you edit...
Your bash_history arrow keys don't work because
Code:
su
gets you get root without root's environment, were
Code:
su -
you gets you root with root's environment.

Code:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
in both user and root .bashrc files. Source 'em or logout and back in.
Gets you a red prompt for root, but you have to use
Code:
su -
to invoke it.
seen that one on the net, didn't understand it. plus it is saying debain when I am NOT running Debain .. soo it too may not work
 
Old 01-14-2017, 12:46 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
Try using single quotes instead of double, like you did for your user prompt.
 
Old 01-14-2017, 12:58 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by grail View Post
Try using single quotes instead of double, like you did for your user prompt.
OK --
 
Old 01-14-2017, 01:12 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
OK this is in order. System bashrc, root .bashrc, userx (me) .bashrc

Code:
[userx@voider.org ~]$ sudo cat /etc/bash/bashrc
# /etc/bash/bashrc

# Do not edit this file.
# Place your readable configs in /etc/bash/bashrc.d/*.sh 

if [[ $- != *i* ]] ; then
	# Shell is non-interactive.  Be done now!
	return
fi

if [ -d /etc/bash/bashrc.d/ ]; then
	for f in /etc/bash/bashrc.d/*.sh; do
		[ -r "$f" ] && . "$f"
	done
	unset f
fi

if [ -f /usr/bin/neofetch ]; then neofetch; fi

# Setup a red prompt for root and a green one for users.
NORMAL='\[\e[0m\]'
RED='\[\e[1;31m\]'
GREEN='\[\e[1;32m\]'
if [[ $EUID == 0 ]] ; then
PS1='$RED\u [ $NORMAL\w$RED ]# $NORMAL'
else
PS1='$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL'
fi
Code:
[userx@voider.org ~]$ sudo cat /root/.bashrc
# .bashrc

# If not running interactively, don't do anything
[[ $- != *i* ]] && return


alias ls='ls --color=auto'
PS1='[\u@\H \W]\$ '
Code:
[userx@voider.org ~]$ cat .bashrc
# .bashrc

# If not running interactively, don't do anything
[[ $- != *i* ]] && return


alias ls='ls --color=auto'
PS1='[\u@\H \W]\$ '
that bold line works as a system wide command, but that change prompt does not.

this isn't helping
Code:
[userx@voider.org ~]$ echo $EUID
1000
[userx@voider.org ~]$ su
Password: 
# echo $EUID

# su -
# echo $EUID

# echo $UID

# exit
# echo $UID

#
exit to get me back from su - to just su to echo $UID. both are coming up blank - null - empty

now when I change that test in /etc/bash/bashrc to this
Code:
# Setup a red prompt for root and a green one for users.
NORMAL='\[\e[0m\]'
RED='\[\e[1;31m\]'
GREEN='\[\e[1;32m\]'

#if [[ $EUID == 0 ]] ; then


if [[ $(id -u) -eq 0 ]] ; then
PS1='$RED\u [ $NORMAL\w$RED ]# $NORMAL'
else
PS1='$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL'
fi
then run this
Code:
[userx@voider.org ~]$ su
Password: 
# id
uid=0(root) gid=0(root) groups=0(root)
# id -u
0
# su -
# id -u
0
#
still nothing. all I am getting is this prompt.
Code:
[userx@voider.org ~]$ su
Password: 
#

Last edited by BW-userx; 01-14-2017 at 01:21 PM.
 
Old 01-14-2017, 01:34 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
hmmm ... seems I was not clear As per your original post, without the addition of variables, use single quotes:
Code:
if [[ $( id -u ) -eq 0 ]] ; then
PS1="\[\e[34m\]\u\[\e[m\]@\[\e[34m\]\H\[\e[m\] [\[\e[33m\]\W\[\e[m\]]\\>>$"
else
PS1='[\u@\h \W]\<>$ '
fi

# becomes

if [[ $( id -u ) -eq 0 ]] ; then
PS1='\[\e[34m\]\u\[\e[m\]@\[\e[34m\]\H\[\e[m\] [\[\e[33m\]\W\[\e[m\]]\\>>$ '
else
PS1='[\u@\h \W]\<>$ '
fi
When using double quotes you need to be aware of how many escapes you need in certain places or you get unusual responses.

This will not affect simple su, as was pointed out earlier you are not entering root's environment fully so this file you have changed will not be sourced.
 
Old 01-14-2017, 02:56 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by grail View Post
hmmm ... seems I was not clear As per your original post, without the addition of variables, use single quotes:
Code:
if [[ $( id -u ) -eq 0 ]] ; then
PS1="\[\e[34m\]\u\[\e[m\]@\[\e[34m\]\H\[\e[m\] [\[\e[33m\]\W\[\e[m\]]\\>>$"
else
PS1='[\u@\h \W]\<>$ '
fi

# becomes

if [[ $( id -u ) -eq 0 ]] ; then
PS1='\[\e[34m\]\u\[\e[m\]@\[\e[34m\]\H\[\e[m\] [\[\e[33m\]\W\[\e[m\]]\\>>$ '
else
PS1='[\u@\h \W]\<>$ '
fi
When using double quotes you need to be aware of how many escapes you need in certain places or you get unusual responses.

This will not affect simple su, as was pointed out earlier you are not entering root's environment fully so this file you have changed will not be sourced.
sorry - I wiped my system and re-installed and well I figured I'd go from that linux from scratch in the system bashrc which is where this code should go to get system wide usage so I do not have to deal with my user .bashrc in this matter, yes?

if you look cose enough you'd see I replaced that " " with ' ' within the if else it is just the prompt code that is different, still regardless if I use
Code:
if [[ $( id -u ) -eq 0 ]] ; then or 
if [[ $EUID == 0 ]] ; then
it does not work.

look see
Code:
if [[ $(id -u) -eq 0 ]] ; then
PS1='$RED\u [ $NORMAL\w$RED ]# $NORMAL'
else
PS1='$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL'
fi
single quotes are being used. still no go.

look see


Code:
  GNU nano 2.7.4                                                                          File: /etc/bash/bashrc                                                                                     

# /etc/bash/bashrc

# Do not edit this file.
# Place your readable configs in /etc/bash/bashrc.d/*.sh

if [[ $- != *i* ]] ; then
        # Shell is non-interactive.  Be done now!
        return
fi

if [ -d /etc/bash/bashrc.d/ ]; then
        for f in /etc/bash/bashrc.d/*.sh; do
                [ -r "$f" ] && . "$f"
        done
        unset f
fi

if [ -f /usr/bin/neofetch ]; then neofetch; fi


if [[ $( id -u ) -eq 0 ]] ; then
PS1='\[\e[34m\]\u\[\e[m\]@\[\e[34m\]\H\[\e[m\]
[\[\e[33m\]\W\[\e[m\]]\\>>$ '
else
PS1='[\u@\h \W]\<>$ '
fi
I still get this
Code:
[userx@voider.org ~]$ su
Password: 
# su -
#

I think it is that the terminal starts out user, when it is still running and gets changed to root by su it does not look into that /etc/bash/basrc file again because it is already running(?) so it does not get sourced again?

Last edited by BW-userx; 01-14-2017 at 03:15 PM.
 
Old 01-14-2017, 03:32 PM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
seen that one on the net, didn't understand it. plus it is saying debain when I am NOT running Debain .. soo it too may not work
You have to understand it to try it?
I ran that on Slackware.
I've had it for 8 years.
 
Old 01-14-2017, 03:43 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Habitual View Post
You have to understand it to try it?
I ran that on Slackware.
I've had it for 8 years.
damn ... figures now that I am looking for that one post on this Debian_Chroot ... I cannot find it. SO i just stuck that in my if else in system bashrc it did nothing, so I stuck it in my ~/.bashrc and still I get nothing when changing from user to su (root) just the pound sign #
 
Old 01-14-2017, 06:41 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Code:
[userx@voider.org]:
[~]>>$  ps -p $$

Thank you
PID TTY TIME CMD
19323 pts/2 00:00:00 bash
[userx@voider.org]:
[~]>>$ su
Password:
# ps -p $$
PID TTY TIME CMD
15778 pts/2 00:00:00 sh
# su -
# ps -p $$
PID TTY TIME CMD
15947 pts/2 00:00:00 sh
# chsh -s /bin/bash
Changing shell for root.
Shell changed.
#

Now it is working.. just need to tweek the prompt

[root@voider.org userx]2
Now I got it.. failed to check shell for root ... always is something simple over looked item.
 
  


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
Web Root Dir Change and New Dir Added issue in CentOS 5 Ginnyz Linux - Server 2 06-01-2009 08:29 PM
How to change resolution from the root prompt? banjzooie Linux - Software 1 02-26-2006 06:01 PM
Change prompt on FC1 (not motd or /etc/issue) ultreen144 Linux - Newbie 1 06-02-2004 10:56 AM
how to change my prompt ? lzg8056 Linux - Newbie 14 11-30-2003 09:00 PM
how to change prompt ashesh Linux - General 5 04-16-2003 06:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:26 AM.

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