executing history command as another user

rupert160

I am confused as to why I'm having problems executing a history command as another user to return all that user's history - I need to loop though all users and get their history

cat /etc/passwd | awk -F: '{print $1}' > ${system}.users.txt
while read username; do
   echo $username
      sudo -u $username bash -c 'export HISTTIMEFORMAT="%F %T "; history' >../log/${username}.hist
      #sudo -u $username bash -c 'export HISTTIMEFORMAT="%F %T "; history'
done < ./${system}.users.txt

I get no history output when I run: sudo -u anotheruser history

to troubleshoot I tried: sudo -u anotheruser bash -c 'which history'

Also I ran : sudo find / -name 'history' -type f

and I get no returned executable.

Can anybody tell me why a sudo command can't be executed by another user?

dave_thompson_085

(1) history is a shell builtin; there is no executable

(2) bash -c makes bash noninteractive, and noninteractive shells don't use history. This is not very well documented, but I need both -i and set -o history to get history into a shell with -c:

bash -ic 'set -o history; history'

(Tested in 4.1.2(1) on RedHat and 4.3.11(1) on Ubuntu)

(3) timestamps aren't written to the history file by default, so unless you've (previously) set/forced HISTTIMEFORMAT for your users or they have (previously) deliberately set it, lines/entries read from the history file will (all) have the current time

(4) sudo changes the userid and groupid but not the rest of the environment, so your command will look for the history file of the user running it, not the one in $username; add -i

(5) It might be easier to just read $5/.bash_history -- at least for userids that run bash and have a home directory, which system userids mostly won't

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Executing Alias Command as Another User Fails

From Dev

How to view command history of another user in Linux?

From Dev

How to get the command line history of another user?

From Dev

Put a command in history without executing it

From Dev

Executing a command from another command

From Dev

Save command to history without executing in bash

From Dev

put history command onto command line without executing it

From Dev

Mac terminal: How to print a history command on the command prompt without executing it?

From Dev

How do I add a command to bash history without executing it?

From Dev

how to save a command in bash history without executing it programmatically?

From Dev

How can I add a command to the Bash history without executing it?

From Dev

always prompt the user before executing a command in the shell

From Dev

always prompt the user before executing a command in the shell

From Dev

Problem executing command as a different user with sudo -u

From Dev

Ubuntu - command history not available for newly created user

From Dev

Wait on shell command to finish before executing another elisp command

From Dev

scripting a write command to another user

From Dev

Enable user to execute one command as another user

From Dev

After executing a command in the terminal, I sometimes can't execute another

From Dev

Sending a bash command to another script as a string instead of executing it

From Dev

After executing a command in the terminal, I sometimes can't execute another

From Dev

Why find complains when executing another find command with option +

From Dev

Command not executing

From Dev

Command not executing

From Dev

How to have separate command history for different sessions for the same user?

From Dev

In what situations would a user not let the shell record his command into history?

From Dev

how to identify which user has executed the commands through history command

From Dev

Calling another user command inside the user defined command

From Dev

su options - running command as another user

Related Related

  1. 1

    Executing Alias Command as Another User Fails

  2. 2

    How to view command history of another user in Linux?

  3. 3

    How to get the command line history of another user?

  4. 4

    Put a command in history without executing it

  5. 5

    Executing a command from another command

  6. 6

    Save command to history without executing in bash

  7. 7

    put history command onto command line without executing it

  8. 8

    Mac terminal: How to print a history command on the command prompt without executing it?

  9. 9

    How do I add a command to bash history without executing it?

  10. 10

    how to save a command in bash history without executing it programmatically?

  11. 11

    How can I add a command to the Bash history without executing it?

  12. 12

    always prompt the user before executing a command in the shell

  13. 13

    always prompt the user before executing a command in the shell

  14. 14

    Problem executing command as a different user with sudo -u

  15. 15

    Ubuntu - command history not available for newly created user

  16. 16

    Wait on shell command to finish before executing another elisp command

  17. 17

    scripting a write command to another user

  18. 18

    Enable user to execute one command as another user

  19. 19

    After executing a command in the terminal, I sometimes can't execute another

  20. 20

    Sending a bash command to another script as a string instead of executing it

  21. 21

    After executing a command in the terminal, I sometimes can't execute another

  22. 22

    Why find complains when executing another find command with option +

  23. 23

    Command not executing

  24. 24

    Command not executing

  25. 25

    How to have separate command history for different sessions for the same user?

  26. 26

    In what situations would a user not let the shell record his command into history?

  27. 27

    how to identify which user has executed the commands through history command

  28. 28

    Calling another user command inside the user defined command

  29. 29

    su options - running command as another user

HotTag

Archive