LoginSignup
0
0

More than 5 years have passed since last update.

CentOS 5でSFTP専用ユーザ&read-onlyモード

Posted at

はじめに

  • CentOS 5でリードオンリーモードでSFTP専用ユーザを作る必要があったので、その備忘録

結論

デフォルトで入っているOpenSSHでは、特定ユーザやグループを「Match」で特定することに対応しておらず、OpenSSHをソースからインストールする必要がありました。

環境

  • CentOS 5
  • OpenSSH 7.6

インストール

  • コンパイル
./configure \
--prefix=/usr/local/openssh-7.6p1 \
--with-ssl-dir=/usr/local/openssl \
--with-tcp-wrappers \
--with-ssl-engine \
--with-pam \
--with-md5-passwords
make && make install
  • バージョン隠蔽
cd /usr/local
ln -s openssh-7.6p1 openssh
  • 起動スクリプト

既存SSサーバはTCP22で起動しているので、TCP2222で起動するようにしました。

#!/bin/bash
#
# sshd    Start up the OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: SSH is a protocol for secure remote shell access. \
#              This service starts up the OpenSSH server daemon.
#
# processname: sshd
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /usr/local/openssh/etc/sshd_config
# pidfile: /var/run/sshd-2222.pid

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd

RETVAL=0
prog="sshd-2222"
lockfile=/var/lock/subsys/$prog

# Some functions to make the below more readable
KEYGEN=/usr/local/openssh/bin/ssh-keygen
SSHD=/usr/local/openssh/sbin/sshd-2222
RSA_KEY=/usr/local/openssh/etc/ssh_host_rsa_key
DSA_KEY=/usr/local/openssh/etc/ssh_host_dsa_key
ECDSA_KEY=/usr/local/openssh/etc/ssh_host_ecdsa_key
ED25519_KEY=/usr/local/openssh/etc/ssh_host_ed25519_key
PID_FILE=/var/run/sshd-2222.pid

fips_enabled() {
    if [ -r /proc/sys/crypto/fips_enabled ]; then
        cat /proc/sys/crypto/fips_enabled
    else
        echo 0
    fi
}

do_rsa_keygen() {
    if [ ! -s $RSA_KEY ]; then
        echo -n $"Generating SSH2 RSA host key: "
        if $KEYGEN -q -t rsa -f $RSA_KEY -N '' >&/dev/null; then
            chmod 600 $RSA_KEY
            chmod 644 $RSA_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $RSA_KEY.pub
            fi
            success $"RSA key generation"
            echo
        else
            failure $"RSA key generation"
            echo
            exit 1
        fi
    fi
}

do_dsa_keygen() {
    if [ ! -s $DSA_KEY -a `fips_enabled` -eq 0 ]; then
        echo -n $"Generating SSH2 DSA host key: "
        if $KEYGEN -q -t dsa -f $DSA_KEY -N '' >&/dev/null; then
            chmod 600 $DSA_KEY
            chmod 644 $DSA_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $DSA_KEY.pub
            fi
            success $"DSA key generation"
            echo
        else
            failure $"DSA key generation"
            echo
            exit 1
        fi
        fi
}

do_ecdsa_keygen() {
    if [ ! -s $ECDSA_KEY ]; then
        echo -n $"Generating SSH2 ECDSA host key: "
        if $KEYGEN -q -t ecdsa -f $ECDSA_KEY -N '' >&/dev/null; then
            chmod 600 $ECDSA_KEY
            chmod 644 $ECDSA_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $ECDSA_KEY.pub
            fi
            success $"ECDSA key generation"
            echo
        else
            failure $"ECDSA key generation"
            echo
            exit 1
        fi
    fi
}

do_ed25519_keygen() {
    if [ ! -s $ED25519_KEY ]; then
        echo -n $"Generating SSH2 ED25519 host key: "
        if $KEYGEN -q -t ed25519 -f $ED25519_KEY -N '' >&/dev/null; then
            chmod 600 $ED25519_KEY
            chmod 644 $ED25519_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $ED25519_KEY.pub
            fi
            success $"ED25519 key generation"
            echo
        else
            failure $"ED25519 key generation"
            echo
            exit 1
        fi
    fi
}

do_restart_sanity_check()
{
    $SSHD -t
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        failure $"Configuration file or keys are invalid"
        echo
    fi
}

start()
{
    # Create keys if necessary
    do_rsa_keygen
    do_dsa_keygen
    do_ecdsa_keygen
    do_ed25519_keygen

    echo -n $"Starting $prog:"
    $SSHD $OPTIONS && success || failure
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $lockfile
    echo
}

stop()
{
    echo -n $"Stopping $prog:"
    killproc $SSHD -TERM
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    echo
}

reload()
{
    echo -n $"Reloading $prog:"
    killproc -p $PID_FILE $SSHD -HUP
    RETVAL=$?
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    reload)
        reload
        ;;
    condrestart)
        if [ -f $lockfile ]; then
            do_restart_sanity_check
            if [ $RETVAL -eq 0 ]; then
                stop
                # avoid race
                sleep 3
                start
            fi
        fi
        ;;
    status)
        status $SSHD
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL
  • chkconfig追加
chkconfig --add sshd-2222
chkconfig sshd-2222 on
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0