Centos7 etc/profile和/bin被误删除的解决办法

Centos7 /etc/profile和/bin被误删除的解决办法

/etc/profile被删除的解决办法

/etc/profile被误删除了,ls、vim等shell命令都报如下错误:

-bash: /usr/bin/ls: Argument list too long

shell命令基本都在/usr/bin,/usr/sbin,/bin,/sbin,/usr/X11R6/bin中有定义,
因此通过下面这条命令,重新取出shell命令

export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin
新建立/etc/profile文件

通过vim /etc/profile新建立profile文件,把如下命令拷贝进去,再使用source /etc/profile是文件立即生效

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
#ulimit -SHn 1024000

/bin被误删除的解决办法

使用source /etc/profile报如下错误:
-bash: /usr/libexec/grepconf.sh: /bin/sh: bad interpreter: No such file or directory

(base) [root@kafka1 etc]# source /etc/profile
-bash: /usr/libexec/grepconf.sh: /bin/sh: bad interpreter: No such file or directory

发现是/bin目录被误删除,此时ssh远程连接服务器时,会无法连接:报错Permission deny,用户名或密码不正确。
在这里插入图片描述
补救办法是,在linux服务器上查找bash文件

(base) [root@kafka1 /]# whereis bash
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz

如下图所示:直接建立/bin目录到/usr/bin目录的软连接
在这里插入图片描述
创建软链接ln(link)的方法:

ln -s 源文件 目标文件
-s 选项,表示创建软连接。
源文件   就是要连接到的文件
目标文件  就是要创建的软连接文件,目标文件可以看作源文件的快捷命令

具体命令如下:

ln -s /usr/bin /bin

在这里插入图片描述
建立软连接成功后,再source /etc/profile即可成功

source /etc/profile
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章