6.1 打开终端输入sudo su回车
sudo su
6.2 输入当前用户密码回车然后输入passwd root
passwd root
6.3 重复输入两次新密码
6.4 设置完成
ssh user@192.168.2.129
sudo shutdown -h now
C#ssh以下为远程代码
using Renci.SshNet;
using Renci.SshNet.Common;
public static class SshHelper
{
///
/// windows关机
///
///IP地址
///用户名
///密码
///延时时间,单位秒
public static void WindowsShutDown(string host, string username, string password, int time = 0)
{
using (SshClient sshClient = new SshClient(host, username, password))
{
sshClient.Connect();
var cmd = sshClient.RunCommand($"shutdown -s -t {time}");
Check(cmd);
}
}
///
/// IOS关机
///
///IP地址
///用户名
///密码
///
public static void IOSShutDown(string host, string username, string password, string rootpws)
{
using (SshClient sshClient = new SshClient(host, username, password))
{
sshClient.Connect();
try
{
var cmd = sshClient.RunCommand($"echo '{rootpws}' | sudo -S shutdown -h now");
Check(cmd);
}
catch (SshConnectionException ex)
{
//关机后会引起连接中断
Console.WriteLine(ex.Message);
}
}
}
///
/// 检查指令是否错误
///
///
private static void Check(SshCommand cmd)
{
if (!string.IsNullOrEmpty(cmd.Error))
{
throw new Exception(cmd.Error);
}
}
}