AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / unix / Perguntas / 469625
Accepted
somethingSomething
somethingSomething
Asked: 2018-09-18 10:59:42 +0800 CST2018-09-18 10:59:42 +0800 CST 2018-09-18 10:59:42 +0800 CST

Não estou obtendo a saída que quero do sed ao editar sshd_config

  • 772

Estou escrevendo um programa para configurar novas Debianinstalações, mas não consigo sedfazer o que quero. Quero adicionar novos usuários sshd_configcomoallowed users

mas recebo isso: Este é o resultado que recebo agora:

  6
  7 AllowUsers user
  8 AllowUsers something78
  9 AllowUsers something7
 10 AllowUsers something78
 11 AllowUsers something79
 12 AllowUsers something78
 13 AllowUsers something7
 14 AllowUsers something78

onde deveria estar: O resultado esperado é este:

     AllowUsers user
     AllowUsers something7
     AllowUsers something78
     AllowUsers something79

Aqui está o código:

setUPsshd()
 4 {
 5     if grep "Port $PORT" /etc/ssh/sshd_config
 6     then
 7         echo "sshd already set, skipping!"
 8     else
 9         #/bin/cp -f "$CURRENTDIR"/sshd_config /etc/ssh/sshd_config
10         sed -i "s/Port 22/Port $PORT/" /etc/ssh/sshd_config
11         for user in `awk -F: '$3 > 1000 { print $1 }' /etc/passwd`
12         do
13             sed -i "/AllowUsers/a AllowUsers $user" /etc/ssh/sshd_config
14         done
15         USERNAME=`awk -F: '$3 == 1000 { print $1 }' /etc/passwd`
16         if ! grep "AllowUsers $USERNAME" /etc/ssh/sshd_config
17         then
18             sed -i "/AllowUsers/a AllowUsers $USERNAME" /etc/ssh/sshd_config
19         fi
20         echo "chmod 644 /etc/ssh/sshd_config"
21         echo "/etc/init.d/ssh restart"
22     fi
23 }

Aqui está a saída de depuração:

+ PORT=22301
+ setUPsshd
+ grep 'Port 22' /etc/ssh/sshd_config
+ /bin/cp -f /tmp/svaka/sshd_config /etc/ssh/sshd_config
+ sed -i 's/Port 22/Port 22301/' /etc/ssh/sshd_config
++ awk -F: '$3 > 1000 { print $1 }' /etc/passwd
+ for user in `awk -F: '$3 > 1000 { print $1 }' /etc/passwd`
+ sed -i '/AllowUsers/a AllowUsers something79' /etc/ssh/sshd_config
+ for user in `awk -F: '$3 > 1000 { print $1 }' /etc/passwd`
+ sed -i '/AllowUsers/a AllowUsers something7' /etc/ssh/sshd_config
++ awk -F: '$3 == 1000 { print $1 }' /etc/passwd
+ USERNAME=something78
+ grep 'AllowUsers something78' /etc/ssh/sshd_config
+ sed -i '/AllowUsers/a AllowUsers something78' /etc/ssh/sshd_config
+ echo 'chmod 644 /etc/ssh/sshd_config'
chmod 644 /etc/ssh/sshd_config
+ echo '/etc/init.d/ssh restart'
/etc/init.d/ssh restart

PERGUNTA:

Como adiciono os usuários AllowedUsersno sshd_configsem as duplicatas? também você sabe o que está acontecendo no meu código?

sshd sed
  • 2 2 respostas
  • 660 Views

2 respostas

  • Voted
  1. Gerard H. Pille
    2018-09-18T12:02:03+08:002018-09-18T12:02:03+08:00
    sed -i "/AllowUsers/a AllowUsers $user" /etc/ssh/sshd_config
    

    adiciona um "AllowUsers $user" após cada linha AllowUsers existente.

    Eu simplesmente substituiria os seds por um

    echo "AllowUsers $user" >> /etc/ssh/sshd_config
    echo "AllowUsers $USERNAME" >> /etc/ssh/sshd_config
    
    • 1
  2. Best Answer
    somethingSomething
    2018-09-19T07:38:44+08:002018-09-19T07:38:44+08:00

    A saída não é como eu pedi originalmente, mas funciona, então foi assim que eu fiz:

        users=""
        /bin/cp -f "$CURRENTDIR"/sshd_config /etc/ssh/sshd_config
        sed -i "s/Port 22/Port $PORT/" /etc/ssh/sshd_config
        for user in `awk -F: '$3 >= 1000 { print $1 }' /etc/passwd`
        do
            users+="${user} "
        done
        if grep "AllowUsers" /etc/ssh/sshd_config
        then
            sed -i "/AllowUsers/c\AllowUsers $users" /etc/ssh/sshd_config
        else
            sed -i "6 a \
            AllowUsers $users" /etc/ssh/sshd_config
        fi
    

    Esta linha encontra a linha com o padrão AllowUserse substitui a linha inteira pela nova AllowUsers $users

    sed -i "/AllowUsers/c\AllowUsers $users" /etc/ssh/sshd_config
    

    Esta linha anexa o texto AllowUsers $usersapós o 6th linese o arquivo ainda não contiver AllowUsersum aviso de que está escrito em duas linhas:

    sed -i "6 a \
    AllowUsers $users" /etc/ssh/sshd_config
    

    Agora o programa escreve sshd_configdesta forma, AllowUsersé usado uma vez com uma spacelista separada de usuários:

     1 # Package generated configuration file
     2 # See the sshd_config(5) manpage for details
     3
     4 # What ports, IPs and protocols we listen for
     5 Port 22300
     6
     7 AllowUsers user something78 something79 something7
     8
    
    • 0

relate perguntas

  • Linux grep o que no arquivo 1 está no arquivo 2 [duplicado]

  • como grep linhas após a terceira vírgula com condição

  • remova o número de linhas duplicadas com base na correspondência antes da primeira vírgula

  • Como posso melhorar este script de conversão de personagens?

  • Como remover uma única linha entre duas linhas

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Como exportar uma chave privada GPG e uma chave pública para um arquivo

    • 4 respostas
  • Marko Smith

    ssh Não é possível negociar: "nenhuma cifra correspondente encontrada", está rejeitando o cbc

    • 4 respostas
  • Marko Smith

    Como podemos executar um comando armazenado em uma variável?

    • 5 respostas
  • Marko Smith

    Como configurar o systemd-resolved e o systemd-networkd para usar o servidor DNS local para resolver domínios locais e o servidor DNS remoto para domínios remotos?

    • 3 respostas
  • Marko Smith

    Como descarregar o módulo do kernel 'nvidia-drm'?

    • 13 respostas
  • Marko Smith

    apt-get update error no Kali Linux após a atualização do dist [duplicado]

    • 2 respostas
  • Marko Smith

    Como ver as últimas linhas x do log de serviço systemctl

    • 5 respostas
  • Marko Smith

    Nano - pule para o final do arquivo

    • 8 respostas
  • Marko Smith

    erro grub: você precisa carregar o kernel primeiro

    • 4 respostas
  • Marko Smith

    Como baixar o pacote não instalá-lo com o comando apt-get?

    • 7 respostas
  • Martin Hope
    rocky Como exportar uma chave privada GPG e uma chave pública para um arquivo 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add retorna com: "Erro ao conectar ao agente: nenhum arquivo ou diretório" 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll status systemctl mostra: "Estado: degradado" 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim Como podemos executar um comando armazenado em uma variável? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S Por que /dev/null é um arquivo? Por que sua função não é implementada como um programa simples? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 Como ver as últimas linhas x do log de serviço systemctl 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - pule para o final do arquivo 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla Por que verdadeiro e falso são tão grandes? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis Substitua a string em um arquivo de texto enorme (70 GB), uma linha 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya Por que o Linux usa LF como caractere de nova linha? 2017-12-20 05:48:21 +0800 CST

Hot tag

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve