AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[bashrc](server)

Martin Hope
Marshall Davis
Asked: 2022-01-08 07:55:42 +0800 CST

Bash 的起始目录

  • 1

我有一个 Docker 容器正在运行。我使用如下命令连接到容器:

docker exec -it container_name bash

这以 root 身份登录/var/www/html。我想知道为什么这个目录,以及我将如何改变它。这实际上是一个 Web 应用程序的容器,我确实想从 Web 应用程序内容根目录开始,但它位于docker-compose.yml文件中定义的不同名称的卷上。

我检查了一下.profile,.bashrc似乎没有任何迹象表明它会从这个目录开始并且很明显会改变。

bashrc docker
  • 1 个回答
  • 173 Views
Martin Hope
OB7DEV
Asked: 2021-07-18 15:36:06 +0800 CST

将日期保存到实际的 bash 历史文件

  • 1

将“HISTTIMEFORMAT”添加到 bashrc 时,可以在运行“history”命令时获取执行命令的时间戳。

但是时间戳本身不会保存到 bash_history 文件中(至少不是纯文本)。

我正在寻找一种将时间戳写入文件本身的解决方案,以便可以在用户空间之外的编辑器中查看来自各种工作站的归档 .bash_history 文件,并且仍然包含执行命令时的时间戳。

如果时间戳被保存到历史文件本身,但在打开 bash_history 文件时无法在编辑器中查看,并且仍然可以通过在流氓 bash 历史文件上使用 history 命令本身来查看这些时间戳,那么这也将够了。

谢谢

bashrc
  • 3 个回答
  • 369 Views
Martin Hope
Dom
Asked: 2021-01-22 08:26:17 +0800 CST

如何设置将在 shebang 中受到尊重的命令别名

  • 0

我在我的 Linux 服务器上运行多个版本的 PHP。我在 bashrc 中添加了一个别名,以将php命令指向特定版本alias php='/usr/bin/php7.3'。

当我实际使用 php 命令执行 PHP 脚本时,这可以按预期工作,例如php myscript.php

但是,如果我创建myscript.php一个可执行文件并将 shebang#!/usr/bin/env php放在顶部,它会使用默认版本的 PHP 执行脚本,并且不使用我的 bashrc 中定义的别名。

我知道我可以将 shebang 更改为#!/usr/bin/env /usr/bin/php7.3,但我需要全局,而不是逐个文件。

php是否可以为shebang 中尊重的命令设置别名?

linux php bash bashrc alias
  • 1 个回答
  • 1237 Views
Martin Hope
Iain Hallam
Asked: 2017-02-21 10:02:05 +0800 CST

CentOS 7 上的 bash PATH 是从哪里获取 /usr/local/bin 的?

  • 9

我刚刚使用 centos/7 框和 Vagrant 启动了一个全新的 CentOS 7 VM(bash 版本 4.2.46),当我以 vagrant 用户身份登录时,这是我的 PATH:

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin

以 root 身份登录时,路径如下:

/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

我知道这/etc/profile是添加/usr/local/sbin:/usr/sbin(对于大多数用户来说,在最后,对于 root 用户来说),并且/etc/skel/.bash_profile最终在 vagrant 主目录中添加/home/vagrant/.local/bin:/home/vagrant/bin,但是初始设置来自哪里?

目前,我无法解释为什么用户最终/usr/local/bin:/usr/bin会进入他们的 PATH,而 root 会得到其他人。

(坦率地说,根 PATH 的顺序似乎很奇怪,因为它/usr/local/sbin应该/usr/sbin在最后,根据/etc/profile.)

bash bashrc centos7
  • 5 个回答
  • 27682 Views
Martin Hope
Joum
Asked: 2016-12-28 05:01:57 +0800 CST

如果用户破坏了 bashrc 命令,则注销用户

  • 1

当用户通过 SSH 访问我的 Linux 主机时,我正在运行一个小脚本。此脚本应为用户验证和/或设置 Google Authenticator MFA 访问权限。

现在它按预期工作,但有一个警告 - 在 MFA 配置过程中的任何时候,如果用户 (ie) CTRL+C的,设置向导被中断,但 SSH 会话继续。我需要它来注销试图访问的用户。

我怎样才能做到这一点?

这是我在.bashrc文件底部添加的内容(请注意,这对我来说很新,并且我愿意接受对我当前尝试的批评/改进)。

# MFA validation/configuration

if [[ -n $SSH_CONNECTION ]] ; then
        echo "SSH connection to remote host successful."
        echo "testing if MFA is configured..."

        # is this test enough?
        file="$HOME/.google_authenticator"

        if [ -f "$file" ] ; then
                printf "MFA configured, you may proceed.\n"
        else
                printf "MFA not configured; running setup wizard.\n"

                # the command runs, but the else bit is never reached
                if google-authenticator ; then
                        # I reach this point if I go to the end of the wizard
                        echo "MFA setup successful"
                else
                        # this point is never reached
                        echo "MFA setup failed - logging you out"
                        exit
                fi
        fi
fi
ssh bashrc google-authenticator
  • 1 个回答
  • 253 Views
Martin Hope
v78
Asked: 2016-09-17 11:52:19 +0800 CST

'source .bash_profile' 失败,但 'source path/to/.bash_profile' 有效

  • 1

我正在尝试在 sudo-less 用户内部的远程主机上获取我的 .bashrc 文件。我收到以下回复。

sh-4.2$ source .bash_profile
sh-4.2$ source: .bash_profile: file not found

sh-4.2$ source ~/.bash_profile
[user@hera ~]$

为什么会出现这种行为?

添加图像

.bash_profile 的内容

.bashrc 的内容

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

# added by Anaconda3 4.1.1 installer
export PATH="/home/tensorflow/anaconda3/bin:$PATH"
linux bash ssh bashrc
  • 2 个回答
  • 1117 Views
Martin Hope
Chris
Asked: 2012-07-22 19:52:02 +0800 CST

.bashrc: shopt: 未找到

  • 11

刚刚使用 Ubuntu 12.04 配置了一个新的 Rackspace 实例,并从他们的 github 上下载了 rbenv 并安装了它。

执行时出现以下错误. ./~bashrc

$ . ~/.bashrc
sh: 18: /home/deployer/.bashrc: shopt: not found
sh: 26: /home/deployer/.bashrc: shopt: not found
sh: 110: /home/deployer/.bashrc: shopt: not found
sh: 28: /etc/bash_completion: [[: not found
sh: 34: /etc/bash_completion: [[: not found
sh: 51: /etc/bash_completion: Bad substitution

这是我的 .bashrc(用于用户部署程序),我唯一添加的是顶部的 rbenv 位。

$ cat .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

if [ -d "${RBENV_ROOT}" ]; then
  export PATH="${RBENV_ROOT}/bin:${PATH}"
  eval "$(rbenv init -)"
fi

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

更新:

评论中要求的测试:

$ echo $SHELL
/bin/sh
$  /bin/sh -c 'echo $SHELL'
/bin/sh
$ cat /proc/$$/cmdline
sh
bashrc
  • 3 个回答
  • 36406 Views
Martin Hope
gsgx
Asked: 2012-06-21 05:01:43 +0800 CST

如何在 SSH 登录时使用自定义 .bashrc 文件

  • 25

我发现在与我合作的新公司中,我经常不得不访问生命周期相对较短的 linux 服务器。在这些服务器中的每一个上,我都有一个帐户,但每当创建一个新帐户时,我都必须经历通过我的 .bashrc 进行传输的麻烦。然而,有可能在大约一个月的时间内该服务器将不再存在。我还必须在短时间内(几分钟)访问许多其他服务器,在这些服务器上传输我的 .bashrc 是不值得的,但由于我在很多服务器上工作,这加起来浪费了很多时间。

我不想更改服务器上的任何内容,但我想知道是否有一种方法可以拥有“每个连接”.bashrc,因此每当我通过 SSH 连接到服务器时,我的设置都会用于该会话。

如果可能的话,如果我可以对其他配置文件(如 gitconfig 文件)执行相同的操作,那就太好了。

bash ssh configuration bashrc
  • 6 个回答
  • 29749 Views
Martin Hope
hackerhasid
Asked: 2012-06-19 09:15:30 +0800 CST

bash 选项卡补全仅适用于 root

  • 9

我设置了一个 Ubuntu 12 服务器(在 rackspace 云上使用他们的预配置图像)并创建了一个非 root 用户。当我以标准用户身份登录时,在终端按 [TAB] 输出一个实际的选项卡并按 [UP] 输出 ^[[A 。但是,如果我以 root 身份登录,[UP] 将输入我的最后一个命令并且 [TAB] 会自动完成。

.bashrc 文件在两个用户之间看起来完全一样。我真的不知道还有什么地方可以看。

ubuntu bash command-line-interface bashrc ubuntu-12.04
  • 1 个回答
  • 2211 Views
Martin Hope
Adamski
Asked: 2010-02-20 09:27:16 +0800 CST

crontab 环境

  • 7

我编写了各种脚本来启动 Java 服务器应用程序,这些应用程序通常在关闭之前运行 24 小时(通过调用具有不同参数的相同脚本)。

该脚本依赖于文件中定义的环境变量:~/<user>.env,我从.bashrc.

从命令行调用脚本时这很好用,但是如果我想将脚本添加为 crontab 条目,我会遇到.bashrc未读取的问题。

我的问题:解决这个问题的最佳实践方法是什么?我意识到我可以定义一个 crontab 条目,例如:

* * * * 1-5 /usr/bin/bash -c '. /home/myuser/myuser.env && /home/myuser/scripts/myscript.sh'

...但这看起来很丑陋。或者,我可以myuser.env在每个脚本的开头获取源,但这将成为维护的噩梦。

任何帮助表示赞赏。

solaris bash cron bashrc
  • 6 个回答
  • 10621 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve