LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 03-26-2015, 10:03 PM   #1
vivanguarda
Member
 
Registered: Sep 2008
Location: RJ-Brazil
Distribution: Slackware
Posts: 181

Rep: Reputation: 5
Bashrc and PS1 Creation


Good Evening, slackers!!!


I would like to create an identification for root and intend to use PS1 color customisation { PS1="\[\e[41;30m\][\h|\w ]\[\e[0m\]} . But, it's important to get a contrast during ssh sessions on Cisco routers and remote server machines.

We know that ssh forces Login and Interative Shell Invocation. Then where have I create my PS1 set in a /etc/.bashrc, /etc/.bash_profile or another directory and see a different color prompt during "su" root login?
 
Old 03-26-2015, 10:53 PM   #2
qweasd
Member
 
Registered: May 2010
Posts: 621

Rep: Reputation: Disabled
When going from one Slackware 14.1 box to the other via ssh, you get login bash shell, and so you want the stanza in .bash_profile on the remote host. Ditto for su - on a localhost. But when using su (no -) on a localhost, you get the interactive bash shell, and you want the stanza in .bashrc.

I actually use color coding, and I prefer it in .bash_profile only. This enables colors for ssh and su -, but not su. Since I don't use su for anything, this is just what I need.

Last edited by qweasd; 03-26-2015 at 10:57 PM.
 
Old 03-27-2015, 02:39 AM   #3
zakame
Member
 
Registered: Apr 2012
Location: Philippines
Distribution: Debian, Ubuntu, Slackware
Posts: 295

Rep: Reputation: 181Reputation: 181
Might be of interest: https://github.com/nojhan/liquidprompt
 
1 members found this post helpful.
Old 03-27-2015, 07:18 AM   #4
phi11ip
Member
 
Registered: Jul 2007
Location: Nottingham, UK
Distribution: Slackware64-current
Posts: 93

Rep: Reputation: 23
I have this in my root and all other users .bashrc on the remote machines :-
Code:
if ps ax | grep sshd: | grep -v grep &>/dev/null; then      #test if this is a remote login
    PS1='\[\033[1;34m\][\u\[\033[1;33m\]@\h\[\033[1;34m\]:\w]\$ \[\033[0m\]'
else
    PS1='\[\033[1;34m\][\u:\w]\$ \[\033[0m\]'
fi
export PS1
Works on my little home network. Not too sure if this would work in your situation though.
 
Old 03-27-2015, 08:31 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,622

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by vivanguarda View Post
Good Evening, slackers!!!
I would like to create an identification for root and intend to use PS1 color customisation { PS1="\[\e[41;30m\][\h|\w ]\[\e[0m\]} . But, it's important to get a contrast during ssh sessions on Cisco routers and remote server machines.

We know that ssh forces Login and Interative Shell Invocation. Then where have I create my PS1 set in a /etc/.bashrc, /etc/.bash_profile or another directory and see a different color prompt during "su" root login?
This isn't just for Slackware, since PS1 and prompts are pretty universal. I settled on one a few years ago, which I like:
Code:
PS1="\[\033[1;36m\][\u@\[\033[1;37m\]\h:\[\033[1;33m\]\w]\\$\[\033[0m\] "
SUDO_PS1="\[\033[1;31m\][\u@\[\033[1;37m\]\h:\[\033[1;33m\]\w]\\$\[\033[0m\] "
This way, a 'normal' user will be in cyan, the hostname will be white, and the current directory is in yellow. When you run "sudo -s", you will get the root user ID in red...a good indication that you're doing something as root.

Shove those in .bashrc and enjoy. You can also just use the SUDO_PS1 as a standard PS1 in your root .bashrc.
 
Old 03-27-2015, 10:27 AM   #6
vivanguarda
Member
 
Registered: Sep 2008
Location: RJ-Brazil
Distribution: Slackware
Posts: 181

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by TB0ne View Post
This isn't just for Slackware, since PS1 and prompts are pretty universal. I settled on one a few years ago, which I like:
Thanks!!! But slackware has specials directories to use bashrc and invoke su'ed login, isn't it? In this ssh case what can I do?
 
Old 03-27-2015, 12:08 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,622

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by vivanguarda View Post
Thanks!!! But slackware has specials directories to use bashrc and invoke su'ed login, isn't it? In this ssh case what can I do?
No. .bashrc is present for any user using bash. SSH is only a protocol to connect, and it still runs the same she'll.
 
Old 03-27-2015, 04:24 PM   #8
vivanguarda
Member
 
Registered: Sep 2008
Location: RJ-Brazil
Distribution: Slackware
Posts: 181

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by phi11ip View Post
I have this in my root and all other users .bashrc on the remote machines :-
Code:
if ps ax | grep sshd: | grep -v grep &>/dev/null; then      #test if this is a remote login
    PS1='\[\033[1;34m\][\u\[\033[1;33m\]@\h\[\033[1;34m\]:\w]\$ \[\033[0m\]'
else
    PS1='\[\033[1;34m\][\u:\w]\$ \[\033[0m\]'
fi
export PS1
Works on my little home network. Not too sure if this would work in your situation though.
I am like a coil!!!
1)Do you have a local lan or a ssh remote acess using ISP service? After that, can you see yours PS1 setting "A" [user@hostname:~]$ in another remote machine "B" differ you among a lot of shells?
2)Do you do both root and normal user's .bashrc and .bash_profile?
 
Old 03-28-2015, 06:55 AM   #9
phi11ip
Member
 
Registered: Jul 2007
Location: Nottingham, UK
Distribution: Slackware64-current
Posts: 93

Rep: Reputation: 23
Quote:
Originally Posted by vivanguarda View Post
I am like a coil!!!
1)Do you have a local lan or a ssh remote acess using ISP service? After that, can you see yours PS1 setting "A" [user@hostname:~]$ in another remote machine "B" differ you among a lot of shells?
2)Do you do both root and normal user's .bashrc and .bash_profile?
1)This is my home network, that is a LAN with my desktop machine and laptop. Both running Slackware64 current. With the second part of the question do you mean "Do the different prompts standout from each other?". If this is what you mean, then it does for me. YMMV depending on the background colour of your terminal which can be adjusted very easily.

2) I have .bashrc and .bash_profile for all users including root and on both machines so as I can ssh from desktop to laptop or vice versa.

There is 1 disadvantage with this if you ssh into a remote machine then the remote machine will also display the same prompt for local users. I have only just noticed this. If this is to be used on a server environment this would affect everybody who ssh'd into the server as well as users logging in locally on the server. It works for me as I'm the only user on my little home network.

My apologies for raising your hopes.
 
Old 03-28-2015, 09:41 AM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
You can do somthing like this:
Code:
who -m | grep -q '([^:]\+)' && PS1="(ssh) $PS1" || PS1="(local) $PS1"
...adjusting PS1 strings as desired. Still works when you've su'd or sudo'd too.
It even works if you ssh user@localhost, unlike the ps ax | grep sshd: approach, which can't differenciate one session from another.

Last edited by GazL; 03-28-2015 at 09:46 AM.
 
2 members found this post helpful.
Old 03-28-2015, 10:06 AM   #11
vivanguarda
Member
 
Registered: Sep 2008
Location: RJ-Brazil
Distribution: Slackware
Posts: 181

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by phi11ip View Post

There is 1 disadvantage with this if you ssh into a remote machine then the remote machine will also display the same prompt for local users. I have only just noticed this. If this is to be used on a server environment this would affect everybody who ssh'd into the server as well as users logging in locally on the server. It works for me as I'm the only user on my little home network.

My apologies for raising your hopes.

I'm going ahead looking for my problem!

But yours disadvantage occurrence... I think it's an usual bash behaviour so it attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon like a secure shell daemon sshd. In this case it reads and executes commands from ~/.bashrc, if that file exists and is readable.

Invoked by remote shell daemon

https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
 
Old 03-28-2015, 07:09 PM   #12
OldHolborn
Member
 
Registered: Jul 2012
Posts: 229

Rep: Reputation: 190Reputation: 190
This formed the basis for mine, it works well.

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

The origin page for that suggests some improvements too

http://stackoverflow.com/questions/2...62727#22362727
 
1 members found this post helpful.
Old 03-29-2015, 07:53 AM   #13
phi11ip
Member
 
Registered: Jul 2007
Location: Nottingham, UK
Distribution: Slackware64-current
Posts: 93

Rep: Reputation: 23
Quote:
Originally Posted by GazL View Post
You can do somthing like this:
Code:
who -m | grep -q '([^:]\+)' && PS1="(ssh) $PS1" || PS1="(local) $PS1"
...adjusting PS1 strings as desired. Still works when you've su'd or sudo'd too.
It even works if you ssh user@localhost, unlike the ps ax | grep sshd: approach, which can't differenciate one session from another.
Aha! This is a far more elegant and robust solution than mine. I still like to have different colours and style of prompts between local and remote prompts so have modified your solution thus:-
Code:
ps_local='\[\033[1;34m\][\u:\w]\$ \[\033[0m\]'
ps_ssh='\[\033[1;34m\][\u\[\033[1;33m\]@\h\[\033[1;34m\]:\w]\$ \[\033[0m\]'
who -m | grep -q '([^:]\+)' && PS1="(ssh) $ps_ssh" || PS1="$ps_local"
export PS1
Works a treat with no shortcomings :-)
 
1 members found this post helpful.
  


Reply



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
[SOLVED] /etc/bashrc ,dircolors and /root/.bash_profile ,/.bashrc not exist in LFS-7.5(sec9.3) jaassi Linux From Scratch 2 09-20-2014 05:04 PM
~/.bashrc, /etc/bash.bashrc files not read? Tachtory Slackware 3 01-04-2014 12:25 AM
[SOLVED] How to use my /home/kangjoo/.bashrc rather than root/.bashrc kangjoo.lee Linux - Newbie 2 11-05-2012 03:38 PM
.bashrc / .bash_profile /etc/profile /etc/bashrc deadeyes Red Hat 2 02-13-2010 11:22 AM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 10:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 02:18 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