User specific hosts in Linux - linux

I want to ssh to a server, I have its IP address. It's suffering to remember it's IP, I want to ssh to it using a host name. But I have no authority to write the /etc/hosts.
Is there a user specific hosts table that I have access to?

Don't create an alias. This is exactly what ssh config is for.
You can add a host alias in your ssh config in ~/.ssh/config.
Example:
Host foo
HostName 172.16.254.1
Now when you type ssh foo it will execute ssh 172.16.254.1.
You can also add a user to the config. Example:
Host foo
HostName 172.16.254.1
User bar

Related

ssh tunnel from azure bastion hosts

Is it possible to do ssh tunneling (SSH port forwarding) from azure bastion host?
Like we do normally from a jump box:
ssh -i path/to/private_key -L 127.0.0.1:FORWARD_PORT:VM_IP:APPLICATION_PORT user#jumphost.net
ssh -i path/to/private_key -L 127.0.0.1:8080:10.0.0.1:8080 user#jumphost.net
Do you really need port fowarding? Your use case can perfectly use TCP forwarding like so, with the following SSH config.
Host JumpHost1
Hostname jumphost1.net
User user_jh1
Host JumpHost2
Hostname jumphost2.net
User user_jh2
ProxyCommand ssh -W %h:%p JumpHost1
Host AppBox
Hostname appbox_behind_firewall.net
User app_user
ProxyCommand ssh -W %h:%p JumpHost2
Then you can easily do ssh AppBox without issue. You'll need to have your local public key authenticated to each jumphost and the appbox. Which you should be able to easily do with ssh-copy-id if you are doing this with OpenSSH

Shell script remotely

I have one script running on server and doing some job on other server
I have many scp commands and ssh commands, this is why each time I have to enter the remote server password at each remote command.
is there any way to establish ssh connection between the servers so I type the remote password only once?
thanks
I would suggest to setup an ssh config together with ssh keys. In a nutshell the config will hold an alias for one or more remote servers.
ssh remote_server1
ssh remote server2
While your config file will look something like this:
Host remote_server1
Hostname 192.168.1.12
user elmo
IdentityFile ~/.ssh/keys/remote.key
...
If an ssh config file is not for you (although I can highly recommend it), you can use sshpass as well.
sshpass -p 't#uyM59bQ' ssh username#server.example.com
Do note that the above does expose your password. If someone else has access to your account, the history command will show the code snippet above.

Kerberos and ssh multiple identities

Perhaps the answer to this question is that it is not possible but hopefully someone knows how to get around this issue. In the past, before the admins configured Kerberos in our school machines I was able to create ssh keys for several purposes. The way this was done was via the ~/.ssh/config file in my machine and the ~/.ssh/authorized_keys in the server. An example of my ssh config file goes as follows
Host sayHI
IdentityFile path/to/sayHiPrivateKey
HostName servername
User myusername
Host sayHey
IdentityFile path/to/sayHeyPrivateKey
HostName servername
User myusername
Then in the authorized_keys file I would have
command="echo hi" ssh-rsa sayHiPublicKeyLONGSTRING....
command="echo hey" ssh-rsa sayHeyPublicKeyLONGSTRING....
ssh-rsa otherkeysformypasswordlessentry
With this setup I could do something like
$ ssh sayHI
hi
$ ssh sayHey
hey
Unfortunately, this has now stopped since we are now using Kerberos to authenticate every day. What this means is that now I have do
$ kinit username#SERVERNAME
Once I put in my password I can use ssh as follows:
$ ssh -K username#hostname
and now I have access. If I do
$ ssh sayHi
this will not work since it asks for my password. If I do
$ ssh -K sayHi
this logs me in but it completely ignores the fact that this was supposed to use an identity so that I can run the command echo hi. Instead it just uses the kerberos credentials and logs me in. So now that I have explain the functionality that I once had, does anyone know if it is possible to recover this using kerberos and ssh? The multiple identities files was useful specially if you wanted to let a friend run a command on your behalf without giving them your password (http://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch08_02.htm). I really hope this is still possible somehow. Thanks in advance.
It depends if the server still accepts pubkey authentication or not (it looks like it does not, otherwise the kerberos authentication wouldn't make any sense).
Depending on the vendor of the OS, there might be possible to set up .k5login, but it will probably not solve the issue. Better way to differentiate between the commands would be using some alias on your client:
alias sayHi="ssh -K host echo Hi"
alias sayHello="ssh -K host echo Hello"
in your ~/.bashrc.

Tunnel SSH: access a server blocked by firewall through another server

I have 1 pc and 2 servers.
Each device has a user associated with it:
pc (10.0.0.10) -> pc_user
server1 (10.0.0.146) -> server1_user
server2 (192.168.0.3) -> server2_user
There is a firewall blocking everything from "pc" to "server2".
The goal is to acess "server2" from "pc" through a SSH tunnel to "server1".
How can I do it?
If using openssh:
TRIVIAL WAY
PC> ssh server1_user#server1
server1> ssh server2_user#server2
PROXY WAY
Get a netcat on server1, if you can't install one, you can try to statically compile one (check busybox), download one (find server1 and OS version and check it's repos). If you have python/perl, there are "script implementations" of the command.
On your ~/.ssh/config file add:
Host server1
HostName 10.0.0.146
User server1_user
Host server2
ProxyCommand ssh -C -q server1 /<server1_path_to>/nc 192.168.0.3 22
User server2_user
ssh server2 will prompt for both passwords, if you're not using key authentication.
Since OpenSSH 5.4 netcat is not required for proxying
Host server2
ProxyCommand ssh -W %h:%p server1
User server2_user
TUNNEL WAY
PC TTY1> ssh -L 2222:192.168.0.3:22 server1_user#server1
PC TTY2> ssh server2_user#localhost -p 2222

Changing a hostname permanently in Ubuntu

I want to create a shell script that can change the hostname of my Ubuntu permanently. Whenever I use the hostname New_hostname command, it returns to the original hostname after I restart the machine.
I found out that the only way I can change this permanently is by modifying the file in /etc/hostname and save it. Is there some way I can do this using a shell script only? I also have a password.
The hostnamectl combines setting the hostname via the hostname command and editing /etc/hostname. Unfortunately, editing /etc/hosts still has to be done separately.
hostnamectl set-hostname <new-hostname>
Type
echo "myNewHostName" > /etc/hostname
in any shell with root access near you..
You may also want to take a look at the file /etc/hosts, cf. http://pricklytech.wordpress.com/2013/04/24/ubuntu-change-hostname-permanently-using-the-command-line/.
In Ubuntu 18.04 LTS
Hostname changing via SSH is reverted after reboot in Ubuntu 18.04. Make permanent change as following way.
1. Edit /etc/cloud/cloud.cfg
sudo nano /etc/cloud/cloud.cfg
Set preserve_hostname to true
preserve_hostname: true
2. Run hostnamectl
hostnamectl set-hostname new-host-name
3. Reboot
sudo reboot
Change hostname permanently without reboot
/etc/hosts
127.0.0.1 persistent_host_name
/etc/hostname
persistent_host_name
Apply changes Immediately
$ sudo hostname persistent_host_name
Check changes
$ hostname
persistent_host_name
Typically, you would need to change it in these files:
/etc/hostname
/etc/hosts
If you are using some advanced printers, also here:
/etc/printcap
This is why I would recommend doing it manually - but search the old hostnames first. To find all occurrences in /etc:
sudo grep -iRI "_OLDHOSTNAME_" /etc 2>/dev/null
Then change the _OLDHOSTNAME_ in every occurrence.
Done.
To chaneg the Hostname permanet in ubuntu machine
Go to :
#vim /etc/hostname
Type the hostname inside the file you want to set for the machine
Then save and the file
After saving the document run this command
# hostname -F /etc/hostname
Then edit the /etc/hosts file
#vim /etc/hosts
type the ip hostname inside the file
Then Logout of of the machine and relogin into the machine
If you just want to change host name, because its getting displayed as a command prompt in the terminal. Then you can replace \h in PS1 with "desired_host_name" in ~/.bashrc
Like in ~/.bashrc put this line at end of file:
export PS2="continue-> ";
export PS1="\u#3050:~$ ";
Change Hostname on Ubuntu 18.04
Definition
A hostname is a label that identifies a machine on the network. You shouldn’t use the same hostname on two different machines on a same network.
Prerequisites
User should have a sudo privileges
Change the Hostname
Change the hostname using hostnamectl command. If you want to change the hostname to new_hostname
sudo hostnamectl set-hostname new_hostname
It will not change the hostname directly. You want to preserve the changes permanently then you have to edit cloud.cfg file
sudo nano /etc/cloud/cloud.cfg
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
Save the file and close your editor.
Verify your Changes
You can verify your changes using command hostnamectl it will show new_hostname under Static hostname
PS: Source Link

Resources