144

I have a .bash_profile in my home directory, but it isn't getting run on login. If I do the following, then things seem to be as I expect:

ssh myhost
bash
source ~/.bash_profile

But normally that all happens on login. Thoughts?

James A. Rosen
  • 2,202
  • 7
  • 23
  • 26

9 Answers9

248

Use:

chsh

Enter your password and state the path to the shell you want to use.

For Bash that would be /bin/bash. For Zsh that would be /usr/bin/zsh.

akira
  • 61,009
  • 17
  • 135
  • 165
44

On top of akira's answer, you can also edit your /etc/passwd file to specify your default shell.

You will find a line like this example:

john:x:1000:1000:john,,,:/home/john:/bin/sh

The shell is specified at the end.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
John T
  • 163,373
  • 27
  • 341
  • 348
14

Enable bash:

$ /bin/bash

Change shell for user:

$ sudo usermod -s /bin/bash username

where:

  -s, --shell SHELL             new login shell for the user account
User5678015
  • 241
  • 2
  • 2
  • 3
    (1) What do you mean by “enable bash”? (2) The user wants to change *his own* login shell on a remote system.  Why do you assume that he has `sudo` access on that system?  Why do you provide instructions in terms of changing another user’s login shell? – G-Man Says 'Reinstate Monica' Jan 26 '18 at 03:24
  • 1
    Using chsh (as suggested above) did not work for me. This command did! – Per Lindberg Feb 12 '19 at 09:30
6

To make any shell your default, first verify it is installed and recognized on your computer by looking at the contents of /etc/shells:

$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/fish

Then use chsh to change your shell:

$ sudo chsh -s /usr/bin/bash $(whoami) # or sudo chsh -s /bin/bash $(whoami)

References

  1. https://linux.die.net/man/1/cat
  2. https://linux.die.net/man/1/whoami
  3. https://linux.die.net/man/5/shells
  4. https://linux.die.net/man/1/chsh
Behrang
  • 2,032
  • 5
  • 19
  • 26
6

You might check your terminal program. It might be configured to run /bin/sh rather than /bin/bash

Bash executes .bash_profile only for login sessions. .bashrc is executed for all bash sessions, not only login sessions. Try sourcing .bash_profile from .bashrc (avoid circular dependency!) or configuring your terminal program to run /bin/bash -l as a shell program.

Tadeusz A. Kadłubowski
  • 2,195
  • 2
  • 22
  • 27
  • 2
    terminal program has nothing to do with the problem because it is the sshd on the remote machine, which spawns the new shell. – akira Sep 26 '09 at 04:31
5

If you somehow don't see your username in the /etc/passwd file [this is the case when your system is under control of some other domain e.g. in IT companies] Or it says "user not found" with chsh option than below process might help you.

The logic behind the below trick -> On Ubuntu, /bin/sh is dash. You can switch your system to using bash. On Ubuntu, /bin/sh is a symbolic link to dash. You can make it a symbolic link to bash instead.To change it, run

sudo dpkg-reconfigure dash

And press No to switch to bash.

Now, go to Terminal->Edit->preferences->Command and tick the checkbox with statement

Run command as login shell

And that's it.

saurabh gupta
  • 151
  • 1
  • 2
3

One alternative is to rename your startup script into .profile. This file is being source by most Unix shells.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
mathk
  • 151
  • 4
0

There's not enough information in your question for me to say for sure, but I've hit the same problem before. Assuming you've already get /bin/bash set in your password entry, it may be the way your terminal launches.

If you're trying to launch a GUI terminal, say gnome-terminal you may be expecting the shell to read your bash startup files. However, this doesn't happen on Ubuntu and maybe other systems by default.

The way I've fixed it on Ubuntu is to edit the gnome-terminal preferences, and set the startup command to be bash -l. -l is short for --login. This tells bash to startup as as login shell, which causes it to load the startup scripts as you get when logging in via ssh.

I'm sure there's a good rationale for this being the way it is, but I found it surprising and a more than a bit annoying as I share the same profiles across linux, cywgin and macos systems.

edk750
  • 101
  • 1
0

I had this same error, and the above answers did not work for me. For some reason my shell was still loading as just /bin/sh instead of /bin/bash and I have the command to in my .profile as it is supposed to be in Ubuntu 16.04 but I also have a blank .bash_profile file that appears to be being read by .bashrc instead of .profile.

So to get everything to work I simply added the command I needed to launch into my .bash_profile file and then everything worked on next ssh.

So I would say if you have all three files: .bashrc, .bash_profile, .profile make sure your .bash_profile file has stuff in it you want to load on login to the shell environment.

james-see
  • 103
  • 6