2020年修改版:几年来,我一直在使用下面发布的systemd-answer,对此感到非常满意。
为了便于与远程文件,我设置的工作afuse
与sshfs
从运行.bashrc
本地用户。bash初始化后,所有远程树~/.ssh/config/
都将按需安装在~/scp/
,并且具有正确的访问权限/限制,因此可以正常工作。
当前位于(一个文件,源于).bashrc
:
exists(){ [[ ${1:0:1} == "/" ]] && { test -f $1 || test -d $1; } \
|| command -v $1 >/dev/null 2>&1; }
# Run an afuse process as daemon
if pgrep -u $USER -f "afuse.*$USER/scp" >/dev/null ; then
echo "afuse/sshfs already running"
else
exists afuse && exists sshfs && exists $HOME/scp \
&& export TAfuseX=$(mktemp --suffix=__afuse) && chmod u+x "$TAfuseX" \
&& echo -e "#!/bin/sh\ngrep '^Host ..' ~/.ssh/co* |colrm 1 5" > $TAfuseX \
&& echo "running afuse/sshfs with nohup" \
&& nohup afuse -o mount_template="sshfs %r:/ %m" \
-o unmount_template="fusermount -u -z %m" \
-o populate_root_command=$TAfuseX \
~/scp 2>&1 > ~/afuse.log
fi
(以上内容已包含来自@ sato-katsura的有用提示)
上面的操作可以确保它正常运行,直到机器关闭为止。它与X停止/启动无关。万一发生灾难性崩溃(我从未见过),下一个shell将重新生成它。
即使将其迁移到之前,您是否看到任何有用的优化,陷阱或其他提示systemd
?您如何将其包装到systemd用户单元中?
几年来,我一直在使用这个systemd用户服务单元定义文件:afuse.service
[Unit]
Description="SSHFS via Afuse automounter"
AssertPathExists=%h/scp/
AssertFileIsExecutable=/usr/bin/afuse
AssertFileIsExecutable=/usr/bin/sshfs
[Service]
Type=forking
WorkingDirectory=%h/scp
ExecStart=/usr/bin/afuse \
-o fsname=AutoSCP \
-o timeout=300 \
-o auto_unmount \
-o flushwrites \
-o mount_template="sshfs -o ServerAliveInterval=10 -o reconnect %%r:/ %%m" \
-o unmount_template="fusermount -u -z %%m" .
Restart=always
PrivateTmp=true
#NoNewPrivileges=true <- That option breaks this unit, why?
[Install]
WantedBy=default.target
在以下位置创建afuse.service
具有上述内容的文件:
~/.config/systemd/user/
如果仅为您安装/etc/systemd/user/
如果为所有本地用户安装使用以下方法安装此服务:
mkdir ~/scp # Or Home Directories of all existing users + /etc/skel/
systemctl --user daemon-reload # If root, then omit '--user'
# If enabling only for the current user:
systemctl --user enable afuse.service
# If enabling for all users, execute as root:
# systemctl --user --global enable service
systemctl --user start afuse.service
如果需要,您可以将SSH-AGENT配置为不将套接字放置在中/tmp/...
,而是将其放置在之下~
,那么该服务应该能够使用代理。(如果有需求,我也可以添加确切的步骤,@ dirdi)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句