0

I've created an SSH non root/non super user with an authorized_key to remotely login to my server and shut it down however, i'm trying to do this from within the authorized_key file by using the command="" syntax

I have the following in the authorized_keys file

command="shutdown -p now",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa 

However when trying to execute the ssh login, while the user is able to login....the command doesn't seem to be executed.

What is the correct syntax for commands when being used in the authorized_keys?

how does no-port-forwarding,no-x11-forwarding,...etc affect the user's ability to login with the command remotely?

the command

/usr/bin/ssh -2 -i /path/to/.ssh/rsa -p 22 -vvv -l user xxx.xxx.0.25

debug information

debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug2: input_userauth_pk_ok: fp 
// REMOVED
debug3: sign_and_send_pubkey: RSA 
// REMOVED
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Saving password to keychain failed
debug3: Incorrect RSA1 identifier
debug1: read PEM private key done: type RSA
Identity added: /.... //removed
(/.../.ssh/shutdown_rsa) // removed 
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to xxx.xxx.0.25 ([xxx.xxx.0.25]:22). //removed
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug2: callback start
debug2: fd 3 setting TCP_NODELAY
debug3: packet_set_tos: set IP_TOS 0x10
debug2: client_session2_setup: id 0
debug2: channel 0: request pty-req confirm 1
debug1: Sending environment.
debug3: Ignored env TERM_PROGRAM
debug3: Ignored env SHELL
debug3: Ignored env TERM
debug3: Ignored env TMPDIR
debug3: Ignored env Apple_PubSub_Socket_Render
debug3: Ignored env TERM_PROGRAM_VERSION
debug3: Ignored env TERM_SESSION_ID
debug3: Ignored env USER
debug3: Ignored env SSH_AUTH_SOCK
debug3: Ignored env __CF_USER_TEXT_ENCODING
debug3: Ignored env PATH
debug3: Ignored env PWD
debug3: Ignored env XPC_FLAGS
debug3: Ignored env XPC_SERVICE_NAME
debug3: Ignored env SHLVL
debug3: Ignored env HOME
debug3: Ignored env LOGNAME
debug1: Sending env LC_CTYPE = UTF-8
debug2: channel 0: request env confirm 0
debug3: Ignored env DISPLAY
debug3: Ignored env SECURITYSESSIONID
debug3: Ignored env _
debug2: channel 0: request shell confirm 1
debug2: callback done
14
  • 1
    what is the verbose output (-vvv) of your ssh command? Are there any errors in server side log?
    – Jakuje
    Sep 1, 2015 at 14:15
  • 1
    Can you confirm the remote user is logging in as root on the server-to-be-shut-down?
    – MadHatter
    Sep 1, 2015 at 14:17
  • 2
    Please edit your question to show the actual ssh command being run by the user, and the output that it produces. Running ssh with -v or -vv would be helpful.
    – Kenster
    Sep 1, 2015 at 14:31
  • @Jakuje no errors
    – Kendall
    Sep 1, 2015 at 14:49
  • 2
    Probably the user doesn't have PATH set to include wherever shutdown is. Try changing the command to /usr/sbin/shutdown -p now (or whatever the full path is) Sep 2, 2015 at 18:25

4 Answers 4

-5

Directive "command" in autorhized_keys DOES NOT executes specified command it only allows user to run this particular command remontly using this key.

4
  • 1
    That is simply wrong. I've just tested it with a key restricted with command="uname -a", and when I do ssh remoteserver -i limited_key "ls" the command uname -a is run.
    – MadHatter
    Sep 1, 2015 at 16:01
  • @MadHatter ...for a minute i thought Mateusz Pacek was correct...however I am confused by your example as it seems to contradict what you are trying to argue against
    – Kendall
    Sep 1, 2015 at 19:18
  • No. I'm not saying you can't excute commands generally in this manner. I'm saying you can't necessarily execute commands that are weaselly about privilege. Normal users can't shut systems down. In some cases, shutdown will let other users run it, but we don't exactly know what. If you would try the sudo and ssh root@ routes, we'd have a much clearer idea what was and wasn't possible.
    – MadHatter
    Sep 1, 2015 at 19:43
  • I confirm this statement is utterly false, see the truth in my answer. Sep 24, 2021 at 6:44
1

Two guesses:

Protocol 2 public key consist of: options, key‐type, base64-encoded key, comment

I don't see the base-64 encoded key in the line in your authorized_keys file.

Identity added: /.... //removed
(/.../.ssh/shutdown_rsa) // removed 

Are you presenting more than one key? Your -vvv isn't complete You left out the important bit about which key was being offered. E.g.,

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/Kendall/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
3
  • THAT....was intentional...I'm not sure what relevance it is to the problem?
    – Kendall
    Sep 1, 2015 at 20:29
  • 2
    What was intentional? Is the key actually on the line in authorized_keys or not? Does the debug info show the correct key being accepted? Sep 1, 2015 at 22:10
  • Yes. and Yes @MarkWagner. I ommited the key information from the details posted( that was intentional)
    – Kendall
    Sep 2, 2015 at 20:23
0

Same thing happened to me. Everything was in place but I was still logging-in normally without any specific command to be executed. It got solved until I double-checked which identity the ssh client was using, by adding -v for verbose mode. I realized it was using a cached key from ssh-agent, which I had to kill first. There is indeed not too much magic in that setup.

1
  • ? What does the command option in file ~/.ssh/authorized_keys ??? Sep 24, 2021 at 6:28
0

The provided command is always executed without arguments. The environement variable SSH2_ORIGINAL_COMMAND countains the command that was typed by the user.

You may consult the documentation here : https://support.ssh.com/manuals/server-admin/64/ssh-server-g3.html

command="command"

This is used to specify a "forced command" that will be executed on the server side instead of anything else when the user is authenticated. The command supplied by the user (if any) is put in the environment variable SSH2_ORIGINAL_COMMAND. The command is run on a pty if the connection requests a pty; otherwise it is run without a tty. Quotes may be used in the command if escaped with backslashes.

This option is useful for restricting certain public keys to perform just a specific operation. An example might be a key that permits remote backups but nothing else. Notice that the client may specify TCP/IP and/or X11 forwarding, unless they are explicitly denied (see no-port-forwarding and no-x11-forwarding below).

If terminal is explicitly allowed in the ssh-server-config.xml file, the forced command is run only when the user tries to run remote commands. If the user requests a shell, he can get it normally and the forced command is not run.

If a forced command is defined in the ssh-server-config.xml file, it overrides any commands in the authorization files. The configuration file might also allow only specific commands, or deny all remote commands. These restrictions apply also to commands in the authorization file.

For more information on command restrictions in the configuration file, see command.

2
  • This answer is not completely wrong, but in part, and is misleading; please edit accordingly. First of all, when it comes to SSH, the reference is OpenSSH, not some proprietary implementation. There is usually no ssh-server-config.xml (it's sshd_config instead), and the environment variable usually is SSH_ORIGINAL_COMMAND, not SSH2_ORIGINAL_COMMAND. There also is no such thing as an "explicitly allowed terminal".
    – Binarus
    Mar 7, 2022 at 9:26
  • Furthermore, the statement that the provided command is always executed without arguments is extremely misleading. In authorized_keys, you can provide a command in the form command param1 param2, which gets executed as expected. What you can't do is the following: Have a forced command in authorized_keys of the form command (without parameters) and then add add the parameters in the client like that: ssh ... command param1 param2. In this case, command will be executed without parameters; in fact, it doesn't matter what command and which parameters you are giving in the client.
    – Binarus
    Mar 7, 2022 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .