Ubuntu sshd_配置通过systemd进行更改

Ubuntu sshd_配置通过systemd进行更改,ubuntu,raspberry-pi,systemd,sshd,Ubuntu,Raspberry Pi,Systemd,Sshd,我试图在Raspberry Pi上运行Ubuntu的第一次启动时自动执行一些任务。我有一个Systemd服务,它只运行一次就自动终止。作为其中的一部分,我试图在sshd_config上更新配置,并尝试了我能想到的所有可能的事情,在谷歌上搜索,但都没有成功。希望有人能在这里投入更多的经验来处理这些事情 # disable password login echo "First Boot - disabling ssh password login" sed -i 's/PasswordAuthent

我试图在Raspberry Pi上运行Ubuntu的第一次启动时自动执行一些任务。我有一个Systemd服务,它只运行一次就自动终止。作为其中的一部分,我试图在sshd_config上更新配置,并尝试了我能想到的所有可能的事情,在谷歌上搜索,但都没有成功。希望有人能在这里投入更多的经验来处理这些事情

# disable password login
echo "First Boot - disabling ssh password login"
sed -i 's/PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
Systemctl和Syslog在执行过程中不显示任何错误。如果我在命令行上运行上述命令,它的行为将与预期的一样

我尝试过的其他事情

Attempt 1: Assuming permission errors due to in-place sed file creation. I have routed the output to a temp file on printing the contents it looks right but the actual location i.e. /etc/ssh/sshd_config has no changes

TFILE=`mktemp --tmpdir tfile.XXXXX`
trap `rm -f $TFILE` 0 1 2 3 15
sed 's/PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config > $TFILE
cat $TFILE > /etc/ssh/sshd_config

Attempt 2: Read somewhere that /etc/ssh/sshd_config is a symlink to file in /usr and get copied over and hence executing first line copies it to /etc and changes on top 

sed -i '' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
于2002年2月23日更新:

服务文件

[Unit]
Description=First boot script
ConditionPathExists=/first_boot.sh

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/first_boot.sh
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target
勾选“nologin”—— 简单如:touch/etc/nologin;
然后是rm-f/etc/nologin。

谢谢你们这些家伙的指点,我终于让它起作用了。“脱离接触”的几年在技术上实现了巨大的飞跃。我遇到的问题是Ubuntu
cloud init
SystemD的一部分将更新
sshd_config
文件,执行顺序基本上覆盖了我的更改

这是我最后一个有效的服务文件

[Unit]
Description=Post first boot script
ConditionPathExists=!/first_boot.sh
ConditionPathExists=/post_first_boot.sh

# run customisation and package installations after networking & ssh are up
After=cloud-config.service
Before=cloud-final.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/post_first_boot.sh
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

如果这是一个
.ini
文件,您应该签出

普通的linux软件包,易于安装,可以让您直接在bash中使用命令

sudo apt-get install -q -y 
就我而言

mimeapps="/home/frank/.config/mimeapps.list"
# create a new / ensure-there-is this section
crudini --set $mimeapps 'Default Applications'
# set a property in this section
crudini --set $mimeapps 'Default Applications' x-scheme-handler/http google-chrome.desktop
当心: “相等”符号周围是否有空格。根据特定的ini文件,可能需要最后一个regexp sed进行清理。我是这样处理的:

# crudini creates spaces around the ini sign
# this inplace search-and-replace removes any space before/after equal signs
_sanitizeIni(){
    [[ "$#" -eq 1 ]] || _fail "_sanitizeIni needs exactly 1 parameter"
    sed -i -E 's/\s?=\s?/=/g' $1
}

请向我们展示服务文件的内容。还要在配置中添加一些虚拟行,以检查额外脚本替换整个文件的可能性感谢指针。我在sshd_config中添加了一个代码片段,以检查它是否被替换。但是片段保留了下来,但是我感兴趣的实际值正在改变。我想我现在需要在UbuntuSystemd中搜索哪个服务正在替换我感兴趣的值。欢迎来到StackOverflow!我怀疑Nataraj只允许通过SSH密钥登录。你能解释一下nologin和它有什么关系吗?可能是的,或者在sshd中禁用pw auth是他必须禁用用户远程登录的唯一想法。我的想法和你的一样好-啊,对不起,你没有。。。