How to edit remote files (on a guest machine in vagrant) using an editor installed on the host? - remote-access

I use vagrant in windows and work with linux boxes, sometimes i need to edit a file that is not in the shared vagrant folder, like apache2 config files. I don't like using a command line editor like nano or vim.

If you really need to use your own IDE(like PHPstorm) you may connect via sftp by setting it up on your on your IDE, pointing to the root directory('/'). You should also check if the user is connecting to have the right access to the file, much better if you can allow 'root' to connect remotely to your vagrant box.

Related

How can I create symlink on Windows to remote Linux folder using SSH?

I want to create a link to remote Linux folder from Windows 10 machine. I see Windows has SFTP and SCP commands built in for accessing remote machines. So there must be some way to create links as well.
I wonder if we can we do something like below? (forgive the syntax)
mklink /D c:\remotelinks\remoteDir ssh:user#host//linuxdir
A straight link like you mention maybe not, but you have some options:
https://github.com/billziss-gh/winfsp/releases/tag/v1.2POST1
https://medium.com/#danielmarinomirallestaset/mounting-linux-fs-in-windows-10-using-sshfs-e29c7ca81c87
http://makerlab.cs.hku.hk/index.php/en/mapping-network-drive-over-ssh-in-windows
After setting up something like you might be able to create links or shortcuts.

How to run SUDO command in WinSCP to transfer files from Windows to linux [duplicate]

This question already has answers here:
WinSCP connect to Amazon AMI EC2 Instance changing user after login to "root"
(6 answers)
Closed last year.
I am trying to use WinSCP to transfer files over to a Linux Instance from Windows.
I'm using private key for my instance to login to Amazon instance using ec2-user. However ec2-user does not have access to write to the Linux instance
How do I sudo su - to access the root directory and write to the Linux box, using WinSCP or any other file transfer method?
Thanks
I know this is old, but it is actually very possible.
Go to your WinSCP profile (Session > Sites > Site Manager)
Click on Edit > Advanced... > Environment > SFTP
Insert sudo su -c /usr/lib/sftp-server in "SFTP Server" (note this path might be different in your system)
Save and connect
Source
AWS Ubuntu 18.04:
There is an option in WinSCP that does exactly what you are looking for:
AFAIK you can't do that.
What I did at my place of work, is transfer the files to your home (~) folder (or really any folder that you have full permissions in, i.e chmod 777 or variants) via WinSCP, and then SSH to to your linux machine and sudo from there to your destination folder.
Another solution would be to change permissions of the directories you are planning on uploading the files to, so your user (which is without sudo privileges) could write to those dirs.
I would also read about WinSCP Remote Commands for further detail.
Usually all users will have write access to /tmp.
Place the file to /tmp and then login to putty , then you can sudo and copy the file.
I just wanted to mention for SUSE Enterprise server V15.2 on an EC2 Instance the command to add to winSCP SFTP server commands is :
sudo su -c /usr/lib/ssh/sftp-server
I didn't have enough Reputation points to add a comment to the original answer but I had to fish this out so I wanted to add it.
ssh to FreePBX and run the commands stated below in your terminal:
sudo nano -f /etc/sudoers.d/my_config_file
YourUserName ALL=(ALL) NOPASSWD:ALL
sudo systemctl restart sshd
Winscp:
under session login ==> Advanced ==> SFTP
Change SFTP Server to:
sudo /usr/libexec/openssh/sftp-server
I do have the same issue, and I am not sure whether it is possible or not,
tried the above solutions are not worked for me.
for a workaround, I am going with moving the files to my HOME directory, editing and replacing the files with SSH.
Tagging this answer which helped me, might not answer the actual question
If you are using password instead of private key, please refer to this answer for tested working solution on Ubuntu 16.04.5 and 20.04.1
https://stackoverflow.com/a/65466397/2457076

creating symbolic link: Protocol error

I have created linux (Centos) on my virtual box. When I ssh to it and I try to create symbolic link (on my shared folder with full access):
ln -s path/folder/example myFolder
I get error:
ln: creating symbolic link `myFolder': Protocol error
Is there any reason or solution to this?
Right click on your command prompt and "Run as Administrator" (or whatever program you're using to make those symlinks needs to be run as adminstrator).
Here is the solution in a ticket on virtualbox.org :
https://www.virtualbox.org/ticket/10085#comment:32
(Note: Execute "whoami /priv" in console with Administrative privileges)
I have done some Windows research on this bug.
If your user is of the Administrator type (rather than a Standard
account), there's no way to run VB with symlinks working without the
UAC prompt.
If you have a Standard user account (or if you are willing to switch
your account to Standard and create a separate Admin account), the
solution exists. You will, however, need admin privileges to
accomplish several of the steps (not necesarrily for your user
account).
Run the Local Security Policy (Win+R, type "secpol.msc", confirm UAC). Then navigate to "Local Policies->User Rights Assignment".
Find the permission called "Create symbolic links" and double-click it. Add your user to the list of objects having this
permission. Or you can add the "Users" group. Log off and log on. You
can check the success by running in console:
whoami /priv
If you see the privilege SeCreateSymbolicLinkPrivilege enabled, you've
done well.
Make sure your user has proper access to the shared folder on host system.
As mentioned here earlier, execute
VBoxManage setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1
in a console from the C:\Program Files\VirtualBox folder. Don't forget
to substitute VM_NAME with the name of your VM and SHARE_NAME with the
name of the shared folder (as it appears in the VM settings).
You're done.
If you happen to own one of the "lesser" version of windows (e.g. 7
Home), you don't have the "secpol.msc" utility. See
How do I grant SeCreateSymbolicLink on Windows Vista Home edition
for help.
It's a problem with the host filesystem. Check the permissions on the host folder are adequate and the the host filesystem supports symlinks.
As I understand, Windows VirtualBox hosts doesn't support links (from https://www.virtualbox.org/manual/ch04.html#sharedfolders)
Starting with version 4.0, VirtualBox shared folders also support
symbolic links (symlinks), under the following conditions:
The host operating system must support symlinks (i.e. a Mac, Linux or
Solaris host is required).
Currently only Linux and Solaris Guest Additions support symlinks.
For security reasons the guest OS is not allowed to create symlinks by
default. If you trust the guest OS to not abuse the functionality, you
can enable creation of symlinks for "sharename" with:
VBoxManage setextradata "VM name"
VBoxInternal2/SharedFoldersEnableSymlinksCreate/sharename 1
find the cmd.exe file.
Right click on the cmd.exe .
Select "Run as Administrator" menu option.
It should work perfectly.
By default VirtualBox VM disables creation of symbolic links.
Oracle VM VirtualBox shared folders support symbolic links under the following conditions:
The host operating system must support symlinks. For example, a Mac OS X, Linux, or Oracle Solaris host is required.
Currently only Linux and Oracle Solaris Guest Additions support symlinks.
For security reasons the guest OS is not allowed to create symlinks by default.
To enable creation of symlinks for a shared folder you should do:
Run this command on your host (where {VM_name} is the name of VM and {share_folder_name} is the name of shared directory):
$ VBoxManage setextradata "{VM_name}" VBoxInternal2/SharedFoldersEnableSymlinksCreate/{share_folder_name} 1
In my case it looks like:
$ VBoxManage setextradata "ubuntu16" VBoxInternal2/SharedFoldersEnableSymlinksCreate/shared 1
Reboot the VM.
More information you can find here:
https://docs.oracle.com/cd/E97728_01/E97727/html/sharedfolders.html
https://www.virtualbox.org/ticket/18572?cversion=0&cnum_hist=2
Basically you need to again tell VirtualBox to enable symlinks. Here's the complete solution
Have this in your vagrant file,
config.vm.provider "virtualbox" do |v|
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/Folder_Name", "1"]
end
I recommended comment out any sync folder and let default ./(at host) synced at /vagrant(at guest). This will make sure your shared folder(Folder_Name) name is always vagrant as mentioned here https://www.vagrantup.com/docs/synced-folders/. Also, you won't face issues with ssh
If the symlink protocol error returns after restart or still persists run
VBoxManage setextradata "VM_Name" VBoxInternal2/SharedFoldersEnableSymlinksCreate/Folder_Name 1
vagrant halt
vagrant up
vagrant ssh
Usually, VBoxManagee is located at C:\Program Files\Oracle\VirtualBox\
Verify working of symlink by going to /vagrant and running
touch test.txt && ln -s test.txt test1.txt
PS: I have tried this on Windows Home which has limited accessibility to security settings
I observed the same error in virtual box ( Lubuntu running in Windows 10). Issue got resolved by running "Oracle VM Virtualbox" using 'Run As Administrator' option.
I solved this issue by changing the local security policy in my windows. Search for local security policy and open it then on the left-hand side select Local Policies -> User Rights Assignments -> Create Symbolic Links
Click on Add User or Group
Type in everyone in the select user/group prompt.
Reboot your system
It should work.

WinSCP: Text search on remote files

I use WinSCP to get access on the remote files of our project. How can I search for some text/words in all remote files/directories using WinSCP?
WinSCP does not support text searching in its primary GUI.
But there's a built-in extension to Search recursively for text in remote directory.
This is a universal solution that works with SFTP, even if the server does not allow shell access, or even for FTP or WebDAV sessions.
Alternatively, you may be able to make use of WinSCP console window (Commands > Open Terminal) to launch the search from the command-line.
grep -r "text_to_search_for" *
This of course requires a shell access to the server.

Configuring PATH globally for SSH remote commands

I have a problem with the PATH variable on Debian Linux when executing commands via SSH. This happens for example when I use GIT or Mercurial and I have them installed in /opt instead of /usr/local. But I can also reproduce this easily like this:
When I login to the server via SSH in a normal way and then do echo $PATH then I get the PATH which I have configured in /etc/profile:
/usr/local/bin:/usr/bin:/bin:/usr/games:/opt/maven/bin:/opt/ant/bin:/opt/mercurial/bin:/opt/git/bin
But when I do ssh user#server 'echo $PATH' instead then I get this:
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
The same happens when I use GIT or Mercurial with an ssh URL. It can't find hg or git executable on the server because it don't get the PATH from /etc/profile.
So the question is: Is there a "more global" way to configure the PATH so it also works with SSH remote execution? Configuring the PATH per user is not an option. Specifying the full path to git/hg executables when using git/hg on the client is also not an option. And I know that I could use symlinks or wrapper scripts in /usr/local/bin to get it working but I'm searching explicitly for a possibility to have a correct PATH when executing commands remotely via SSH.
On Debian, the standard environment is setup through pam_env (in /etc/pam.d/sshd), which will read /etc/environment and /etc/security/pam_env.conf. You can either edit those, or you add another pam_env line to the pam configuration, pointing to an environment file specific to SSH logins.
While not an answer to your problem, from this link, it looks like ssh has a path settings of its own.
The ssh server set some environment variables at the start of the session. You can create a ~/.ssh/environment file on the ssh server to set additional variables (assuming there is a BSD openssh server on the server), but the server must be configured to allow to change the environment.
If the above doesn't work, you can set the remote command. In mercurial this is the --remotecmd switch for push and pull.
I solved this issue by including all necessary files to the .bashrc
For instance:
. /etc/profile.d/rvm.sh
. /etc/profile.d/bash_completion.sh

Resources