0

I want to delete this string:

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"centos\" rather than the user \"root\".';echo;sleep 10"

in below file:

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"centos\" rather than the user \"root\".';echo;sleep 10" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCV9sc7lTrpAJb1UARLmNA08XD8Dy0hGJiX1E5qYk8MkH36xbBtcO6xeuncFr619pu+R/08jkYn9GIXlgPzD9THUpEI+m/OSp5nVPgIamiN7LudOSks6zwk9THkxeSmW95KNEHW5h8Y8MoB0wXzGdr0yiV32nLmvyG18JU6

1
  • @quixotic: actually it looks like authorized_keys which can have options as shown and normally does NOT have hostname/IP Apr 9 '17 at 4:35
0

The file is an authorized_keys file for SSH, often found in ~/.ssh/authorized_keys. It has a number of fields, and the field you would like to remove is the first optional "options" field.

The second field is the "keytype" field, which will have one of the values ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521, ssh-ed25519, ssh-dss or ssh-rsa, according to the sshd manual.

We may use this fact to remove the options field:

sed -E 's/^.* (ecdsa-sha2-(nistp384|nistp521)|ssh-(ed25519|dss|rsa))/\1/' ~/.ssh/authorized_keys >~/.ssh/authorized_keys.new &&
mv ~/.ssh/authorized_keys.new ~/.ssh/authorized_keys &&
chmod go-rwx ~/.ssh/authorized_keys

Or, you may use sed -i ... to change the file in-place if you know how this usually works on your Unix (the -i flag works slightly differently in different implementations of sed).

The sed editing command s/^.* (ecdsa-sha2-(nistp384|nistp521)|ssh-(ed25519|dss|rsa))/\1/ will match the extended regular expression ^.* (ecdsa-sha2-(nistp384|nistp521)|ssh-(ed25519|dss|rsa)) and replace the matched text with the key type found on the line. The expression matches anything from the beginning of each line of input, up to, and including, one of the valid key types.

-1
sed -i 's/your_string//g' filename

You will likely need to escape some punctuation in your_string.

1
  • 2
    Or more easily sed -i 's/^.* ssh-rsa/ssh-rsa/' file (given we know it's an RSA key; if not there are about a dozen possibilities) Apr 9 '17 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.