squid启动失败,无任何报错,status命令提示/var/run/squid.pid: (13) Permission denied

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010039418/article/details/81775673

注:本文基于CentOS 6.5编写

背景

今天在倒腾squid代理,做个正向代理,本来是很简单的,但是配置完后要启动时就不对了,什么报错都没有,直接一个failed。

[root@CentOS-6-5 /home]# service squid start
root
Starting squid:                                            [FAILED]

分析

使用service方式启动失败,但是直接使用squid命令启动又是OK的,

[root@CentOS-6-5 /home]# squid -f /etc/squid/squid.conf
[root@CentOS-6-5 /home]# ps aux | grep squid
root      48805  0.0  0.3  71180  3200 ?        Ss   23:12   0:00 squid -f /etc/squid/squid.conf
squid     48807  0.2  1.0  74200 10632 ?        S    23:12   0:00 (squid) -f /etc/squid/squid.conf
squid     48808  0.0  0.1  20084  1080 ?        S    23:12   0:00 (unlinkd)
root      48810  0.0  0.0 103256   832 pts/1    S+   23:13   0:00 grep squid
[root@CentOS-6-5 /home]# 

这让我很不解,手动启动后使用status查询状态时,出现一些端倪,

[root@CentOS-6-5 /home]# service squid status
squid (pid  48807) is running...
squid: ERROR: Could not read pid file
    /var/run/squid.pid: (13) Permission denied
[root@CentOS-6-5 /home]# 

很明显是权限问题。但是我是以squid用户启动,所有的缓存目录的用户属主也是squid,pid文件的组也是squid,

[root@CentOS-6-5 /home]# ll -h /var/run/squid.pid
-rw-r--r--. 1 root squid 6 Aug 16 23:12 /var/run/squid.pid
[root@CentOS-6-5 /home]# 

甚至后来全改为777,也照样失败。

既然是使用service启动失败,那就查看squid的service文件。从脚本可知,start里也是因为无法获取pid文件而失败的(service文件里添加set -x打印调试信息),

start() {
    probe

    parse=`$SQUID -k parse -f $SQUID_CONF 2>&1`
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        echo -n $"Starting $prog: "
        echo_failure
        echo
        echo "$parse"
        return 1
    fi
    for adir in $CACHE_SWAP; do
        if [ ! -d $adir/00 ]; then
            echo -n "init_cache_dir $adir... "
            $SQUID -z -F -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
        fi
    done
    echo -n $"Starting $prog: "
    $SQUID $SQUID_OPTS -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        timeout=0;
        while : ; do
            [ ! -f /var/run/squid.pid ] || break
            if [ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]; then
                RETVAL=1
                break
            fi
            sleep 1 && echo -n "."
            timeout=$((timeout+1))
        done
    fi
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQUID
    [ $RETVAL -eq 0 ] && echo_success
    [ $RETVAL -ne 0 ] && echo_failure
    echo
    return $RETVAL
}

在start后一直等待获取/var/run/squid.pid

[ ! -f /var/run/squid.pid ] 

20s后仍无法获取然后就失败了。

尝试删pid文件,删缓存目录,卸载重装,还是没用,就在要放弃的时候,突然网上有人提到SELinux,我想起之前搞docker的时候也被它坑了一波,赶紧查看系统上的设置。

果然啊,果然,

[root@CentOS-6-5 /home]# getenforce
Enforcing
[root@CentOS-6-5 /home]# 

SELinux果然是开启的,赶紧关闭了再试下,

[root@CentOS-6-5 /home]# setenforce 0
[root@CentOS-6-5 /home]# service squid start
Starting squid:                                            [  OK  ]
[root@CentOS-6-5 /home]# 

done!

看来以后凡是有奇异的权限问题,第一个就要拿SELinux开刀!

后记

其实使用原生squid的配置文件是OK的,但是我调整了缓存目录。在开启SELinux时应该是没有给相应权限,导致无法获取pid文件。应该有SELinux命令能给squid添加权限,但是我并不了解SElinux,也没有特别的需求,因此直接关闭了SELinux。

猜你喜欢

转载自blog.csdn.net/u010039418/article/details/81775673