0

So, I am an intermediate linux user, I have just discovered sed and want to use it to automate some configuration during my usual install/configure script.

I have scrolled through all of the forums to locate an issue similar to mine, however I have not seen many using the same format as I am and I am not sure why. I came to the script I have below through looking at a number of other sed scripts and using the ones that worked with what I am trying to do. Then I put it in a script and it throws the error.

I am unable to locate the issue in this sed script:

sudo sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' "/etc/ssh/sshd_config"
sudo sed -i 's/X11Forwarding yes/#X11Forwarding yes/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#StrictModes yes/StrictModes yes/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#IgnoreRhosts yes/IgnoreRhosts yes/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#HostbasedAuthentication no/HostbasedAuthentication no/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#RhostsRSAAuthentication no/RhostsRSAAuthentication no/g' "/etc/ssh/sshd_config"

I have also written the command like:

sudo sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g;s/#PermitRootLogin yes/PermitRootLogin no/g;s/X11Forwarding yes/#X11Forwarding yes/g;s/#StrictModes yes/StrictModes yes/g;s/#IgnoreRhosts yes/IgnoreRhosts yes/g;s/#HostbasedAuthentication no/HostbasedAuthentication no/g;s/#RhostsRSAAuthentication no/RhostsRSAAuthentication no/g' "/etc/ssh/sshd_config"

I get the same error:

: No such file or directoryhd_config

On both when run in a script with other arguments that all complete with no issues. I can run both of the commands successfully by themselves.

The full script is:

#!/bin/bash
# Title:    Ubuntu Setup
# Author:   Matthew Williams
# Date:     10/31/2016
echo "***Ubuntu Setup Script Log***" `date +%d%m%Y_%H:%M.%S`  | tee UbuntuSetupLog.txt
sudo apt-get install -y libpcsclite1 pcscd pcsc-tools libssl-dev libpam0g-dev pkg-config libpcsclite-dev gdebi opensc unity-tweak-tool gnome-do openssh-server openssh-client byobu | tee -a UbuntuSetupLog.txt
echo "***Files Installed***" `date +%d%m%Y_%H:%M.%S`  | tee -a UbuntuSetupLog.txt
#
# Configure Networking / SSH
#
echo "***Configuring Networking/SSH***" | tee -a UbuntuSetupLog.txt
#
sudo sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g;s/#PermitRootLogin yes/PermitRootLogin no/g;s/X11Forwarding yes/#X11Forwarding yes/g;s/#StrictModes yes/StrictModes yes/g;s/#IgnoreRhosts yes/IgnoreRhosts yes/g;s/#HostbasedAuthentication no/HostbasedAuthentication no/g;s/#RhostsRSAAuthentication no/RhostsRSAAuthentication no/g' "/etc/ssh/sshd_config"
sudo service sshd restart 2>&1 | tee -a UbuntuSetupLog.txt
#
echo "***Configuring Networking/SSH Complete***" `date +%d%m%Y_%H:%M.%S`  | tee -a UbuntuSetupLog.txt
#
echo "***Script Complete***"

The full return is:

***Ubuntu Setup Script Log*** 02112016_15:28.38
Reading package lists...
Building dependency tree...
Reading state information...
byobu is already the newest version (5.106-0ubuntu1).
libpam0g-dev is already the newest version (1.1.8-3.2ubuntu2).
libpcsclite-dev is already the newest version (1.8.14-1ubuntu1).
libpcsclite1 is already the newest version (1.8.14-1ubuntu1).
pkg-config is already the newest version (0.29.1-0ubuntu1).
gdebi is already the newest version (0.9.5.7ubuntu1).
gnome-do is already the newest version (0.95.3-5).
opensc is already the newest version (0.15.0-1ubuntu1).
pcsc-tools is already the newest version (1.4.25-1).
pcscd is already the newest version (1.8.14-1ubuntu1).
unity-tweak-tool is already the newest version (0.0.7ubuntu2).
libssl-dev is already the newest version (1.0.2g-1ubuntu4.5).
openssh-client is already the newest version (1:7.2p2-4ubuntu2.1).
openssh-server is already the newest version (1:7.2p2-4ubuntu2.1).
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
***Files Installed*** 02112016_15:28.38
***Configuring Networking/SSH***
: No such file or directoryhd_config
***Configuring Networking/SSH Complete*** 02112016_15:28.38
***Script Complete***

I need this script to automate a number of other machines. I know there are better methods and everyone has their own favorite way to to it. My only question is what am I doing wrong in sed? I am unsure what I need to do to change this script and I have been looking online fruitlessly for about a week.

I believe the error is in the way I am calling the file but not sure how else to do it.

1
1

Your script was saved using CR+LF (Windows) line endings. The shell does not understand those – it uses only LF as the line ending marker, and the CR becomes part of the command. Thus:

  • Your logs are being written to the file UbuntuSetupLog.txt<CR>. (Unfortunately both CR and LF are allowed in file names...)
  • Your sed commands are trying to edit the file /etc/ssh/sshd_config<CR>, and the CR byte mangles the error message when it's being printed (acting as an actual carriage return).

Use dos2unix or frodos to convert the script. Or if you prefer:

sed -i 's/\r$//' myscript.sh
2
  • I'll try that and return the result. – Matthew Williams Nov 3 '16 at 13:13
  • This was the issue, I thank you for locating my stupid mistake. I was coding this in Notepad++ on a different machine than my normal one. I forgot to do the EOL Conversion since I was on a windows machine at the time. For those that need that setting. It is located in Edit>EOL Conversion. – Matthew Williams Nov 3 '16 at 15:23

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.