Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Limit bash script to its owner

I have different users in /home. Each user has a start.sh script and a start.conf for configuration. Also start.sh in linked to /bin/nameoftheuser to make typing easier (This adds more complexity to the question).

I want to limit the script to the user that works with it. What I currently do is this:

cd
if [ -f start.conf ]; then
    . start.conf
else
    echo "start.conf not found"
    exit 2
fi

if [ $(whoami) != $SCRIPTUSER ]; then
    echo "You are logged in with $(whoami). Only $SCRIPTUSER is allowed to use this script"
    exit 2
fi

$SCRIPTUSER is defined in start.conf. But when I'm logged in with the wrong user and I want to use start.sh, the wrong configuration file gets included so the second part of the code doesn't catch the mistake.

In the end, each user should limit their start.sh script excluselive to them and others should not be able to call it by accident


One idea could be comparing the owner of $0 to the current user. But when there is a symbolic link in /bin/nameoftheuser, the symbolic link is owned by root.

Including the configuration file and reading what user is allowed to use it and compare $(whoami) to it, does also not satisfy. This is because a different user could call it and include their own start.conf

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Why don't you simply use file permissions?

chmod 700 script.sh

That's it.

over 4 years ago · Santiago Trujillo Denunciar

0

This will find the owner of the symbolic link

owner=$(stat --format '%U' $(readlink -f /bin/nameoftheuser))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda