1

When logging on to servers I would like to have time displayed on my bash prompt so when I check previous things I've done, I can see at what time I did it. I think it's a particular useful feature when trying to debug things.
It can look like this:

[root@server 11:46:02 ~]# ls
file1 file2
[root@server 11:47:50 ~]# whoami
root

I found that I kind of can do this with ssh:

ssh root@server -t "export PS1='[\u@\h $(date +%H:%M:%S) \W]\$ '; exec bash"

ssh root@server -t "export PS1='[\u@\h \t \W]\$ '; exec bash"

But obviously the date won't be updated each time I type something new.
I haven't figured how to do it from PuTTY at all - I see there is a setting in Connections -> Data to send Environment variables to the server, but that doesn't work.

Note that I do not want to change anything on the server side (no sshd change, no bashrc or profile change) :)

  • You do not to change something server sided? But how about adding a mere script? I tried around very long with this topic and also came to no solution without configuring sshd. – h0ch5tr4355 Sep 30 '15 at 10:50
  • My reason for not wanting to change anything server side or adding scripts is I manage a great bunch of servers and it would be too inconvenient to do this. Also, I sometimes connect to servers that are not 'owned' by me and leaving scripts behind is not nice. BTW what did you try with? Putty or ssh? – w00t Oct 1 '15 at 10:31
3

I cannot help you with putty, but note that bash has a code for the current time so you can replace $(date +%H:%M:%S) by \t.

  • Heey that's great! Thanks for the suggestion, it works. – w00t Sep 30 '15 at 10:36
0

Even though I've not actually found the answer for PuTTY, I have found a better alternative:
1. install Cygwin (and enjoy better productivity)
2. add Cygwin shortcut to my PATH so I can do WIN+R and type cyg to open terminal
3. add this function in Cygwin ~/.bashrc :

ssh () { /usr/bin/ssh "$@" -t "export PS1='[\t \u@\h \W]\$ '; exec bash"; }

Reopen Cygwin or source ~/.bashrc and ssh user@server and prompt is how I want it *:
[12:03:08 user@server ~]$

*It works on CentOS or AWS Linux. Not working on Ubuntu because it overrides $PS1.

Disclaimer: stuck on Windows, cannot install Ubuntu nor dual-boot. VM not an option. /Cry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.