I am trying to change the system static IP via the dashboard. For example....
Press button 1, Static IP will be 192.168.1.01/24
Press button 2, Static IP will be 192.168.1.02/24
Press button 3, Static IP will be 192.168.1.03/24
and so on.
I have used the exec node before, but it only works (at least for me) with command lines. It doesn't seem to be able to open a file and make changes, for example to the dhcpcd.conf file where the static IP is set.
Any ideas on if this can be done, or is it just outside the limits of Node-Red.
By default you won't be able to write to those files as the node red user will not have access to them. If you modify the permissions on the files to allow access by a user group (probably make a new group just for this purpose) and add the node red user to that group then you should be able to do it.
Assuming that you are working on a Linux platform or similar, as Colin says, the system configuration files require root user privileges in order to make changes.
While you could run Node-RED under the root user, we tend to strongly discourage that since it means that if anyone got access to Node-RED or you created a flow that was a bit overly enthusiastic shall we say, your whole system could be toast.
In addition, changing config files generally then requires the service that reads them to be reloaded. In this case, I think that you would have to restart the device's network stack though I'm not an expert in that area so I might be wrong.
Putting aside the access requirements for a second, the correct way to change the IP address from the command line on a running Linux system is:
So you don't need to edit a file and the command is, I think, dynamic so no restart required.
You will note though that root access is still required (the sudo prefix runs the command in the context of the root user).
Now, it is possible to set a user to be allowed to run specific commands under sudo without requiring a manually entered password that would normally be needed.
You cannot run a command from Node-RED that requires a password to be entered at the command line but you could allow the user to run the command without a password and that would let you call the command using the exec node and driven by your Dashboard.
Remember if you are accessing NR from a different device (say you are using FireFox on your PC to access NR of a Pi on your network) and you do this, you probably lose connection to NR until you access it with the new IP address.
I believe ifconfig is now considered obsolete and one should be using ip. However I believe that either of these will only change the address until the next reboot. The way to change it in Debian based distributions (unless using something like Network Manager) is to set in in /etc/dhcpcd.conf as the OP has suggested.
Thanks for replies everyone. The ip command works, but as noted when system is rebooted you loose the address. If anyone needs this in the future the command you type in Exec node is.....
sudo ip addr add 192.168.x.xx/xx dev ethx
It didn't require a system reboot.
Can i just confirm it is NOT possible to modify the a static IP in dhcpcd,conf directly. When I type sudo nano /etc/dhcpcd.conf in the Exec node it spits out an error, so I assume it can't be done this way.
Also on the permissions. I am not an expert in Linux and still learning. But is it possible to restrict a user to a preset list of commands ie only the ones you want the user to access via the dashboard?
The reason for that is that it needs a password, and you have no way of entering that via the exec node. There are several ways of solving this. One is to tell the system that the node-red user does not need to enter a password when using sudo nano. That is a bit dangerous though as it could edit any file on the system. The better way is to add the node red user to the group that has access to the file you want to edit. If you run ls -l /etc/dhcpcd.conf
it will say something like -rw-rw-r-- 1 root netdev 1433 Jun 25 2018 /etc/dhcpcd.conf
That means that it is owned by the user root and the group is netdev, and that root and members of the netdev group have read/write access whilst others have only read access. Your system may show a different group name.
To allow the node red user to edit the file you can add that user to the netdev group. So if you are on a pi and the node-red user is pi you can run sudo adduser pi netdev
If you now log in as user pi (if you are already logged in as that user then log out and back in again) you can check that you are in the netdev group by running groups
and you should be able to edit the file without having to use sudo, so just nano /etc/dhcpcd.conf
You will probably have to restart node-red to allow it to pick up the new group.
However, you still won't be able to run nano from an exec node as nano needs a UI (unless it can be run without a ui, I don't know). Instead you could use sed to find the ip address and change it.