How do I start in bash when ssh'ing into my server?

30

6

I have a hosted Debian server. When I log in via ssh, I'm greeted with a sh environment. How do I change it so I start in a bash environment?

bbbgscott

Posted 2013-02-05T04:59:58.937

Reputation:

1

Not really a programming question. In the future, you'd probably best ask questions like this on Unix/Linux.

– tylerl – 2013-02-05T05:06:04.610

Answers

35

As a regular user, you can change your default login shell using the chsh command. Here is an example:

chsh -s /bin/bash

Another option is to use usermod as root:

usermod -s /bin/bash username

jordanm

Posted 2013-02-05T04:59:58.937

Reputation: 553

1usermod worked like a charm. Is there some way to set it to default to bash when I add a new user, or do I have to run the command each time? – None – 2013-02-05T05:34:36.293

If you use adduser, it should set it to bash by default. The default is configured in /etc/adduser.conf. Otherwise specify the shell explicitly with useradd. – jordanm – 2013-02-05T05:42:14.850

5

You edit /etc/passwd where the last entry is the default shell. Make it /bin/bash.

Alternatively, you could alter alter the system default of /bin/sh not being bash.

Dirk Eddelbuettel

Posted 2013-02-05T04:59:58.937

Reputation: 1 191

on jailbroken ios this is the only way. – Wyatt8740 – 2015-03-24T23:55:45.827

1Yo, what's up with the drive-by downvote? Eg Ubuntu does default to /bin/sh being /bin/dash. And for what it is worth the other two answers are identical and achive the same end by different means. Whatever. – Dirk Eddelbuettel – 2013-02-05T05:03:02.963

4Manual edits of /etc/passwd are highly discouraged. An editing mistake can break logins for all users, requiring recovery media or a boot to single user mode to repair. There are tools such as usermod for changing /etc/passwd. – jordanm – 2013-02-05T05:04:19.827

1Nonsense. Running Linux since '94 here. Never busted /etc/passwd. – Dirk Eddelbuettel – 2013-02-05T05:04:50.903

2Good to hear you have been very careful. Not everyone has. Another note on your update, launching /bin/bash as /bin/sh is the same as executing it with --posix, which may have undesirable results. – jordanm – 2013-02-05T05:06:39.670

Look, I've been a Debian developer/maintainer since 1995 too. We used to have bash as a default, we switched to simpler shells for a variety of reason. I have been using /bin/bash as my shell all those years on all machines. You need a more concrete argument. – Dirk Eddelbuettel – 2013-02-05T05:08:02.527

See "posix mode" in the bash manpage. When used in scripting as /bin/sh, it's not really a problem (which is what I believe you are referring to). In interactive use, differences are more likely to be noticed. E.g. When executed as /bin/sh not all RC files are sourced. – jordanm – 2013-02-05T05:14:54.863

2

You need to edit your user profile, you can do this directly by editing the /etc/passwd file, or you can use the usermod command to do it for you. The syntax you're looking for looks something like this:

usermod -s /bin/bash joeuser

tylerl

Posted 2013-02-05T04:59:58.937

Reputation: 2 064

1

For the case where you're trying to use a shared account (for whatever reason) and can't change the default shell, then you can run

ssh -t <user@hostname> bash -l

If you need to keep your environment from some other shell, then you can run that shell first; for example

ssh -t <user@hostname> ksh -c bash -l

UKMonkey

Posted 2013-02-05T04:59:58.937

Reputation: 111

That works pretty well, but why the -l option? – nbkhope – 2018-03-05T22:25:50.750

@nbkhope it will ensure that the .bashrc is run for you. – UKMonkey – 2018-03-06T00:03:16.017

1

Neither chsh or usermod were working for me, but I found that you can do this through PuTTY.

Go to Connection > SSH and set the Remote command to bash.

Note that you won't be able to exit to your default shell, it will just close the connection.

Chris

Posted 2013-02-05T04:59:58.937

Reputation: 133

1

Default system shell /bin/sh in recent Ubuntu releases is configured to be /bin/dash. By simply running following command:

sudo dpkg-reconfigure dash

you can change it back to old default of /bin/bash.

With this, you can achieve desired effect of having bash as interactive shell without changing any user settings (no chsh or usermod), and it will work for all users who currently have shell set to /bin/sh.

There is only one small downside to this: Ubuntu boot time might slightly increase, because dash takes less memory to load and slightly faster to run (no wonder - it is so limited in features). But I think it will be rather difficult to measure this effect, especially for hosting environment.

Also, it is sometimes annoying to see shell scripts that fail to work properly because they use some bash advanced features which are not supported by dash. Using this recipe will make sure this will not happen.

For more information, see Ubuntu wiki about this issue.

mvp

Posted 2013-02-05T04:59:58.937

Reputation: 3 705

you should keep the use cases apart: /bin/sh is used as the interpreter for POSIX compatible shell scripts - it's not necessarily the best interactive shell; if you want bash to be your system shell, you should set your preferred login shell using chsh or proper adduser calls. using dpkg-reconfigure is a bad choice if userA wants zsh and userB wants bash and both insist in having /bin/sh as their default shell. – umläute – 2013-02-05T12:49:19.010

if userA is explicitly configured to zsh, and userB to bash they will have it. If userC configured to sh, he will have bash, which is current Ubuntu default for new users anyway – mvp – 2013-02-05T17:41:39.513

sure, but your solution suggested something like dpkg-reconfigure zsh to set zsh as /bin/sh, and then dpkg-reconfigure dash to set dash as /bin/sh which is kind of a deadlock; i'm mainly saying that it is preferrably to set the login shell to the shell one wants to use rather than going through hoops to make /bin/sh a good login shell. – umläute – 2013-02-05T19:46:59.277

I only said that dpkg-reconfigure dash can make sh point to bash, rather than crippled dash. All other shells will be still intact. – mvp – 2013-02-05T21:09:53.040