Crontab could not create directory .ssh
最近在利用 crontab 构建自动备份时,遇到了一个问题。我的脚本中包含了用于服务器用户切换使用的 ssh 命令。当我登录到服务器上时,脚本执行正常;当我没有登录到服务器上时,脚本执行失败,错误提示是:
Could not create directory '/home/server/.ssh'
在登录到服务器上后,可以看到 “/home/server/.ssh” 路径是存在的,也是正常的。
各个用户间的 ssh key 也已经生成好了,在登录到服务器上后,手动调用主脚本,可以正常的完成脚本的执行流程,但是由 crontab 自动调用时,仍然无法正常工作。
在网上查找相关信息的时候,在 stackoverflow 上有人问类似的问题,但是并没有人给出了可行的解答。 rsync code will run, but not in cron. 原文链接
资料查找没有找到可行的结果,只能自己想办法了。因为登录和不登录结果不一样,首先怀疑是不是登录导致环境变量有所改变。为了确认这一点,我在主脚本中加入了 echo 命令,来打印 SSH 使用到的环境变量。具体的环境变量包含以下内容:
DISPLAY The DISPLAY variable indicates the location of the X11 server. It is automatically set by ssh to point to a value of the form
``hostname:n'', where ``hostname'' indicates the host where the shell runs, and `n' is an integer >= 1. ssh uses this special value to
forward X11 connections over the secure channel. The user should normally not set DISPLAY explicitly, as that will render the X11 con-
nection insecure (and will require the user to manually copy any required authorization cookies). HOME Set to the path of the user's home directory. LOGNAME Synonym for USER; set for compatibility with systems that use this variable. MAIL Set to the path of the user's mailbox. PATH Set to the default PATH, as specified when compiling ssh. SSH_ASKPASS If ssh needs a passphrase, it will read the passphrase from the current terminal if it was run from a terminal. If ssh does not have a
terminal associated with it but DISPLAY and SSH_ASKPASS are set, it will execute the program specified by SSH_ASKPASS and open an X11
window to read the passphrase. This is particularly useful when calling ssh from a .xsession or related script. (Note that on some
machines it may be necessary to redirect the input from /dev/null to make this work.) SSH_AUTH_SOCK Identifies the path of a UNIX-domain socket used to communicate with the agent. SSH_CONNECTION Identifies the client and server ends of the connection. The variable contains four space-separated values: client IP address, client
port number, server IP address, and server port number. SSH_ORIGINAL_COMMAND This variable contains the original command line if a forced command is executed. It can be used to extract the original arguments. SSH_TTY This is set to the name of the tty (path to the device) associated with the current shell or command. If the current session has no
tty, this variable is not set. TZ This variable is set to indicate the present time zone if it was set when the daemon was started (i.e. the daemon passes the value on
to new connections). USER Set to the name of the user logging in.
通过打印上述环境变量,发现登录前和登录后打印出的结果完全一致,但是没有登录的话,自动执行脚本就会提示“Could not create directory ‘/home/server/.ssh’”
这个问题还是很奇怪,不知道为什么会这样。为了去解决这个问题,我尝试在本地部署一个自动执行脚本,用于自动登录到服务器上调用服务器自动备份脚本,来实现自动备份。当自动登录部署完成后,发现了一个新的问题:当我登录在服务器上时,自动登录可以执行成功;当我没有登录到服务器上时,自动登录无法执行成功。在没有登录上服务器时,用户目录和登录上之后不一样??
为了验证这个想法,执行一下 mount 命令,结果如下:
/dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/cgroup type tmpfs (rw)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
none on /run/user type tmpfs (rw,noexec,nosuid,nodev,size=104857600,mode=0755)
none on /sys/fs/pstore type pstore (rw)
systemd on /sys/fs/cgroup/systemd type cgroup (rw,noexec,nosuid,nodev,none,name=systemd)
/home/server/.Private on /home/server type ecryptfs (ecryptfs_check_dev_ruid,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_unlink_sigs,ecryptfs_sig=a1f33ec3321f0ef9,ecryptfs_fnek_sig=705c22000a171cfe)
从 mount 的结果上看,用户目录是被 mount 上的。再次进行测试,在 crontab 里通过自动脚本执行 mount 命令。对比发现,在自动执行时,用户目录是没有被挂载的。那么,这就是问题原因了:
在没有登录到服务器上时,用户目录没有被挂载。因为没有被挂载,所以用户目录的权限不对!!
为了验证这个想法,再次登录到服务器上,手动将用户目录进行卸载
sudo umount /home/server
卸载掉用户目录后,再次进入目录中进行查看
server@gitlabserver:~$ ls -a
. .. Access-Your-Private-Data.desktop
.bash_history .ecryptfs .kde
.Private README.txt .viminfo
我们发现,用户目录到内容变了。
因为服务器用户目录是经过了 “ecryptfs” 加密,所以在没有登录和登录后不一样。知道了这一点,解决自动登录到方法也就变得简单了:将用户目录中的 .ssh 目录拷贝到卸载后的用户目录中。
经过上述的拷贝,自动执行的脚本终于可以顺利执行了,问题圆满解决。
我在 stackoverflow 上回答了这个问题。自己记录一下,看看是不是能帮到别人: stackoverflow
欢迎转载,转载请保留:
转自Elta 的博客:http://www.eltaera.com//2017/01/18/crontab_ssh.html
Crontab could not create directory .ssh的更多相关文章
- cygwin Could not create directory '/home/Administrator/.ssh'
在cygwin下运行: ssh-keygen -C "634772208@qq.com" -t rsa 时,出现如下错误: cygwin Could not create dire ...
- org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create directory /user/hive/warehouse/page_view. Name node is in safe mode
FAILED: Error in metadata: MetaException(message:Got exception: org.apache.hadoop.ipc.RemoteExceptio ...
- vsftp关于"550 create directory operation failed"问题解决
前提: 昨天晚上配置好了vsftp, 但登陆后,除了浏览,什么也干不了.(如新建文件/文件夹, 删除文件, 重命名等都不可操作) 都是弹出 "550 create directory ope ...
- [hadoop]Cannot create directory /mdrill/tablelist/fact_seller_all_d. Name node is in safe mode.
在执行mdrill创建表的时候报如下异常(蓝色部分为关键): [mdrill@hadoop1101 bin]$ ./bluewhale mdrill create ./create.sql higo ...
- 550 Create directory operation failed
往Linux系统中上传文件时候,我们经常会使用FTP连接Linux,也经常会使用mkdir命令来创建目录.最近发现用mkdir创建目录时提示550 Create directory operation ...
- Linux虚拟主机通过FTP软件创建目录时提示550 Create Directory Operation Failed
更新时间:2017-06-07 13:26:11 分享: 问题描述 通过FTP软件连接Linux虚拟主机,在尝试创建新目录时,服务器返回错误提示:550 Create Directory Oper ...
- 非root用户启动redis容器报错mkdir: cannot create directory '/bitnami/redis': Permission denied
问题:使用docker启动容器时,报错如下 zh@debian:~/testPath$ docker-compose up redis Starting testpath_redis_1 ... do ...
- HTTP Status 500 - Unable to create directory
分析原因: 例如:java web项目 上传图片创建文件夹cd /data/apps/static-web/sjk/driver/attachment/编号/文件名称.jpg 在创建文件目录 /dat ...
- Hadoop "Cannot create directory .Name node is in safe mode."解决方案
转载自:http://www.waitig.com/hadoop-name-node-is-in-safe-mode.html 在使用Hadoop建立文件的时候,出现“Cannot create di ...
随机推荐
- word 书签排序算法
直接上代码 /// <summary> /// 通过计算插入引文的位置格式化合适的引文序号 /// </summary> /// <returns></ret ...
- 二叉树查找(C#)
参考文章: http://www.cnblogs.com/huangxincheng/archive/2012/07/21/2602375.html http://www.cnblogs.com/xi ...
- hashmap如何初始化
现在知道的有两种: 1) Map<String, String> hashMap = new HashMap<String, String>(){ { ...
- c#之时间戳与DateTime的相互转换
1. 时间戳转 DateTime static DateTime GetServerNow(ulong serverTime) { DateTime dateTimeStart = TimeZone. ...
- 在windos 环境下安装
在windows 环境下安装node 和 StrongLoop需要一些几个步骤. 本人使用的安装软件,文章最后的分享. 1,安装Git: 2,安装Node.js: 3,安装npm: 4,安装Stron ...
- onchange、oninput、onpropertyChange事件的异同
onchange事件适用于input\textarea\select元素上,支持各大浏览器,该事件只有在键盘或者鼠标操作改变对象属性,且失去焦点时触发,脚本触发无效: oninput事件适用于inpu ...
- Android L(5.0)源码之手势识别OnTouchListener
在Activity中,因为要监听触摸屏的触摸事件和手势时间,所以该Activity必须实现OnTouchListener和OnGestureListener两个接口,并重写其中的方法.本人根据andr ...
- MySQL测试环境遇到 mmap(xxx bytes) failed; errno 12解决方法
查看Mysql日志 InnoDB: Initializing buffer pool, size = 128.0M InnoDB: mmap( bytes) failed; errno InnoDB: ...
- S3C2440硬件IIC详解
S3C2440A RISC微处理器可以支持一个多主控IIC 总线串行接口.一条专用串行数据线(SDA)和一条专用串行时钟线(SCL)传递连接到IIC总线的总线主控和外设之间的信息.SDA和SCL线都为 ...
- 安卓canvas操作的总结
2014.07.03 使用canvas绘图 需求:公司需要绘制波形图,类似数学上的正弦波,一条是参考值,一条是实际曲线 解决方法:采用canvas绘图.保存为图片,以供导出 这里提供一个学习的demo ...