问题
I am dealing with Linux SFTP server using .Net (SshClient) code, so I am creating folder on SFTP server and creating files also. For this I am using following code,
using (var client = new SshClient(WebConfigurationManager.AppSettings["SftpServer"], WebConfigurationManager.AppSettings["SftpUserName"], WebConfigurationManager.AppSettings["SftpPassword"]))
{
client.Connect();
if (client.IsConnected)
{
// Check if the Folder Already Exists
var ifExists = client.RunCommand("cat /etc/passwd | grep -w " + ftpUserName);
// If the Folder and User Does Not exists , create both
if (string.IsNullOrEmpty(ifExists.Result))
{
client.RunCommand("echo Asdf@123 | sudo -S mkdir /sftpusers/" + ftpUserName);
client.RunCommand("echo Asdf@123 | sudo -S mkdir /sftpusers/" + ftpUserName + "/import"); // this is for creating folder
client.RunCommand("echo Asdf@123 | sudo -S mkdir /sftpusers/" + ftpUserName + "/schema");
client.RunCommand("echo Asdf@123 | sudo -S touch /sftpusers/" + ftpUserName + "/schema/fileName.txt");
sftpCreated = true;
}
}
client.Disconnect();
}
This code is working file to create a folder and text files on SFTP but Now I am trying to write data to that file, and for that I am using following code,
client.RunCommand("echo Asdf@123 | sudo -S echo hi user >> /sftpusers/asb/schema/filename.txt");
But it is not working. I am getting error says Error = "bash: /sftpusers/asb/schema/filename.txt: Permission denied\n
.
I am little confused that, if same command i.e. echo pwd | sudo -s cmd
is creating folder, files on SFTP server then why it failing while writing content to file.
Need help on this as early as possible.
回答1:
sudo touch ./blabla
Will create a blabla
file as root with root(!) permissions. The owner of this file will be root and most probably the write will be only allowed for root.
sudo echo 123 > ./blabla
This will execute echo
as root, and redirect the output to blabla
as the shell(!) user. sudo
makes the command echo
run as root. But the redirection >
is done by the shell, not by sudo
. So the redirection is done by the however opened the shell, in your case by a user that isn't root, who can't access the file.
You have to write to the file as root for it to work. You can run a subshell as root:
echo 123 | sudo sh -c 'cat > ./blabla'
// or better for scripting, cause no double quoting is needed
echo 123 | sudo sh -c 'cat > "$1"' -- ./blabla
This will echo 123
and execute sh
as root with the command cat
redirected to the first argument of the script, that is blabla
. Note that as the shell is run as root, the redirection is also done as root, so the shell is able to open and write to the file.
Alternatively a more clean approach is to make a tee
.
echo 123 | sudo tee blabla >/dev/null
Remember to redirect tee
's stdout to null, so it doesn't print anything.
Do:
client.RunCommand("echo Asdf@123 | sudo -S sh -c 'echo hi user >> "$1"' -- /sftpusers/asb/schema/filename.txt");
Note that because you use sudo -S
to read the password (which is unsafe and kind of bad), you can't use stdin to pass written string to tee
.
来源:https://stackoverflow.com/questions/55529974/how-to-write-content-to-file-on-linux-sftp-server-using-sshclient