bash 在“vagrant ssh”时自动chdir到vagrant目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17864047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:01:45  来源:igfitidea点击:

Automatically chdir to vagrant directory upon "vagrant ssh"

bashshellvagrant

提问by Rob Kinyon

So, I've got a bunch of vagrant VMs running some flavor of Linux (centos, ubuntu, whatever). I would like to automatically ensure that a "vagrant ssh" will also "cd /vagrant" so that no-one has to remember to do that whenever they log in.

所以,我有一堆运行某种 Linux 风格的流浪虚拟机(centos、ubuntu 等等)。我想自动确保“vagrant ssh”也将“cd /vagrant”,这样任何人在登录时都不必记住这样做。

I've figured out (duh!) that echo "\n\ncd /vagrant" >> /home/vagrant/.bashrcwill do the trick. What I don't know is how to ensure that this only happens if the cd command isn't already there. I'm not a shell expert, so I'm completely confused here. :)

我已经想通了(废话!)那echo "\n\ncd /vagrant" >> /home/vagrant/.bashrc会奏效。我不知道如何确保只有在 cd 命令不存在时才会发生这种情况。我不是 shell 专家,所以我在这里完全感到困惑。:)

采纳答案by Terry Wang

cdis a Bash shell built-in, as long as a shell is installed it should be there.

cd是内置的 Bash shell,只要安装了 shell,它就应该在那里。

Also, be aware that ~/.bash_profileis for interactive login shell, if you add cd /vagrantin ~vagrant/.bashrc, it may NOT work.

此外,要知道这~/.bash_profile是交互式登录shell,如果添加cd /vagrant~vagrant/.bashrc,它可能无法正常工作。

Because distros like Ubuntu does NOT have this file -> ~/.bash_profileby default and instead use ~/.bashrcand ~/.profile

因为像 Ubuntu 这样的发行版没有这个文件 ->~/.bash_profile默认情况下,而是使用~/.bashrc~/.profile

If someone creates a ~/.bash_profilefor vagrant user on Ubuntu, ~vagrant/.bashrcwill not be read.

如果有人~/.bash_profile在 Ubuntu 上为 vagrant 用户创建了一个,~vagrant/.bashrc则不会被读取。

回答by httpete

I put

我放

echo "cd /vagrant_projects/my-project" >> /home/vagrant/.bashrc

echo "cd /vagrant_projects/my-project" >> /home/vagrant/.bashrc

in my provision.sh, and it works like a charm.

在 my 中provision.sh,它就像一个魅力。

回答by q0rban

You can do this by using the config.ssh.extra_argssetting in your Vagrantfile:

您可以使用config.ssh.extra_argsVagrantfile 中的设置来执行此操作:

  config.ssh.extra_args = ["-t", "cd /vagrant; bash --login"]

Then anytime you run vagrant sshyou will be in the /vagrantdirectory.

然后,无论何时运行,vagrant ssh您都会在/vagrant目录中。

回答by Menasheh

You need to add cd /vagrantto your .bashrc in the vm. The best way to do this is in your provisioner script.

您需要cd /vagrant在 vm 中添加到您的 .bashrc 中。最好的方法是在您的供应商脚本中。

If you don't have a provisioner script, make one by adding this line to your Vagrantfile before end:

如果您没有配置程序脚本,请在end以下内容之前将此行添加到您的 Vagrantfile 中:

config.vm.provision "shell", path: "scripts/vagrant/provisioner.sh", privileged: false

Path is relative to the project root where the Vagrantfile is, and privileged depends on your project and what else is in your provisioner script which might need to be privileged. I use priveleged false and sudo explicitly when necessary.

路径相对于 Vagrantfile 所在的项目根目录,特权取决于您的项目以及您的供应商脚本中可能需要特权的其他内容。必要时,我会明确使用 priveleged false 和 sudo。

And in the provisioner script:

在供应商脚本中:

if ! grep -q "cd /vagrant" ~/.bashrc ; then 
    echo "cd /vagrant" >> ~/.bashrc 
fi 

This will add cd /vagrantto .bashrc, but only if it isn't there already. This is useful if you reprovision, as it will prevent your .bashrc from getting cluttered.

这将添加cd /vagrant到 .bashrc,但前提是它不存在。如果您重新配置,这很有用,因为它可以防止您的 .bashrc 变得混乱。

Some answers mention a conflict with .bash_profile. If the above code doesn't work, you can try the same line with .bash_profileor .profileinstead of .bashrc. However, I've been using vagrant with ubuntu guests. My Laravel/homestead box based on Ubuntu has a .bash_profileand a .profilebut having cd /vagrantin .bashrcdidwork for me when using vagrant sshwithout changing or deleting the other files.

一些答案提到了与 .bash_profile 的冲突。如果上面的代码不起作用,您可以尝试使用.bash_profile.profile代替相同的行.bashrc。但是,我一直在与 ubuntu 客人一起使用 vagrant。基于Ubuntu我Laravel /宅基地箱具有.bash_profile.profile,但具有cd /vagrant.bashrc没有使用的时候,我的工作vagrant ssh没有更改或删除其他文件。

回答by gonzaloriestra

You can also do it this way:

你也可以这样做:

vagrant ssh -c "cd /vagrant && bash"

vagrant ssh -c "cd /vagrant && bash"

And you could include it in a script to launch it (like ./vagrant-ssh).

您可以将其包含在脚本中以启动它(如./vagrant-ssh)。

回答by bfitzpatrick

You can add cd /vagrantto your .bashrcand it will run the command when you ssh. The /bashrcyou want is in /home/vagrant(the user you login as when you vagrant ssh.) You can just stick the new line at the bottom of the file.

您可以添加cd /vagrant到您的.bashrc,它会在您 ssh 时运行该命令。在/bashrc你想要的是在/home/vagrant(你登录,当您的用户vagrant ssh可以只要坚持新线在该文件的底部)。

回答by captainchhala

May be this can help. Edit the Vagrantfileas replace your username with vagrant

可能这会有所帮助。编辑Vagrantfileas 替换您的用户名vagrant

`
 config.vm.provision "shell" do |s|
 s.inline = <<-SHELL
 # Change directory automatically on ssh login
 if ! grep -qF "cd /home/vagrant/ansible" /home/vagrant/.bashrc ;
 then echo "cd    /home/vagrant/ansible" >> /home/vagrant/.bashrc ; fi
 chown vagrant. /home/vagrant/.bashrc
` 

回答by openCivilisation

Ideally we just want to alter the vagrant ssh behaviour.

理想情况下,我们只想改变流浪的 ssh 行为。

In my case, I wanted something that didn't affect any other processes in the environment, so we can do something like this in the vagrant file-

就我而言,我想要一些不影响环境中任何其他进程的东西,所以我们可以在 vagrant 文件中做这样的事情 -

    VAGRANT_COMMAND = ARGV[0]
    if VAGRANT_COMMAND == "ssh"
        config.ssh.extra_args = ["-t", "cd /vagrant; bash --login"]
    end