I need help with .bash_profile

Silent-Hunter

Hardcore Member
Joined
May 29, 2010
Messages
3,126
I put a script to add a key to ssh-agent in my .bash_profile, and it's running before the login prompt comes up. This is bad because when I input the password it's displayed in the open. I want it to run after I login.

I'm running Devuan, if that matters. Is it not possible to run interactive things automatically after login?

I hope it's okay to ask about this here.
 

FBnil

Waiting to Champion the Pyra to the World...
Joined
Dec 14, 2012
Messages
2,969
Location
Yurp
You can delay execution with
Code:
(sleep 3; echo "months") &
echo "two"
Other ways that come to mind: anacron and expect.
Also, you can

Code:
stty -echo
echo "And you will never know...."
stty echo
 
Last edited:

Silent-Hunter

Hardcore Member
Joined
May 29, 2010
Messages
3,126
Why not just make an ssh key pair and then use that instead?
I have that. I'm using autossh, and it needs to be able to authenticate automatically to recreate the tunnel if the connection drops.
[doublepost=1543354284,1543354244][/doublepost]
when do you start ssh-agent ?
I'll just post my .bash_profile file.
[doublepost=1543354622][/doublepost]
Code:
SSH_ENV="$HOME/.ssh/environment"

function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
     echo succeeded
     chmod 600 "${SSH_ENV}"
     . "${SSH_ENV}" > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
     #ps ${SSH_AGENT_PID} doesn't work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi
I did also try delaying it by inserting a sleep command, but that just made it take longer to give me the prompt for my key password, it didn't let the login prompt come up until after that still.
 

PowerGod

Hardcore Member
Joined
Jun 20, 2011
Messages
3,158
Are you using the "-f" parameter in autossh ?

-f Causes autossh to drop to the background before running ssh. The
-f flag is stripped from arguments passed to ssh. Note that there
is a crucial a difference between the -f with autossh, and -f
with ssh: when used with autossh, ssh will be *unable* to ask for
passwords or passphrases.
 

Silent-Hunter

Hardcore Member
Joined
May 29, 2010
Messages
3,126
Yes, the problem isn't really with autossh. For some reason, .bash_profile is running before the login prompt comes up, everything I looked up says it runs after login.
 

jurop

Still Fresh
Joined
Aug 11, 2018
Messages
7
Age
49
I just tested this launching a shell and .bash_profile is definitively executed BEFORE leaving the shell to the user. IMHO this makes perfect sense since the files are supposed to run programmatically every time the shell is executed.

See this StackExchange answer:
superuser.com/questions/183870/difference-between-bashrc-and-bash-profile/183980#183980
When you login (eg: type username and password) via console, either physically sitting at the machine when booting, or remotely via ssh: .bash_profile is executed to configure things before the initial command prompt.
There are many options to hide user inputs like passwords etc... in a tty, see for example here at Stackoverflow:
stackoverflow.com/questions/ 5633472/how-do-i-turn-off-echo-in-a-terminal

Note:links not working since forum software detected thos as potential spam! Copy&paste those instead adding the https at the front
 

levi

Still fresh, damnit!
Joined
Oct 6, 2008
Messages
11,964
Location
Somewhere off the coast of the EU
On my arch systems, my init script replacement starts a login executable which appears on each of my numbered ttys, and it's only on logging in via that that it actually starts bash, and my .bash_profile runs. I don't know why devuan isn't doing this.

Run ps -eH to get a tree view of all the processes. A snippet of mine looks like this:
Code:
  611 ?        00:00:00   login
  664 tty1     00:00:00     bash
 

Silent-Hunter

Hardcore Member
Joined
May 29, 2010
Messages
3,126
I just tested this launching a shell and .bash_profile is definitively executed BEFORE leaving the shell to the user. IMHO this makes perfect sense since the files are supposed to run programmatically every time the shell is executed.

See this StackExchange answer:
superuser.com/questions/183870/difference-between-bashrc-and-bash-profile/183980#183980

There are many options to hide user inputs like passwords etc... in a tty, see for example here at Stackoverflow:
stackoverflow.com/questions/ 5633472/how-do-i-turn-off-echo-in-a-terminal

Note:links not working since forum software detected thos as potential spam! Copy&paste those instead adding the https at the front
Thanks, I will try that! Because that's all I care about, the visibility of the password.
[doublepost=1543390009,1543385061][/doublepost]
On my arch systems, my init script replacement starts a login executable which appears on each of my numbered ttys, and it's only on logging in via that that it actually starts bash, and my .bash_profile runs. I don't know why devuan isn't doing this.

Run ps -eH to get a tree view of all the processes. A snippet of mine looks like this:
Code:
  611 ?        00:00:00   login
  664 tty1     00:00:00     bash
Code:
  PID TTY          TIME CMD
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00   ksoftirqd/0
    5 ?        00:00:00   kworker/0:0H
    6 ?        00:00:00   kworker/u4:0
    7 ?        00:00:00   rcu_sched
    8 ?        00:00:00   rcu_bh
    9 ?        00:00:00   migration/0
   10 ?        00:00:00   lru-add-drain
   11 ?        00:00:00   watchdog/0
   12 ?        00:00:00   cpuhp/0
   13 ?        00:00:00   cpuhp/1
   14 ?        00:00:00   watchdog/1
   15 ?        00:00:00   migration/1
   16 ?        00:00:00   ksoftirqd/1
   18 ?        00:00:00   kworker/1:0H
   19 ?        00:00:00   kdevtmpfs
   20 ?        00:00:00   netns
   21 ?        00:00:00   khungtaskd
   22 ?        00:00:00   oom_reaper
   23 ?        00:00:00   writeback
   24 ?        00:00:00   kcompactd0
   25 ?        00:00:01   kworker/1:1
   26 ?        00:00:00   ksmd
   27 ?        00:00:00   crypto
   28 ?        00:00:00   kintegrityd
   29 ?        00:00:00   bioset
   30 ?        00:00:00   kblockd
   31 ?        00:00:00   devfreq_wq
   32 ?        00:00:00   watchdogd
   33 ?        00:00:00   kworker/0:1
   34 ?        00:00:00   kswapd0
   35 ?        00:00:00   vmstat
   47 ?        00:00:00   kthrotld
   49 ?        00:00:00   ipv6_addrconf
   89 ?        00:00:00   acpi_thermal_pm
   90 ?        00:00:00   ata_sff
   91 ?        00:00:00   scsi_eh_0
   92 ?        00:00:00   scsi_tmf_0
   93 ?        00:00:00   scsi_eh_1
   94 ?        00:00:00   scsi_tmf_1
   96 ?        00:00:00   kworker/u4:2
   98 ?        00:00:00   bioset
  100 ?        00:00:00   kworker/0:1H
  101 ?        00:00:00   kworker/1:1H
  108 ?        00:00:00   scsi_eh_2
  109 ?        00:00:00   scsi_tmf_2
  110 ?        00:00:00   usb-storage
  112 ?        00:00:00   bioset
  127 ?        00:00:00   kworker/u5:0
  132 ?        00:00:00   jbd2/sda1-8
  133 ?        00:00:00   ext4-rsv-conver
  145 ?        00:00:00   bioset
  373 ?        00:00:00   cfg80211
  381 ?        00:00:00   i915/signal:0
  941 ?        00:00:00   f2fs_flush-8:17
  942 ?        00:00:00   f2fs_gc-8:17
 1921 ?        00:00:00   kauditd
 2116 ?        00:00:02   irq/16-b43
 3725 ?        00:00:00   kworker/1:0
 4352 ?        00:00:00   kworker/0:2
 4576 ?        00:00:00   kworker/1:2
 4666 ?        00:00:00   kworker/u4:1
 4924 ?        00:00:00   kworker/0:0
    1 ?        00:00:01 init
  320 ?        00:00:00   udevd
 1336 ?        00:00:00   rsyslogd
 1397 ?        00:00:00   cups-browsed
 1449 ?        00:00:00   dbus-daemon
 1471 ?        00:00:00   cron
 1494 ?        00:00:00   cupsd
 1497 ?        00:00:00   gpm
 1518 ?        00:00:00   bluetoothd
 1548 ?        00:00:00   avahi-daemon
 1550 ?        00:00:00     avahi-daemon
 1553 ?        00:00:00   colord
 1564 ?        00:00:00   elogind
 1597 ?        00:00:00   irqbalance
 1652 ?        00:00:00   pcscd
 1668 ?        00:00:00   ntpd
 1670 ?        00:00:02   NetworkManager
 2133 ?        00:00:00     dhclient
 1892 ?        00:00:00   master
 2305 ?        00:00:00     pickup
 2306 ?        00:00:00     qmgr
 1920 ?        00:00:00   polkitd
 1936 ?        00:00:01   ssh-agent
 1941 ?        00:00:00   screen
 1942 pts/0    00:00:00     tail
 1943 tty1     00:00:00   login
 1967 tty1     00:00:00     bash
 1981 tty1     00:00:00       startx
 2003 tty1     00:00:00         xinit
 2004 tty1     00:00:13           Xorg
 2007 tty1     00:00:00             pkexec <defunct>
 2082 tty1     00:00:00           x-window-manage
 1944 tty2     00:00:00   getty
 1945 tty3     00:00:00   getty
 1946 tty4     00:00:00   getty
 1947 tty5     00:00:00   getty
 1948 tty6     00:00:00   getty
 1955 ?        00:00:00   wpa_supplicant
 1957 ?        00:00:00   ModemManager
 2011 ?        00:00:00   console-kit-dae
 2106 tty1     00:00:00   dbus-launch
 2107 ?        00:00:00   dbus-daemon
 2283 ?        00:00:00   sshd
 2328 ?        00:00:00   sh
 2340 ?        00:00:00     i3-battery-popu
 2345 ?        00:00:00       i3-battery-popu
 5124 ?        00:00:00         sleep
 2330 ?        00:00:00   sh
 2337 ?        00:00:00     xscreensaver
 2332 ?        00:00:00   sh
 2334 ?        00:00:00     urxvt
 2364 ?        00:00:00       urxvt
 2376 pts/1    00:00:00       screen
 2339 ?        00:00:00   sh
 2344 ?        00:00:09     i3bar
 2365 ?        00:00:00       sh
 2366 ?        00:00:03         i3status
 2371 ?        00:00:00   pulseaudio
 2373 ?        00:00:00   rtkit-daemon
 2443 ?        00:00:00   bash
 2446 ?        00:00:33     vivaldi-bin
 2463 ?        00:00:00       cat
 2464 ?        00:00:00       cat
 2466 ?        00:00:00       vivaldi-sandbox
 2467 ?        00:00:00         vivaldi-bin
 2469 ?        00:00:00           vivaldi-bin
 2590 ?        00:00:27             vivaldi-bin
 2654 ?        00:00:06             vivaldi-bin
 2677 ?        00:00:01             vivaldi-bin
 2686 ?        00:00:02             vivaldi-bin
 2697 ?        00:00:00             vivaldi-bin
 2742 ?        00:00:03             vivaldi-bin
 3092 ?        00:00:14             vivaldi-bin
 5030 ?        00:00:00             vivaldi-bin
 2509 ?        00:00:00       vivaldi-bin
 2559 ?        00:00:00         vivaldi-bin
 2475 ?        00:00:00   gvfsd
 2567 ?        00:00:00   dunst
 2924 ?        00:00:00   sh
 2925 ?        00:00:02     x-terminal-emul
 2926 ?        00:00:00       x-terminal-emul
 2932 pts/2    00:00:00       bash
 4645 pts/2    00:00:00         ssh
 5093 ?        00:00:00   sh
 5094 ?        00:00:00     x-terminal-emul
 5095 ?        00:00:00       x-terminal-emul
 5096 pts/3    00:00:00       bash
 5116 pts/3    00:00:00         sudo
 5125 pts/3    00:00:00           ps

I also tried what the link @jurop sent said to do, and it worked to make the password invisible at least. It's still odd that it happens before the login, but oh well.
 

levi

Still fresh, damnit!
Joined
Oct 6, 2008
Messages
11,964
Location
Somewhere off the coast of the EU
Okay, that looks the same as mine in that it spawns a bash from a login execution. The only difference is your login is run on tty1 while mine is run on '?' which I assume means it spawns on all ttys. You're also running the screen terminal multiplexer which isn't installed here before the creation of the ttys, but I think that may just be a conincidental difference between you and me.

If your login prompt is coming from login, I don't see why it's running your .bash_profile early. Perhaps it spawns your bash fork before asking for your password, which sounds like a potential security gotcha to me.

Edit: Also, in the login prompt provided by login on my machines, the password is already configured not to echo out to screen.
 

Silent-Hunter

Hardcore Member
Joined
May 29, 2010
Messages
3,126
My login prompt password doesn't echo, but the ssh-agent one that spawned before the login prompt did. But I fixed that. It's strange, yes. Fortunately, this netbook doesn't have any ports forwarded to the internet, so it shouldn't be insecure hopefully.

Thanks for the help everyone!
 
Top